7 #ifndef MYSQL_CONNECTION_H
8 #define MYSQL_CONNECTION_H
17 #include <boost/scoped_ptr.hpp>
19 #include <mysqld_error.h>
63 (void) mysql_stmt_free_result(statement_);
67 MYSQL_STMT* statement_;
88 template <
typename Fun,
typename... Args>
91 for (
unsigned count = 0; count < 5; ++count) {
92 status = fun(args...);
93 if (status != ER_LOCK_DEADLOCK) {
141 if (mysql_ == NULL) {
150 if (mysql_ != NULL) {
159 operator MYSQL*()
const {
170 class MySqlConnection;
266 static std::pair<uint32_t, uint32_t>
371 uint32_t valid_lifetime, time_t& cltt);
404 template<
typename StatementIndex>
408 ConsumeResultFun process_result) {
411 std::vector<MYSQL_BIND> in_bind_vec;
413 in_bind_vec.push_back(in_binding->getMySqlBinding());
417 if (!in_bind_vec.empty()) {
420 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
421 checkError(status, index,
"unable to bind parameters for select");
425 std::vector<MYSQL_BIND> out_bind_vec;
427 out_bind_vec.push_back(out_binding->getMySqlBinding());
429 if (!out_bind_vec.empty()) {
430 status = mysql_stmt_bind_result(
statements_[index], &out_bind_vec[0]);
431 checkError(status, index,
"unable to bind result parameters for select");
436 checkError(status, index,
"unable to execute");
438 status = mysql_stmt_store_result(
statements_[index]);
439 checkError(status, index,
"unable to set up for storing all results");
443 while ((status = mysql_stmt_fetch(
statements_[index])) ==
448 process_result(out_bindings);
450 }
catch (
const std::exception& ex) {
461 checkError(status, index,
"unable to fetch results");
463 }
else if (status == MYSQL_DATA_TRUNCATED) {
466 <<
" returned truncated data");
484 template<
typename StatementIndex>
488 std::vector<MYSQL_BIND> in_bind_vec;
490 in_bind_vec.push_back(in_binding->getMySqlBinding());
494 int status = mysql_stmt_bind_param(
statements_[index],
495 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
496 checkError(status, index,
"unable to bind parameters");
503 if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
507 if (mysql_errno(
mysql_) == ER_BAD_NULL_ERROR) {
510 checkError(status, index,
"unable to execute");
528 template<
typename StatementIndex>
532 std::vector<MYSQL_BIND> in_bind_vec;
534 in_bind_vec.push_back(in_binding->getMySqlBinding());
538 int status = mysql_stmt_bind_param(
statements_[index],
539 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
540 checkError(status, index,
"unable to bind parameters");
547 if ((mysql_errno(
mysql_) == ER_DUP_ENTRY)
548 #ifdef ER_FOREIGN_DUPLICATE_KEY
549 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY)
551 #ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
552 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
554 #ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
555 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
560 checkError(status, index,
"unable to execute");
564 return (static_cast<uint64_t>(mysql_stmt_affected_rows(
statements_[index])));
611 template<
typename StatementIndex>
612 void checkError(
const int status,
const StatementIndex& index,
615 switch(mysql_errno(
mysql_)) {
622 case CR_SERVER_GONE_ERROR:
624 case CR_OUT_OF_MEMORY:
625 case CR_CONNECTION_ERROR: {
641 "fatal database error or connectivity lost");
648 << mysql_error(
mysql_) <<
" (error code "
649 << mysql_errno(
mysql_) <<
")");
706 #endif // MYSQL_CONNECTION_H
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.
void selectQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings, MySqlBindingCollection &out_bindings, ConsumeResultFun process_result)
Executes SELECT query using prepared statement.
MySqlHolder mysql_
MySQL connection handle.
Fetch and Release MySQL Results.
void insertQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes INSERT prepared statement.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Convert time_t value to database time.
DB_LOG & arg(T first, Args...args)
Pass parameters to replace logger placeholders.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
void commit()
Commits transaction.
MySqlTransaction(MySqlConnection &conn)
Constructor.
std::function< void(MySqlBindingCollection &)> ConsumeResultFun
Function invoked to process fetched row.
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
void checkError(const int status, const StatementIndex &index, const char *what)
Check Error and Throw Exception.
int retryOnDeadlock(Fun &fun, Args...args)
Retry on InnoDB deadlock.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
MySqlFreeResult(MYSQL_STMT *statement)
Constructor.
Common database connection class.
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.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
isc::asiolink::IOServicePtr io_service_
IOService object, used for all ASIO operations.
int MysqlQuery(MYSQL *mysql, const char *stmt)
Execute a literal statement.
~MySqlFreeResult()
Destructor.
void clearStatements()
Clears prepared statements and text statements.
virtual ~MySqlConnection()
Destructor.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
uint64_t updateDeleteQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes UPDATE or DELETE prepared statement and returns the number of affected rows.
std::vector< MYSQL_STMT * > statements_
Prepared statements.
const int MLM_MYSQL_FETCH_FAILURE
MySQL fetch failure code.
MySqlHolder()
Constructor.
RAII object representing MySQL transaction.
void startTransaction()
Starts Transaction.
MySqlConnection(const ParameterMap ¶meters, IOServiceAccessorPtr io_accessor=IOServiceAccessorPtr(), DbCallback callback=DbCallback())
Constructor.
void markUnusable()
Sets the unusable flag to true.
void startRecoverDbConnection()
The recover connection.
std::vector< MySqlBindingPtr > MySqlBindingCollection
Collection of bindings.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
void rollback()
Rollback Transactions.
IOServiceAccessorPtr io_service_accessor_
Accessor function which returns the IOService that can be used to recover the connection.
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
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.
~MySqlHolder()
Destructor.
MySQL Selection Statements.
Exception thrown on failure to execute a database function.
Key is NULL but was specified NOT NULL.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
DbCallback callback_
The callback used to recover the connection.
Database duplicate entry error.
~MySqlTransaction()
Destructor.
Common MySQL Connector Pool.