summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-24 03:43:16 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2017-05-04 20:44:32 +0100
commit94927bfef6548ac20118586bfcd51e60b296e569 (patch)
tree90cc2d268219acc412321433421fb8fa7c798c5b
parentStandalone split (diff)
downloadlibdbpp-mysql-94927bfef6548ac20118586bfcd51e60b296e569.tar.bz2
libdbpp-mysql-94927bfef6548ac20118586bfcd51e60b296e569.tar.xz
libdbpp-mysql-94927bfef6548ac20118586bfcd51e60b296e569.zip
Beginnings of embbeded MySQL
-rw-r--r--libmysqlpp/Jamfile.jam13
-rw-r--r--libmysqlpp/embeddedmy-connection.cpp16
-rw-r--r--libmysqlpp/embeddedmy-connection.h20
-rw-r--r--libmysqlpp/embeddedmy-mock.cpp52
-rw-r--r--libmysqlpp/embeddedmy-mock.h30
-rw-r--r--libmysqlpp/my-connection.cpp4
-rw-r--r--libmysqlpp/my-connection.h6
-rw-r--r--libmysqlpp/my-error.h3
-rw-r--r--libmysqlpp/unittests/Jamfile.jam15
-rw-r--r--libmysqlpp/unittests/testMain.cpp156
-rw-r--r--libmysqlpp/unittests/testmysql.cpp156
-rw-r--r--libmysqlpp/unittests/testmysqle.cpp16
12 files changed, 330 insertions, 157 deletions
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 : : : : <include>/usr/include/mysql ;
+lib mysqld : : : : <include>/usr/include/mysql <dll-path>/usr/lib/mysql <library-path>/usr/lib/mysql ;
lib adhocutil : : : : <include>/usr/include/adhocutil ;
lib dbppcore : : : : <include>/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 :
<library>dbppcore
;
+lib dbpp-mysql-embedded :
+ [ glob embeddedmy-*.cpp ] :
+ <library>adhocutil
+ <library>dbpp-mysql
+ <library>mysqld
+ <library>boost_system
+ <library>boost_filesystem
+ : :
+ <library>mysqld
+ ;
+
build-project unittests ;
package.install install : <install-source-root>. : : 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 <mysql.h>
+
+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 <boost/filesystem/convenience.hpp>
+#include <buffer.h>
+
+namespace MySQL {
+ namespace Embedded {
+
+ Mock::Mock(const std::string & name, const std::vector<boost::filesystem::path> & 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 <mockDatabase.h>
+#include <boost/filesystem/path.hpp>
+#include <visibility.h>
+
+namespace MySQL {
+namespace Embedded {
+
+class DLL_PUBLIC Mock : public DB::MockDatabase {
+ public:
+ Mock(const std::string & name, const std::vector<boost::filesystem::path> & 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 <connection.h>
+#include <visibility.h>
#include "my-error.h"
#include <mysql.h>
#include <boost/shared_ptr.hpp>
@@ -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 <error.h>
#include <mysql.h>
#include <exception.h>
+#include <visibility.h>
namespace MySQL {
- class Error : public AdHoc::Exception<DB::Error> {
+ class DLL_PUBLIC Error : public AdHoc::Exception<DB::Error> {
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
+ : : :
+ <define>ROOT=\"$(me)\"
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//dbpp-mysql-embedded
+ <library>dbpptestcore
+ <library>boost_utf
+ <library>boost_system
+ <library>boost_filesystem
+ <dependency>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 <modifycommand.h>
+#include <selectcommand.h>
+#include <column.h>
+#include <testCore.h>
+#include <fstream>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+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 <boost/test/unit_test.hpp>
#include <my-mock.h>
-#include <my-error.h>
#include <definedDirs.h>
-#include <modifycommand.h>
-#include <selectcommand.h>
-#include <column.h>
-#include <connection.h>
-#include <testCore.h>
-#include <fstream>
-#include <boost/date_time/posix_time/posix_time.hpp>
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 <boost/test/unit_test.hpp>
+
+#include <embeddedmy-mock.h>
+#include <definedDirs.h>
+
+class StandardMockDatabase : public MySQL::Embedded::Mock {
+ public:
+ StandardMockDatabase() : MySQL::Embedded::Mock("mysqlmock", {
+ rootDir / "mysqlschema.sql" })
+ {
+ }
+};
+
+#include "testMain.cpp"
+