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;
 
  264    static std::vector<std::string>
 
  281    static std::pair<uint32_t, uint32_t>
 
  285               const std::string& timer_name = std::string(),
 
  286               unsigned int id = 0);
 
  303                        const std::string& timer_name = std::string());
 
  355    template<
typename StatementIndex>
 
  357        if (statements_[index]->mysql == 0) {
 
  359                      "MySQL pointer for the prepared statement is NULL as a result of connectivity loss");
 
  361        return (statements_[index]);
 
 
  431                                 uint32_t valid_lifetime, time_t& cltt);
 
  481    template<
typename StatementIndex>
 
  488        std::vector<MYSQL_BIND> in_bind_vec;
 
  490            in_bind_vec.push_back(in_binding->getMySqlBinding());
 
  494        if (!in_bind_vec.empty()) {
 
  497                                           in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
 
  498            checkError(status, index, 
"unable to bind parameters for select");
 
  502        std::vector<MYSQL_BIND> out_bind_vec;
 
  504            out_bind_vec.push_back(out_binding->getMySqlBinding());
 
  506        if (!out_bind_vec.empty()) {
 
  507            status = mysql_stmt_bind_result(
getStatement(index), &out_bind_vec[0]);
 
  508            checkError(status, index, 
"unable to bind result parameters for select");
 
  513        checkError(status, index, 
"unable to execute");
 
  516        checkError(status, index, 
"unable to set up for storing all results");
 
  520        while ((status = mysql_stmt_fetch(
getStatement(index))) ==
 
  525                process_result(out_bindings);
 
  527            } 
catch (
const std::exception& ex) {
 
  538            checkError(status, index, 
"unable to fetch results");
 
  540        } 
else if (status == MYSQL_DATA_TRUNCATED) {
 
  543                      << 
" returned truncated data");
 
 
  561    template<
typename StatementIndex>
 
  565        std::vector<MYSQL_BIND> in_bind_vec;
 
  567            in_bind_vec.push_back(in_binding->getMySqlBinding());
 
  572                                           in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
 
  573        checkError(status, index, 
"unable to bind parameters");
 
  580            if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
 
  584            if (mysql_errno(
mysql_) == ER_BAD_NULL_ERROR) {
 
  587            checkError(status, index, 
"unable to execute");
 
 
  605    template<
typename StatementIndex>
 
  609        std::vector<MYSQL_BIND> in_bind_vec;
 
  611            in_bind_vec.push_back(in_binding->getMySqlBinding());
 
  616                                           in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
 
  617        checkError(status, index, 
"unable to bind parameters");
 
  624            if ((mysql_errno(
mysql_) == ER_DUP_ENTRY)
 
  625#ifdef ER_FOREIGN_DUPLICATE_KEY
 
  626                || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY)
 
  628#ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
 
  629                || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
 
  631#ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
 
  632                || (mysql_errno(
mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
 
  637            checkError(status, index, 
"unable to execute");
 
  641        return (
static_cast<uint64_t
>(mysql_stmt_affected_rows(
getStatement(index))));
 
 
  696    template<
typename StatementIndex>
 
  697    void checkError(
const int status, 
const StatementIndex& index,
 
  700            switch(mysql_errno(
mysql_)) {
 
  707            case CR_SERVER_GONE_ERROR:
 
  709            case CR_OUT_OF_MEMORY:
 
  710            case CR_CONNECTION_ERROR: {
 
  726                          "fatal database error or connectivity lost");
 
  733                          << mysql_error(
mysql_) << 
" (error code " 
  734                          << mysql_errno(
mysql_) << 
")");
 
 
  768        const char* cipher = mysql_get_ssl_cipher(
mysql_);
 
  769        return (cipher ? std::string(cipher) : 
"");
 
 
  789    void setIntParameterValue(
const std::string& name, int64_t min, int64_t max, T& value);
 
  795    std::vector<MYSQL_STMT*> statements_;
 
 
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Common database connection class.
util::ReconnectCtlPtr reconnectCtl()
The reconnect settings.
void markUnusable()
Sets the unusable flag to true.
DatabaseConnection(const ParameterMap ¶meters, DbCallback callback=DbCallback())
Constructor.
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.
static std::string KEA_ADMIN_
Holds location to kea-admin.
MySqlHolder mysql_
MySQL connection handle.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters, const IOServiceAccessorPtr &ac=IOServiceAccessorPtr(), const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string(), unsigned int id=0)
Get the schema version.
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.
static void initializeSchema(const ParameterMap ¶meters)
Initialize schema.
uint64_t updateDeleteQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes UPDATE or DELETE prepared statement and returns the number of affected rows.
static std::vector< std::string > toKeaAdminParameters(ParameterMap const ¶ms)
Convert MySQL library parameters to kea-admin parameters.
void openDatabase()
Open Database.
std::string getTlsCipher()
Get the TLS cipher.
void prepareStatements(const TaggedStatement *start_statement, const TaggedStatement *end_statement)
Prepare statements.
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.
MYSQL_STMT * getStatement(StatementIndex index) const
Returns a prepared statement by an index.
void selectQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings, MySqlBindingCollection &out_bindings, ConsumeResultFun process_result)
Executes SELECT query using prepared statement.
void rollback()
Rollbacks current transaction.
static void ensureSchemaVersion(const ParameterMap ¶meters, const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Retrieve schema version, validate it against the hardcoded version, and attempt to initialize the sch...
Fetch and Release MySQL Results.
~MySqlFreeResult()
Destructor.
MySqlFreeResult(MYSQL_STMT *statement)
Constructor.
MySqlHolder()
Constructor.
~MySqlHolder()
Destructor.
~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.