13 #include <boost/lexical_cast.hpp> 30 : conn_(conn), committed_(false) {
55 const char* host =
"localhost";
64 unsigned int port = 0;
73 if (sport.size() > 0) {
77 port = boost::lexical_cast<unsigned int>(sport);
86 if (port > numeric_limits<uint16_t>::max()) {
91 const char* user = NULL;
100 const char* password = NULL;
104 password = spassword.c_str();
109 const char* name = NULL;
113 name = sname.c_str();
128 if (stimeout.size() > 0) {
132 connect_timeout = boost::lexical_cast<unsigned int>(stimeout);
149 if ((connect_timeout == 0) ||
150 (connect_timeout > numeric_limits<int>::max())) {
152 stimeout <<
") must be an integer greater than 0");
163 int result = mysql_options(
mysql_, MYSQL_OPT_RECONNECT, &auto_reconnect);
170 const char *wait_time =
"SET SESSION wait_timeout = 30 * 86400";
171 result = mysql_options(
mysql_, MYSQL_INIT_COMMAND, wait_time);
181 const char *sql_mode =
"SET SESSION sql_mode ='STRICT_ALL_TABLES'";
182 result = mysql_options(
mysql_, MYSQL_INIT_COMMAND, sql_mode);
190 result = mysql_options(
mysql_, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout);
206 MYSQL* status = mysql_real_connect(
mysql_, host, user, password, name,
207 port, NULL, CLIENT_FOUND_ROWS);
227 static_cast<int>(index) <<
") or indexed prepared " <<
228 "statement is not null");
236 "statement structure, reason: " << mysql_error(
mysql_));
239 int status = mysql_stmt_prepare(
statements_[index], text, strlen(text));
242 text <<
">, reason: " << mysql_error(
mysql_));
251 tagged_statement != end_statement; ++tagged_statement) {
252 if (tagged_statement->index >=
statements_.size()) {
253 statements_.resize(tagged_statement->index + 1, NULL);
258 tagged_statement->text);
294 MYSQL_TIME& output_time) {
300 const uint32_t valid_lifetime,
301 MYSQL_TIME& expire) {
307 uint32_t valid_lifetime, time_t& cltt) {
316 int status = mysql_query(
mysql_,
"START TRANSACTION");
319 "reason: " << mysql_error(
mysql_));
326 if (mysql_commit(
mysql_) != 0) {
335 if (mysql_rollback(
mysql_) != 0) {
std::vector< std::string > text_statements_
Raw text of statements.
We want to reuse the database backend connection and exchange code for other uses,...
MySqlHolder mysql_
MySQL connection handle.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Convert time_t value to database time.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Converts time_t value to database time.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
#define DB_LOG_DEBUG(LEVEL, MESSAGE)
Macros.
void commit()
Commits transaction.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Convert Database Time to Lease Times.
Exception thrown on failure to open database.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const my_bool MLM_FALSE
MySQL false value.
Exception thrown if name of database is not specified.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Converts Database Time to Lease Times.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
void clearStatements()
Clears prepared statements and text statements.
const int MYSQL_DEFAULT_CONNECTION_TIMEOUT
virtual ~MySqlConnection()
Destructor.
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::vector< MYSQL_STMT * > statements_
Prepared statements.
void startTransaction()
Starts Transaction.
void rollback()
Rollback Transactions.
void commit()
Commit Transactions.
void prepareStatement(uint32_t index, const char *text)
Prepare Single Statement.
void prepareStatements(const TaggedStatement *start_statement, const TaggedStatement *end_statement)
Prepare statements.
void openDatabase()
Open Database.
MySQL Selection Statements.
Exception thrown on failure to execute a database function.
~MySqlTransaction()
Destructor.
Common MySQL Connector Pool.