33 session_(NULL), consistency_(CASS_CONSISTENCY_QUORUM), schema_meta_(NULL),
34 keyspace_meta_(NULL), force_consistency_(true) {
40 CassError rc = CASS_OK;
54 CassFuture* close_future = cass_session_close(
session_);
55 cass_future_wait(close_future);
57 "CqlConnection::~CqlConnection(): cass_sesssion_close() != CASS_OK",
59 rc = cass_future_error_code(close_future);
60 cass_future_free(close_future);
81 const char* contact_points =
"127.0.0.1";
82 std::string scontact_points;
85 contact_points = scontact_points.c_str();
90 const char* port = NULL;
99 const char* user = NULL;
103 user = suser.c_str();
108 const char* password = NULL;
109 std::string spassword;
112 password = spassword.c_str();
117 const char* keyspace =
"keatest";
118 std::string skeyspace;
121 keyspace = skeyspace.c_str();
126 const char* reconnect_wait_time = NULL;
127 std::string sreconnect_wait_time;
129 sreconnect_wait_time =
getParameter(
"reconnect-wait-time");
130 reconnect_wait_time = sreconnect_wait_time.c_str();
135 const char* connect_timeout = NULL;
136 std::string sconnect_timeout;
139 connect_timeout = sconnect_timeout.c_str();
144 const char* request_timeout = NULL;
145 std::string srequest_timeout;
148 request_timeout = srequest_timeout.c_str();
153 const char* tcp_keepalive = NULL;
154 std::string stcp_keepalive;
157 tcp_keepalive = stcp_keepalive.c_str();
162 std::string stcp_nodelay;
170 cass_cluster_set_contact_points(
cluster_, contact_points);
172 if (user && password) {
173 cass_cluster_set_credentials(
cluster_, user, password);
179 port_number = boost::lexical_cast<int32_t>(port);
180 if (port_number < 1 || port_number > 65535) {
182 "CqlConnection::openDatabase(): " 183 "port outside of range, expected " 184 "1-65535, instead got " 187 }
catch (
const boost::bad_lexical_cast& ex) {
189 "CqlConnection::openDatabase(): invalid " 190 "port, expected castable to int, instead got " 192 <<
"\", " << ex.
what());
194 cass_cluster_set_port(
cluster_, port_number);
197 if (reconnect_wait_time) {
198 int32_t reconnect_wait_time_number;
200 reconnect_wait_time_number =
201 boost::lexical_cast<int32_t>(reconnect_wait_time);
202 if (reconnect_wait_time_number < 0) {
204 "CqlConnection::openDatabase(): invalid reconnect " 205 "wait time, expected positive number, instead got " 206 << reconnect_wait_time);
208 }
catch (
const boost::bad_lexical_cast& ex) {
210 "CqlConnection::openDatabase(): " 211 "invalid reconnect wait time, expected " 212 "castable to int, instead got \"" 213 << reconnect_wait_time <<
"\", " << ex.
what());
215 cass_cluster_set_reconnect_wait_time(
cluster_,
216 reconnect_wait_time_number);
219 if (connect_timeout) {
220 int32_t connect_timeout_number;
222 connect_timeout_number =
223 boost::lexical_cast<int32_t>(connect_timeout);
224 if (connect_timeout_number < 0) {
226 "CqlConnection::openDatabase(): " 227 "invalid connect timeout, expected " 228 "positive number, instead got " 231 }
catch (
const boost::bad_lexical_cast& ex) {
233 "CqlConnection::openDatabase(): invalid connect timeout, " 234 "expected castable to int, instead got \"" 235 << connect_timeout <<
"\", " << ex.
what());
237 cass_cluster_set_connect_timeout(
cluster_, connect_timeout_number);
240 if (request_timeout) {
241 int32_t request_timeout_number;
243 request_timeout_number =
244 boost::lexical_cast<int32_t>(request_timeout);
245 if (request_timeout_number < 0) {
247 "CqlConnection::openDatabase(): " 248 "invalid request timeout, expected " 249 "positive number, instead got " 252 }
catch (
const boost::bad_lexical_cast& ex) {
254 "CqlConnection::openDatabase(): invalid request timeout, " 255 "expected castable to int, instead got \"" 256 << request_timeout <<
"\", " << ex.
what());
258 cass_cluster_set_request_timeout(
cluster_, request_timeout_number);
262 int32_t tcp_keepalive_number;
264 tcp_keepalive_number = boost::lexical_cast<int32_t>(tcp_keepalive);
265 if (tcp_keepalive_number < 0) {
267 "CqlConnection::openDatabase(): " 268 "invalid TCP keepalive, expected " 269 "positive number, instead got " 272 }
catch (
const boost::bad_lexical_cast& ex) {
274 "CqlConnection::openDatabase(): invalid TCP keepalive, " 275 "expected castable to int, instead got \"" 276 << tcp_keepalive <<
"\", " << ex.
what());
278 cass_cluster_set_tcp_keepalive(
cluster_, cass_true,
279 tcp_keepalive_number);
282 if (stcp_nodelay ==
"true") {
283 cass_cluster_set_tcp_nodelay(
cluster_, cass_true);
288 CassFuture* connect_future =
290 cass_future_wait(connect_future);
291 const std::string error =
293 "cass_session_connect_keyspace() != CASS_OK",
295 rc = cass_future_error_code(connect_future);
296 cass_future_free(connect_future);
310 "!cass_schema_meta_keyspace_by_name()");
316 CassError rc = CASS_OK;
321 "CqlConnection::prepareStatements(): " 322 "duplicate statement with name " 323 << tagged_statement.
name_);
328 cass_future_wait(future);
329 const std::string error =
331 " cass_session_prepare() != CASS_OK",
332 future, tagged_statement.
name_);
333 rc = cass_future_error_code(future);
335 cass_future_free(future);
341 cass_future_free(future);
370 CassError cass_error = cass_future_error_code(future);
371 const char* error_message;
372 size_t error_message_size;
373 cass_future_error_message(future, &error_message, &error_message_size);
375 std::stringstream stream;
376 if (statement_tag && std::strlen(statement_tag) > 0) {
378 stream <<
"Statement ";
379 stream << statement_tag;
382 stream <<
"Session action ";
384 if (cass_error == CASS_OK) {
385 stream <<
" executed succesfully.";
387 stream <<
" failed, Kea error: " << what
388 <<
", Cassandra error code: " << cass_error_desc(cass_error)
389 <<
", Cassandra future error: " << error_message;
We want to reuse the database backend connection and exchange code for other uses,...
StatementMap statements_
Pointer to external array of tagged statements containing statement name, array of names of bind para...
void setConsistency(bool force, CassConsistency consistency)
Set consistency.
StatementTag name_
Short description of the query.
virtual void commit()
Commit Transactions.
const CassPrepared * prepared_statement_
Internal Cassandra object representing the prepared statement.
#define DB_LOG_DEBUG(LEVEL, MESSAGE)
Macros.
bool force_consistency_
CQL consistency enabled.
void startTransaction()
Start transaction.
void prepareStatements(StatementMap &statements)
Prepare statements.
void openDatabase()
Open database.
virtual void rollback()
Rollback Transactions.
Common database connection class.
virtual ~CqlConnection()
Destructor.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Exception thrown on failure to open database.
char const *const StatementTag
Statement index representing the statement name.
const CassKeyspaceMeta * keyspace_meta_
Keyspace meta information, used for UDTs.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines a single statement or query.
CassConsistency consistency_
CQL consistency.
CassSession * session_
CQL session handle.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
CassCluster * cluster_
CQL connection handle.
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::unordered_map< StatementTag, CqlTaggedStatement, StatementTagHash, StatementTagEqual > StatementMap
A container for all statements.
const CassSchemaMeta * schema_meta_
#define DB_LOG_ERROR(MESSAGE)
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static const std::string checkFutureError(const std::string &what, CassFuture *future, StatementTag statement_tag=NULL)
Check for errors.
std::pair< StatementTag, CqlTaggedStatement > StatementMapEntry
A type for a single entry on the statements map.
Exception thrown on failure to execute a database function.
CqlConnection(const ParameterMap ¶meters)
Constructor.
char const *const text_
Text representation of the actual query.