diff --git a/libs/libmysqlxx/include/mysqlxx/Connection.h b/libs/libmysqlxx/include/mysqlxx/Connection.h index 43593f5edf2..b541f22f935 100644 --- a/libs/libmysqlxx/include/mysqlxx/Connection.h +++ b/libs/libmysqlxx/include/mysqlxx/Connection.h @@ -52,11 +52,12 @@ public: /// Создать соединение. Connection( - const char* db, - const char* server, - const char* user = 0, - const char* password = 0, + const char * db, + const char * server, + const char * user = 0, + const char * password = 0, unsigned port = 0, + const char * socket = "", unsigned timeout = MYSQLXX_DEFAULT_TIMEOUT, unsigned rw_timeout = MYSQLXX_DEFAULT_RW_TIMEOUT); @@ -73,6 +74,7 @@ public: const char * user, const char * password, unsigned port, + const char * socket, unsigned timeout = MYSQLXX_DEFAULT_TIMEOUT, unsigned rw_timeout = MYSQLXX_DEFAULT_RW_TIMEOUT); @@ -84,7 +86,8 @@ public: std::string server = cfg.getString(config_name + ".host"); std::string user = cfg.getString(config_name + ".user"); std::string password = cfg.getString(config_name + ".password"); - unsigned port = cfg.getInt(config_name + ".port"); + unsigned port = cfg.getInt(config_name + ".port", 0); + std::string socket = cfg.getString(config_name + ".socket", ""); unsigned timeout = cfg.getInt(config_name + ".connect_timeout", @@ -96,7 +99,7 @@ public: cfg.getInt("mysql_rw_timeout", MYSQLXX_DEFAULT_RW_TIMEOUT)); - connect(db.c_str(), server.c_str(), user.c_str(), password.c_str(), port, timeout, rw_timeout); + connect(db.c_str(), server.c_str(), user.c_str(), password.c_str(), port, socket.c_str(), timeout, rw_timeout); } /// Было ли произведено соединение с MySQL. diff --git a/libs/libmysqlxx/include/mysqlxx/Pool.h b/libs/libmysqlxx/include/mysqlxx/Pool.h index 1de92dede7a..9b3eaf7e10a 100644 --- a/libs/libmysqlxx/include/mysqlxx/Pool.h +++ b/libs/libmysqlxx/include/mysqlxx/Pool.h @@ -165,6 +165,7 @@ public: const std::string & user_ = "", const std::string & password_ = "", unsigned port_ = 0, + const std::string & socket_ = "", unsigned connect_timeout_ = MYSQLXX_DEFAULT_TIMEOUT, unsigned rw_timeout_ = MYSQLXX_DEFAULT_RW_TIMEOUT, unsigned default_connections_ = MYSQLXX_POOL_DEFAULT_START_CONNECTIONS, @@ -178,8 +179,8 @@ public: max_connections{other.max_connections}, db{other.db}, server{other.server}, user{other.user}, password{other.password}, - port{other.port}, connect_timeout{other.connect_timeout}, - rw_timeout{other.rw_timeout} + port{other.port}, socket{other.socket}, + connect_timeout{other.connect_timeout}, rw_timeout{other.rw_timeout} {} Pool & operator=(const Pool &) = delete; @@ -225,6 +226,7 @@ private: std::string user; std::string password; unsigned port; + std::string socket; unsigned connect_timeout; unsigned rw_timeout; diff --git a/libs/libmysqlxx/src/Connection.cpp b/libs/libmysqlxx/src/Connection.cpp index 463fd467baf..37dbfc971ad 100644 --- a/libs/libmysqlxx/src/Connection.cpp +++ b/libs/libmysqlxx/src/Connection.cpp @@ -34,12 +34,13 @@ Connection::Connection( const char* user, const char* password, unsigned port, + const char * socket, unsigned timeout, unsigned rw_timeout) : driver(std::make_unique()) { is_connected = false; - connect(db, server, user, password, port, timeout, rw_timeout); + connect(db, server, user, password, port, socket, timeout, rw_timeout); } Connection::Connection(const std::string & config_name) @@ -60,6 +61,7 @@ void Connection::connect(const char* db, const char* user, const char* password, unsigned port, + const char * socket, unsigned timeout, unsigned rw_timeout) { @@ -88,7 +90,7 @@ void Connection::connect(const char* db, if (mysql_options(driver.get(), MYSQL_OPT_LOCAL_INFILE, nullptr)) throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get())); - if (!mysql_real_connect(driver.get(), server, user, password, db, port, nullptr, driver->client_flag)) + if (!mysql_real_connect(driver.get(), server, user, password, db, port, *socket ? socket : nullptr, driver->client_flag)) throw ConnectionFailed(errorMessage(driver.get()), mysql_errno(driver.get())); /// Установим кодировки по умолчанию - UTF-8. diff --git a/libs/libmysqlxx/src/Pool.cpp b/libs/libmysqlxx/src/Pool.cpp index 0d54e12bfa5..b9565b2b0ef 100644 --- a/libs/libmysqlxx/src/Pool.cpp +++ b/libs/libmysqlxx/src/Pool.cpp @@ -45,16 +45,29 @@ Pool::Pool(const Poco::Util::AbstractConfiguration & cfg, const std::string & co password = cfg.has(config_name + ".password") ? cfg.getString(config_name + ".password") : cfg.getString(parent_config_name + ".password"); + + if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket") + && !cfg.has(parent_config_name + ".port") && !cfg.has(parent_config_name + ".socket")) + throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket"); + port = cfg.has(config_name + ".port") ? cfg.getInt(config_name + ".port") - : cfg.getInt(parent_config_name + ".port"); + : cfg.getInt(parent_config_name + ".port", 0); + socket = cfg.has(config_name + ".socket") + ? cfg.getString(config_name + ".socket") + : cfg.getString(parent_config_name + ".socket", ""); } else { db = cfg.getString(config_name + ".db", ""); user = cfg.getString(config_name + ".user"); password = cfg.getString(config_name + ".password"); - port = cfg.getInt(config_name + ".port"); + + if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket")) + throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket"); + + port = cfg.getInt(config_name + ".port", 0); + socket = cfg.getString(config_name + ".socket", ""); } connect_timeout = cfg.getInt(config_name + ".connect_timeout", @@ -158,6 +171,7 @@ void Pool::Entry::forceConnected() const pool->user.c_str(), pool->password.c_str(), pool->port, + pool->socket.c_str(), pool->connect_timeout, pool->rw_timeout); } @@ -195,6 +209,7 @@ Pool::Connection * Pool::allocConnection(bool dont_throw_if_failed_first_time) user.c_str(), password.c_str(), port, + socket.c_str(), connect_timeout, rw_timeout); }