diff options
-rw-r--r-- | libpqpp/pq-binarycolumn.cpp | 26 | ||||
-rw-r--r-- | libpqpp/pq-binarycolumn.h | 8 | ||||
-rw-r--r-- | libpqpp/pq-bulkselectcommand.cpp | 14 | ||||
-rw-r--r-- | libpqpp/pq-bulkselectcommand.h | 19 | ||||
-rw-r--r-- | libpqpp/pq-column.cpp | 97 | ||||
-rw-r--r-- | libpqpp/pq-column.h | 41 | ||||
-rw-r--r-- | libpqpp/pq-command.cpp | 29 | ||||
-rw-r--r-- | libpqpp/pq-command.h | 81 | ||||
-rw-r--r-- | libpqpp/pq-connection.cpp | 40 | ||||
-rw-r--r-- | libpqpp/pq-connection.h | 63 | ||||
-rw-r--r-- | libpqpp/pq-cursorselectcommand.cpp | 19 | ||||
-rw-r--r-- | libpqpp/pq-cursorselectcommand.h | 39 | ||||
-rw-r--r-- | libpqpp/pq-error.cpp | 6 | ||||
-rw-r--r-- | libpqpp/pq-error.h | 13 | ||||
-rw-r--r-- | libpqpp/pq-mock.cpp | 170 | ||||
-rw-r--r-- | libpqpp/pq-mock.h | 31 | ||||
-rw-r--r-- | libpqpp/pq-modifycommand.cpp | 10 | ||||
-rw-r--r-- | libpqpp/pq-modifycommand.h | 13 | ||||
-rw-r--r-- | libpqpp/pq-prepared.cpp | 9 | ||||
-rw-r--r-- | libpqpp/pq-prepared.h | 11 | ||||
-rw-r--r-- | libpqpp/pq-selectbase.cpp | 9 | ||||
-rw-r--r-- | libpqpp/pq-selectbase.h | 17 | ||||
-rw-r--r-- | libpqpp/unittests/testpq.cpp | 153 |
23 files changed, 441 insertions, 477 deletions
diff --git a/libpqpp/pq-binarycolumn.cpp b/libpqpp/pq-binarycolumn.cpp index 1c6f9e2..1e75343 100644 --- a/libpqpp/pq-binarycolumn.cpp +++ b/libpqpp/pq-binarycolumn.cpp @@ -2,10 +2,7 @@ #include "pq-selectbase.h" #include <error.h> -PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : - PQ::Column(s, f) -{ -} +PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : PQ::Column(s, f) { } void PQ::BinaryColumn::apply(DB::HandleField & h) const @@ -15,29 +12,28 @@ PQ::BinaryColumn::apply(DB::HandleField & h) const return; } switch (oid) { - case 18: //CHAROID: - case 1043: //VARCHAROID: - case 25: //TEXTOID: - case 142: //XMLOID: - h.string({ value(), length() }); + case 18: // CHAROID: + case 1043: // VARCHAROID: + case 25: // TEXTOID: + case 142: // XMLOID: + h.string({value(), length()}); break; - case 16: //BOOLOID: + case 16: // BOOLOID: h.boolean(valueAs<bool>()); break; - case 21: //INT2OID: + case 21: // INT2OID: h.integer(be16toh(valueAs<uint16_t>())); break; - case 23: //INT4OID: + case 23: // INT4OID: h.integer(be32toh(valueAs<uint32_t>())); break; - case 20: //INT8OID: + case 20: // INT8OID: h.integer(be64toh(valueAs<uint64_t>())); break; - case 17: //BYTEAOID + case 17: // BYTEAOID h.blob(DB::Blob(value(), length())); break; default: throw DB::ColumnTypeNotSupported(); } } - diff --git a/libpqpp/pq-binarycolumn.h b/libpqpp/pq-binarycolumn.h index 14719a3..402d0bb 100644 --- a/libpqpp/pq-binarycolumn.h +++ b/libpqpp/pq-binarycolumn.h @@ -5,13 +5,11 @@ namespace PQ { class BinaryColumn : public Column { - public: - BinaryColumn(const SelectBase *, unsigned int field); + public: + BinaryColumn(const SelectBase *, unsigned int field); - void apply(DB::HandleField &) const override; + void apply(DB::HandleField &) const override; }; } #endif - - diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp index c098aad..ff9f2f4 100644 --- a/libpqpp/pq-bulkselectcommand.cpp +++ b/libpqpp/pq-bulkselectcommand.cpp @@ -1,13 +1,12 @@ #include "pq-bulkselectcommand.h" -#include "pq-connection.h" #include "pq-column.h" +#include "pq-connection.h" #include "pq-error.h" -PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr & opts) : +PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, + const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr & opts) : DB::Command(sql), - PQ::SelectBase(sql, pqco), - PQ::PreparedStatement(conn, sql, opts), - executed(false) + PQ::SelectBase(sql, pqco), PQ::PreparedStatement(conn, sql, opts), executed(false) { } @@ -15,8 +14,8 @@ void PQ::BulkSelectCommand::execute() { if (!executed) { - execRes = c->checkResult( - PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), &formats.front(), binary), + execRes = c->checkResult(PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), + &formats.front(), binary), PGRES_TUPLES_OK); nTuples = PQntuples(execRes); tuple = -1; @@ -39,4 +38,3 @@ PQ::BulkSelectCommand::fetch() return false; } } - diff --git a/libpqpp/pq-bulkselectcommand.h b/libpqpp/pq-bulkselectcommand.h index 5ff8a88..9c1c51b 100644 --- a/libpqpp/pq-bulkselectcommand.h +++ b/libpqpp/pq-bulkselectcommand.h @@ -1,26 +1,25 @@ #ifndef PQ_BULKSELECTCOMMAND_H #define PQ_BULKSELECTCOMMAND_H -#include "pq-selectbase.h" #include "pq-prepared.h" -#include <vector> +#include "pq-selectbase.h" #include <map> +#include <vector> namespace PQ { class Connection; class Column; class BulkSelectCommand : public SelectBase, public PreparedStatement { - public: - BulkSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr &); + public: + BulkSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptionsCPtr & pqco, + const DB::CommandOptionsCPtr &); - bool fetch() override; - void execute() override; + bool fetch() override; + void execute() override; - private: - mutable bool executed; + private: + mutable bool executed; }; } #endif - - diff --git a/libpqpp/pq-column.cpp b/libpqpp/pq-column.cpp index 0753c50..e4d85d8 100644 --- a/libpqpp/pq-column.cpp +++ b/libpqpp/pq-column.cpp @@ -1,14 +1,11 @@ #include "pq-column.h" -#include "pq-selectbase.h" #include "pq-error.h" -#include <cstring> +#include "pq-selectbase.h" #include <boost/date_time/posix_time/posix_time.hpp> +#include <cstring> PQ::Column::Column(const SelectBase * s, unsigned int i) : - DB::Column(PQfname(s->execRes, (int)i), i), - sc(s), - oid(PQftype(sc->execRes, (int)colNo)), - buf(nullptr) + DB::Column(PQfname(s->execRes, (int)i), i), sc(s), oid(PQftype(sc->execRes, (int)colNo)), buf(nullptr) { } @@ -45,61 +42,63 @@ PQ::Column::apply(DB::HandleField & h) const return; } switch (oid) { - case 18: //CHAROID: - case 1043: //VARCHAROID: - case 25: //TEXTOID: - case 142: //XMLOID: + case 18: // CHAROID: + case 1043: // VARCHAROID: + case 25: // TEXTOID: + case 142: // XMLOID: default: - h.string({ value(), length() }); + h.string({value(), length()}); break; - case 16: //BOOLOID: + case 16: // BOOLOID: h.boolean(value()[0] == 't'); break; - case 21: //INT2OID: - case 23: //INT4OID: - case 20: //INT8OID: + case 21: // INT2OID: + case 23: // INT4OID: + case 20: // INT8OID: h.integer(atol(value())); break; - case 1700: //NUMERICOID: - case 700: //FLOAT4OID: - case 701: //FLOAT8OID: + case 1700: // NUMERICOID: + case 700: // FLOAT4OID: + case 701: // FLOAT8OID: h.floatingpoint(atof(value())); break; - case 704: //TINTERVALOID - case 1083: //TIMEOID: - case 1186: //INTERVALOID - { - int days = 0, hours = 0, minutes = 0, seconds = 0, fractions = 0, flen1 = 0, flen2 = 0; - const char * val = value(); - // NOLINTNEXTLINE(hicpp-vararg) - if (sscanf(val, "%d %*[days] %d:%d:%d.%n%d%n", &days, &hours, &minutes, &seconds, &flen1, &fractions, &flen2) >= 4) { - h.interval(boost::posix_time::time_duration((24 * days) + hours, minutes, seconds, - fractions * (long)pow(10, boost::posix_time::time_res_traits::num_fractional_digits() + flen1 - flen2))); - } - else { - h.interval(boost::posix_time::duration_from_string(val)); - } - break; + case 704: // TINTERVALOID + case 1083: // TIMEOID: + case 1186: // INTERVALOID + { + int days = 0, hours = 0, minutes = 0, seconds = 0, fractions = 0, flen1 = 0, flen2 = 0; + const char * val = value(); + // NOLINTNEXTLINE(hicpp-vararg) + if (sscanf(val, "%d %*[days] %d:%d:%d.%n%d%n", &days, &hours, &minutes, &seconds, &flen1, &fractions, + &flen2) + >= 4) { + h.interval(boost::posix_time::time_duration((24 * days) + hours, minutes, seconds, + fractions + * (long)pow(10, + boost::posix_time::time_res_traits::num_fractional_digits() + flen1 - flen2))); } - case 1082: //DATEOID: - h.timestamp(boost::posix_time::ptime( - boost::gregorian::from_string(value()))); + else { + h.interval(boost::posix_time::duration_from_string(val)); + } + break; + } + case 1082: // DATEOID: + h.timestamp(boost::posix_time::ptime(boost::gregorian::from_string(value()))); break; - case 702: //ABSTIMEOID: - case 1114: //TIMESTAMPOID: - case 1184: //TIMESTAMPTZOID: + case 702: // ABSTIMEOID: + case 1114: // TIMESTAMPOID: + case 1184: // TIMESTAMPTZOID: h.timestamp(boost::posix_time::time_from_string(value())); break; - case 17: //BYTEAOID - { - if (buf) { - PQfreemem(buf); - } - size_t len; - buf = PQunescapeBytea(valueAsPtr<unsigned char>(), &len); - h.blob(DB::Blob(buf, len)); - break; + case 17: // BYTEAOID + { + if (buf) { + PQfreemem(buf); } + size_t len; + buf = PQunescapeBytea(valueAsPtr<unsigned char>(), &len); + h.blob(DB::Blob(buf, len)); + break; + } } } - diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index b0a568d..e5bb9d2 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -7,27 +7,34 @@ namespace PQ { class SelectBase; class Column : public DB::Column { - public: - Column(const SelectBase *, unsigned int field); - ~Column(); + public: + Column(const SelectBase *, unsigned int field); + ~Column(); - [[nodiscard]] bool isNull() const override; - void apply(DB::HandleField &) const override; + [[nodiscard]] bool isNull() const override; + void apply(DB::HandleField &) const override; - protected: - template<typename T> - inline T valueAs() const { return *(T*)(value()); } - template<typename T> - inline T * valueAsPtr() const { return (T*)(value()); } - const char * value() const; - std::size_t length() const; + protected: + template<typename T> + inline T + valueAs() const + { + return *(T *)(value()); + } + template<typename T> + inline T * + valueAsPtr() const + { + return (T *)(value()); + } + const char * value() const; + std::size_t length() const; - const SelectBase * sc; - const Oid oid; - // Buffer for PQunescapeBytea - mutable unsigned char * buf; + const SelectBase * sc; + const Oid oid; + // Buffer for PQunescapeBytea + mutable unsigned char * buf; }; } #endif - diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 147d988..98cd725 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -1,38 +1,28 @@ #include "pq-command.h" #include "pq-connection.h" +#include <boost/date_time/posix_time/posix_time.hpp> +#include <compileTimeFormatter.h> #include <cstdlib> #include <cstring> -#include <compileTimeFormatter.h> -#include <boost/date_time/posix_time/posix_time.hpp> #include <factory.h> NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory); AdHocFormatter(PQCommondStatement, "pStatement_id%?"); PQ::Command::Command(Connection * conn, const std::string & sql, const DB::CommandOptionsCPtr & opts) : - DB::Command(sql), - hash(opts && opts->hash ? *opts->hash : std::hash<std::string>()(sql)), - stmntName(PQCommondStatement::get(hash)), - c(conn) + DB::Command(sql), hash(opts && opts->hash ? *opts->hash : std::hash<std::string>()(sql)), + stmntName(PQCommondStatement::get(hash)), c(conn) { } PQ::CommandOptions::CommandOptions(std::size_t hash, const DB::CommandOptionsMap & map) : - DB::CommandOptions(hash), - fetchTuples(get(map, "page-size", 35)), - useCursor(!isSet(map, "no-cursor")), + DB::CommandOptions(hash), fetchTuples(get(map, "page-size", 35)), useCursor(!isSet(map, "no-cursor")), fetchBinary(isSet(map, "fetch-binary")) { } -PQ::CommandOptions::CommandOptions(std::size_t hash, - unsigned int ft, - bool uc, - bool fb) : - DB::CommandOptions(hash), - fetchTuples(ft), - useCursor(uc), - fetchBinary(fb) +PQ::CommandOptions::CommandOptions(std::size_t hash, unsigned int ft, bool uc, bool fb) : + DB::CommandOptions(hash), fetchTuples(ft), useCursor(uc), fetchBinary(fb) { } @@ -72,9 +62,9 @@ PQ::Command::paramsAtLeast(unsigned int n) } AdHocFormatter(PQCommandParamFmt, "%?"); -template<typename ... T> +template<typename... T> void -PQ::Command::paramSet(unsigned int n, const T & ... v) +PQ::Command::paramSet(unsigned int n, const T &... v) { paramsAtLeast(n); bufs[n] = std::make_unique<std::string>(PQCommandParamFmt::get(v...)); @@ -176,4 +166,3 @@ PQ::Command::bindNull(unsigned int n) values[n] = nullptr; bufs[n].reset(); } - diff --git a/libpqpp/pq-command.h b/libpqpp/pq-command.h index 101afd8..839c991 100644 --- a/libpqpp/pq-command.h +++ b/libpqpp/pq-command.h @@ -1,72 +1,69 @@ #ifndef PQ_COMMAND_H #define PQ_COMMAND_H +#include "pq-connection.h" #include <command.h> #include <libpq-fe.h> -#include <vector> #include <memory> +#include <vector> #include <visibility.h> -#include "pq-connection.h" namespace PQ { class Connection; class DLL_PUBLIC CommandOptions : public DB::CommandOptions { - public: - CommandOptions(std::size_t, const DB::CommandOptionsMap &); - explicit CommandOptions(std::size_t hash, - unsigned int fetchTuples = 35, - bool useCursor = true, - bool fetchBinary = false); + public: + CommandOptions(std::size_t, const DB::CommandOptionsMap &); + explicit CommandOptions( + std::size_t hash, unsigned int fetchTuples = 35, bool useCursor = true, bool fetchBinary = false); - unsigned int fetchTuples; - bool useCursor; - bool fetchBinary; + unsigned int fetchTuples; + bool useCursor; + bool fetchBinary; }; using CommandOptionsPtr = std::shared_ptr<CommandOptions>; using CommandOptionsCPtr = std::shared_ptr<const CommandOptions>; class Command : public virtual DB::Command { - public: - Command(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); + public: + Command(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); + + void bindParamI(unsigned int, int) override; + void bindParamI(unsigned int, long int) override; + void bindParamI(unsigned int, long long int) override; + void bindParamI(unsigned int, unsigned int) override; + void bindParamI(unsigned int, long unsigned int) override; + void bindParamI(unsigned int, long long unsigned int) override; - void bindParamI(unsigned int, int) override; - void bindParamI(unsigned int, long int) override; - void bindParamI(unsigned int, long long int) override; - void bindParamI(unsigned int, unsigned int) override; - void bindParamI(unsigned int, long unsigned int) override; - void bindParamI(unsigned int, long long unsigned int) override; + void bindParamB(unsigned int, bool) override; - void bindParamB(unsigned int, bool) override; + void bindParamF(unsigned int, double) override; + void bindParamF(unsigned int, float) override; - void bindParamF(unsigned int, double) override; - void bindParamF(unsigned int, float) override; + void bindParamS(unsigned int, const Glib::ustring &) override; + void bindParamS(unsigned int, const std::string_view &) override; - void bindParamS(unsigned int, const Glib::ustring&) override; - void bindParamS(unsigned int, const std::string_view&) override; + void bindParamT(unsigned int, const boost::posix_time::time_duration &) override; + void bindParamT(unsigned int, const boost::posix_time::ptime &) override; - void bindParamT(unsigned int, const boost::posix_time::time_duration &) override; - void bindParamT(unsigned int, const boost::posix_time::ptime &) override; + void bindParamBLOB(unsigned int, const DB::Blob &) override; - void bindParamBLOB(unsigned int, const DB::Blob &) override; + void bindNull(unsigned int) override; - void bindNull(unsigned int) override; - protected: - void prepareSql(std::stringstream & psql, const std::string & sql) const; - Connection::StatementHash hash; - const std::string stmntName; - Connection * const c; + protected: + void prepareSql(std::stringstream & psql, const std::string & sql) const; + Connection::StatementHash hash; + const std::string stmntName; + Connection * const c; - void paramsAtLeast(unsigned int); - template<typename ... T> - void paramSet(unsigned int, const T & ... t); - void paramSet(unsigned int, const std::string_view &); - std::vector<const char *> values; - std::vector<int> lengths; - std::vector<int> formats; - std::vector<std::unique_ptr<std::string>> bufs; + void paramsAtLeast(unsigned int); + template<typename... T> void paramSet(unsigned int, const T &... t); + void paramSet(unsigned int, const std::string_view &); + std::vector<const char *> values; + std::vector<int> lengths; + std::vector<int> formats; + std::vector<std::unique_ptr<std::string>> bufs; }; } #endif - diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index f767b57..af74fac 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -1,17 +1,18 @@ #include "pq-connection.h" -#include "pq-error.h" #include "pq-bulkselectcommand.h" #include "pq-cursorselectcommand.h" +#include "pq-error.h" #include "pq-modifycommand.h" -#include <unistd.h> -#include <poll.h> #include <boost/assert.hpp> #include <compileTimeFormatter.h> +#include <poll.h> +#include <unistd.h> NAMEDFACTORY("postgresql", PQ::Connection, DB::ConnectionFactory); -static void setup() __attribute__((constructor(101))); -static void setup() +static void setup() __attribute__((constructor(101))); +static void +setup() { // NOLINTNEXTLINE(hicpp-no-array-decay) BOOST_ASSERT(PQisthreadsafe() == 1); @@ -23,13 +24,9 @@ noNoticeProcessor(void *, const char *) { } -PQ::ConnectionError::ConnectionError(const PGconn * conn) : - PQ::Error(conn) -{ -} +PQ::ConnectionError::ConnectionError(const PGconn * conn) : PQ::Error(conn) { } -PQ::Connection::Connection(const std::string & info) : - conn(PQconnectdb(info.c_str())) +PQ::Connection::Connection(const std::string & info) : conn(PQconnectdb(info.c_str())) { if (PQstatus(conn) != CONNECTION_OK) { auto dc = std::unique_ptr<PGconn, decltype(&PQfinish)>(conn, &PQfinish); @@ -83,7 +80,9 @@ void PQ::Connection::ping() const { // NOLINTNEXTLINE(hicpp-signed-bitwise) - struct pollfd fd { PQsocket(conn), POLLRDHUP | POLLERR | POLLHUP | POLLNVAL, 0 }; + struct pollfd fd { + PQsocket(conn), POLLRDHUP | POLLERR | POLLHUP | POLLNVAL, 0 + }; if (PQstatus(conn) != CONNECTION_OK || poll(&fd, 1, 0)) { if (inTx()) { throw ConnectionError(conn); @@ -96,7 +95,6 @@ PQ::Connection::ping() const } } - DB::SelectCommandPtr PQ::Connection::select(const std::string & sql, const DB::CommandOptionsCPtr & opts) { @@ -150,14 +148,14 @@ void PQ::Connection::endBulkUpload(const char * msg) { switch (PQputCopyEnd(conn, msg)) { - case 0:// block + case 0: // block sleep(1); endBulkUpload(msg); return; - case 1:// success + case 1: // success checkResultFree(PQgetResult(conn), PGRES_COMMAND_OK); return; - default:// -1 is error + default: // -1 is error throw Error(conn); } } @@ -166,18 +164,19 @@ size_t PQ::Connection::bulkUploadData(const char * data, size_t len) const { switch (PQputCopyData(conn, data, len)) { - case 0:// block + case 0: // block sleep(1); return bulkUploadData(data, len); - case 1:// success + case 1: // success return len; - default:// -1 is error + default: // -1 is error throw Error(conn); } } static const std::string selectLastVal("SELECT lastval()"); -static const DB::CommandOptionsCPtr selectLastValOpts = std::make_shared<DB::CommandOptions>(std::hash<std::string>()(selectLastVal)); +static const DB::CommandOptionsCPtr selectLastValOpts + = std::make_shared<DB::CommandOptions>(std::hash<std::string>()(selectLastVal)); int64_t PQ::Connection::insertId() { @@ -194,4 +193,3 @@ PQ::Connection::serverVersion() const { return PQserverVersion(conn); } - diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h index 833330b..ec1ae81 100644 --- a/libpqpp/pq-connection.h +++ b/libpqpp/pq-connection.h @@ -1,57 +1,56 @@ #ifndef PQ_CONNECTION_H #define PQ_CONNECTION_H +#include "pq-error.h" +#include <c++11Helpers.h> #include <connection.h> -#include <set> #include <libpq-fe.h> +#include <set> #include <visibility.h> -#include <c++11Helpers.h> -#include "pq-error.h" namespace PQ { class ConnectionError : public virtual Error, public virtual DB::ConnectionError { - public: - explicit ConnectionError(const PGconn *); + public: + explicit ConnectionError(const PGconn *); }; class DLL_PUBLIC Connection : public DB::Connection { - public: - using StatementHash = std::size_t; - using PreparedStatements = std::map<StatementHash, std::string>; + public: + using StatementHash = std::size_t; + using PreparedStatements = std::map<StatementHash, std::string>; - explicit Connection(const std::string & info); - ~Connection() override; + explicit Connection(const std::string & info); + ~Connection() override; - SPECIAL_MEMBERS_MOVE_RO(Connection); + SPECIAL_MEMBERS_MOVE_RO(Connection); - void beginTxInt() override; - void commitTxInt() override; - void rollbackTxInt() override; - void ping() const override; - void execute(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; - DB::BulkDeleteStyle bulkDeleteStyle() const override; - DB::BulkUpdateStyle bulkUpdateStyle() const override; + void beginTxInt() override; + void commitTxInt() override; + void rollbackTxInt() override; + void ping() const override; + void execute(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; + DB::BulkDeleteStyle bulkDeleteStyle() const override; + DB::BulkUpdateStyle bulkUpdateStyle() const override; - DB::SelectCommandPtr select(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; - DB::ModifyCommandPtr modify(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; + DB::SelectCommandPtr select(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; + DB::ModifyCommandPtr modify(const std::string & sql, const DB::CommandOptionsCPtr & = nullptr) override; - int64_t insertId() override; - int serverVersion() const; + int64_t insertId() override; + int serverVersion() const; - void beginBulkUpload(const char *, const char *) override; - void endBulkUpload(const char *) override; - size_t bulkUploadData(const char *, size_t) const override; + void beginBulkUpload(const char *, const char *) override; + void endBulkUpload(const char *) override; + size_t bulkUploadData(const char *, size_t) const override; - PGresult * checkResult(PGresult * res, int expected, int alternative = -1) const; - void checkResultFree(PGresult * res, int expected, int alternative = -1) const; + PGresult * checkResult(PGresult * res, int expected, int alternative = -1) const; + void checkResultFree(PGresult * res, int expected, int alternative = -1) const; - PGconn * conn; - mutable PreparedStatements preparedStatements; + PGconn * conn; + mutable PreparedStatements preparedStatements; - private: - static bool checkResultInt(PGresult * res, int expected, int alternative); + private: + static bool checkResultInt(PGresult * res, int expected, int alternative); }; } #endif - diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 820518f..1981bc8 100644 --- a/libpqpp/pq-cursorselectcommand.cpp +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -7,14 +7,11 @@ AdHocFormatter(PQCursorSelectDeclare, "DECLARE %? NO SCROLL CURSOR WITH HOLD FOR AdHocFormatter(PQCursorSelectFetch, "FETCH %? IN %?"); AdHocFormatter(PQCursorSelectClose, "CLOSE %?"); -PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::string & sql, const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr & opts) : +PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::string & sql, + const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr & opts) : DB::Command(sql), - PQ::SelectBase(sql, pqco), - PQ::Command(conn, sql, opts), - executed(false), - fTuples(pqco ? pqco->fetchTuples : 35), - s_fetch(PQCursorSelectFetch::get(fTuples, stmntName)), - s_close(PQCursorSelectClose::get(stmntName)) + PQ::SelectBase(sql, pqco), PQ::Command(conn, sql, opts), executed(false), fTuples(pqco ? pqco->fetchTuples : 35), + s_fetch(PQCursorSelectFetch::get(fTuples, stmntName)), s_close(PQCursorSelectClose::get(stmntName)) { } @@ -41,8 +38,8 @@ PQ::CursorSelectCommand::execute() if (s_declare.empty()) { s_declare = mkdeclare(); } - c->checkResultFree( - PQexecParams(c->conn, s_declare.c_str(), values.size(), nullptr, &values.front(), &lengths.front(), &formats.front(), binary), + c->checkResultFree(PQexecParams(c->conn, s_declare.c_str(), values.size(), nullptr, &values.front(), + &lengths.front(), &formats.front(), binary), PGRES_COMMAND_OK); fetchTuples(); createColumns(execRes); @@ -53,7 +50,8 @@ PQ::CursorSelectCommand::execute() void PQ::CursorSelectCommand::fetchTuples() { - execRes = c->checkResult(PQexecParams(c->conn, s_fetch.c_str(), 0, nullptr, nullptr, nullptr, nullptr, binary), PGRES_TUPLES_OK); + execRes = c->checkResult( + PQexecParams(c->conn, s_fetch.c_str(), 0, nullptr, nullptr, nullptr, nullptr, binary), PGRES_TUPLES_OK); nTuples = PQntuples(execRes); tuple = -1; } @@ -79,4 +77,3 @@ PQ::CursorSelectCommand::fetch() return false; } } - diff --git a/libpqpp/pq-cursorselectcommand.h b/libpqpp/pq-cursorselectcommand.h index f75c36b..853be47 100644 --- a/libpqpp/pq-cursorselectcommand.h +++ b/libpqpp/pq-cursorselectcommand.h @@ -1,34 +1,33 @@ #ifndef PQ_CURSORSELECTCOMMAND_H #define PQ_CURSORSELECTCOMMAND_H -#include "pq-selectbase.h" #include "pq-command.h" -#include <vector> +#include "pq-selectbase.h" #include <map> +#include <vector> namespace PQ { class Connection; class Column; class CursorSelectCommand : public SelectBase, public Command { - public: - CursorSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptionsCPtr &, const DB::CommandOptionsCPtr &); - virtual ~CursorSelectCommand(); - - bool fetch() override; - void execute() override; - - private: - void fetchTuples(); - std::string mkdeclare() const; - - mutable bool executed; - unsigned int fTuples; - std::string s_declare; - std::string s_fetch; - std::string s_close; + public: + CursorSelectCommand( + Connection *, const std::string & sql, const PQ::CommandOptionsCPtr &, const DB::CommandOptionsCPtr &); + virtual ~CursorSelectCommand(); + + bool fetch() override; + void execute() override; + + private: + void fetchTuples(); + std::string mkdeclare() const; + + mutable bool executed; + unsigned int fTuples; + std::string s_declare; + std::string s_fetch; + std::string s_close; }; } #endif - - diff --git a/libpqpp/pq-error.cpp b/libpqpp/pq-error.cpp index 964d66b..ca33a26 100644 --- a/libpqpp/pq-error.cpp +++ b/libpqpp/pq-error.cpp @@ -1,13 +1,9 @@ #include "pq-error.h" -PQ::Error::Error(const PGconn * conn) : - msg(PQerrorMessage(conn)) -{ -} +PQ::Error::Error(const PGconn * conn) : msg(PQerrorMessage(conn)) { } std::string PQ::Error::message() const noexcept { return msg; } - diff --git a/libpqpp/pq-error.h b/libpqpp/pq-error.h index 08d86b9..9c14e2d 100644 --- a/libpqpp/pq-error.h +++ b/libpqpp/pq-error.h @@ -2,20 +2,19 @@ #define PQ_ERROR_H #include <error.h> -#include <libpq-fe.h> #include <exception.h> +#include <libpq-fe.h> namespace PQ { class Error : public AdHoc::Exception<DB::Error> { - public: - explicit Error(const PGconn *); + public: + explicit Error(const PGconn *); - std::string message() const noexcept override; + std::string message() const noexcept override; - private: - std::string msg; + private: + std::string msg; }; } #endif - diff --git a/libpqpp/pq-mock.cpp b/libpqpp/pq-mock.cpp index 018dad4..34d903e 100644 --- a/libpqpp/pq-mock.cpp +++ b/libpqpp/pq-mock.cpp @@ -1,46 +1,46 @@ #include "pq-mock.h" #include "pq-connection.h" +#include <boost/algorithm/string.hpp> #include <compileTimeFormatter.h> #include <modifycommand.h> #include <selectcommand.h> #include <selectcommandUtil.impl.h> -#include <boost/algorithm/string.hpp> NAMEDFACTORY("postgresql", PQ::Mock, DB::MockDatabaseFactory); namespace PQ { -Mock::Mock(const std::string & masterdb, const std::string & name, const std::vector<std::filesystem::path> & ss) : - MockServerDatabase(masterdb, name, "postgresql"), - tablespacePath(std::filesystem::temp_directory_path() / testDbName), - serverVersion(std::static_pointer_cast<Connection>(master)->serverVersion()) -{ - try { - CreateNewDatabase(); - PlaySchemaScripts(ss); - SetTablesToUnlogged(); - } - catch (...) { - DropDatabase(); - throw; + Mock::Mock(const std::string & masterdb, const std::string & name, const std::vector<std::filesystem::path> & ss) : + MockServerDatabase(masterdb, name, "postgresql"), + tablespacePath(std::filesystem::temp_directory_path() / testDbName), + serverVersion(std::static_pointer_cast<Connection>(master)->serverVersion()) + { + try { + CreateNewDatabase(); + PlaySchemaScripts(ss); + SetTablesToUnlogged(); + } + catch (...) { + DropDatabase(); + throw; + } } -} -AdHocFormatter(MockConnStr, "user=postgres dbname=%?"); -DB::ConnectionPtr -Mock::openConnection() const -{ - return std::make_shared<Connection>(MockConnStr::get(boost::algorithm::to_lower_copy(testDbName))); -} - -AdHocFormatter(MockSetUnlogged, "ALTER TABLE %?.%? SET UNLOGGED"); -void -Mock::SetTablesToUnlogged() const -{ - if (!hasUnloggedTables()) { - return; + AdHocFormatter(MockConnStr, "user=postgres dbname=%?"); + DB::ConnectionPtr + Mock::openConnection() const + { + return std::make_shared<Connection>(MockConnStr::get(boost::algorithm::to_lower_copy(testDbName))); } - auto s = master->select(R"SQL( + + AdHocFormatter(MockSetUnlogged, "ALTER TABLE %?.%? SET UNLOGGED"); + void + Mock::SetTablesToUnlogged() const + { + if (!hasUnloggedTables()) { + return; + } + auto s = master->select(R"SQL( SELECT n.nspname, c.relname FROM pg_class c, pg_namespace n WHERE c.relkind = 'r' @@ -55,69 +55,69 @@ AND NOT EXISTS ( AND ck.relpersistence = 'p') AND n.oid = c.relnamespace ORDER BY 1, 2)SQL"); - s->bindParamS(0, "pg_catalog"); - s->bindParamS(1, "information_schema"); - unsigned int n = 0; - do { - n = 0; - for (const auto [ nspname, relname ] : s->as<std::string, std::string>()) { - master->execute(MockSetUnlogged::get(nspname, relname)); - n += 1; - } - } while(n); -} + s->bindParamS(0, "pg_catalog"); + s->bindParamS(1, "information_schema"); + unsigned int n = 0; + do { + n = 0; + for (const auto [nspname, relname] : s->as<std::string, std::string>()) { + master->execute(MockSetUnlogged::get(nspname, relname)); + n += 1; + } + } while (n); + } -Mock::~Mock() -{ - Mock::DropDatabase(); -} + Mock::~Mock() + { + Mock::DropDatabase(); + } -bool -Mock::hasUnloggedTables() const -{ - // v9.5 server required for unlogged tables - return (serverVersion >= 90500); -} + bool + Mock::hasUnloggedTables() const + { + // v9.5 server required for unlogged tables + return (serverVersion >= 90500); + } -bool -Mock::hasCopyToProgram() const -{ - // v9.3 server required to use COPY ... TO PROGRAM ... - return (serverVersion >= 90300); -} + bool + Mock::hasCopyToProgram() const + { + // v9.3 server required to use COPY ... TO PROGRAM ... + return (serverVersion >= 90300); + } -AdHocFormatter(MockCreateTablespaceDir, "COPY (SELECT '%?') TO PROGRAM 'xargs mkdir -p'"); -AdHocFormatter(MockCreateTablespace, "CREATE TABLESPACE %? LOCATION '%?'"); -AdHocFormatter(MockCreateDatabase, "CREATE DATABASE %? TABLESPACE %?"); -AdHocFormatter(MockDropTablespace, "DROP TABLESPACE IF EXISTS %?"); -AdHocFormatter(MockDropTablespaceDir, "COPY (SELECT '%?') TO PROGRAM 'xargs rm -rf'"); + AdHocFormatter(MockCreateTablespaceDir, "COPY (SELECT '%?') TO PROGRAM 'xargs mkdir -p'"); + AdHocFormatter(MockCreateTablespace, "CREATE TABLESPACE %? LOCATION '%?'"); + AdHocFormatter(MockCreateDatabase, "CREATE DATABASE %? TABLESPACE %?"); + AdHocFormatter(MockDropTablespace, "DROP TABLESPACE IF EXISTS %?"); + AdHocFormatter(MockDropTablespaceDir, "COPY (SELECT '%?') TO PROGRAM 'xargs rm -rf'"); -void -Mock::CreateNewDatabase() const -{ - if (hasCopyToProgram()) { - DropDatabase(); - master->execute(MockCreateTablespaceDir::get(tablespacePath)); - master->execute(MockCreateTablespace::get(testDbName, tablespacePath.string())); - master->execute(MockCreateDatabase::get(testDbName, testDbName)); - } - else { - MockServerDatabase::CreateNewDatabase(); + void + Mock::CreateNewDatabase() const + { + if (hasCopyToProgram()) { + DropDatabase(); + master->execute(MockCreateTablespaceDir::get(tablespacePath)); + master->execute(MockCreateTablespace::get(testDbName, tablespacePath.string())); + master->execute(MockCreateDatabase::get(testDbName, testDbName)); + } + else { + MockServerDatabase::CreateNewDatabase(); + } } -} -void -Mock::DropDatabase() const -{ - auto t = master->modify("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE LOWER(datname) = LOWER(?)"); - t->bindParamS(0, testDbName); - t->execute(); - MockServerDatabase::DropDatabase(); - if (hasCopyToProgram()) { - master->execute(MockDropTablespace::get(testDbName)); - master->execute(MockDropTablespaceDir::get(tablespacePath)); + void + Mock::DropDatabase() const + { + auto t = master->modify( + "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE LOWER(datname) = LOWER(?)"); + t->bindParamS(0, testDbName); + t->execute(); + MockServerDatabase::DropDatabase(); + if (hasCopyToProgram()) { + master->execute(MockDropTablespace::get(testDbName)); + master->execute(MockDropTablespaceDir::get(tablespacePath)); + } } -} } - diff --git a/libpqpp/pq-mock.h b/libpqpp/pq-mock.h index 4218f43..a8d9143 100644 --- a/libpqpp/pq-mock.h +++ b/libpqpp/pq-mock.h @@ -1,31 +1,30 @@ #ifndef MOCKPQDATASOURCE_H #define MOCKPQDATASOURCE_H -#include <mockDatabase.h> +#include "pq-connection.h" #include <filesystem> +#include <mockDatabase.h> #include <visibility.h> -#include "pq-connection.h" namespace PQ { class DLL_PUBLIC Mock : public DB::MockServerDatabase { - public: - Mock(const std::string & master, const std::string & name, const std::vector<std::filesystem::path> & ss); - ~Mock() override; + public: + Mock(const std::string & master, const std::string & name, const std::vector<std::filesystem::path> & ss); + ~Mock() override; - SPECIAL_MEMBERS_MOVE_RO(Mock); + SPECIAL_MEMBERS_MOVE_RO(Mock); - [[nodiscard]] DB::ConnectionPtr openConnection() const override; + [[nodiscard]] DB::ConnectionPtr openConnection() const override; - protected: - void CreateNewDatabase() const override; - void DropDatabase() const override; - void SetTablesToUnlogged() const; - [[nodiscard]] bool hasUnloggedTables() const; - [[nodiscard]] bool hasCopyToProgram() const; - const std::filesystem::path tablespacePath; - const int serverVersion; + protected: + void CreateNewDatabase() const override; + void DropDatabase() const override; + void SetTablesToUnlogged() const; + [[nodiscard]] bool hasUnloggedTables() const; + [[nodiscard]] bool hasCopyToProgram() const; + const std::filesystem::path tablespacePath; + const int serverVersion; }; } #endif - diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index 797c678..4c25a7e 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -1,19 +1,18 @@ #include "pq-modifycommand.h" +#include "pq-connection.h" #include "pq-error.h" #include <cstdlib> -#include "pq-connection.h" PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, const DB::CommandOptionsCPtr & opts) : - DB::Command(sql), - DB::ModifyCommand(sql), - PQ::PreparedStatement(conn, sql, opts) + DB::Command(sql), DB::ModifyCommand(sql), PQ::PreparedStatement(conn, sql, opts) { } unsigned int PQ::ModifyCommand::execute(bool anc) { - PGresult * res = PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0); + PGresult * res + = PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0); c->checkResult(res, PGRES_COMMAND_OK, PGRES_TUPLES_OK); unsigned int rows = atoi(PQcmdTuples(res)); PQclear(res); @@ -22,4 +21,3 @@ PQ::ModifyCommand::execute(bool anc) } return rows; } - diff --git a/libpqpp/pq-modifycommand.h b/libpqpp/pq-modifycommand.h index 0aac386..36de37a 100644 --- a/libpqpp/pq-modifycommand.h +++ b/libpqpp/pq-modifycommand.h @@ -1,20 +1,17 @@ #ifndef PQ_MODIFYCOMMAND_H #define PQ_MODIFYCOMMAND_H -#include <modifycommand.h> -#include "pq-prepared.h" #include "pq-connection.h" +#include "pq-prepared.h" +#include <modifycommand.h> namespace PQ { class ModifyCommand : public DB::ModifyCommand, public PreparedStatement { - public: - ModifyCommand(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); + public: + ModifyCommand(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); - unsigned int execute(bool) override; + unsigned int execute(bool) override; }; } #endif - - - diff --git a/libpqpp/pq-prepared.cpp b/libpqpp/pq-prepared.cpp index 3162349..287eda8 100644 --- a/libpqpp/pq-prepared.cpp +++ b/libpqpp/pq-prepared.cpp @@ -2,9 +2,7 @@ #include "pq-connection.h" PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, const DB::CommandOptionsCPtr & opts) : - DB::Command(sql), - Command(c, sql, opts), - pstmt(nullptr) + DB::Command(sql), Command(c, sql, opts), pstmt(nullptr) { } @@ -20,8 +18,7 @@ PQ::PreparedStatement::prepare() const } std::stringstream psql; prepareSql(psql, sql); - c->checkResultFree(PQprepare( - c->conn, stmntName.c_str(), psql.str().c_str(), values.size(), nullptr), PGRES_COMMAND_OK); + c->checkResultFree( + PQprepare(c->conn, stmntName.c_str(), psql.str().c_str(), values.size(), nullptr), PGRES_COMMAND_OK); return (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str()); } - diff --git a/libpqpp/pq-prepared.h b/libpqpp/pq-prepared.h index 6b6dff1..6d32f5f 100644 --- a/libpqpp/pq-prepared.h +++ b/libpqpp/pq-prepared.h @@ -5,14 +5,13 @@ namespace PQ { class PreparedStatement : public Command { - protected: - PreparedStatement(Connection *, const std::string &, const DB::CommandOptionsCPtr &); - virtual ~PreparedStatement() = default; + protected: + PreparedStatement(Connection *, const std::string &, const DB::CommandOptionsCPtr &); + virtual ~PreparedStatement() = default; - const char * prepare() const; - mutable const char * pstmt; + const char * prepare() const; + mutable const char * pstmt; }; } #endif - diff --git a/libpqpp/pq-selectbase.cpp b/libpqpp/pq-selectbase.cpp index 93dee50..49b78d2 100644 --- a/libpqpp/pq-selectbase.cpp +++ b/libpqpp/pq-selectbase.cpp @@ -1,14 +1,10 @@ #include "pq-selectbase.h" -#include "pq-column.h" #include "pq-binarycolumn.h" +#include "pq-column.h" #include "pq-command.h" PQ::SelectBase::SelectBase(const std::string & sql, const PQ::CommandOptionsCPtr & pqco) : - DB::Command(sql), - DB::SelectCommand(sql), - nTuples(0), - tuple(0), - execRes(nullptr), + DB::Command(sql), DB::SelectCommand(sql), nTuples(0), tuple(0), execRes(nullptr), binary(pqco ? pqco->fetchBinary : false) { } @@ -33,4 +29,3 @@ PQ::SelectBase::createColumns(PGresult * execRes) } } } - diff --git a/libpqpp/pq-selectbase.h b/libpqpp/pq-selectbase.h index bda6348..87dcc9d 100644 --- a/libpqpp/pq-selectbase.h +++ b/libpqpp/pq-selectbase.h @@ -1,25 +1,24 @@ #ifndef PQ_SELECTBASE_H #define PQ_SELECTBASE_H +#include "pq-command.h" #include <libpq-fe.h> #include <selectcommand.h> -#include "pq-command.h" namespace PQ { class SelectBase : public DB::SelectCommand { friend class Column; - protected: - SelectBase(const std::string & sql, const PQ::CommandOptionsCPtr & pqco); - ~SelectBase(); + protected: + SelectBase(const std::string & sql, const PQ::CommandOptionsCPtr & pqco); + ~SelectBase(); - void createColumns(PGresult *); + void createColumns(PGresult *); - unsigned int nTuples, tuple; - PGresult * execRes; - bool binary; + unsigned int nTuples, tuple; + PGresult * execRes; + bool binary; }; } #endif - diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 660a388..76a5d5d 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -1,33 +1,33 @@ #define BOOST_TEST_MODULE TestPQ #include <boost/test/unit_test.hpp> -#include <definedDirs.h> -#include <modifycommand.h> -#include <selectcommand.h> +#include <boost/date_time/posix_time/posix_time.hpp> #include <column.h> -#include <pq-mock.h> -#include <testCore.h> +#include <definedDirs.h> +#include <fileUtils.h> #include <fstream> -#include <boost/date_time/posix_time/posix_time.hpp> -#include <pq-error.h> -#include <pq-connection.h> +#include <modifycommand.h> #include <pq-command.h> +#include <pq-connection.h> +#include <pq-error.h> +#include <pq-mock.h> +#include <selectcommand.h> #include <selectcommandUtil.impl.h> -#include <fileUtils.h> +#include <testCore.h> class StandardMockDatabase : public DB::PluginMock<PQ::Mock> { - public: - StandardMockDatabase() : DB::PluginMock<PQ::Mock>("PQmock", { - rootDir / "pqschema.sql" }, "user=postgres dbname=postgres") - { - } +public: + StandardMockDatabase() : + DB::PluginMock<PQ::Mock>("PQmock", {rootDir / "pqschema.sql"}, "user=postgres dbname=postgres") + { + } }; -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE(StandardMockDatabase); -BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); +BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore); -BOOST_AUTO_TEST_CASE( transactions ) +BOOST_AUTO_TEST_CASE(transactions) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE( transactions ) BOOST_REQUIRE_EQUAL(false, ro->inTx()); } -BOOST_AUTO_TEST_CASE( bindAndSend ) +BOOST_AUTO_TEST_CASE(bindAndSend) { auto rw = DB::MockDatabase::openConnectionTo("PQmock"); @@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) BOOST_REQUIRE_EQUAL(0, mod->execute(true)); } -BOOST_AUTO_TEST_CASE( bindAndSelect ) +BOOST_AUTO_TEST_CASE(bindAndSelect) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE( bindAndSelect ) BOOST_REQUIRE_EQUAL(1, rows); } -BOOST_AUTO_TEST_CASE( selectInTx ) +BOOST_AUTO_TEST_CASE(selectInTx) { auto db = DB::MockDatabase::openConnectionTo("PQmock"); @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE( selectInTx ) db->finish(); } -BOOST_AUTO_TEST_CASE( bindAndSelectOther ) +BOOST_AUTO_TEST_CASE(bindAndSelectOther) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -136,14 +136,15 @@ BOOST_AUTO_TEST_CASE( bindAndSelectOther ) assertColumnValueHelper(*select, 1, 123.45); assertColumnValueHelper(*select, 2, std::string_view("some text with a ; in it and a ' too")); assertColumnValueHelper(*select, 3, true); - assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, nullptr})); + assertColumnValueHelper( + *select, 4, boost::posix_time::ptime_from_tm({3, 6, 23, 27, 3, 115, 0, 0, 0, 0, nullptr})); assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12)); rows += 1; } BOOST_REQUIRE_EQUAL(1, rows); } -BOOST_AUTO_TEST_CASE( testP2MockScriptDir ) +BOOST_AUTO_TEST_CASE(testP2MockScriptDir) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -156,7 +157,7 @@ BOOST_AUTO_TEST_CASE( testP2MockScriptDir ) } } -BOOST_AUTO_TEST_CASE( bulkload ) +BOOST_AUTO_TEST_CASE(bulkload) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -172,21 +173,21 @@ BOOST_AUTO_TEST_CASE( bulkload ) throw std::runtime_error("Couldn't open bulk.sample"); } std::array<char, BUFSIZ> buf {}; - for (std::streamsize r; (r = in.readsome(buf.data(), buf.size())) > 0; ) { + for (std::streamsize r; (r = in.readsome(buf.data(), buf.size())) > 0;) { ro->bulkUploadData(buf.data(), r); } ro->endBulkUpload(nullptr); assertScalarValueHelper(*count, 800); } -BOOST_AUTO_TEST_CASE( nofetch ) +BOOST_AUTO_TEST_CASE(nofetch) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto count = ro->select("SELECT * FROM bulktest"); count->execute(); } -BOOST_AUTO_TEST_CASE( bigIterate ) +BOOST_AUTO_TEST_CASE(bigIterate) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); @@ -198,7 +199,7 @@ BOOST_AUTO_TEST_CASE( bigIterate ) BOOST_REQUIRE_EQUAL(800, rows); } -BOOST_AUTO_TEST_CASE( insertId ) +BOOST_AUTO_TEST_CASE(insertId) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto ins = ro->modify("INSERT INTO idtest(foo) VALUES(1)"); @@ -208,7 +209,7 @@ BOOST_AUTO_TEST_CASE( insertId ) } } -BOOST_AUTO_TEST_CASE( reconnect ) +BOOST_AUTO_TEST_CASE(reconnect) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto rok = DB::MockDatabase::openConnectionTo("PQmock"); @@ -228,7 +229,7 @@ BOOST_AUTO_TEST_CASE( reconnect ) ro->modify("TRUNCATE TABLE test")->execute(); } -BOOST_AUTO_TEST_CASE( reconnectInTx ) +BOOST_AUTO_TEST_CASE(reconnectInTx) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto rok = DB::MockDatabase::openConnectionTo("PQmock"); @@ -244,7 +245,7 @@ BOOST_AUTO_TEST_CASE( reconnectInTx ) BOOST_REQUIRE_THROW(ro->ping(), DB::ConnectionError); } -BOOST_AUTO_TEST_CASE( statementReuse ) +BOOST_AUTO_TEST_CASE(statementReuse) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro); @@ -267,7 +268,7 @@ BOOST_AUTO_TEST_CASE( statementReuse ) } } -BOOST_AUTO_TEST_CASE( bulkSelect ) +BOOST_AUTO_TEST_CASE(bulkSelect) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto co = std::make_shared<PQ::CommandOptions>(0, 35, false); @@ -275,14 +276,14 @@ BOOST_AUTO_TEST_CASE( bulkSelect ) sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { - totalInt += i; - count += 1; - }); + totalInt += i; + count += 1; + }); BOOST_REQUIRE_EQUAL(20, totalInt); BOOST_REQUIRE_EQUAL(8, count); } -BOOST_AUTO_TEST_CASE( selectWithSmallPages ) +BOOST_AUTO_TEST_CASE(selectWithSmallPages) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto co = std::make_shared<PQ::CommandOptions>(0, 1, true); @@ -290,14 +291,14 @@ BOOST_AUTO_TEST_CASE( selectWithSmallPages ) sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { - totalInt += i; - count += 1; - }); + totalInt += i; + count += 1; + }); BOOST_REQUIRE_EQUAL(20, totalInt); BOOST_REQUIRE_EQUAL(8, count); } -BOOST_AUTO_TEST_CASE( dateoid ) +BOOST_AUTO_TEST_CASE(dateoid) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto co = std::make_shared<PQ::CommandOptions>(0, 1, false); @@ -307,52 +308,60 @@ BOOST_AUTO_TEST_CASE( dateoid ) } } -BOOST_AUTO_TEST_CASE( insertReturning ) +BOOST_AUTO_TEST_CASE(insertReturning) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto co = std::make_shared<PQ::CommandOptions>(0, 35, false); auto sel = ro->select("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", co); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { - totalInt += i; - count += 1; - }); + totalInt += i; + count += 1; + }); BOOST_REQUIRE_EQUAL(4, totalInt); BOOST_REQUIRE_EQUAL(1, count); } -BOOST_AUTO_TEST_CASE( closeOnError ) +BOOST_AUTO_TEST_CASE(closeOnError) { auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); - BOOST_REQUIRE_THROW({ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ + BOOST_REQUIRE_THROW( + { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { ro->execute("nonsense"); }); - }, DB::Error); - BOOST_REQUIRE_THROW({ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ + }, + DB::Error); + BOOST_REQUIRE_THROW( + { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { ro->execute("nonsense"); }); }); - }, DB::Error); + }, + DB::Error); ro->beginTx(); - BOOST_REQUIRE_THROW({ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ + BOOST_REQUIRE_THROW( + { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { ro->execute("nonsense"); }); - }, DB::Error); - BOOST_REQUIRE_THROW({ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ - ro->select("SELECT * FROM test")->forEachRow<>([&ro](){ + }, + DB::Error); + BOOST_REQUIRE_THROW( + { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { + ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { ro->execute("nonsense"); }); }); - }, DB::Error); + }, + DB::Error); ro->commitTx(); } -BOOST_AUTO_TEST_CASE( blobs ) +BOOST_AUTO_TEST_CASE(blobs) { auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); std::vector<char> buf(29); @@ -377,7 +386,7 @@ BOOST_AUTO_TEST_CASE( blobs ) } } -BOOST_AUTO_TEST_CASE( fetchAsBinary ) +BOOST_AUTO_TEST_CASE(fetchAsBinary) { auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); std::vector<char> buf(29); @@ -396,7 +405,8 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) BOOST_REQUIRE_EQUAL(r.value<0>(), blob); } *opts->hash += 1; - sel = ro->select("SELECT CAST(length(data) AS BIGINT) big, CAST(length(data) AS SMALLINT) small FROM blobtest", opts); + sel = ro->select( + "SELECT CAST(length(data) AS BIGINT) big, CAST(length(data) AS SMALLINT) small FROM blobtest", opts); for (const auto & r : sel->as<int64_t, int64_t>()) { BOOST_REQUIRE_EQUAL(r.value<0>(), buf.size()); BOOST_REQUIRE_EQUAL(r.value<1>(), buf.size()); @@ -420,7 +430,7 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) } } -BOOST_AUTO_TEST_CASE( largeBlob ) +BOOST_AUTO_TEST_CASE(largeBlob) { auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); ro->execute("TRUNCATE TABLE blobtest"); @@ -441,7 +451,7 @@ BOOST_AUTO_TEST_CASE( largeBlob ) } } -BOOST_AUTO_TEST_CASE( bulkPerfTest ) +BOOST_AUTO_TEST_CASE(bulkPerfTest) { auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); auto sel = ro->select(R"SQL(select s a, cast(s as numeric(7,1)) b, cast(s as text) c, @@ -450,8 +460,8 @@ BOOST_AUTO_TEST_CASE( bulkPerfTest ) from generate_series(1, 1000) s)SQL"); int64_t tot = 0; - for (const auto & [a,b,c,d,e,f] : sel->as<int64_t, double, std::string_view, - boost::posix_time::time_duration, boost::posix_time::ptime, bool>()) { + for (const auto & [a, b, c, d, e, f] : sel->as<int64_t, double, std::string_view, boost::posix_time::time_duration, + boost::posix_time::ptime, bool>()) { tot += a + b + c.length() + d.hours() + e.time_of_day().hours() + f; } BOOST_REQUIRE_EQUAL(tot, 1013265); @@ -459,11 +469,10 @@ BOOST_AUTO_TEST_CASE( bulkPerfTest ) BOOST_AUTO_TEST_SUITE_END(); -BOOST_AUTO_TEST_CASE( connfail ) +BOOST_AUTO_TEST_CASE(connfail) { BOOST_REQUIRE_THROW( - (void)DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), - DB::ConnectionError); + (void)DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), DB::ConnectionError); try { (void)DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"); } @@ -472,12 +481,12 @@ BOOST_AUTO_TEST_CASE( connfail ) } } -BOOST_AUTO_TEST_CASE( ssl ) +BOOST_AUTO_TEST_CASE(ssl) { - auto conn = DB::ConnectionFactory::createNew("postgresql", "host=randomdan.homeip.net user=gentoo dbname=postgres sslmode=require"); + auto conn = DB::ConnectionFactory::createNew( + "postgresql", "host=randomdan.homeip.net user=gentoo dbname=postgres sslmode=require"); BOOST_REQUIRE(conn); auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(conn); BOOST_REQUIRE(pqconn); BOOST_REQUIRE(PQgetssl(pqconn->conn)); } - |