7 #ifndef MYSQL_CONNECTION_H 8 #define MYSQL_CONNECTION_H 16 #include <boost/scoped_ptr.hpp> 18 #include <mysqld_error.h> 62 (void) mysql_stmt_free_result(statement_);
66 MYSQL_STMT* statement_;
108 if (mysql_ != NULL) {
119 operator MYSQL*()
const {
128 class MySqlConnection;
309 uint32_t valid_lifetime, time_t& cltt);
342 template<
typename StatementIndex>
348 std::vector<MYSQL_BIND> in_bind_vec;
350 in_bind_vec.push_back(in_binding->getMySqlBinding());
354 if (!in_bind_vec.empty()) {
356 status = mysql_stmt_bind_param(
statements_[index], &in_bind_vec[0]);
357 checkError(status, index,
"unable to bind parameters for select");
361 std::vector<MYSQL_BIND> out_bind_vec;
363 out_bind_vec.push_back(out_binding->getMySqlBinding());
365 if (!out_bind_vec.empty()) {
366 status = mysql_stmt_bind_result(
statements_[index], &out_bind_vec[0]);
367 checkError(status, index,
"unable to bind result parameters for select");
372 checkError(status, index,
"unable to execute");
374 status = mysql_stmt_store_result(
statements_[index]);
375 checkError(status, index,
"unable to set up for storing all results");
379 while ((status = mysql_stmt_fetch(
statements_[index])) ==
384 process_result(out_bindings);
386 }
catch (
const std::exception& ex) {
397 checkError(status, index,
"unable to fetch results");
399 }
else if (status == MYSQL_DATA_TRUNCATED) {
402 <<
" returned truncated data");
420 template<
typename StatementIndex>
423 std::vector<MYSQL_BIND> in_bind_vec;
425 in_bind_vec.push_back(in_binding->getMySqlBinding());
429 int status = mysql_stmt_bind_param(
statements_[index], &in_bind_vec[0]);
430 checkError(status, index,
"unable to bind parameters");
437 if (mysql_errno(
mysql_) == ER_DUP_ENTRY) {
440 checkError(status, index,
"unable to execute");
458 template<
typename StatementIndex>
461 std::vector<MYSQL_BIND> in_bind_vec;
463 in_bind_vec.push_back(in_binding->getMySqlBinding());
467 int status = mysql_stmt_bind_param(
statements_[index], &in_bind_vec[0]);
468 checkError(status, index,
"unable to bind parameters");
474 checkError(status, index,
"unable to execute");
478 return (static_cast<uint64_t>(mysql_stmt_affected_rows(
statements_[index])));
528 template<
typename StatementIndex>
529 void checkError(
const int status,
const StatementIndex& index,
530 const char* what)
const {
532 switch(mysql_errno(
mysql_)) {
539 case CR_SERVER_GONE_ERROR:
541 case CR_OUT_OF_MEMORY:
542 case CR_CONNECTION_ERROR:
547 .arg(mysql_errno(
mysql_));
558 "fatal database errror or connectivity lost");
564 << mysql_error(
mysql_) <<
" (error code " 565 << mysql_errno(
mysql_) <<
")");
592 #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,...
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.
void commit()
Commits transaction.
MySqlTransaction(MySqlConnection &conn)
Constructor.
std::function< void(MySqlBindingCollection &)> ConsumeResultFun
Function invoked to process fetched row.
bool invokeDbLostCallback() const
Invokes the connection's lost connectivity callback.
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.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Exception thrown on failure to open database.
#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...
MySqlConnection(const ParameterMap ¶meters)
Constructor.
~MySqlFreeResult()
Destructor.
void clearStatements()
Clears prepared statements and text statements.
virtual ~MySqlConnection()
Destructor.
const int MLM_MYSQL_FETCH_SUCCESS
MySQL fetch success code.
Defines the logger used by the top-level component of kea-dhcp-ddns.
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.
#define DB_LOG_ERROR(MESSAGE)
std::vector< MySqlBindingPtr > MySqlBindingCollection
Collection of bindings.
void rollback()
Rollback Transactions.
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 checkError(const int status, const StatementIndex &index, const char *what) const
Check Error and Throw Exception.
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.
Database duplicate entry error.
~MySqlTransaction()
Destructor.
Common MySQL Connector Pool.