From 243d81acba65a3ef07c00d2069d2e1727a7361ed Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 May 2015 02:47:49 +0100 Subject: Support reading a my.cnf group and correcting passing null to mysql_real_connect --- libmysqlpp/connection.cpp | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'libmysqlpp') diff --git a/libmysqlpp/connection.cpp b/libmysqlpp/connection.cpp index ab0e2e2..b6d1b1b 100644 --- a/libmysqlpp/connection.cpp +++ b/libmysqlpp/connection.cpp @@ -4,20 +4,42 @@ #include "modifycommand.h" #include "reflection.h" #include +#include class Opts { public: Opts() { port = 3306; } - std::string server; - std::string user; - std::string password; - std::string database; + typedef boost::optional OptString; + OptString server; + OptString user; + OptString password; + OptString database; unsigned int port; - std::string unix_socket; + OptString unix_socket; + OptString options; static Reflector::Vars vars; }; +const char * +operator~(const Opts::OptString & os) +{ + if (os) { + return os->c_str(); + } + return NULL; +} + +namespace std { + template + std::istream & + operator>>(std::istream & s, boost::optional & o) + { + o = T(); + return (s >> *o); + } +} + Reflector::Vars Opts::vars = { Map(Opts, server), Map(Opts, user), @@ -25,6 +47,7 @@ Reflector::Vars Opts::vars = { Map(Opts, database), Map(Opts, unix_socket), Map(Opts, port), + Map(Opts, options), }; @@ -34,8 +57,11 @@ MySQL::Connection::Connection(const std::string & str) : { Opts o(Reflector::NameValueNew(str)); mysql_init(&conn); - if (mysql_real_connect(&conn, o.server.c_str(), o.user.c_str(), o.password.c_str(), o.database.c_str(), - o.port, o.unix_socket.c_str(), CLIENT_LOCAL_FILES | CLIENT_MULTI_STATEMENTS) == NULL) { + if (o.options) { + mysql_options(&conn, MYSQL_READ_DEFAULT_GROUP, ~o.options); + } + if (mysql_real_connect(&conn, ~o.server, ~o.user, ~o.password, ~o.database, + o.port, ~o.unix_socket, CLIENT_LOCAL_FILES | CLIENT_MULTI_STATEMENTS) == NULL) { throw ConnectionError(); } if (mysql_set_character_set(&conn, "utf8")) { -- cgit v1.2.3