Mercurial > code
diff C++/DriverPostgres.h @ 172:a61cddaf7547
Rename Driver postgres
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 11 Sep 2013 15:27:00 +0200 |
parents | C++/DriverPG.h@e47c4e9e3f9d |
children | f0cca031bcce |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/DriverPostgres.h Wed Sep 11 15:27:00 2013 +0200 @@ -0,0 +1,119 @@ +/* + * DriverPostgres.h -- PostgreSQL driver + * + * Copyright (c) 2013, David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS + */ + +#ifndef _DRIVER_PG_H_ +#define _DRIVER_PG_H_ + +#include <map> +#include <memory> +#include <string> + +#include <libpq-fe.h> + +#include "Driver.h" + +/** + * Query implementation for PostgreSQL. + */ +class QueryPostgres : public Query +{ +public: + struct PQQueryDeleter + { + void operator()(PGresult *result) + { + PQclear(result); + } + }; + + typedef std::unique_ptr<PGresult, PQQueryDeleter> PostgresResult; + +private: + PostgresResult m_result; + +protected: + virtual QueryCheck checkRequest(int row, const std::string &column, ColumnType type); + + virtual Date checkDate(int row, const std::string &column); + + virtual bool checkBool(int row, const std::string &column); + + virtual double checkDouble(int row, const std::string &column); + + virtual int checkInt(int row, const std::string &column); + + virtual std::string checkString(int row, const std::string &column); + + // Avoid copy + QueryPostgres(const QueryPostgres &src); + + // Avoid copy + QueryPostgres & operator=(const QueryPostgres &src); + +public: + QueryPostgres(PostgresResult); + ~QueryPostgres(); + + virtual int countRows(); + + virtual int countColumns(); + + virtual bool isNull(int row, const std::string &column); + + virtual void dump(); +}; + +class DriverPostgres : public Driver +{ +public: + struct PGDeleter { + void operator()(PGconn *conn) { + PQfinish(conn); + } + }; + + typedef std::unique_ptr<PGconn, PGDeleter> PostgresConn; + +private: + PostgresConn m_connection; + + /** + * Convert the Params from the config + * to the PostgreSQL string. + * + * @param settings the table + * @return a string to be used + */ + std::string convert(Params &settings); + +public: + DriverPostgres(); + ~DriverPostgres(); + + virtual bool connect(const Params ¶ms); + + virtual std::unique_ptr<Query> query(const std::string &command); + + virtual std::string description() const; +}; + +#endif // !_DRIVER_PG_H_