Mercurial > code
changeset 190:d263f85f43a4
Update escape string for PostgreSQL strings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Nov 2013 10:25:58 +0100 |
parents | cc1e5fe1ee2c |
children | 5f75779cc7eb |
files | C++/Driver.h C++/DriverPostgres.cpp C++/DriverPostgres.h |
diffstat | 3 files changed, 29 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Driver.h Tue Nov 26 20:36:59 2013 +0100 +++ b/C++/Driver.h Wed Nov 27 10:25:58 2013 +0100 @@ -146,6 +146,7 @@ * @param row the row number (starts from 0) * @param column the the column name * @return the value + * @throw Query::Error on error */ template <class T> T get(int row, const std::string &column); @@ -333,7 +334,7 @@ * * @param request the request to use * @return a result - * @throw Query::Exception on failure + * @throw Query::Error on failure */ Query::Ptr query(Request::Ptr request); @@ -359,7 +360,7 @@ * * @param query the SQL command * @return a result - * @throw Query::Exception on failure + * @throw Query::Error on failure */ virtual Query::Ptr query(const std::string &command) = 0;
--- a/C++/DriverPostgres.cpp Tue Nov 26 20:36:59 2013 +0100 +++ b/C++/DriverPostgres.cpp Wed Nov 27 10:25:58 2013 +0100 @@ -170,9 +170,10 @@ * Request PostgreSQL * -------------------------------------------------------- */ -RequestPostgres::RequestPostgres(const std::string &command) +RequestPostgres::RequestPostgres(std::shared_ptr<PGconn> conn, const std::string &command) : Request(command) { + m_connection = conn; } std::string RequestPostgres::bindBoolean(bool value) @@ -203,19 +204,17 @@ std::string RequestPostgres::bindString(std::string value) { - std::ostringstream oss; + std::string result; + char *tmp; - oss << "'"; - for (auto c : value) { - if (c == '\'') - oss << "\\'"; - else - oss << c; - } + tmp = PQescapeLiteral(m_connection.get(), value.c_str(), value.length()); + if (tmp == nullptr) + return ""; - oss << "'"; + result = std::string(tmp); + PQfreemem(tmp); - return oss.str(); + return result; } /* -------------------------------------------------------- @@ -260,14 +259,14 @@ return false; } - m_connection = PostgresConn(conn); + m_connection = std::shared_ptr<PGconn>(conn, PGDeleter()); return true; } Request::Ptr DriverPostgres::prepare(const std::string &command) { - return std::make_shared<RequestPostgres>(command); + return std::make_shared<RequestPostgres>(m_connection, command); } Query::Ptr DriverPostgres::query(const std::string &cmd) @@ -308,9 +307,7 @@ { std::ostringstream oss; - // TODO: ADD VERSION - - oss << "PostgreSQL driver"; + oss << "PostgreSQL driver (version " << PQlibVersion() << ")"; return oss.str(); }
--- a/C++/DriverPostgres.h Tue Nov 26 20:36:59 2013 +0100 +++ b/C++/DriverPostgres.h Wed Nov 27 10:25:58 2013 +0100 @@ -77,6 +77,9 @@ public: /** * Constructor used by DriverPostgres + * + * @param driver the driver + * @param result the result */ QueryPostgres(Driver::Ptr driver, PostgresResult result); @@ -116,6 +119,9 @@ * @brief Request implementation for PostgreSQL. */ class RequestPostgres : public Request { +private: + std::shared_ptr<PGconn> m_connection; + protected: /** * @copydoc Request::bindBoolean @@ -144,9 +150,13 @@ public: /** - * @copydoc Request::Request + * Construct a request for PostgreSQL. + * + * @param conn the connection + * @param command the command */ - RequestPostgres(const std::string &command); + RequestPostgres(std::shared_ptr<PGconn> conn, + const std::string &command); }; /** @@ -164,7 +174,7 @@ } }; - using PostgresConn = std::unique_ptr<PGconn, PGDeleter>; + using PostgresConn = std::shared_ptr<PGconn>; private: PostgresConn m_connection;