From 94927bfef6548ac20118586bfcd51e60b296e569 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Dec 2015 03:43:16 +0000 Subject: Beginnings of embbeded MySQL --- libmysqlpp/Jamfile.jam | 13 +++ libmysqlpp/embeddedmy-connection.cpp | 16 ++++ libmysqlpp/embeddedmy-connection.h | 20 +++++ libmysqlpp/embeddedmy-mock.cpp | 52 ++++++++++++ libmysqlpp/embeddedmy-mock.h | 30 +++++++ libmysqlpp/my-connection.cpp | 4 + libmysqlpp/my-connection.h | 6 +- libmysqlpp/my-error.h | 3 +- libmysqlpp/unittests/Jamfile.jam | 15 ++++ libmysqlpp/unittests/testMain.cpp | 156 +++++++++++++++++++++++++++++++++++ libmysqlpp/unittests/testmysql.cpp | 156 +---------------------------------- libmysqlpp/unittests/testmysqle.cpp | 16 ++++ 12 files changed, 330 insertions(+), 157 deletions(-) create mode 100644 libmysqlpp/embeddedmy-connection.cpp create mode 100644 libmysqlpp/embeddedmy-connection.h create mode 100644 libmysqlpp/embeddedmy-mock.cpp create mode 100644 libmysqlpp/embeddedmy-mock.h create mode 100644 libmysqlpp/unittests/testMain.cpp create mode 100644 libmysqlpp/unittests/testmysqle.cpp (limited to 'libmysqlpp') diff --git a/libmysqlpp/Jamfile.jam b/libmysqlpp/Jamfile.jam index c49b085..f7cb67d 100644 --- a/libmysqlpp/Jamfile.jam +++ b/libmysqlpp/Jamfile.jam @@ -1,10 +1,12 @@ import package ; lib mysqlclient : : : : /usr/include/mysql ; +lib mysqld : : : : /usr/include/mysql /usr/lib/mysql /usr/lib/mysql ; lib adhocutil : : : : /usr/include/adhocutil ; lib dbppcore : : : : /usr/include/dbpp ; lib boost_date_time ; lib boost_system ; +lib boost_filesystem ; lib dbpp-mysql : [ glob my-*.cpp ] : @@ -21,6 +23,17 @@ lib dbpp-mysql : dbppcore ; +lib dbpp-mysql-embedded : + [ glob embeddedmy-*.cpp ] : + adhocutil + dbpp-mysql + mysqld + boost_system + boost_filesystem + : : + mysqld + ; + build-project unittests ; package.install install : . : : dbpp-mysql : [ glob my-*.h ] ; diff --git a/libmysqlpp/embeddedmy-connection.cpp b/libmysqlpp/embeddedmy-connection.cpp new file mode 100644 index 0000000..f0bb24d --- /dev/null +++ b/libmysqlpp/embeddedmy-connection.cpp @@ -0,0 +1,16 @@ +#include "embeddedmy-connection.h" + +NAMEDFACTORY("embeddedmysql", MySQL::Embedded::Connection, DB::ConnectionFactory); + +namespace MySQL { + namespace Embedded { + Connection::Connection(const std::string &) + { + mysql_init(&conn); + mysql_options(&conn, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); + if (mysql_real_connect(&conn, NULL, NULL, NULL, "database1", 0, NULL, 0)) { + throw MySQL::Error(&conn); + } + } + } +} diff --git a/libmysqlpp/embeddedmy-connection.h b/libmysqlpp/embeddedmy-connection.h new file mode 100644 index 0000000..3ceba2f --- /dev/null +++ b/libmysqlpp/embeddedmy-connection.h @@ -0,0 +1,20 @@ +#ifndef EMBEDDEDMY_CONNECTION_H +#define EMBEDDEDMY_CONNECTION_H + +#include "my-connection.h" +#include "my-error.h" +#include + +namespace MySQL { + namespace Embedded { + class Connection : public ::MySQL::Connection { + public: + Connection(const std::string &); + + private: + }; + } +} + +#endif + diff --git a/libmysqlpp/embeddedmy-mock.cpp b/libmysqlpp/embeddedmy-mock.cpp new file mode 100644 index 0000000..0ad0cc5 --- /dev/null +++ b/libmysqlpp/embeddedmy-mock.cpp @@ -0,0 +1,52 @@ +#include "embeddedmy-connection.h" +#include "embeddedmy-mock.h" +#include +#include + +namespace MySQL { + namespace Embedded { + + Mock::Mock(const std::string & name, const std::vector & ss) : + MockDatabase(name), + testDbPath(boost::filesystem::path("/tmp") / stringbf("embeddedmysql-%d-%d", getpid(), ++DB::MockDatabase::mocked)) + { + CreateNewDatabase(); + PlaySchemaScripts(ss); + } + + Mock::~Mock() + { + DropDatabase(); + } + + DB::Connection * + Mock::openConnection() const + { + return new Connection(testDbPath.string()); + } + + void + Mock::CreateNewDatabase() const + { + boost::filesystem::create_directories(testDbPath); + const auto datadir = stringbf("--datadir=%s", testDbPath.string()); + static const char * opts[] = { + typeid(this).name(), + datadir.c_str(), + "--bootstrap", + NULL + }; + static const char * groups[] = { NULL }; + mysql_library_init(3, (char**)opts, (char**)groups); + sleep(20); + } + + void + Mock::DropDatabase() const + { + mysql_library_end(); + boost::filesystem::remove_all(testDbPath); + } + } +} + diff --git a/libmysqlpp/embeddedmy-mock.h b/libmysqlpp/embeddedmy-mock.h new file mode 100644 index 0000000..0b90b4b --- /dev/null +++ b/libmysqlpp/embeddedmy-mock.h @@ -0,0 +1,30 @@ +#ifndef MOCKMYSQLDATASOURCE_H +#define MOCKMYSQLDATASOURCE_H + +#include +#include +#include + +namespace MySQL { +namespace Embedded { + +class DLL_PUBLIC Mock : public DB::MockDatabase { + public: + Mock(const std::string & name, const std::vector & ss); + ~Mock(); + + protected: + void DropDatabase() const override; + void CreateNewDatabase() const override; + + DB::Connection * openConnection() const override; + + private: + const boost::filesystem::path testDbPath; +}; + +} +} + +#endif + diff --git a/libmysqlpp/my-connection.cpp b/libmysqlpp/my-connection.cpp index acc89cd..04c1f8c 100644 --- a/libmysqlpp/my-connection.cpp +++ b/libmysqlpp/my-connection.cpp @@ -77,6 +77,10 @@ MySQL::Connection::Connection(const std::string & str) } } +MySQL::Connection::Connection() +{ +} + MySQL::Connection::~Connection() { mysql_close(&conn); diff --git a/libmysqlpp/my-connection.h b/libmysqlpp/my-connection.h index f6acb39..fdc3e5a 100644 --- a/libmysqlpp/my-connection.h +++ b/libmysqlpp/my-connection.h @@ -2,6 +2,7 @@ #define MY_CONNECTION_H #include +#include #include "my-error.h" #include #include @@ -14,7 +15,7 @@ namespace MySQL { class LoadContext; - class Connection : public DB::Connection { + class DLL_PUBLIC Connection : public DB::Connection { public: Connection(const std::string & info); ~Connection(); @@ -37,6 +38,9 @@ namespace MySQL { mutable MYSQL conn; + protected: + Connection(); + private: my_bool my_true; diff --git a/libmysqlpp/my-error.h b/libmysqlpp/my-error.h index b694843..394a793 100644 --- a/libmysqlpp/my-error.h +++ b/libmysqlpp/my-error.h @@ -4,9 +4,10 @@ #include #include #include +#include namespace MySQL { - class Error : public AdHoc::Exception { + class DLL_PUBLIC Error : public AdHoc::Exception { public: Error(MYSQL_STMT *); Error(MYSQL *); diff --git a/libmysqlpp/unittests/Jamfile.jam b/libmysqlpp/unittests/Jamfile.jam index 167939a..c7bf637 100644 --- a/libmysqlpp/unittests/Jamfile.jam +++ b/libmysqlpp/unittests/Jamfile.jam @@ -22,3 +22,18 @@ run testmysql ; +run + testmysqle.cpp + : : : + ROOT=\"$(me)\" + BOOST_TEST_DYN_LINK + ..//dbpp-mysql-embedded + dbpptestcore + boost_utf + boost_system + boost_filesystem + mysqlschema.sql + : + testmysqle + ; + diff --git a/libmysqlpp/unittests/testMain.cpp b/libmysqlpp/unittests/testMain.cpp new file mode 100644 index 0000000..0811d5c --- /dev/null +++ b/libmysqlpp/unittests/testMain.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include + +BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); + +BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); + +BOOST_AUTO_TEST_CASE( transactions ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + BOOST_REQUIRE_EQUAL(false, ro->inTx()); + ro->beginTx(); + BOOST_REQUIRE_EQUAL(true, ro->inTx()); + ro->rollbackTx(); + BOOST_REQUIRE_EQUAL(false, ro->inTx()); + + ro->beginTx(); + 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("mysqlmock"); + + auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); + mod->bindParamI(0, testInt); + mod->bindParamF(1, testDouble); + mod->bindParamS(2, testString); + mod->bindParamB(3, testBool); + mod->bindParamT(4, testDateTime); + mod->bindParamT(5, testInterval); + mod->execute(); + delete mod; + delete rw; +} + +BOOST_AUTO_TEST_CASE( bindAndSelect ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?"); + select->bindParamI(0, testInt); + select->execute(); + int rows = 0; + while (select->fetch()) { + assertColumnValueHelper(*select, 0, testInt); + assertColumnValueHelper(*select, 1, testDouble); + assertColumnValueHelper(*select, 2, testString); + assertColumnValueHelper(*select, 3, testBool); + assertColumnValueHelper(*select, 4, testDateTime); + assertColumnValueHelper(*select, 5, testInterval); + rows += 1; + } + delete select; + BOOST_REQUIRE_EQUAL(1, rows); + delete ro; +} + +BOOST_AUTO_TEST_CASE( bindAndSelectOther ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ?"); + select->bindParamI(0, testInt); + select->execute(); + int rows = 0; + while (select->fetch()) { + assertColumnValueHelper(*select, 0, 4); + assertColumnValueHelper(*select, 1, 123.45); + assertColumnValueHelper(*select, 2, std::string("some text")); + assertColumnValueHelper(*select, 3, true); + assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); + 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( bulkload ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); + // Test empty + ro->beginBulkUpload("bulktest", ""); + ro->endBulkUpload(NULL); + assertScalarValueHelper(*count, 0); + // Test sample file + ro->beginBulkUpload("bulktest", ""); + std::ifstream in((rootDir / "bulk.sample").string()); + if (!in.good()) throw std::runtime_error("Couldn't open bulk.sample"); + char buf[BUFSIZ]; + for (std::streamsize r; (r = in.readsome(buf, sizeof(buf))) > 0; ) { + ro->bulkUploadData(buf, r); + } + ro->endBulkUpload(NULL); + assertScalarValueHelper(*count, 800); + + delete count; + delete ro; +} + +BOOST_AUTO_TEST_CASE( bigIterate ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + + auto count = ro->newSelectCommand("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("mysqlmock"); + auto ins = ro->newModifyCommand("INSERT INTO inserts(num) VALUES(?)"); + ins->bindParamI(0, 4); + ins->execute(); + BOOST_REQUIRE_EQUAL(1, ro->insertId()); + ins->bindParamI(0, 40); + ins->execute(); + BOOST_REQUIRE_EQUAL(2, ro->insertId()); + ins->bindParamI(0, -4); + ins->execute(); + BOOST_REQUIRE_EQUAL(3, ro->insertId()); + delete ins; + delete ro; +} + +BOOST_AUTO_TEST_CASE( errors ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error); + delete ro; + BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError); +} + +BOOST_AUTO_TEST_SUITE_END(); + + diff --git a/libmysqlpp/unittests/testmysql.cpp b/libmysqlpp/unittests/testmysql.cpp index 00c15aa..d363716 100644 --- a/libmysqlpp/unittests/testmysql.cpp +++ b/libmysqlpp/unittests/testmysql.cpp @@ -2,15 +2,7 @@ #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include class StandardMockDatabase : public MySQL::Mock { public: @@ -20,151 +12,5 @@ class StandardMockDatabase : public MySQL::Mock { } }; -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); - -BOOST_FIXTURE_TEST_SUITE( Core, DB::TestCore ); - -BOOST_AUTO_TEST_CASE( transactions ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - BOOST_REQUIRE_EQUAL(false, ro->inTx()); - ro->beginTx(); - BOOST_REQUIRE_EQUAL(true, ro->inTx()); - ro->rollbackTx(); - BOOST_REQUIRE_EQUAL(false, ro->inTx()); - - ro->beginTx(); - 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("mysqlmock"); - - auto mod = rw->newModifyCommand("INSERT INTO test VALUES(?, ?, ?, ?, ?, ?)"); - mod->bindParamI(0, testInt); - mod->bindParamF(1, testDouble); - mod->bindParamS(2, testString); - mod->bindParamB(3, testBool); - mod->bindParamT(4, testDateTime); - mod->bindParamT(5, testInterval); - mod->execute(); - delete mod; - delete rw; -} - -BOOST_AUTO_TEST_CASE( bindAndSelect ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id = ?"); - select->bindParamI(0, testInt); - select->execute(); - int rows = 0; - while (select->fetch()) { - assertColumnValueHelper(*select, 0, testInt); - assertColumnValueHelper(*select, 1, testDouble); - assertColumnValueHelper(*select, 2, testString); - assertColumnValueHelper(*select, 3, testBool); - assertColumnValueHelper(*select, 4, testDateTime); - assertColumnValueHelper(*select, 5, testInterval); - rows += 1; - } - delete select; - BOOST_REQUIRE_EQUAL(1, rows); - delete ro; -} - -BOOST_AUTO_TEST_CASE( bindAndSelectOther ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto select = ro->newSelectCommand("SELECT * FROM test WHERE id != ?"); - select->bindParamI(0, testInt); - select->execute(); - int rows = 0; - while (select->fetch()) { - assertColumnValueHelper(*select, 0, 4); - assertColumnValueHelper(*select, 1, 123.45); - assertColumnValueHelper(*select, 2, std::string("some text")); - assertColumnValueHelper(*select, 3, true); - assertColumnValueHelper(*select, 4, boost::posix_time::ptime_from_tm({ 3, 6, 23, 27, 3, 115, 0, 0, 0, 0, 0})); - 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( bulkload ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto count = ro->newSelectCommand("SELECT COUNT(*) FROM bulktest"); - // Test empty - ro->beginBulkUpload("bulktest", ""); - ro->endBulkUpload(NULL); - assertScalarValueHelper(*count, 0); - // Test sample file - ro->beginBulkUpload("bulktest", ""); - std::ifstream in((rootDir / "bulk.sample").string()); - if (!in.good()) throw std::runtime_error("Couldn't open bulk.sample"); - char buf[BUFSIZ]; - for (std::streamsize r; (r = in.readsome(buf, sizeof(buf))) > 0; ) { - ro->bulkUploadData(buf, r); - } - ro->endBulkUpload(NULL); - assertScalarValueHelper(*count, 800); - - delete count; - delete ro; -} - -BOOST_AUTO_TEST_CASE( bigIterate ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - - auto count = ro->newSelectCommand("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("mysqlmock"); - auto ins = ro->newModifyCommand("INSERT INTO inserts(num) VALUES(?)"); - ins->bindParamI(0, 4); - ins->execute(); - BOOST_REQUIRE_EQUAL(1, ro->insertId()); - ins->bindParamI(0, 40); - ins->execute(); - BOOST_REQUIRE_EQUAL(2, ro->insertId()); - ins->bindParamI(0, -4); - ins->execute(); - BOOST_REQUIRE_EQUAL(3, ro->insertId()); - delete ins; - delete ro; -} - -BOOST_AUTO_TEST_CASE( errors ) -{ - auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); - BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error); - delete ro; - BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError); -} - -BOOST_AUTO_TEST_SUITE_END(); +#include "testMain.cpp" diff --git a/libmysqlpp/unittests/testmysqle.cpp b/libmysqlpp/unittests/testmysqle.cpp new file mode 100644 index 0000000..0d1defb --- /dev/null +++ b/libmysqlpp/unittests/testmysqle.cpp @@ -0,0 +1,16 @@ +#define BOOST_TEST_MODULE TestEmbeddedMySQL +#include + +#include +#include + +class StandardMockDatabase : public MySQL::Embedded::Mock { + public: + StandardMockDatabase() : MySQL::Embedded::Mock("mysqlmock", { + rootDir / "mysqlschema.sql" }) + { + } +}; + +#include "testMain.cpp" + -- cgit v1.2.3