From 142c4ee136448d51db58fa5904c3f0f51679758e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 9 Apr 2018 11:48:47 +0100 Subject: C++17 Replaces lots of things with new C++17 variants of them. Updates much code to be compatible with C++17 version libadhocutil. Note that this includes changes to how mock databases are instantiated. --- libdbpp/Jamfile.jam | 4 +-- libdbpp/column.h | 12 +++---- libdbpp/command.h | 4 +-- libdbpp/command_fwd.h | 8 ++--- libdbpp/connection.cpp | 26 ++++----------- libdbpp/connection.h | 22 ++++++------- libdbpp/connectionPool.cpp | 6 ++-- libdbpp/connectionPool.h | 10 +++--- libdbpp/connection_fwd.h | 5 +-- libdbpp/createMockDb.cpp | 2 +- libdbpp/mockDatabase.cpp | 23 +------------ libdbpp/mockDatabase.h | 34 ++++++++++++++----- libdbpp/modifycommand.h | 1 - libdbpp/selectcommand.h | 5 ++- libdbpp/tablepatch.cpp | 16 ++++----- libdbpp/tablepatch.h | 4 +-- libdbpp/unittests/Jamfile.jam | 4 +-- libdbpp/unittests/mockdb.cpp | 16 ++++----- libdbpp/unittests/mockdb.h | 6 ++-- libdbpp/unittests/testConnection.cpp | 14 +++----- libdbpp/unittests/testConnectionPool.cpp | 4 +-- libdbpp/unittests/testPatch.cpp | 4 +-- libdbpp/unittests/testUtils.cpp | 56 ++++++++++++++++---------------- 23 files changed, 134 insertions(+), 152 deletions(-) diff --git a/libdbpp/Jamfile.jam b/libdbpp/Jamfile.jam index 8152a3a..4ea9c44 100644 --- a/libdbpp/Jamfile.jam +++ b/libdbpp/Jamfile.jam @@ -4,7 +4,7 @@ import lex ; lib boost_date_time : : boost_date_time ; lib boost_filesystem ; lib boost_system ; -lib boost_thread ; +lib pthread ; lib boost_program_options ; lib adhocutil : : : : /usr/include/adhocutil ; lib boost_utf : : boost_unit_test_framework ; @@ -14,7 +14,7 @@ lib dbppcore : ..//glibmm adhocutil boost_system - boost_thread + pthread boost_filesystem . : : diff --git a/libdbpp/column.h b/libdbpp/column.h index cfe5ab5..393736a 100644 --- a/libdbpp/column.h +++ b/libdbpp/column.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include #include #include "dbTypes.h" @@ -61,13 +61,13 @@ namespace DB { void operator>>(Blob &) const; /// STL like wrapper for optional types. template - void operator>>(boost::optional & v) const { + void operator>>(std::optional & v) const { if (!isNull()) { v = T(); - operator>>(v.get()); + operator>>(*v); } else { - v = boost::none; + v = {}; } } @@ -76,7 +76,7 @@ namespace DB { /// This column's name. const std::string name; }; - typedef boost::shared_ptr ColumnPtr; + typedef std::shared_ptr ColumnPtr; } #endif diff --git a/libdbpp/command.h b/libdbpp/command.h index aa91282..eed0561 100644 --- a/libdbpp/command.h +++ b/libdbpp/command.h @@ -4,7 +4,7 @@ #include "command_fwd.h" #include #include -#include +#include #include #include #include @@ -34,7 +34,7 @@ namespace DB { virtual ~CommandOptions() = default; /// An (optional) hash of the SQL statement. - boost::optional hash; + std::optional hash; protected: /// Helper function to extract values from a CommandOptionsMap diff --git a/libdbpp/command_fwd.h b/libdbpp/command_fwd.h index 0a35b18..36776d2 100644 --- a/libdbpp/command_fwd.h +++ b/libdbpp/command_fwd.h @@ -3,17 +3,17 @@ #include #include -#include +#include namespace DB { typedef std::map CommandOptionsMap; class CommandOptions; class Command; - typedef boost::shared_ptr CommandPtr; + typedef std::shared_ptr CommandPtr; class ModifyCommand; - typedef boost::shared_ptr ModifyCommandPtr; + typedef std::shared_ptr ModifyCommandPtr; class SelectCommand; - typedef boost::shared_ptr SelectCommandPtr; + typedef std::shared_ptr SelectCommandPtr; } diff --git a/libdbpp/connection.cpp b/libdbpp/connection.cpp index 3d954a6..e771854 100644 --- a/libdbpp/connection.cpp +++ b/libdbpp/connection.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include DB::ConnectionError::ConnectionError() : @@ -34,19 +33,6 @@ DB::Connection::execute(const std::string & sql, const CommandOptions * opts) modify(sql, opts)->execute(true); } - -DB::SelectCommandPtr -DB::Connection::select(const std::string & sql, const CommandOptions * opts) -{ - return DB::SelectCommandPtr(newSelectCommand(sql, opts)); -} - -DB::ModifyCommandPtr -DB::Connection::modify(const std::string & sql, const CommandOptions * opts) -{ - return DB::ModifyCommandPtr(newModifyCommand(sql, opts)); -} - void DB::Connection::finish() const { @@ -181,7 +167,7 @@ DB::Connection::bulkUploadData(FILE * in) const } AdHocFormatter(PluginLibraryFormat, "libdbpp-%?.so"); -boost::optional +std::optional DB::Connection::resolvePlugin(const std::type_info &, const std::string & name) { return PluginLibraryFormat::get(name); @@ -199,19 +185,21 @@ DB::TransactionRequired::message() const throw() return "A transaction must be opened before performing this operation"; } -DB::TransactionScope::TransactionScope(DB::Connection * c) : +DB::TransactionScope::TransactionScope(std::weak_ptr c) : conn(c) { - conn->beginTx(); + conn.lock()->beginTx(); } DB::TransactionScope::~TransactionScope() { + if (conn.expired()) return; + if (std::uncaught_exception()) { - conn->rollbackTx(); + conn.lock()->rollbackTx(); } else { - conn->commitTx(); + conn.lock()->commitTx(); } } diff --git a/libdbpp/connection.h b/libdbpp/connection.h index f3cd014..7d27a22 100644 --- a/libdbpp/connection.h +++ b/libdbpp/connection.h @@ -8,7 +8,8 @@ #include #include #include -#include +#include +#include #include "error.h" namespace AdHoc { @@ -19,7 +20,9 @@ namespace DB { class Command; class CommandOptions; class SelectCommand; + typedef std::shared_ptr SelectCommandPtr; class ModifyCommand; + typedef std::shared_ptr ModifyCommandPtr; class TablePatch; enum BulkDeleteStyle { @@ -84,7 +87,7 @@ namespace DB { }; /// Base class for connections to a database. - class DLL_PUBLIC Connection { + class DLL_PUBLIC Connection : public std::enable_shared_from_this { public: virtual ~Connection(); @@ -118,13 +121,9 @@ namespace DB { /// @param s the location of the script. virtual void executeScript(std::istream & f, const boost::filesystem::path & s); /// Create a new select command with the given SQL. - virtual SelectCommand * newSelectCommand(const std::string & sql, const CommandOptions * = nullptr) = 0; - /// Create a new select command with the given SQL [smart pointer]. - virtual boost::shared_ptr select(const std::string & sql, const CommandOptions * = nullptr); + virtual SelectCommandPtr select(const std::string & sql, const CommandOptions * = nullptr) = 0; /// Create a new modify command with the given SQL. - virtual ModifyCommand * newModifyCommand(const std::string & sql, const CommandOptions * = nullptr) = 0; - /// Create a new modify command with the given SQL [smart pointer]. - virtual boost::shared_ptr modify(const std::string & sql, const CommandOptions * = nullptr); + virtual ModifyCommandPtr modify(const std::string & sql, const CommandOptions * = nullptr) = 0; /// Begin a bulk upload operation. /// @param table the target table. @@ -146,7 +145,7 @@ namespace DB { PatchResult patchTable(TablePatch * tp); /// AdHoc plugin resolver helper for database connectors. - static boost::optional resolvePlugin(const std::type_info &, const std::string &); + static std::optional resolvePlugin(const std::type_info &, const std::string &); protected: /// Create a new connection. @@ -174,17 +173,18 @@ namespace DB { class DLL_PUBLIC TransactionScope { public: /// Create a new helper and associated transaction on the given connection. - TransactionScope(DB::Connection *); + TransactionScope(std::weak_ptr); ~TransactionScope(); private: TransactionScope(const TransactionScope &) = delete; void operator=(const TransactionScope &) = delete; - Connection * conn; + std::weak_ptr conn; }; typedef AdHoc::Factory ConnectionFactory; + typedef std::shared_ptr ConnectionFactoryCPtr; } #endif diff --git a/libdbpp/connectionPool.cpp b/libdbpp/connectionPool.cpp index 9dc3a1c..67ea3ca 100644 --- a/libdbpp/connectionPool.cpp +++ b/libdbpp/connectionPool.cpp @@ -12,20 +12,20 @@ namespace DB { { } - Connection * + ConnectionPtr ConnectionPool::createResource() const { return factory->create(connectionString); } void - ConnectionPool::returnTestResource(const Connection * c) const + ConnectionPool::returnTestResource(const ConnectionCPtr & c) const { c->finish(); } void - ConnectionPool::testResource(const Connection * c) const + ConnectionPool::testResource(const ConnectionCPtr & c) const { c->ping(); } diff --git a/libdbpp/connectionPool.h b/libdbpp/connectionPool.h index 859c269..2300abb 100644 --- a/libdbpp/connectionPool.h +++ b/libdbpp/connectionPool.h @@ -3,6 +3,7 @@ #include #include +#include #include "connection.h" namespace DB { @@ -18,16 +19,17 @@ namespace DB { protected: /// Create a new connection. - Connection * createResource() const override; + ConnectionPtr createResource() const override; /// Ping a connection. - void returnTestResource(const Connection *) const override; + void returnTestResource(const ConnectionCPtr &) const override; /// Ping a connection. - void testResource(const Connection *) const override; + void testResource(const ConnectionCPtr &) const override; private: - const ConnectionFactory * factory; + const ConnectionFactoryCPtr factory; const std::string connectionString; }; + typedef std::shared_ptr ConnectionPoolPtr; } #endif diff --git a/libdbpp/connection_fwd.h b/libdbpp/connection_fwd.h index 0594490..616c6b8 100644 --- a/libdbpp/connection_fwd.h +++ b/libdbpp/connection_fwd.h @@ -1,11 +1,12 @@ #ifndef DB_CONNECTION_FWD_H #define DB_CONNECTION_FWD_H -#include +#include namespace DB { class Connection; - typedef boost::shared_ptr ConnectionPtr; + typedef std::shared_ptr ConnectionPtr; + typedef std::shared_ptr ConnectionCPtr; } #endif diff --git a/libdbpp/createMockDb.cpp b/libdbpp/createMockDb.cpp index 8e2dffe..006e30e 100644 --- a/libdbpp/createMockDb.cpp +++ b/libdbpp/createMockDb.cpp @@ -67,7 +67,7 @@ main(int argc, char ** argv) } std::cout << "Tearing down database..." << std::endl; - delete mock; + mock.reset(); std::cout << " done." << std::endl; return 0; } diff --git a/libdbpp/mockDatabase.cpp b/libdbpp/mockDatabase.cpp index 5e78ca1..80d4732 100644 --- a/libdbpp/mockDatabase.cpp +++ b/libdbpp/mockDatabase.cpp @@ -7,15 +7,6 @@ #include #include -namespace AdHoc { - template <> - PluginOf::~PluginOf() - { - // This implementation doesn't delete .implementation as - // mock databases simply unregister themselves (via destructor) - // when the mock framework tears them down. - } -} INSTANTIATEPLUGINOF(DB::MockDatabase); INSTANTIATEFACTORY(DB::MockDatabase, const std::string &, const std::string &, const std::vector &); PLUGINRESOLVER(DB::MockDatabaseFactory, DB::Connection::resolvePlugin); @@ -24,18 +15,8 @@ namespace DB { unsigned int MockDatabase::mocked = 0; -MockDatabase::MockDatabase(const std::string & name) : - mockName(name) -{ - AdHoc::PluginManager::getDefault()->add(AdHoc::PluginPtr(new AdHoc::PluginOf(this, mockName, __FILE__, __LINE__))); -} - -MockDatabase::~MockDatabase() -{ - AdHoc::PluginManager::getDefault()->remove(mockName); -} -Connection * +ConnectionPtr MockDatabase::openConnectionTo(const std::string & mockName) { return AdHoc::PluginManager::getDefault()->get(mockName)->implementation()->openConnection(); @@ -75,7 +56,6 @@ MockDatabase::PlaySchemaScript(DB::Connection * conn, const boost::filesystem::p AdHocFormatter(MockServerDatabaseName, "libdbpp_mock_%?_%?_%?"); MockServerDatabase::MockServerDatabase(const std::string & masterdb, const std::string & name, const std::string & type) : - MockDatabase(name), master(DB::ConnectionFactory::createNew(type, masterdb)), testDbName(MockServerDatabaseName::get(name, getpid(), ++mocked)) { @@ -83,7 +63,6 @@ MockServerDatabase::MockServerDatabase(const std::string & masterdb, const std:: MockServerDatabase::~MockServerDatabase() { - delete master; } const std::string & diff --git a/libdbpp/mockDatabase.h b/libdbpp/mockDatabase.h index 5d3ee52..2510c73 100644 --- a/libdbpp/mockDatabase.h +++ b/libdbpp/mockDatabase.h @@ -14,14 +14,13 @@ class DLL_PUBLIC MockDatabase : public AdHoc::AbstractPluginImplementation { public: /// Creates and registers a new database. /// @param mockName the name the database will register as. - MockDatabase(const std::string & mockName); - virtual ~MockDatabase(); + virtual ~MockDatabase() = default; /// Open a connection to this database instance. - virtual DB::Connection * openConnection() const = 0; + virtual ConnectionPtr openConnection() const = 0; /// Open a connection to a named mock database. - static Connection * openConnectionTo(const std::string &); + static ConnectionPtr openConnectionTo(const std::string &); protected: /// Implementation specific method to create a new database. @@ -33,8 +32,6 @@ class DLL_PUBLIC MockDatabase : public AdHoc::AbstractPluginImplementation { /// Implementation specific method to drop a database. virtual void DropDatabase() const = 0; - /// The name of this mocked database. - const std::string mockName; /// Internal counter of mocked databases (for unique name generation) static unsigned int mocked; }; @@ -45,7 +42,7 @@ class DLL_PUBLIC MockServerDatabase : public MockDatabase { public: /// Create and register a new database. /// @param masterdb connection to server with permissions to create a new database. - /// @param name the name of the mock to register as. + /// @param name the prefix to use when creating databases. /// @param type the database type. MockServerDatabase(const std::string & masterdb, const std::string & name, const std::string & type); virtual ~MockServerDatabase(); @@ -58,11 +55,32 @@ class DLL_PUBLIC MockServerDatabase : public MockDatabase { virtual void DropDatabase() const override; /// Connection to the master database. - DB::Connection * master; + DB::ConnectionPtr master; /// The name of the database that was created on the server. const std::string testDbName; }; +template +class PluginMock { + public: + PluginMock(const std::string & m, const std::string & name, const std::vector & s) : + mockName(name) + { + AdHoc::PluginManager::getDefault()->create(mockName, __FILE__, __LINE__, m, name, s); + } + ~PluginMock() + { + AdHoc::PluginManager::getDefault()->remove(mockName); + } + const std::string & databaseName() const + { + return std::dynamic_pointer_cast(AdHoc::PluginManager::getDefault()->get(mockName)->implementation())->databaseName(); + } + + /// The name of this mocked database. + const std::string mockName; +}; + typedef AdHoc::Factory &> MockDatabaseFactory; } diff --git a/libdbpp/modifycommand.h b/libdbpp/modifycommand.h index 53152aa..e0aa42f 100644 --- a/libdbpp/modifycommand.h +++ b/libdbpp/modifycommand.h @@ -4,7 +4,6 @@ #include "command.h" #include "error.h" #include -#include namespace DB { /// Exception thrown when an update affected no rows when some were expected. diff --git a/libdbpp/selectcommand.h b/libdbpp/selectcommand.h index 32b5cbe..0d2a4ca 100644 --- a/libdbpp/selectcommand.h +++ b/libdbpp/selectcommand.h @@ -9,8 +9,7 @@ #include #include #include -#include -#include +#include #include #include @@ -117,7 +116,7 @@ namespace DB { /// Get the index of a column by name. unsigned int getOrdinal(const Glib::ustring &) const; /// Push each row through a function accepting one value per column - template> + template> void forEachRow(const Func & func); /// Support for a C++ row range for template diff --git a/libdbpp/tablepatch.cpp b/libdbpp/tablepatch.cpp index 40a1b41..c7b5597 100644 --- a/libdbpp/tablepatch.cpp +++ b/libdbpp/tablepatch.cpp @@ -27,7 +27,7 @@ DB::Connection::patchTable(TablePatch * tp) if (!inTx()) { throw TransactionRequired(); } - TransactionScope tx(this); + TransactionScope tx(shared_from_this()); bool ownedExpr = false; if (!tp->srcExpr && !tp->src.empty()) { tp->srcExpr = new DB::StaticSqlWriter(tp->src); @@ -61,7 +61,7 @@ push(boost::format & f, typename Container::const_iterator & i, const Value & v, template static inline unsigned int -appendIf(AdHoc::Buffer & buf, const Container & c, const boost::function & sel, const Separator & sep, const std::string & fmts, const Ps & ... ps) +appendIf(AdHoc::Buffer & buf, const Container & c, const std::function & sel, const Separator & sep, const std::string & fmts, const Ps & ... ps) { auto fmt = AdHoc::Buffer::getFormat(fmts); unsigned int x = 0; @@ -70,8 +70,8 @@ appendIf(AdHoc::Buffer & buf, const Container & c, const boost::function 0) { buf.appendbf("%s", sep); } - push(*fmt, i, ps...); - buf.append(fmt->str()); + push(fmt, i, ps...); + buf.append(fmt.str()); x += 1; } } @@ -189,7 +189,7 @@ DB::Connection::patchDeletes(TablePatch * tp) break; } } - auto del = ModifyCommandPtr(newModifyCommand(toDelSql)); + auto del = modify(toDelSql); unsigned int offset = 0; tp->srcExpr->bindParams(del.get(), offset); if (tp->insteadOfDelete) { @@ -271,7 +271,7 @@ DB::Connection::patchUpdates(TablePatch * tp) // ----------------------------------------------------------------- // Execute the bulk update command --------------------------------- // ----------------------------------------------------------------- - auto upd = ModifyCommandPtr(newModifyCommand(updSql)); + auto upd = modify(updSql); unsigned int offset = 0; tp->srcExpr->bindParams(upd.get(), offset); if (tp->where) { @@ -299,7 +299,7 @@ DB::Connection::patchUpdates(TablePatch * tp) // ----------------------------------------------------------------- // Execute the bulk update command --------------------------------- // ----------------------------------------------------------------- - auto upd = ModifyCommandPtr(newModifyCommand(updSql)); + auto upd = modify(updSql); unsigned int offset = 0; tp->where->bindParams(upd.get(), offset); if (tp->where) { @@ -357,7 +357,7 @@ DB::Connection::patchInserts(TablePatch * tp) append(toInsSql, tp->cols, ", ", "%s", selfCols); toInsSql.append(")\n"); patchInsertsSelect(toInsSql, tp); - auto ins = ModifyCommandPtr(newModifyCommand(toInsSql)); + auto ins = modify(toInsSql); unsigned int offset = 0; tp->srcExpr->bindParams(ins.get(), offset); if (tp->order) { diff --git a/libdbpp/tablepatch.h b/libdbpp/tablepatch.h index 84fada1..f942401 100644 --- a/libdbpp/tablepatch.h +++ b/libdbpp/tablepatch.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include namespace DB { class SqlWriter; @@ -20,7 +20,7 @@ namespace DB { typedef std::set ColumnNames; typedef ColumnNames PrimaryKey; typedef PrimaryKey::const_iterator PKI; - typedef boost::function AuditFunction; + typedef std::function AuditFunction; public: /// Default constructor diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam index 83e1ad4..69b25e9 100644 --- a/libdbpp/unittests/Jamfile.jam +++ b/libdbpp/unittests/Jamfile.jam @@ -6,7 +6,7 @@ lib pq ; lib boost_date_time ; lib boost_system ; lib boost_utf : : boost_unit_test_framework ; -lib IceUtil ; +lib Ice++11 ; run testConnection.cpp mockdb.cpp @@ -105,7 +105,7 @@ run ..//adhocutil dbpp-local-postgresql boost_utf - IceUtil + Ice++11 util.sql : testUtils diff --git a/libdbpp/unittests/mockdb.cpp b/libdbpp/unittests/mockdb.cpp index cf29e01..6b5a7cc 100644 --- a/libdbpp/unittests/mockdb.cpp +++ b/libdbpp/unittests/mockdb.cpp @@ -46,29 +46,29 @@ MockDb::execute(const std::string & sql, const DB::CommandOptions *) executed.push_back(sql); } -DB::SelectCommand * -MockDb::newSelectCommand(const std::string &, const DB::CommandOptions *) +DB::SelectCommandPtr +MockDb::select(const std::string &, const DB::CommandOptions *) { return nullptr; } -DB::ModifyCommand * -MockDb::newModifyCommand(const std::string &, const DB::CommandOptions *) +DB::ModifyCommandPtr +MockDb::modify(const std::string &, const DB::CommandOptions *) { return nullptr; } -MockMock::MockMock(const std::string &, const std::string & name, const std::vector & ss) : - DB::MockDatabase(name) +MockMock::MockMock(const std::string &, const std::string &, const std::vector & ss) : + DB::MockDatabase() { CreateNewDatabase(); PlaySchemaScripts(ss); } -DB::Connection * +DB::ConnectionPtr MockMock::openConnection() const { - return new MockDb(""); + return std::make_shared(""); } void diff --git a/libdbpp/unittests/mockdb.h b/libdbpp/unittests/mockdb.h index b67dfe4..53fd451 100644 --- a/libdbpp/unittests/mockdb.h +++ b/libdbpp/unittests/mockdb.h @@ -16,8 +16,8 @@ class MockDb : public DB::Connection { DB::BulkUpdateStyle bulkUpdateStyle() const override; void execute(const std::string & sql, const DB::CommandOptions *) override; - DB::SelectCommand * newSelectCommand(const std::string &, const DB::CommandOptions *) override; - DB::ModifyCommand * newModifyCommand(const std::string &, const DB::CommandOptions *) override; + DB::SelectCommandPtr select(const std::string &, const DB::CommandOptions *) override; + DB::ModifyCommandPtr modify(const std::string &, const DB::CommandOptions *) override; mutable std::vector executed; }; @@ -25,7 +25,7 @@ class MockDb : public DB::Connection { class MockMock : public DB::MockDatabase { public: MockMock(const std::string &, const std::string &, const std::vector &); - DB::Connection * openConnection() const override; + DB::ConnectionPtr openConnection() const override; void CreateNewDatabase() const override; void DropDatabase() const override; }; diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index e738511..7229f6e 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -16,9 +16,8 @@ BOOST_AUTO_TEST_CASE( create ) auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); BOOST_REQUIRE(mock); // MockDb is fake, just returns nullptr, but the call should otherwise succeed. - BOOST_REQUIRE(!mock->newModifyCommand("")); - BOOST_REQUIRE(!mock->newSelectCommand("")); - delete mock; + BOOST_REQUIRE(!mock->modify("")); + BOOST_REQUIRE(!mock->select("")); } BOOST_AUTO_TEST_CASE( resolve ) @@ -42,7 +41,6 @@ BOOST_AUTO_TEST_CASE( finish ) BOOST_REQUIRE_THROW(mock->finish(), DB::TransactionStillOpen); mock->commitTx(); mock->finish(); - delete mock; } BOOST_AUTO_TEST_CASE( tx ) @@ -62,7 +60,6 @@ BOOST_AUTO_TEST_CASE( tx ) BOOST_REQUIRE_EQUAL(true, mock->inTx()); mock->rollbackTx(); // 0 BOOST_REQUIRE_EQUAL(false, mock->inTx()); - delete mock; } BOOST_AUTO_TEST_CASE( txscope ) @@ -88,7 +85,7 @@ BOOST_AUTO_TEST_CASE( txscope ) BOOST_AUTO_TEST_CASE( savepoints ) { auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); - MockDb * mockdb = dynamic_cast(mock); + auto mockdb = std::dynamic_pointer_cast(mock); BOOST_REQUIRE(mockdb); mock->savepoint("sp"); BOOST_REQUIRE_EQUAL("SAVEPOINT sp", *mockdb->executed.rbegin()); @@ -100,7 +97,6 @@ BOOST_AUTO_TEST_CASE( savepoints ) BOOST_REQUIRE_EQUAL("SAVEPOINT sp2", *mockdb->executed.rbegin()); mock->rollbackToSavepoint("sp1"); BOOST_REQUIRE_EQUAL("ROLLBACK TO SAVEPOINT sp1", *mockdb->executed.rbegin()); - delete mock; } BOOST_AUTO_TEST_CASE( commandOptions ) @@ -118,7 +114,7 @@ BOOST_AUTO_TEST_CASE( commandOptionsPq1 ) {"page-size", "5"} }); BOOST_REQUIRE(optsBase); - auto optsPq = dynamic_cast(optsBase); + auto optsPq = std::dynamic_pointer_cast(optsBase); BOOST_REQUIRE(optsPq); BOOST_REQUIRE(optsBase->hash); BOOST_REQUIRE_EQUAL(12345, *optsBase->hash); @@ -132,7 +128,7 @@ BOOST_AUTO_TEST_CASE( commandOptionsPq2 ) {"page-size", "50"} }); BOOST_REQUIRE(optsBase); - auto optsPq = dynamic_cast(optsBase); + auto optsPq = std::dynamic_pointer_cast(optsBase); BOOST_REQUIRE(optsPq); BOOST_REQUIRE(optsBase->hash); BOOST_REQUIRE_EQUAL(123456, *optsBase->hash); diff --git a/libdbpp/unittests/testConnectionPool.cpp b/libdbpp/unittests/testConnectionPool.cpp index 5c73f1e..124df5b 100644 --- a/libdbpp/unittests/testConnectionPool.cpp +++ b/libdbpp/unittests/testConnectionPool.cpp @@ -5,10 +5,10 @@ #include #include -class MockPool : public PQ::Mock, public DB::ConnectionPool { +class MockPool : public DB::PluginMock, public DB::ConnectionPool { public: MockPool() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { }), + PluginMock("user=postgres dbname=postgres", "pqmock", { }), DB::ConnectionPool(4, 2, "postgresql", stringbf("user=postgres dbname=%s", databaseName())) { } diff --git a/libdbpp/unittests/testPatch.cpp b/libdbpp/unittests/testPatch.cpp index 39ad5e3..b9ede04 100644 --- a/libdbpp/unittests/testPatch.cpp +++ b/libdbpp/unittests/testPatch.cpp @@ -10,10 +10,10 @@ #include #include -class Mock : public PQ::Mock { +class Mock : public DB::PluginMock { public: Mock() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) + DB::PluginMock("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) { } }; diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index 2dca88e..c926a47 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -13,9 +13,9 @@ #include #include -class StandardMockDatabase : public PQ::Mock { +class StandardMockDatabase : public DB::PluginMock { public: - StandardMockDatabase() : PQ::Mock("user=postgres dbname=postgres", "pqmock", { + StandardMockDatabase() : DB::PluginMock("user=postgres dbname=postgres", "pqmock", { rootDir / "util.sql" }) { } @@ -25,8 +25,8 @@ BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); BOOST_AUTO_TEST_CASE( forEachRow ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a LIMIT 1")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a LIMIT 1"); sel->forEachRow( [](auto a, auto b, auto c, auto d, auto e, auto f) { BOOST_REQUIRE_EQUAL(1, a); @@ -40,9 +40,9 @@ BOOST_AUTO_TEST_CASE( forEachRow ) BOOST_AUTO_TEST_CASE( forEachRowNulls ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a DESC LIMIT 1")); - sel->forEachRow, std::string, boost::optional, boost::optional, bool>( + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a DESC LIMIT 1"); + sel->forEachRow, std::string, std::optional, std::optional, bool>( [](auto a, auto b, auto c, auto d, auto e, auto f) { BOOST_REQUIRE_EQUAL(2, a); BOOST_REQUIRE(b); @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE( forEachRowNulls ) BOOST_AUTO_TEST_CASE( stdforOverRows ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); unsigned int count = 0; int64_t totalOfa = 0; std::string totalOfc; @@ -82,8 +82,8 @@ BOOST_AUTO_TEST_CASE( execute ) BOOST_AUTO_TEST_CASE( columns ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e FROM forEachRow ORDER BY a LIMIT 1")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c, d, e FROM forEachRow ORDER BY a LIMIT 1"); sel->execute(); BOOST_REQUIRE_THROW((*sel)[5], DB::ColumnIndexOutOfRange); BOOST_REQUIRE_THROW((*sel)[-1], DB::ColumnIndexOutOfRange); @@ -101,8 +101,8 @@ BOOST_AUTO_TEST_CASE( columns ) BOOST_AUTO_TEST_CASE( extract ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c FROM forEachRow WHERE f")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT a, b, c FROM forEachRow WHERE f"); BOOST_REQUIRE(sel->fetch()); int64_t lint; double ldouble; @@ -154,16 +154,16 @@ BOOST_AUTO_TEST_CASE( bulkLoadFile ) BOOST_AUTO_TEST_CASE( nullBind ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); auto ins = db->modify("INSERT INTO forEachRow VALUES(?, ?, ?, ?, ?, ?)"); - ins->bindParamI(0, boost::optional()); - ins->bindParamF(1, boost::optional()); - ins->bindParamS(2, boost::optional()); - ins->bindParamT(3, boost::optional()); - ins->bindParamT(4, boost::optional()); - ins->bindParamB(5, boost::optional()); + ins->bindParamI(0, std::optional()); + ins->bindParamF(1, std::optional()); + ins->bindParamS(2, std::optional()); + ins->bindParamT(3, std::optional()); + ins->bindParamT(4, std::optional()); + ins->bindParamB(5, std::optional()); ins->execute(); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL")); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL"); unsigned int count = 0; for (const auto & row : sel->as<>()) { (void)row; @@ -183,7 +183,7 @@ BOOST_AUTO_TEST_CASE( iceNullBind ) ins->bindParamT(4, IceUtil::Optional()); ins->bindParamB(5, IceUtil::Optional()); ins->execute(); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL")); + auto sel = db->select("SELECT a, b, c, d, e, f FROM forEachRow WHERE a IS NULL AND b IS NULL AND c IS NULL AND d IS NULL AND e IS NULL AND f IS NULL"); unsigned int count = 0; for (const auto & row : sel->as<>()) { (void)row; @@ -213,8 +213,8 @@ BOOST_AUTO_TEST_CASE( charStarBindNull ) ins->bindParamS(1, ccsc2); ins->execute(); free(cs2); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, c FROM forEachRow")); - for (const auto & row : sel->as, boost::optional>()) { + auto sel = db->select("SELECT a, c FROM forEachRow"); + for (const auto & row : sel->as, std::optional>()) { BOOST_REQUIRE(row[0].isNull()); BOOST_REQUIRE(!row[1].isNull()); } @@ -250,9 +250,9 @@ BOOST_AUTO_TEST_CASE( bindIntPtr ) ins->bindParamI(1, cisc2); ins->execute(); delete is2; - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b FROM forEachRow")); + auto sel = db->select("SELECT a, b FROM forEachRow"); unsigned int total = 0; - for (const auto & row : sel->as, boost::optional>()) { + for (const auto & row : sel->as, std::optional>()) { BOOST_REQUIRE(row[0].isNull()); BOOST_REQUIRE(!row[1].isNull()); total += *row.value<1>(); @@ -336,13 +336,13 @@ testExtractT(DB::SelectCommandPtr sel) { T test; (void)test; for (const auto & row : sel->as()) { testExtractT(row); } - for (const auto & row : sel->as>()) { testExtractT(row); } + for (const auto & row : sel->as>()) { testExtractT(row); } } BOOST_AUTO_TEST_CASE( testExtractTypes ) { - auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); - auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT 1 FROM forEachRow LIMIT 0")); + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + auto sel = db->select("SELECT 1 FROM forEachRow LIMIT 0"); // testExtractT(sel); // testExtractT(sel); // testExtractT(sel); -- cgit v1.2.3