From db1e2f066393af3e58814417de5bbe7ea08a145d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 31 Dec 2015 04:04:31 +0000 Subject: Basics working, could do with a tidy up around mocking --- libmysqlpp/embeddedmy-connection.cpp | 64 ++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'libmysqlpp/embeddedmy-connection.cpp') diff --git a/libmysqlpp/embeddedmy-connection.cpp b/libmysqlpp/embeddedmy-connection.cpp index f0bb24d..2a21450 100644 --- a/libmysqlpp/embeddedmy-connection.cpp +++ b/libmysqlpp/embeddedmy-connection.cpp @@ -1,16 +1,74 @@ #include "embeddedmy-connection.h" +#include "my-opts.h" +#include NAMEDFACTORY("embeddedmysql", MySQL::Embedded::Connection, DB::ConnectionFactory); namespace MySQL { namespace Embedded { - Connection::Connection(const std::string &) + class Opts { + public: + boost::filesystem::path path; + MySQL::OptString database; + MySQL::OptString options; + std::vector serverOptions; + }; + + using namespace AdHoc; + NvpTarget(Opts) OptsTargetMap { + NvpValue(Opts, path), + NvpValue(Opts, database), + NvpValue(Opts, options), + NvpValue(Opts, serverOptions), + }; + + Connection::Connection(const std::string & str) { + std::stringstream i(str); + Opts o; + NvpParse::parse(i, OptsTargetMap, o); mysql_init(&conn); + if (o.options) { + mysql_options(&conn, MYSQL_READ_DEFAULT_GROUP, ~o.options); + } 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); + server = Server::get(o.path, o.serverOptions); + if (mysql_real_connect(&conn, NULL, NULL, NULL, ~o.database, + 0, NULL, CLIENT_LOCAL_FILES | CLIENT_MULTI_STATEMENTS) == NULL) { + throw MySQL::ConnectionError(&conn); + } + if (mysql_set_character_set(&conn, "utf8")) { + throw MySQL::ConnectionError(&conn); + } + } + + Connection::Connection(Server *, const std::string & db) + { + mysql_init(&conn); + mysql_options(&conn, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); + if (mysql_real_connect(&conn, NULL, NULL, NULL, db.c_str(), + 0, NULL, CLIENT_LOCAL_FILES | CLIENT_MULTI_STATEMENTS) == NULL) { + throw MySQL::ConnectionError(&conn); + } + if (mysql_set_character_set(&conn, "utf8")) { + throw MySQL::ConnectionError(&conn); } } + + void Connection::beginBulkUpload(const char * t, const char * e) + { + DB::Connection::beginBulkUpload(t, e); + } + + void Connection::endBulkUpload(const char * msg) + { + DB::Connection::endBulkUpload(msg); + } + + size_t Connection::bulkUploadData(const char * b, size_t s) const + { + return DB::Connection::bulkUploadData(b, s); + } + } } -- cgit v1.2.3