diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 11:48:47 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-09 11:48:47 +0100 |
commit | 142c4ee136448d51db58fa5904c3f0f51679758e (patch) | |
tree | fa80a44fefdba9b21af83ae29c0d1c476b6d5f8c | |
parent | Simplify by replacing enable_if with if constexpr (diff) | |
download | libdbpp-142c4ee136448d51db58fa5904c3f0f51679758e.tar.bz2 libdbpp-142c4ee136448d51db58fa5904c3f0f51679758e.tar.xz libdbpp-142c4ee136448d51db58fa5904c3f0f51679758e.zip |
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.
-rw-r--r-- | libdbpp/Jamfile.jam | 4 | ||||
-rw-r--r-- | libdbpp/column.h | 12 | ||||
-rw-r--r-- | libdbpp/command.h | 4 | ||||
-rw-r--r-- | libdbpp/command_fwd.h | 8 | ||||
-rw-r--r-- | libdbpp/connection.cpp | 26 | ||||
-rw-r--r-- | libdbpp/connection.h | 22 | ||||
-rw-r--r-- | libdbpp/connectionPool.cpp | 6 | ||||
-rw-r--r-- | libdbpp/connectionPool.h | 10 | ||||
-rw-r--r-- | libdbpp/connection_fwd.h | 5 | ||||
-rw-r--r-- | libdbpp/createMockDb.cpp | 2 | ||||
-rw-r--r-- | libdbpp/mockDatabase.cpp | 23 | ||||
-rw-r--r-- | libdbpp/mockDatabase.h | 34 | ||||
-rw-r--r-- | libdbpp/modifycommand.h | 1 | ||||
-rw-r--r-- | libdbpp/selectcommand.h | 5 | ||||
-rw-r--r-- | libdbpp/tablepatch.cpp | 16 | ||||
-rw-r--r-- | libdbpp/tablepatch.h | 4 | ||||
-rw-r--r-- | libdbpp/unittests/Jamfile.jam | 4 | ||||
-rw-r--r-- | libdbpp/unittests/mockdb.cpp | 16 | ||||
-rw-r--r-- | libdbpp/unittests/mockdb.h | 6 | ||||
-rw-r--r-- | libdbpp/unittests/testConnection.cpp | 14 | ||||
-rw-r--r-- | libdbpp/unittests/testConnectionPool.cpp | 4 | ||||
-rw-r--r-- | libdbpp/unittests/testPatch.cpp | 4 | ||||
-rw-r--r-- | 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 : : <name>boost_date_time ; lib boost_filesystem ; lib boost_system ; -lib boost_thread ; +lib pthread ; lib boost_program_options ; lib adhocutil : : : : <include>/usr/include/adhocutil ; lib boost_utf : : <name>boost_unit_test_framework ; @@ -14,7 +14,7 @@ lib dbppcore : <library>..//glibmm <library>adhocutil <library>boost_system - <library>boost_thread + <library>pthread <library>boost_filesystem <include>. : : 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 <glibmm/ustring.h> #include <boost/date_time/posix_time/posix_time_types.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/optional.hpp> +#include <memory> +#include <optional> #include <visibility.h> #include <exception.h> #include "dbTypes.h" @@ -61,13 +61,13 @@ namespace DB { void operator>>(Blob &) const; /// STL like wrapper for optional types. template <typename T> - void operator>>(boost::optional<T> & v) const { + void operator>>(std::optional<T> & 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<Column> ColumnPtr; + typedef std::shared_ptr<Column> 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 <glibmm/ustring.h> #include <boost/date_time/posix_time/posix_time_types.hpp> -#include <boost/shared_ptr.hpp> +#include <memory> #include <boost/lexical_cast.hpp> #include <visibility.h> #include <factory.h> @@ -34,7 +34,7 @@ namespace DB { virtual ~CommandOptions() = default; /// An (optional) hash of the SQL statement. - boost::optional<std::size_t> hash; + std::optional<std::size_t> 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 <string> #include <map> -#include <boost/shared_ptr.hpp> +#include <memory> namespace DB { typedef std::map<std::string, std::string> CommandOptionsMap; class CommandOptions; class Command; - typedef boost::shared_ptr<Command> CommandPtr; + typedef std::shared_ptr<Command> CommandPtr; class ModifyCommand; - typedef boost::shared_ptr<ModifyCommand> ModifyCommandPtr; + typedef std::shared_ptr<ModifyCommand> ModifyCommandPtr; class SelectCommand; - typedef boost::shared_ptr<SelectCommand> SelectCommandPtr; + typedef std::shared_ptr<SelectCommand> 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 <factory.impl.h> #include <compileTimeFormatter.h> #include <sqlParse.h> -#include <boost/shared_ptr.hpp> #include <system_error> 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::string> +std::optional<std::string> 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<DB::Connection> 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 <exception.h> #include <visibility.h> #include <boost/filesystem/path.hpp> -#include <boost/shared_ptr.hpp> +#include <memory> +#include <optional> #include "error.h" namespace AdHoc { @@ -19,7 +20,9 @@ namespace DB { class Command; class CommandOptions; class SelectCommand; + typedef std::shared_ptr<SelectCommand> SelectCommandPtr; class ModifyCommand; + typedef std::shared_ptr<ModifyCommand> 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<Connection> { 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<SelectCommand> 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<ModifyCommand> 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<std::string> resolvePlugin(const std::type_info &, const std::string &); + static std::optional<std::string> 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<DB::Connection>); ~TransactionScope(); private: TransactionScope(const TransactionScope &) = delete; void operator=(const TransactionScope &) = delete; - Connection * conn; + std::weak_ptr<Connection> conn; }; typedef AdHoc::Factory<Connection, std::string> ConnectionFactory; + typedef std::shared_ptr<const ConnectionFactory> 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 <resourcePool.h> #include <visibility.h> +#include <memory> #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<ConnectionPool> 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 <boost/shared_ptr.hpp> +#include <memory> namespace DB { class Connection; - typedef boost::shared_ptr<Connection> ConnectionPtr; + typedef std::shared_ptr<Connection> ConnectionPtr; + typedef std::shared_ptr<const Connection> 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 <plugins.impl.h> #include <factory.impl.h> -namespace AdHoc { - template <> - PluginOf<DB::MockDatabase>::~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<boost::filesystem::path> &); 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<MockDatabase>(this, mockName, __FILE__, __LINE__))); -} - -MockDatabase::~MockDatabase() -{ - AdHoc::PluginManager::getDefault()->remove<MockDatabase>(mockName); -} -Connection * +ConnectionPtr MockDatabase::openConnectionTo(const std::string & mockName) { return AdHoc::PluginManager::getDefault()->get<DB::MockDatabase>(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<typename T> +class PluginMock { + public: + PluginMock(const std::string & m, const std::string & name, const std::vector<boost::filesystem::path> & s) : + mockName(name) + { + AdHoc::PluginManager::getDefault()->create<MockDatabase, T>(mockName, __FILE__, __LINE__, m, name, s); + } + ~PluginMock() + { + AdHoc::PluginManager::getDefault()->remove<MockDatabase>(mockName); + } + const std::string & databaseName() const + { + return std::dynamic_pointer_cast<MockServerDatabase>(AdHoc::PluginManager::getDefault()->get<MockDatabase>(mockName)->implementation())->databaseName(); + } + + /// The name of this mocked database. + const std::string mockName; +}; + typedef AdHoc::Factory<MockDatabase, const std::string &, const std::string &, const std::vector<boost::filesystem::path> &> 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 <visibility.h> -#include <boost/shared_ptr.hpp> 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 <boost/multi_index/ordered_index_fwd.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/mem_fun.hpp> -#include <boost/function/function_fwd.hpp> -#include <boost/shared_ptr.hpp> +#include <functional> #include <visibility.h> #include <exception.h> @@ -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<typename ... Fn, typename Func = boost::function<void(Fn...)>> + template<typename ... Fn, typename Func = std::function<void(Fn...)>> void forEachRow(const Func & func); /// Support for a C++ row range for template<typename ... Fn> 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<typename Separator, typename Container, typename ... Ps> static inline unsigned int -appendIf(AdHoc::Buffer & buf, const Container & c, const boost::function<bool(const typename Container::const_iterator)> & sel, const Separator & sep, const std::string & fmts, const Ps & ... ps) +appendIf(AdHoc::Buffer & buf, const Container & c, const std::function<bool(const typename Container::const_iterator)> & 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<bool(co if (x > 0) { buf.appendbf("%s", sep); } - push<Container>(*fmt, i, ps...); - buf.append(fmt->str()); + push<Container>(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 <connection.h> #include <modifycommand.h> #include <selectcommand.h> -#include <boost/function.hpp> +#include <functional> namespace DB { class SqlWriter; @@ -20,7 +20,7 @@ namespace DB { typedef std::set<ColumnName> ColumnNames; typedef ColumnNames PrimaryKey; typedef PrimaryKey::const_iterator PKI; - typedef boost::function<void(DB::SelectCommandPtr)> AuditFunction; + typedef std::function<void(DB::SelectCommandPtr)> 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 : : <name>boost_unit_test_framework ; -lib IceUtil ; +lib Ice++11 ; run testConnection.cpp mockdb.cpp @@ -105,7 +105,7 @@ run <library>..//adhocutil <library>dbpp-local-postgresql <library>boost_utf - <library>IceUtil + <library>Ice++11 <dependency>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<boost::filesystem::path> & ss) : - DB::MockDatabase(name) +MockMock::MockMock(const std::string &, const std::string &, const std::vector<boost::filesystem::path> & ss) : + DB::MockDatabase() { CreateNewDatabase(); PlaySchemaScripts(ss); } -DB::Connection * +DB::ConnectionPtr MockMock::openConnection() const { - return new MockDb(""); + return std::make_shared<MockDb>(""); } 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<std::string> 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<boost::filesystem::path> &); - 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<MockDb *>(mock); + auto mockdb = std::dynamic_pointer_cast<MockDb>(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<PQ::CommandOptions *>(optsBase); + auto optsPq = std::dynamic_pointer_cast<PQ::CommandOptions>(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<PQ::CommandOptions *>(optsBase); + auto optsPq = std::dynamic_pointer_cast<PQ::CommandOptions>(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 <pq-mock.h> #include <buffer.h> -class MockPool : public PQ::Mock, public DB::ConnectionPool { +class MockPool : public DB::PluginMock<PQ::Mock>, public DB::ConnectionPool { public: MockPool() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { }), + PluginMock<PQ::Mock>("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 <buffer.h> #include <selectcommandUtil.impl.h> -class Mock : public PQ::Mock { +class Mock : public DB::PluginMock<PQ::Mock> { public: Mock() : - PQ::Mock("user=postgres dbname=postgres", "pqmock", { rootDir / "patch.sql" }) + DB::PluginMock<PQ::Mock>("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 <IceUtil/Optional.h> #include <testCore.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 / "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<int64_t, double, std::string, boost::posix_time::ptime, boost::posix_time::time_duration, bool>( [](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<int64_t, boost::optional<double>, std::string, boost::optional<boost::posix_time::ptime>, boost::optional<boost::posix_time::time_duration>, 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<int64_t, std::optional<double>, std::string, std::optional<boost::posix_time::ptime>, std::optional<boost::posix_time::time_duration>, 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<int>()); - ins->bindParamF(1, boost::optional<double>()); - ins->bindParamS(2, boost::optional<Glib::ustring>()); - ins->bindParamT(3, boost::optional<boost::posix_time::ptime>()); - ins->bindParamT(4, boost::optional<boost::posix_time::time_duration>()); - ins->bindParamB(5, boost::optional<bool>()); + ins->bindParamI(0, std::optional<int>()); + ins->bindParamF(1, std::optional<double>()); + ins->bindParamS(2, std::optional<Glib::ustring>()); + ins->bindParamT(3, std::optional<boost::posix_time::ptime>()); + ins->bindParamT(4, std::optional<boost::posix_time::time_duration>()); + ins->bindParamB(5, std::optional<bool>()); 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<boost::posix_time::time_duration>()); ins->bindParamB(5, IceUtil::Optional<bool>()); 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<int64_t>, boost::optional<std::string>>()) { + auto sel = db->select("SELECT a, c FROM forEachRow"); + for (const auto & row : sel->as<std::optional<int64_t>, std::optional<std::string>>()) { 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<int64_t>, boost::optional<double>>()) { + for (const auto & row : sel->as<std::optional<int64_t>, std::optional<double>>()) { 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<T>()) { testExtractT(row); } - for (const auto & row : sel->as<boost::optional<T>>()) { testExtractT(row); } + for (const auto & row : sel->as<std::optional<T>>()) { 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<int8_t>(sel); // testExtractT<int16_t>(sel); // testExtractT<int32_t>(sel); |