summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libpqpp/pq-connection.cpp14
-rw-r--r--libpqpp/pq-connection.h4
-rw-r--r--libpqpp/pq-mock.cpp6
-rw-r--r--libpqpp/pq-mock.h2
-rw-r--r--libpqpp/unittests/testpq.cpp88
5 files changed, 37 insertions, 77 deletions
diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp
index e189acf..214f211 100644
--- a/libpqpp/pq-connection.cpp
+++ b/libpqpp/pq-connection.cpp
@@ -96,20 +96,20 @@ PQ::Connection::ping() const
}
-DB::SelectCommand *
-PQ::Connection::newSelectCommand(const std::string & sql, const DB::CommandOptions * opts)
+DB::SelectCommandPtr
+PQ::Connection::select(const std::string & sql, const DB::CommandOptions * opts)
{
auto pqco = dynamic_cast<const CommandOptions *>(opts);
if (pqco && !pqco->useCursor) {
- return new BulkSelectCommand(this, sql, pqco, opts);
+ return std::make_shared<BulkSelectCommand>(this, sql, pqco, opts);
}
- return new CursorSelectCommand(this, sql, pqco, opts);
+ return std::make_shared<CursorSelectCommand>(this, sql, pqco, opts);
}
-DB::ModifyCommand *
-PQ::Connection::newModifyCommand(const std::string & sql, const DB::CommandOptions * opts)
+DB::ModifyCommandPtr
+PQ::Connection::modify(const std::string & sql, const DB::CommandOptions * opts)
{
- return new ModifyCommand(this, sql, opts);
+ return std::make_shared<ModifyCommand>(this, sql, opts);
}
bool
diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h
index bb102df..0a7fd0a 100644
--- a/libpqpp/pq-connection.h
+++ b/libpqpp/pq-connection.h
@@ -29,8 +29,8 @@ namespace PQ {
DB::BulkDeleteStyle bulkDeleteStyle() const override;
DB::BulkUpdateStyle bulkUpdateStyle() const override;
- DB::SelectCommand * newSelectCommand(const std::string & sql, const DB::CommandOptions *) override;
- DB::ModifyCommand * newModifyCommand(const std::string & sql, const DB::CommandOptions *) 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;
int64_t insertId() override;
int serverVersion() const;
diff --git a/libpqpp/pq-mock.cpp b/libpqpp/pq-mock.cpp
index f2090a4..6cc114f 100644
--- a/libpqpp/pq-mock.cpp
+++ b/libpqpp/pq-mock.cpp
@@ -19,17 +19,17 @@ Mock::Mock(const std::string & masterdb, const std::string & name, const std::ve
}
AdHocFormatter(MockConnStr, "user=postgres dbname=%?");
-PQ::Connection *
+DB::ConnectionPtr
Mock::openConnection() const
{
- return new Connection(MockConnStr::get(boost::algorithm::to_lower_copy(testDbName)));
+ return std::make_shared<Connection>(MockConnStr::get(boost::algorithm::to_lower_copy(testDbName)));
}
AdHocFormatter(MockSetUnlogged, "ALTER TABLE %?.%? SET UNLOGGED");
void
Mock::SetTablesToUnlogged() const
{
- auto c = ConnectionPtr(openConnection());
+ auto c = std::static_pointer_cast<Connection>(openConnection());
if (c->serverVersion() < 90500) return;
auto s = c->select(R"SQL(
SELECT n.nspname, c.relname
diff --git a/libpqpp/pq-mock.h b/libpqpp/pq-mock.h
index f712ef6..8ab2fe1 100644
--- a/libpqpp/pq-mock.h
+++ b/libpqpp/pq-mock.h
@@ -12,7 +12,7 @@ namespace PQ {
Mock(const std::string & master, const std::string & name, const std::vector<boost::filesystem::path> & ss);
~Mock();
- PQ::Connection * openConnection() const override;
+ DB::ConnectionPtr openConnection() const override;
protected:
void DropDatabase() const override;
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp
index 56ab20f..59389c8 100644
--- a/libpqpp/unittests/testpq.cpp
+++ b/libpqpp/unittests/testpq.cpp
@@ -15,9 +15,9 @@
#include <selectcommandUtil.impl.h>
#include <fileUtils.h>
-class StandardMockDatabase : public PQ::Mock {
+class StandardMockDatabase : public DB::PluginMock<PQ::Mock> {
public:
- StandardMockDatabase() : PQ::Mock("user=postgres dbname=postgres", "PQmock", {
+ StandardMockDatabase() : DB::PluginMock<PQ::Mock>("user=postgres dbname=postgres", "PQmock", {
rootDir / "pqschema.sql" })
{
}
@@ -41,15 +41,13 @@ BOOST_AUTO_TEST_CASE( transactions )
BOOST_REQUIRE_EQUAL(true, ro->inTx());
ro->commitTx();
BOOST_REQUIRE_EQUAL(false, ro->inTx());
-
- delete ro;
}
BOOST_AUTO_TEST_CASE( bindAndSend )
{
auto rw = DB::MockDatabase::openConnectionTo("PQmock");
- auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)");
+ auto mod = rw->modify("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)");
mod->bindParamI(0, testInt);
mod->bindParamF(1, testDouble);
mod->bindParamS(2, testString);
@@ -78,19 +76,16 @@ BOOST_AUTO_TEST_CASE( bindAndSend )
mod->bindParamS(4, "2016-01-01T12:34:56");
mod->bindNull(5);
mod->execute();
- delete mod;
- mod = rw->newModifyCommand("DELETE FROM test WHERE string = '?'");
+ mod = rw->modify("DELETE FROM test WHERE string = '?'");
BOOST_REQUIRE_THROW(mod->execute(false), DB::NoRowsAffected);
BOOST_REQUIRE_EQUAL(0, mod->execute(true));
- delete mod;
- delete rw;
}
BOOST_AUTO_TEST_CASE( bindAndSelect )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?");
+ auto select = ro->select("SELECT * FROM test WHERE id = ?");
select->bindParamI(0, testInt);
select->execute();
int rows = 0;
@@ -103,9 +98,7 @@ BOOST_AUTO_TEST_CASE( bindAndSelect )
assertColumnValueHelper(*select, 5, testInterval);
rows += 1;
}
- delete select;
BOOST_REQUIRE_EQUAL(1, rows);
- delete ro;
}
BOOST_AUTO_TEST_CASE( selectInTx )
@@ -123,20 +116,17 @@ BOOST_AUTO_TEST_CASE( selectInTx )
db->finish();
db->beginTx();
- auto select = db->newSelectCommand("SELECT * FROM test");
+ auto select = db->select("SELECT * FROM test");
while (select->fetch()) { }
- delete select;
db->commitTx();
db->finish();
-
- delete db;
}
BOOST_AUTO_TEST_CASE( bindAndSelectOther )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ? AND id != ?");
+ auto select = ro->select("SELECT * FROM test WHERE id != ? AND id != ?");
select->bindParamI(0, testInt);
select->bindParamI(1, testInt + 10);
select->execute();
@@ -150,31 +140,27 @@ BOOST_AUTO_TEST_CASE( bindAndSelectOther )
assertColumnValueHelper(*select, 5, boost::posix_time::time_duration(38, 13, 12));
rows += 1;
}
- delete select;
BOOST_REQUIRE_EQUAL(1, rows);
- delete ro;
}
BOOST_AUTO_TEST_CASE( testP2MockScriptDir )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto select = ro->newSelectCommand("SELECT path FROM test2");
+ auto select = ro->select("SELECT path FROM test2");
select->execute();
while (select->fetch()) {
std::string path;
(*select)[0] >> path;
BOOST_REQUIRE(boost::filesystem::exists(path));
}
- delete select;
- delete ro;
}
BOOST_AUTO_TEST_CASE( bulkload )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest");
+ auto count = ro->select("SELECT COUNT(*) FROM bulktest");
// Test empty
ro->beginBulkUpload("bulktest", "");
ro->endBulkUpload(NULL);
@@ -189,93 +175,77 @@ BOOST_AUTO_TEST_CASE( bulkload )
}
ro->endBulkUpload(NULL);
assertScalarValueHelper(*count, 800);
-
- delete count;
- delete ro;
}
BOOST_AUTO_TEST_CASE( nofetch )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto count = ro->newSelectCommand("SELECT * FROM bulktest");
+ auto count = ro->select("SELECT * FROM bulktest");
count->execute();
- delete count;
- delete ro;
}
BOOST_AUTO_TEST_CASE( bigIterate )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto count = ro->newSelectCommand("SELECT * FROM bulktest");
+ auto count = ro->select("SELECT * FROM bulktest");
unsigned int rows = 0;
while (count->fetch()) {
rows += 1;
}
BOOST_REQUIRE_EQUAL(800, rows);
-
- delete count;
- delete ro;
}
BOOST_AUTO_TEST_CASE( insertId )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto ins = ro->newModifyCommand("INSERT INTO idtest(foo) VALUES(1)");
+ auto ins = ro->modify("INSERT INTO idtest(foo) VALUES(1)");
for (int x = 1; x < 4; x++) {
ins->execute();
BOOST_REQUIRE_EQUAL(x, ro->insertId());
}
- delete ins;
- delete ro;
}
BOOST_AUTO_TEST_CASE( reconnect )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
auto rok = DB::MockDatabase::openConnectionTo("PQmock");
- auto pqconn = dynamic_cast<PQ::Connection *>(ro);
+ auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro);
int pid1 = PQbackendPID(pqconn->conn);
BOOST_REQUIRE(pid1);
ro->ping();
ro->modify("TRUNCATE TABLE test")->execute();
- auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
+ auto kil = rok->modify("SELECT pg_terminate_backend(?)");
kil->bindParamI(0, pid1);
kil->execute();
- delete kil;
usleep(5000);
ro->ping();
int pid2 = PQbackendPID(pqconn->conn);
BOOST_REQUIRE(pid2);
BOOST_REQUIRE(pid1 != pid2);
ro->modify("TRUNCATE TABLE test")->execute();
- delete ro;
- delete rok;
}
BOOST_AUTO_TEST_CASE( reconnectInTx )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
auto rok = DB::MockDatabase::openConnectionTo("PQmock");
- auto pqconn = dynamic_cast<PQ::Connection *>(ro);
+ auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro);
int pid1 = PQbackendPID(pqconn->conn);
BOOST_REQUIRE(pid1);
ro->ping();
ro->beginTx();
- auto kil = rok->newModifyCommand("SELECT pg_terminate_backend(?)");
+ auto kil = rok->modify("SELECT pg_terminate_backend(?)");
kil->bindParamI(0, pid1);
kil->execute();
- delete kil;
usleep(5000);
BOOST_REQUIRE_THROW(ro->ping(), DB::ConnectionError);
- delete ro;
- delete rok;
}
BOOST_AUTO_TEST_CASE( statementReuse )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
- auto pqconn = dynamic_cast<PQ::Connection *>(ro);
+ auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(ro);
BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 0);
ro->modify("DELETE FROM test")->execute();
BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 1);
@@ -288,47 +258,41 @@ BOOST_AUTO_TEST_CASE( statementReuse )
}
}
BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 2);
- auto select = ro->newSelectCommand("SELECT COUNT(id), SUM(id) FROM test");
+ auto select = ro->select("SELECT COUNT(id), SUM(id) FROM test");
while (select->fetch()) {
assertColumnValueHelper(*select, 0, 16);
assertColumnValueHelper(*select, 1, 24);
}
- delete select;
- delete ro;
}
BOOST_AUTO_TEST_CASE( bulkSelect )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
PQ::CommandOptions co(0, 35, false);
- auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co);
+ 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) {
totalInt += i;
count += 1;
});
- delete sel;
BOOST_REQUIRE_EQUAL(20, totalInt);
BOOST_REQUIRE_EQUAL(8, count);
- delete ro;
}
BOOST_AUTO_TEST_CASE( selectWithSmallPages )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
PQ::CommandOptions co(0, 1, true);
- auto sel = ro->newSelectCommand("SELECT * FROM test WHERE id > ?", &co);
+ 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) {
totalInt += i;
count += 1;
});
- delete sel;
BOOST_REQUIRE_EQUAL(20, totalInt);
BOOST_REQUIRE_EQUAL(8, count);
- delete ro;
}
BOOST_AUTO_TEST_CASE( dateoid )
@@ -339,23 +303,20 @@ BOOST_AUTO_TEST_CASE( dateoid )
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>());
}
- delete ro;
}
BOOST_AUTO_TEST_CASE( insertReturning )
{
auto ro = DB::MockDatabase::openConnectionTo("PQmock");
PQ::CommandOptions co(0, 35, false);
- auto sel = ro->newSelectCommand("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", &co);
+ 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;
});
- delete sel;
BOOST_REQUIRE_EQUAL(4, totalInt);
BOOST_REQUIRE_EQUAL(1, count);
- delete ro;
}
BOOST_AUTO_TEST_CASE( closeOnError )
@@ -424,7 +385,7 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary )
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, boost::optional<std::string>, int64_t>()) {
+ 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());
BOOST_REQUIRE(r.value<1>());
@@ -451,7 +412,7 @@ BOOST_AUTO_TEST_CASE( fetchAsBinary )
}
*opts.hash += 1;
sel = ro->select("SELECT NULL, now()", &opts);
- for (const auto & r : sel->as<boost::optional<int64_t>, boost::posix_time::ptime>()) {
+ 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);
}
@@ -495,9 +456,8 @@ BOOST_AUTO_TEST_CASE( ssl )
{
auto conn = DB::ConnectionFactory::createNew("postgresql", "host=randomdan.homeip.net user=gentoo dbname=postgres sslmode=require");
BOOST_REQUIRE(conn);
- auto pqconn = dynamic_cast<PQ::Connection *>(conn);
+ auto pqconn = std::dynamic_pointer_cast<PQ::Connection>(conn);
BOOST_REQUIRE(pqconn);
BOOST_REQUIRE(PQgetssl(pqconn->conn));
- delete conn;
}