2014-04-08 06:51:53 +00:00
|
|
|
#pragma once
|
2011-03-03 19:57:34 +00:00
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
#include <memory>
|
2011-03-03 19:57:34 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2011-03-09 19:18:01 +00:00
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
#include <mysqlxx/Query.h>
|
2016-02-03 01:17:58 +00:00
|
|
|
#include <mysqlxx/Exception.h>
|
2011-03-03 19:57:34 +00:00
|
|
|
|
2011-10-05 20:21:18 +00:00
|
|
|
#define MYSQLXX_DEFAULT_TIMEOUT 60
|
2013-04-24 21:50:19 +00:00
|
|
|
#define MYSQLXX_DEFAULT_RW_TIMEOUT 1800
|
2011-05-13 16:58:53 +00:00
|
|
|
|
2018-10-01 18:04:55 +00:00
|
|
|
/// Disable LOAD DATA LOCAL INFILE because it is insecure
|
|
|
|
#define MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE false
|
2021-02-26 06:49:49 +00:00
|
|
|
/// See https://dev.mysql.com/doc/c-api/5.7/en/c-api-auto-reconnect.html
|
|
|
|
#define MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT true
|
2018-10-01 18:04:55 +00:00
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
{
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/** LibrarySingleton is used for appropriate initialisation and deinitialisation of MySQL library.
|
|
|
|
* Makes single thread-safe call of mysql_library_init().
|
|
|
|
* Usage:
|
2017-04-01 07:20:54 +00:00
|
|
|
* LibrarySingleton::instance();
|
2011-03-18 20:26:54 +00:00
|
|
|
*/
|
2019-08-22 03:24:05 +00:00
|
|
|
class LibrarySingleton : private boost::noncopyable
|
2011-03-15 20:56:42 +00:00
|
|
|
{
|
2019-08-22 03:24:05 +00:00
|
|
|
public:
|
|
|
|
static auto & instance();
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
LibrarySingleton();
|
|
|
|
~LibrarySingleton();
|
2011-03-15 20:56:42 +00:00
|
|
|
};
|
2011-10-05 20:21:18 +00:00
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/** MySQL connection.
|
|
|
|
* Usage:
|
|
|
|
* mysqlxx::Connection connection("Test", "127.0.0.1", "root", "qwerty", 3306);
|
2011-03-18 20:26:54 +00:00
|
|
|
*
|
2017-07-18 11:52:46 +00:00
|
|
|
* Or with Poco library configuration:
|
2017-04-01 07:20:54 +00:00
|
|
|
* mysqlxx::Connection connection("mysql_params");
|
2011-10-05 20:21:18 +00:00
|
|
|
*
|
2017-07-18 11:52:46 +00:00
|
|
|
* Or using socket:
|
|
|
|
* mysqlxx::Connection connection("Test", "localhost", "root", "qwerty", 0, "/path/to/socket/file.sock");
|
|
|
|
*
|
2017-09-09 21:26:02 +00:00
|
|
|
* Or using custom certificate authority file:
|
|
|
|
* mysqlxx::Connection connection("Test", "localhost", "root", "qwerty", 3306, "/path/to/ca/file.pem");
|
|
|
|
*
|
|
|
|
* Or using custom certificate and key file:
|
|
|
|
* mysqlxx::Connection connection("Test", "localhost", "root", "qwerty", 3306, "", "/path/to/cert/file.pem", "/path/to/key/file.pem");
|
|
|
|
*
|
2017-07-18 11:52:46 +00:00
|
|
|
* Attention! It's strictly recommended to use connection in thread where it was created.
|
|
|
|
* In order to use connection in other thread, you should call MySQL C API function mysql_thread_init() before and
|
|
|
|
* mysql_thread_end() after working with it.
|
2011-03-18 20:26:54 +00:00
|
|
|
*/
|
2017-09-08 02:29:47 +00:00
|
|
|
class Connection final : private boost::noncopyable
|
2011-03-03 19:57:34 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-07-18 11:52:46 +00:00
|
|
|
/// For delayed initialisation
|
2017-04-01 07:20:54 +00:00
|
|
|
Connection();
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Creates connection. Either port either socket should be specified.
|
|
|
|
/// If server is localhost and socket is not empty, than socket is used. Otherwise, server and port is used.
|
2017-04-01 07:20:54 +00:00
|
|
|
Connection(
|
2017-07-14 12:45:32 +00:00
|
|
|
const char * db,
|
|
|
|
const char * server,
|
2018-08-10 04:02:56 +00:00
|
|
|
const char * user = nullptr,
|
|
|
|
const char * password = nullptr,
|
2017-04-01 07:20:54 +00:00
|
|
|
unsigned port = 0,
|
2017-07-14 12:45:32 +00:00
|
|
|
const char * socket = "",
|
2017-09-09 21:26:02 +00:00
|
|
|
const char * ssl_ca = "",
|
|
|
|
const char * ssl_cert = "",
|
|
|
|
const char * ssl_key = "",
|
2017-04-01 07:20:54 +00:00
|
|
|
unsigned timeout = MYSQLXX_DEFAULT_TIMEOUT,
|
2018-10-01 18:04:55 +00:00
|
|
|
unsigned rw_timeout = MYSQLXX_DEFAULT_RW_TIMEOUT,
|
2021-02-26 06:49:49 +00:00
|
|
|
bool enable_local_infile = MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE,
|
|
|
|
bool opt_reconnect = MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Creates connection. Can be used if Poco::Util::Application is using.
|
|
|
|
/// All settings will be got from config_name section of configuration.
|
2017-04-01 07:20:54 +00:00
|
|
|
Connection(const std::string & config_name);
|
|
|
|
|
2017-09-08 02:29:47 +00:00
|
|
|
~Connection();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Provides delayed initialization or reconnection with other settings.
|
2017-09-08 02:29:47 +00:00
|
|
|
void connect(const char * db,
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * server,
|
|
|
|
const char * user,
|
|
|
|
const char * password,
|
|
|
|
unsigned port,
|
2017-07-14 12:45:32 +00:00
|
|
|
const char * socket,
|
2017-09-09 21:26:02 +00:00
|
|
|
const char* ssl_ca,
|
|
|
|
const char* ssl_cert,
|
|
|
|
const char* ssl_key,
|
2017-04-01 07:20:54 +00:00
|
|
|
unsigned timeout = MYSQLXX_DEFAULT_TIMEOUT,
|
2018-10-01 18:04:55 +00:00
|
|
|
unsigned rw_timeout = MYSQLXX_DEFAULT_RW_TIMEOUT,
|
2021-02-26 06:49:49 +00:00
|
|
|
bool enable_local_infile = MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE,
|
|
|
|
bool opt_reconnect = MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void connect(const std::string & config_name)
|
|
|
|
{
|
|
|
|
Poco::Util::LayeredConfiguration & cfg = Poco::Util::Application::instance().config();
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
std::string db = cfg.getString(config_name + ".db", "");
|
|
|
|
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", 0);
|
2017-07-14 12:45:32 +00:00
|
|
|
std::string socket = cfg.getString(config_name + ".socket", "");
|
2017-09-09 21:26:02 +00:00
|
|
|
std::string ssl_ca = cfg.getString(config_name + ".ssl_ca", "");
|
|
|
|
std::string ssl_cert = cfg.getString(config_name + ".ssl_cert", "");
|
|
|
|
std::string ssl_key = cfg.getString(config_name + ".ssl_key", "");
|
2018-10-01 18:04:55 +00:00
|
|
|
bool enable_local_infile = cfg.getBool(config_name + ".enable_local_infile", MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE);
|
2021-02-26 06:49:49 +00:00
|
|
|
bool opt_reconnect = cfg.getBool(config_name + ".opt_reconnect", MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
unsigned timeout =
|
|
|
|
cfg.getInt(config_name + ".connect_timeout",
|
|
|
|
cfg.getInt("mysql_connect_timeout",
|
|
|
|
MYSQLXX_DEFAULT_TIMEOUT));
|
|
|
|
|
|
|
|
unsigned rw_timeout =
|
|
|
|
cfg.getInt(config_name + ".rw_timeout",
|
|
|
|
cfg.getInt("mysql_rw_timeout",
|
|
|
|
MYSQLXX_DEFAULT_RW_TIMEOUT));
|
|
|
|
|
2017-09-09 21:26:02 +00:00
|
|
|
connect(
|
|
|
|
db.c_str(),
|
|
|
|
server.c_str(),
|
|
|
|
user.c_str(),
|
|
|
|
password.c_str(),
|
|
|
|
port,
|
|
|
|
socket.c_str(),
|
|
|
|
ssl_ca.c_str(),
|
|
|
|
ssl_cert.c_str(),
|
|
|
|
ssl_key.c_str(),
|
|
|
|
timeout,
|
2018-10-01 18:04:55 +00:00
|
|
|
rw_timeout,
|
2021-02-26 06:49:49 +00:00
|
|
|
enable_local_infile,
|
|
|
|
opt_reconnect);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// If MySQL connection was established.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool connected() const;
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Disconnect from MySQL.
|
2017-04-01 07:20:54 +00:00
|
|
|
void disconnect();
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Tries to reconnect if connection was lost. Is true if connection is established after call.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool ping();
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Creates query. It can be set with query string or later.
|
2017-04-01 07:20:54 +00:00
|
|
|
Query query(const std::string & str = "");
|
|
|
|
|
2017-07-18 11:52:46 +00:00
|
|
|
/// Get MySQL C API MYSQL object.
|
2017-04-01 07:20:54 +00:00
|
|
|
MYSQL * getDriver();
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_ptr<MYSQL> driver;
|
2018-08-13 06:16:58 +00:00
|
|
|
bool is_initialized = false;
|
|
|
|
bool is_connected = false;
|
2011-03-03 19:57:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|