diff options
-rw-r--r-- | libpqpp/pq-bulkselectcommand.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-bulkselectcommand.h | 2 | ||||
-rw-r--r-- | libpqpp/pq-command.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-command.h | 4 | ||||
-rw-r--r-- | libpqpp/pq-connection.cpp | 12 | ||||
-rw-r--r-- | libpqpp/pq-connection.h | 6 | ||||
-rw-r--r-- | libpqpp/pq-cursorselectcommand.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-cursorselectcommand.h | 2 | ||||
-rw-r--r-- | libpqpp/pq-modifycommand.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-modifycommand.h | 2 | ||||
-rw-r--r-- | libpqpp/pq-prepared.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-prepared.h | 2 | ||||
-rw-r--r-- | libpqpp/pq-selectbase.cpp | 2 | ||||
-rw-r--r-- | libpqpp/pq-selectbase.h | 4 | ||||
-rw-r--r-- | libpqpp/unittests/testpq.cpp | 48 |
15 files changed, 48 insertions, 46 deletions
diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp index c9abec5..81f3e48 100644 --- a/libpqpp/pq-bulkselectcommand.cpp +++ b/libpqpp/pq-bulkselectcommand.cpp @@ -3,7 +3,7 @@ #include "pq-column.h" #include "pq-error.h" -PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, const PQ::CommandOptions * pqco, const DB::CommandOptions * 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), diff --git a/libpqpp/pq-bulkselectcommand.h b/libpqpp/pq-bulkselectcommand.h index 6fcf883..5ff8a88 100644 --- a/libpqpp/pq-bulkselectcommand.h +++ b/libpqpp/pq-bulkselectcommand.h @@ -11,7 +11,7 @@ namespace PQ { class Column; class BulkSelectCommand : public SelectBase, public PreparedStatement { public: - BulkSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptions * pqco, const DB::CommandOptions *); + BulkSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptionsCPtr & pqco, const DB::CommandOptionsCPtr &); bool fetch() override; void execute() override; diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 9fd4002..58b5c49 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -9,7 +9,7 @@ NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory); AdHocFormatter(PQCommondStatement, "pStatement_id%?"); -PQ::Command::Command(Connection * conn, const std::string & sql, const DB::CommandOptions * opts) : +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)), diff --git a/libpqpp/pq-command.h b/libpqpp/pq-command.h index 2431639..c2674eb 100644 --- a/libpqpp/pq-command.h +++ b/libpqpp/pq-command.h @@ -22,10 +22,12 @@ namespace PQ { bool useCursor; bool fetchBinary; }; + typedef std::shared_ptr<CommandOptions> CommandOptionsPtr; + typedef std::shared_ptr<const CommandOptions> CommandOptionsCPtr; class Command : public virtual DB::Command { public: - Command(Connection *, const std::string & sql, const DB::CommandOptions *); + Command(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); virtual ~Command() = 0; void bindParamI(unsigned int, int) override; diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index 214f211..3ec8991 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -62,7 +62,7 @@ PQ::Connection::rollbackTxInt() } void -PQ::Connection::execute(const std::string & sql, const DB::CommandOptions *) +PQ::Connection::execute(const std::string & sql, const DB::CommandOptionsCPtr &) { checkResultFree(PQexec(conn, sql.c_str()), PGRES_COMMAND_OK, PGRES_TUPLES_OK); } @@ -97,9 +97,9 @@ PQ::Connection::ping() const DB::SelectCommandPtr -PQ::Connection::select(const std::string & sql, const DB::CommandOptions * opts) +PQ::Connection::select(const std::string & sql, const DB::CommandOptionsCPtr & opts) { - auto pqco = dynamic_cast<const CommandOptions *>(opts); + auto pqco = std::dynamic_pointer_cast<const CommandOptions>(opts); if (pqco && !pqco->useCursor) { return std::make_shared<BulkSelectCommand>(this, sql, pqco, opts); } @@ -107,7 +107,7 @@ PQ::Connection::select(const std::string & sql, const DB::CommandOptions * opts) } DB::ModifyCommandPtr -PQ::Connection::modify(const std::string & sql, const DB::CommandOptions * opts) +PQ::Connection::modify(const std::string & sql, const DB::CommandOptionsCPtr & opts) { return std::make_shared<ModifyCommand>(this, sql, opts); } @@ -176,11 +176,11 @@ PQ::Connection::bulkUploadData(const char * data, size_t len) const } static const std::string selectLastVal("SELECT lastval()"); -static const DB::CommandOptions selectLastValOpts(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() { - BulkSelectCommand getId(this, selectLastVal, nullptr, &selectLastValOpts); + BulkSelectCommand getId(this, selectLastVal, nullptr, selectLastValOpts); int64_t id = -1; while (getId.fetch()) { getId[0] >> id; diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h index 0a7fd0a..597c543 100644 --- a/libpqpp/pq-connection.h +++ b/libpqpp/pq-connection.h @@ -25,12 +25,12 @@ namespace PQ { void commitTxInt() override; void rollbackTxInt() override; void ping() const override; - void execute(const std::string & sql, const DB::CommandOptions * = nullptr) 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::CommandOptions * = nullptr) override; - DB::ModifyCommandPtr modify(const std::string & sql, const DB::CommandOptions * = 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; diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 53d951a..02c9fce 100644 --- a/libpqpp/pq-cursorselectcommand.cpp +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -7,7 +7,7 @@ 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::CommandOptions * pqco, const DB::CommandOptions * 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), diff --git a/libpqpp/pq-cursorselectcommand.h b/libpqpp/pq-cursorselectcommand.h index 4d47229..0f59687 100644 --- a/libpqpp/pq-cursorselectcommand.h +++ b/libpqpp/pq-cursorselectcommand.h @@ -11,7 +11,7 @@ namespace PQ { class Column; class CursorSelectCommand : public SelectBase, public Command { public: - CursorSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptions *, const DB::CommandOptions *); + CursorSelectCommand(Connection *, const std::string & sql, const PQ::CommandOptionsCPtr &, const DB::CommandOptionsCPtr &); virtual ~CursorSelectCommand(); bool fetch() override; diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index b2cf626..c82b3c6 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -3,7 +3,7 @@ #include <stdlib.h> #include "pq-connection.h" -PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, const DB::CommandOptions * opts) : +PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, const DB::CommandOptionsCPtr & opts) : DB::Command(sql), DB::ModifyCommand(sql), PQ::PreparedStatement(conn, sql, opts) diff --git a/libpqpp/pq-modifycommand.h b/libpqpp/pq-modifycommand.h index 18f5a8b..425e5f2 100644 --- a/libpqpp/pq-modifycommand.h +++ b/libpqpp/pq-modifycommand.h @@ -8,7 +8,7 @@ namespace PQ { class ModifyCommand : public DB::ModifyCommand, public PreparedStatement { public: - ModifyCommand(Connection *, const std::string & sql, const DB::CommandOptions *); + ModifyCommand(Connection *, const std::string & sql, const DB::CommandOptionsCPtr &); virtual ~ModifyCommand(); unsigned int execute(bool) override; diff --git a/libpqpp/pq-prepared.cpp b/libpqpp/pq-prepared.cpp index 63a1cb5..6c447f8 100644 --- a/libpqpp/pq-prepared.cpp +++ b/libpqpp/pq-prepared.cpp @@ -1,7 +1,7 @@ #include "pq-prepared.h" #include "pq-connection.h" -PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, const DB::CommandOptions * opts) : +PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, const DB::CommandOptionsCPtr & opts) : DB::Command(sql), Command(c, sql, opts), pstmt(nullptr) diff --git a/libpqpp/pq-prepared.h b/libpqpp/pq-prepared.h index d70a388..6b6dff1 100644 --- a/libpqpp/pq-prepared.h +++ b/libpqpp/pq-prepared.h @@ -6,7 +6,7 @@ namespace PQ { class PreparedStatement : public Command { protected: - PreparedStatement(Connection *, const std::string &, const DB::CommandOptions *); + PreparedStatement(Connection *, const std::string &, const DB::CommandOptionsCPtr &); virtual ~PreparedStatement() = default; const char * prepare() const; diff --git a/libpqpp/pq-selectbase.cpp b/libpqpp/pq-selectbase.cpp index 1024e5a..54b2a57 100644 --- a/libpqpp/pq-selectbase.cpp +++ b/libpqpp/pq-selectbase.cpp @@ -3,7 +3,7 @@ #include "pq-binarycolumn.h" #include "pq-command.h" -PQ::SelectBase::SelectBase(const std::string & sql, const PQ::CommandOptions * pqco) : +PQ::SelectBase::SelectBase(const std::string & sql, const PQ::CommandOptionsCPtr & pqco) : DB::Command(sql), DB::SelectCommand(sql), nTuples(0), diff --git a/libpqpp/pq-selectbase.h b/libpqpp/pq-selectbase.h index 804535d..2a01ede 100644 --- a/libpqpp/pq-selectbase.h +++ b/libpqpp/pq-selectbase.h @@ -3,14 +3,14 @@ #include <libpq-fe.h> #include <selectcommand.h> +#include "pq-command.h" namespace PQ { - class CommandOptions; class SelectBase : public DB::SelectCommand { friend class Column; protected: - SelectBase(const std::string & sql, const PQ::CommandOptions * pqco); + SelectBase(const std::string & sql, const PQ::CommandOptionsCPtr & pqco); ~SelectBase(); void createColumns(PGresult *); diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 59389c8..e8571b5 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -268,8 +268,8 @@ BOOST_AUTO_TEST_CASE( statementReuse ) BOOST_AUTO_TEST_CASE( bulkSelect ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - PQ::CommandOptions co(0, 35, false); - auto sel = ro->select("SELECT * FROM test WHERE id > ?", &co); + auto co = std::make_shared<PQ::CommandOptions>(0, 35, false); + auto sel = ro->select("SELECT * FROM test WHERE id > ?", co); sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { @@ -283,8 +283,8 @@ BOOST_AUTO_TEST_CASE( bulkSelect ) BOOST_AUTO_TEST_CASE( selectWithSmallPages ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - PQ::CommandOptions co(0, 1, true); - auto sel = ro->select("SELECT * FROM test WHERE id > ?", &co); + auto co = std::make_shared<PQ::CommandOptions>(0, 1, true); + auto sel = ro->select("SELECT * FROM test WHERE id > ?", co); sel->bindParamI(0, 1); int totalInt = 0, count = 0; sel->forEachRow<int64_t>([&totalInt, &count](auto i) { @@ -298,8 +298,8 @@ BOOST_AUTO_TEST_CASE( selectWithSmallPages ) BOOST_AUTO_TEST_CASE( dateoid ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - PQ::CommandOptions co(0, 1, false); - auto sel = ro->select("SELECT '2017-01-08'::date", &co); + auto co = std::make_shared<PQ::CommandOptions>(0, 1, false); + auto sel = ro->select("SELECT '2017-01-08'::date", co); for (const auto & r : sel->as<boost::posix_time::ptime>()) { BOOST_REQUIRE_EQUAL(boost::posix_time::ptime(boost::gregorian::date(2017, 1, 8)), r.value<0>()); } @@ -308,8 +308,8 @@ BOOST_AUTO_TEST_CASE( dateoid ) BOOST_AUTO_TEST_CASE( insertReturning ) { auto ro = DB::MockDatabase::openConnectionTo("PQmock"); - PQ::CommandOptions co(0, 35, false); - auto sel = ro->select("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", &co); + 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; @@ -381,10 +381,10 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) std::vector<char> buf(29); memcpy(&buf[0], "This is some binary text data", 29); DB::Blob blob(buf); - PQ::CommandOptions opts(0); - opts.fetchBinary = true; - opts.useCursor = false; - auto sel = ro->select("SELECT data, md5, length(data) FROM blobtest", &opts); + auto opts = std::make_shared<PQ::CommandOptions>(0); + opts->fetchBinary = true; + opts->useCursor = false; + auto sel = ro->select("SELECT data, md5, length(data) FROM blobtest", opts); for (const auto & r : sel->as<DB::Blob, std::optional<std::string>, int64_t>()) { // Assert the DB understood the insert BOOST_REQUIRE_EQUAL(r.value<2>(), buf.size()); @@ -393,25 +393,25 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary ) // Assert the fetch of the data is correct 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); + *opts->hash += 1; + 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()); } - *opts.hash += 1; - sel = ro->select("SELECT true a, false b", &opts); + *opts->hash += 1; + sel = ro->select("SELECT true a, false b", opts); for (const auto & r : sel->as<bool, bool>()) { BOOST_REQUIRE_EQUAL(r.value<0>(), true); BOOST_REQUIRE_EQUAL(r.value<1>(), false); } - *opts.hash += 1; - sel = ro->select("SELECT xmlelement(name xml)", &opts); + *opts->hash += 1; + sel = ro->select("SELECT xmlelement(name xml)", opts); for (const auto & r : sel->as<std::string>()) { BOOST_REQUIRE_EQUAL(r.value<0>(), "<xml/>"); } - *opts.hash += 1; - sel = ro->select("SELECT NULL, now()", &opts); + *opts->hash += 1; + sel = ro->select("SELECT NULL, now()", opts); for (const auto & r : sel->as<std::optional<int64_t>, boost::posix_time::ptime>()) { BOOST_REQUIRE(!r.value<0>()); BOOST_REQUIRE_THROW(r.value<1>(), DB::ColumnTypeNotSupported); @@ -429,10 +429,10 @@ BOOST_AUTO_TEST_CASE( largeBlob ) ins->bindParamBLOB(0, blob); ins->execute(); - PQ::CommandOptions opts(0); - opts.fetchBinary = true; - opts.useCursor = false; - auto sel = ro->select("SELECT data, length(data) FROM blobtest", &opts); + auto opts = std::make_shared<PQ::CommandOptions>(0); + opts->fetchBinary = true; + opts->useCursor = false; + auto sel = ro->select("SELECT data, length(data) FROM blobtest", opts); for (const auto & r : sel->as<DB::Blob, int64_t>()) { BOOST_REQUIRE_EQUAL(r.value<1>(), f.getStat().st_size); BOOST_REQUIRE_EQUAL(r.value<0>(), blob); |