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 {
172 class MySqlConnection;
269 static std::pair<uint32_t, uint32_t>
313 template<
typename StatementIndex>
315 if (statements_[index]->mysql == 0) {
317 "MySQL pointer for the prepared statement is NULL as a result of connectivity loss");
319 return (statements_[index]);
389 uint32_t valid_lifetime, time_t& cltt);
439 template<
typename StatementIndex>
446 std::vector<MYSQL_BIND> in_bind_vec;
448 in_bind_vec.push_back(in_binding->getMySqlBinding());
452 if (!in_bind_vec.empty()) {
455 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
456 checkError(status, index,
"unable to bind parameters for select");
460 std::vector<MYSQL_BIND> out_bind_vec;
462 out_bind_vec.push_back(out_binding->getMySqlBinding());
464 if (!out_bind_vec.empty()) {
465 status = mysql_stmt_bind_result(
getStatement(index), &out_bind_vec[0]);
466 checkError(status, index,
"unable to bind result parameters for select");
471 checkError(status, index,
"unable to execute");
474 checkError(status, index,
"unable to set up for storing all results");
478 while ((status = mysql_stmt_fetch(
getStatement(index))) ==
483 process_result(out_bindings);
485 }
catch (
const std::exception& ex) {
496 checkError(status, index,
"unable to fetch results");
498 }
else if (status == MYSQL_DATA_TRUNCATED) {
501 <<
" returned truncated data");
519 template<
typename StatementIndex>
523 std::vector<MYSQL_BIND> in_bind_vec;
525 in_bind_vec.push_back(in_binding->getMySqlBinding());
530 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
531 checkError(status, index,
"unable to bind parameters");
538 if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
542 if (mysql_errno(
mysql_) == ER_BAD_NULL_ERROR) {
545 checkError(status, index,
"unable to execute");
563 template<
typename StatementIndex>
567 std::vector<MYSQL_BIND> in_bind_vec;
569 in_bind_vec.push_back(in_binding->getMySqlBinding());
574 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
575 checkError(status, index,
"unable to bind parameters");
582 if ((mysql_errno(
mysql_) == ER_DUP_ENTRY)
583 #ifdef ER_FOREIGN_DUPLICATE_KEY
584 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY)
586 #ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
587 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
589 #ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
590 || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
595 checkError(status, index,
"unable to execute");
599 return (
static_cast<uint64_t
>(mysql_stmt_affected_rows(
getStatement(index))));
654 template<
typename StatementIndex>
655 void checkError(
const int status,
const StatementIndex& index,
658 switch(mysql_errno(
mysql_)) {
665 case CR_SERVER_GONE_ERROR:
667 case CR_OUT_OF_MEMORY:
668 case CR_CONNECTION_ERROR: {
684 "fatal database error or connectivity lost");
691 << mysql_error(
mysql_) <<
" (error code "
692 << mysql_errno(
mysql_) <<
")");
726 const char* cipher = mysql_get_ssl_cipher(
mysql_);
727 return (cipher ? std::string(cipher) :
"");
747 void setIntParameterValue(
const std::string& name, int64_t min, int64_t max, T& value);
753 std::vector<MYSQL_STMT*> statements_;
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.
util::ReconnectCtlPtr reconnectCtl()
The reconnect settings.
void markUnusable()
Sets the unusable flag to true.
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.
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.
bool tls_
TLS flag (true when TLS was required, false otherwise).
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.
std::string getTlsCipher()
Get the TLS cipher.
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.
bool getTls() const
Get the TLS flag.
void selectQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings, MySqlBindingCollection &out_bindings, ConsumeResultFun process_result)
Executes SELECT query using prepared statement.
MYSQL_STMT * getStatement(StatementIndex index) const
Returns a prepared statement by an index.
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.
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.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
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.