13 #include <boost/lexical_cast.hpp>
26 bool MySqlHolder::atexit_ = []{atexit([]{mysql_library_end();});
return true;};
32 : conn_(conn), committed_(false) {
56 const char* host =
"localhost";
65 unsigned int port = 0;
74 if (sport.size() > 0) {
78 port = boost::lexical_cast<
unsigned int>(sport);
87 if (port > numeric_limits<uint16_t>::max()) {
92 const char* user = NULL;
101 const char* password = NULL;
105 password = spassword.c_str();
110 const char* name = NULL;
114 name = sname.c_str();
129 if (stimeout.size() > 0) {
133 connect_timeout = boost::lexical_cast<
unsigned int>(stimeout);
150 if ((connect_timeout == 0) ||
151 (connect_timeout > numeric_limits<int>::max())) {
153 stimeout <<
") must be an integer greater than 0");
164 int result = mysql_options(
mysql_, MYSQL_OPT_RECONNECT, &auto_reconnect);
171 const char *wait_time =
"SET SESSION wait_timeout = 30 * 86400";
172 result = mysql_options(
mysql_, MYSQL_INIT_COMMAND, wait_time);
182 const char *sql_mode =
"SET SESSION sql_mode ='STRICT_ALL_TABLES'";
183 result = mysql_options(
mysql_, MYSQL_INIT_COMMAND, sql_mode);
191 result = mysql_options(
mysql_, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout);
207 MYSQL* status = mysql_real_connect(
mysql_, host, user, password, name,
208 port, NULL, CLIENT_FOUND_ROWS);
221 if (autocommit_result != 0) {
234 std::pair<uint32_t, uint32_t>
243 MYSQL_STMT *stmt = mysql_stmt_init(conn.
mysql_);
246 "statement structure, reason: " << mysql_error(conn.
mysql_));
252 const char* version_sql =
"SELECT version, minor FROM schema_version";
253 int status = mysql_stmt_prepare(stmt, version_sql, strlen(version_sql));
256 << version_sql <<
">, reason: "
257 << mysql_error(conn.
mysql_));
263 << version_sql <<
">, reason: "
264 << mysql_errno(conn.
mysql_));
269 memset(bind, 0,
sizeof(bind));
272 bind[0].buffer_type = MYSQL_TYPE_LONG;
273 bind[0].is_unsigned = 1;
275 bind[0].buffer_length =
sizeof(
version);
278 bind[1].buffer_type = MYSQL_TYPE_LONG;
279 bind[1].is_unsigned = 1;
280 bind[1].buffer = &minor;
281 bind[1].buffer_length =
sizeof(minor);
283 if (mysql_stmt_bind_result(stmt, bind)) {
285 << version_sql <<
">, reason: "
286 << mysql_errno(conn.
mysql_));
290 if (mysql_stmt_fetch(stmt)) {
292 << version_sql <<
">, reason: "
293 << mysql_errno(conn.
mysql_));
297 mysql_stmt_close(stmt);
299 return (std::make_pair(version, minor));
301 }
catch (
const std::exception&) {
303 mysql_stmt_close(stmt);
323 static_cast<int>(index) <<
") or indexed prepared " <<
324 "statement is not null");
332 "statement structure, reason: " << mysql_error(
mysql_));
335 int status = mysql_stmt_prepare(
statements_[index], text, strlen(text));
338 text <<
">, reason: " << mysql_error(
mysql_));
347 tagged_statement != end_statement; ++tagged_statement) {
348 if (tagged_statement->index >=
statements_.size()) {
349 statements_.resize(tagged_statement->index + 1, NULL);
354 tagged_statement->text);
390 MYSQL_TIME& output_time) {
396 const uint32_t valid_lifetime,
397 MYSQL_TIME& expire) {
403 uint32_t valid_lifetime, time_t& cltt) {
413 int status = mysql_query(
mysql_,
"START TRANSACTION");
416 "reason: " << mysql_error(
mysql_));
424 if (mysql_commit(
mysql_) != 0) {
434 if (mysql_rollback(
mysql_) != 0) {
std::vector< std::string > text_statements_
Raw text of statements.
We want to reuse the database backend connection and exchange code for other uses, in particular for hook libraries.
bool my_bool
my_bool type in MySQL 8.x.
MySqlHolder mysql_
MySQL connection handle.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Convert time_t value to database time.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Converts time_t value to database time.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
void commit()
Commits transaction.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Convert Database Time to Lease Times.
void checkUnusable()
Throws an exception if the connection is not usable.
Exception thrown on failure to open database.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const my_bool MLM_FALSE
MySQL false value.
Exception thrown if name of database is not specified.
int version()
returns Kea hooks version.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Converts Database Time to Lease Times.
void clearStatements()
Clears prepared statements and text statements.
const int MYSQL_DEFAULT_CONNECTION_TIMEOUT
virtual ~MySqlConnection()
Destructor.
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
std::vector< MYSQL_STMT * > statements_
Prepared statements.
void startTransaction()
Starts Transaction.
void rollback()
Rollback Transactions.
void commit()
Commit Transactions.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
void prepareStatement(uint32_t index, const char *text)
Prepare Single Statement.
void prepareStatements(const TaggedStatement *start_statement, const TaggedStatement *end_statement)
Prepare statements.
void openDatabase()
Open Database.
MySQL Selection Statements.
Exception thrown on failure to execute a database function.
~MySqlTransaction()
Destructor.
Common MySQL Connector Pool.