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_;
88template <
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 {
172class MySqlConnection;
269 static std::pair<uint32_t, uint32_t>
374 uint32_t valid_lifetime, time_t& cltt);
424 template<
typename StatementIndex>
431 std::vector<MYSQL_BIND> in_bind_vec;
433 in_bind_vec.push_back(in_binding->getMySqlBinding());
437 if (!in_bind_vec.empty()) {
440 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
441 checkError(status, index,
"unable to bind parameters for select");
445 std::vector<MYSQL_BIND> out_bind_vec;
447 out_bind_vec.push_back(out_binding->getMySqlBinding());
449 if (!out_bind_vec.empty()) {
450 status = mysql_stmt_bind_result(
statements_[index], &out_bind_vec[0]);
451 checkError(status, index,
"unable to bind result parameters for select");
456 checkError(status, index,
"unable to execute");
458 status = mysql_stmt_store_result(
statements_[index]);
459 checkError(status, index,
"unable to set up for storing all results");
463 while ((status = mysql_stmt_fetch(
statements_[index])) ==
468 process_result(out_bindings);
470 }
catch (
const std::exception& ex) {
481 checkError(status, index,
"unable to fetch results");
483 }
else if (status == MYSQL_DATA_TRUNCATED) {
486 <<
" returned truncated data");
504 template<
typename StatementIndex>
508 std::vector<MYSQL_BIND> in_bind_vec;
510 in_bind_vec.push_back(in_binding->getMySqlBinding());
514 int status = mysql_stmt_bind_param(
statements_[index],
515 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
516 checkError(status, index,
"unable to bind parameters");
523 if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
527 if (mysql_errno(
mysql_) == ER_BAD_NULL_ERROR) {
530 checkError(status, index,
"unable to execute");
548 template<
typename StatementIndex>
552 std::vector<MYSQL_BIND> in_bind_vec;
554 in_bind_vec.push_back(in_binding->getMySqlBinding());
558 int status = mysql_stmt_bind_param(
statements_[index],
559 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
560 checkError(status, index,
"unable to bind parameters");
567 if ((mysql_errno(
mysql_) == ER_DUP_ENTRY)
568#ifdef ER_FOREIGN_DUPLICATE_KEY
569 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY)
571#ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
572 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
574#ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
575 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
580 checkError(status, index,
"unable to execute");
584 return (
static_cast<uint64_t
>(mysql_stmt_affected_rows(
statements_[index])));
639 template<
typename StatementIndex>
640 void checkError(
const int status,
const StatementIndex& index,
643 switch(mysql_errno(
mysql_)) {
650 case CR_SERVER_GONE_ERROR:
652 case CR_OUT_OF_MEMORY:
653 case CR_CONNECTION_ERROR: {
669 "fatal database error or connectivity lost");
676 << mysql_error(
mysql_) <<
" (error code "
677 << mysql_errno(
mysql_) <<
")");
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Common database connection class.
void markUnusable()
Sets the unusable flag to true.
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
void checkUnusable()
Throws an exception if the connection is not usable.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
DbCallback callback_
The callback used to recover the connection.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Database duplicate entry error.
Common MySQL Connector Pool.
isc::asiolink::IOServicePtr io_service_
IOService object, used for all ASIO operations.
MySqlHolder mysql_
MySQL connection handle.
void prepareStatement(uint32_t index, const char *text)
Prepare Single Statement.
std::vector< MYSQL_STMT * > statements_
Prepared statements.
bool isTransactionStarted() const
Checks if there is a transaction in progress.
std::vector< std::string > text_statements_
Raw text of statements.
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.
IOServiceAccessorPtr io_service_accessor_
Accessor function which returns the IOService that can be used to recover the connection.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Convert Database Time to Lease Times.
void commit()
Commits current transaction.
MySqlConnection(const ParameterMap ¶meters, IOServiceAccessorPtr io_accessor=IOServiceAccessorPtr(), DbCallback callback=DbCallback())
Constructor.
void startRecoverDbConnection()
The recover connection.
uint64_t updateDeleteQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes UPDATE or DELETE prepared statement and returns the number of affected rows.
void openDatabase()
Open Database.
void prepareStatements(const TaggedStatement *start_statement, const TaggedStatement *end_statement)
Prepare statements.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
int transaction_ref_count_
Reference counter for transactions.
void startTransaction()
Starts new transaction.
virtual ~MySqlConnection()
Destructor.
std::function< void(MySqlBindingCollection &)> ConsumeResultFun
Function invoked to process fetched row.
void checkError(const int status, const StatementIndex &index, const char *what)
Check Error and Throw Exception.
void selectQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings, MySqlBindingCollection &out_bindings, ConsumeResultFun process_result)
Executes SELECT query using prepared statement.
void clearStatements()
Clears prepared statements and text statements.
void rollback()
Rollbacks current transaction.
Fetch and Release MySQL Results.
~MySqlFreeResult()
Destructor.
MySqlFreeResult(MYSQL_STMT *statement)
Constructor.
MySqlHolder()
Constructor.
~MySqlHolder()
Destructor.
RAII object representing MySQL transaction.
~MySqlTransaction()
Destructor.
void commit()
Commits transaction.
MySqlTransaction(MySqlConnection &conn)
Constructor.
Key is NULL but was specified NOT NULL.
We want to reuse the database backend connection and exchange code for other uses,...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
const int MLM_MYSQL_FETCH_FAILURE
MySQL fetch failure code.
int MysqlQuery(MYSQL *mysql, const char *stmt)
Execute a literal statement.
std::vector< MySqlBindingPtr > MySqlBindingCollection
Collection of bindings.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
int retryOnDeadlock(Fun &fun, Args... args)
Retry on InnoDB deadlock.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
Defines the logger used by the top-level component of kea-lfc.
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
MySQL Selection Statements.