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 {
170class MySqlConnection;
267 static std::pair<uint32_t, uint32_t>
372 uint32_t valid_lifetime, time_t& cltt);
422 template<
typename StatementIndex>
429 std::vector<MYSQL_BIND> in_bind_vec;
431 in_bind_vec.push_back(in_binding->getMySqlBinding());
435 if (!in_bind_vec.empty()) {
438 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
439 checkError(status, index,
"unable to bind parameters for select");
443 std::vector<MYSQL_BIND> out_bind_vec;
445 out_bind_vec.push_back(out_binding->getMySqlBinding());
447 if (!out_bind_vec.empty()) {
448 status = mysql_stmt_bind_result(
statements_[index], &out_bind_vec[0]);
449 checkError(status, index,
"unable to bind result parameters for select");
454 checkError(status, index,
"unable to execute");
456 status = mysql_stmt_store_result(
statements_[index]);
457 checkError(status, index,
"unable to set up for storing all results");
461 while ((status = mysql_stmt_fetch(
statements_[index])) ==
466 process_result(out_bindings);
468 }
catch (
const std::exception& ex) {
479 checkError(status, index,
"unable to fetch results");
481 }
else if (status == MYSQL_DATA_TRUNCATED) {
484 <<
" returned truncated data");
502 template<
typename StatementIndex>
506 std::vector<MYSQL_BIND> in_bind_vec;
508 in_bind_vec.push_back(in_binding->getMySqlBinding());
512 int status = mysql_stmt_bind_param(
statements_[index],
513 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
514 checkError(status, index,
"unable to bind parameters");
521 if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
525 if (mysql_errno(
mysql_) == ER_BAD_NULL_ERROR) {
528 checkError(status, index,
"unable to execute");
546 template<
typename StatementIndex>
550 std::vector<MYSQL_BIND> in_bind_vec;
552 in_bind_vec.push_back(in_binding->getMySqlBinding());
556 int status = mysql_stmt_bind_param(
statements_[index],
557 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
558 checkError(status, index,
"unable to bind parameters");
565 if ((mysql_errno(
mysql_) == ER_DUP_ENTRY)
566#ifdef ER_FOREIGN_DUPLICATE_KEY
567 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY)
569#ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
570 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
572#ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
573 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
578 checkError(status, index,
"unable to execute");
582 return (
static_cast<uint64_t
>(mysql_stmt_affected_rows(
statements_[index])));
637 template<
typename StatementIndex>
638 void checkError(
const int status,
const StatementIndex& index,
641 switch(mysql_errno(
mysql_)) {
648 case CR_SERVER_GONE_ERROR:
650 case CR_OUT_OF_MEMORY:
651 case CR_CONNECTION_ERROR: {
667 "fatal database error or connectivity lost");
674 << mysql_error(
mysql_) <<
" (error code "
675 << 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.