diff options
Diffstat (limited to '')
-rw-r--r-- | lib/pgsql_shim/CMakeLists.txt | 32 | ||||
-rw-r--r-- | lib/pgsql_shim/pgsql_shim.def | 3 | ||||
-rw-r--r-- | lib/pgsql_shim/pgsqlinterface.cpp | 108 | ||||
-rw-r--r-- | lib/pgsql_shim/pgsqlinterface.hpp | 61 |
4 files changed, 204 insertions, 0 deletions
diff --git a/lib/pgsql_shim/CMakeLists.txt b/lib/pgsql_shim/CMakeLists.txt new file mode 100644 index 0000000..327b64a --- /dev/null +++ b/lib/pgsql_shim/CMakeLists.txt @@ -0,0 +1,32 @@ +# Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ + +link_directories(${PostgreSQL_LIBRARY_DIRS}) +include_directories(${PostgreSQL_INCLUDE_DIRS}) + +set(pgsql_shim_SOURCES + pgsql_shim.def + pgsqlinterface.cpp pgsqlinterface.hpp +) + +if(ICINGA2_UNITY_BUILD) + mkunity_target(pgsql_shim pgsql_shim pgsql_shim_SOURCES) +endif() + +add_library(pgsql_shim SHARED ${pgsql_shim_SOURCES}) + +include(GenerateExportHeader) +generate_export_header(pgsql_shim) + +target_link_libraries(pgsql_shim ${PostgreSQL_LIBRARIES}) + +set_target_properties ( + pgsql_shim PROPERTIES + FOLDER Lib + VERSION ${SPEC_VERSION} +) + +install( + TARGETS pgsql_shim + RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/icinga2 +) diff --git a/lib/pgsql_shim/pgsql_shim.def b/lib/pgsql_shim/pgsql_shim.def new file mode 100644 index 0000000..7580d67 --- /dev/null +++ b/lib/pgsql_shim/pgsql_shim.def @@ -0,0 +1,3 @@ +LIBRARY pgsql_shim +EXPORTS + create_pgsql_shim diff --git a/lib/pgsql_shim/pgsqlinterface.cpp b/lib/pgsql_shim/pgsqlinterface.cpp new file mode 100644 index 0000000..95b6e7d --- /dev/null +++ b/lib/pgsql_shim/pgsqlinterface.cpp @@ -0,0 +1,108 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "pgsql_shim/pgsqlinterface.hpp" + +using namespace icinga; + +struct PgsqlInterfaceImpl final : public PgsqlInterface +{ + void Destroy() override + { + delete this; + } + + void clear(PGresult *res) const override + { + PQclear(res); + } + + char *cmdTuples(PGresult *res) const override + { + return PQcmdTuples(res); + } + + char *errorMessage(const PGconn *conn) const override + { + return PQerrorMessage(conn); + } + + size_t escapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error) const override + { + return PQescapeStringConn(conn, to, from, length, error); + } + + PGresult *exec(PGconn *conn, const char *query) const override + { + return PQexec(conn, query); + } + + void finish(PGconn *conn) const override + { + PQfinish(conn); + } + + char *fname(const PGresult *res, int field_num) const override + { + return PQfname(res, field_num); + } + + int getisnull(const PGresult *res, int tup_num, int field_num) const override + { + return PQgetisnull(res, tup_num, field_num); + } + + char *getvalue(const PGresult *res, int tup_num, int field_num) const override + { + return PQgetvalue(res, tup_num, field_num); + } + + int isthreadsafe() const override + { + return PQisthreadsafe(); + } + + int nfields(const PGresult *res) const override + { + return PQnfields(res); + } + + int ntuples(const PGresult *res) const override + { + return PQntuples(res); + } + + char *resultErrorMessage(const PGresult *res) const override + { + return PQresultErrorMessage(res); + } + + ExecStatusType resultStatus(const PGresult *res) const override + { + return PQresultStatus(res); + } + + int serverVersion(const PGconn *conn) const override + { + return PQserverVersion(conn); + } + + PGconn *setdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, const char *dbName, const char *login, const char *pwd) const override + { + return PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, login, pwd); + } + + PGconn *connectdb(const char *conninfo) const override + { + return PQconnectdb(conninfo); + } + + ConnStatusType status(const PGconn *conn) const override + { + return PQstatus(conn); + } +}; + +PgsqlInterface *create_pgsql_shim() +{ + return new PgsqlInterfaceImpl(); +} diff --git a/lib/pgsql_shim/pgsqlinterface.hpp b/lib/pgsql_shim/pgsqlinterface.hpp new file mode 100644 index 0000000..2fe3303 --- /dev/null +++ b/lib/pgsql_shim/pgsqlinterface.hpp @@ -0,0 +1,61 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#ifndef PGSQLINTERFACE_H +#define PGSQLINTERFACE_H + +#include "pgsql_shim/pgsql_shim_export.h" +#include <memory> +#include <libpq-fe.h> + +namespace icinga +{ + +struct PgsqlInterface +{ + PgsqlInterface(const PgsqlInterface&) = delete; + PgsqlInterface& operator=(PgsqlInterface&) = delete; + + virtual void Destroy() = 0; + + virtual void clear(PGresult *res) const = 0; + virtual char *cmdTuples(PGresult *res) const = 0; + virtual char *errorMessage(const PGconn *conn) const = 0; + virtual size_t escapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error) const = 0; + virtual PGresult *exec(PGconn *conn, const char *query) const = 0; + virtual void finish(PGconn *conn) const = 0; + virtual char *fname(const PGresult *res, int field_num) const = 0; + virtual int getisnull(const PGresult *res, int tup_num, int field_num) const = 0; + virtual char *getvalue(const PGresult *res, int tup_num, int field_num) const = 0; + virtual int isthreadsafe() const = 0; + virtual int nfields(const PGresult *res) const = 0; + virtual int ntuples(const PGresult *res) const = 0; + virtual char *resultErrorMessage(const PGresult *res) const = 0; + virtual ExecStatusType resultStatus(const PGresult *res) const = 0; + virtual int serverVersion(const PGconn *conn) const = 0; + virtual PGconn *setdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, const char *dbName, const char *login, const char *pwd) const = 0; + virtual PGconn *connectdb(const char *conninfo) const = 0; + virtual ConnStatusType status(const PGconn *conn) const = 0; + +protected: + PgsqlInterface() = default; + ~PgsqlInterface() = default; +}; + +struct PgsqlInterfaceDeleter +{ + void operator()(PgsqlInterface *ifc) const + { + ifc->Destroy(); + } +}; + +} + +extern "C" +{ + PGSQL_SHIM_EXPORT icinga::PgsqlInterface *create_pgsql_shim(); +} + +typedef icinga::PgsqlInterface *(*create_pgsql_shim_ptr)(); + +#endif /* PGSQLINTERFACE_H */ |