Kea  1.5.0
cql_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
2 // Copyright (C) 2015-2017 Deutsche Telekom AG.
3 //
4 // Authors: Razvan Becheriu <razvan.becheriu@qualitance.com>
5 // Andrei Pavel <andrei.pavel@qualitance.com>
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 
19 #ifndef CQL_CONNECTION_H
20 #define CQL_CONNECTION_H
21 
23 
24 #include <cassandra.h>
25 
26 #include <cstring>
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <unordered_map>
31 #include <utility>
32 #include <vector>
33 
34 namespace isc {
35 namespace db {
36 
40 typedef std::pair<uint32_t, uint32_t> VersionPair;
41 
43 typedef char const* const StatementTag;
44 
48 constexpr uint32_t CQL_DRIVER_VERSION_MAJOR = CASS_VERSION_MAJOR;
49 constexpr uint32_t CQL_DRIVER_VERSION_MINOR = CASS_VERSION_MINOR;
51 
54 constexpr uint32_t CQL_SCHEMA_VERSION_MAJOR = 3u;
55 constexpr uint32_t CQL_SCHEMA_VERSION_MINOR = 0u;
57 
60 
63 
65  char const* const text_;
66 
68  const CassPrepared* prepared_statement_;
69 
71  bool is_raw_;
72 
76  CqlTaggedStatement(StatementTag name, char const* const text)
77  : name_(name), text_(text), prepared_statement_(NULL), is_raw_(false) {
78  }
79 
84  CqlTaggedStatement(StatementTag name, char const* const text, bool const& is_raw)
85  : name_(name), text_(text), prepared_statement_(NULL), is_raw_(is_raw) {
86  }
87 };
88 
93  size_t operator()(StatementTag const& key) const {
94  return std::hash<std::string>{}(std::string(key));
95  }
96 };
97 
100  bool operator()(StatementTag const& lhs, StatementTag const& rhs) const {
101  return std::strcmp(lhs, rhs) == 0;
102  }
103 };
104 
106 typedef std::unordered_map<StatementTag, CqlTaggedStatement,
108 
110 typedef std::pair<StatementTag, CqlTaggedStatement> StatementMapEntry;
111 
120 public:
125  explicit CqlConnection(const ParameterMap& parameters);
126 
128  virtual ~CqlConnection();
129 
140  void prepareStatements(StatementMap& statements);
141 
160  void openDatabase();
161 
163  void setConsistency(bool force, CassConsistency consistency);
164 
166  void startTransaction();
167 
169  virtual void commit();
170 
172  virtual void rollback();
173 
184  static const std::string
185  checkFutureError(const std::string& what,
186  CassFuture* future,
187  StatementTag statement_tag = NULL);
188 
192 
194  CassCluster* cluster_;
195 
197  CassSession* session_;
198 
200  CassConsistency consistency_;
201 
202  // @brief Schema meta information, used for UDTs
203  const CassSchemaMeta* schema_meta_;
204 
206  const CassKeyspaceMeta* keyspace_meta_;
207 
210 };
211 
212 typedef std::shared_ptr<CqlConnection> CqlConnectionPtr;
213 
214 } // namespace db
215 } // namespace isc
216 
217 #endif // CQL_CONNECTION_H
isc::db::CqlConnection::cluster_
CassCluster * cluster_
CQL connection handle.
Definition: cql_connection.h:194
isc::db::VersionPair
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
Definition: cql_connection.h:40
isc::db::StatementTagHash
Hash function for StatementMap keys.
Definition: cql_connection.h:92
isc::db::CqlTaggedStatement
Defines a single statement or query.
Definition: cql_connection.h:59
isc::db::CqlTaggedStatement::CqlTaggedStatement
CqlTaggedStatement(StatementTag name, char const *const text, bool const &is_raw)
Constructor.
Definition: cql_connection.h:84
isc::db::CqlConnection::schema_meta_
const CassSchemaMeta * schema_meta_
Definition: cql_connection.h:203
isc::db::StatementTagHash::operator()
size_t operator()(StatementTag const &key) const
Definition: cql_connection.h:93
isc::db::CqlConnection::openDatabase
void openDatabase()
Open database.
Definition: cql_connection.cc:78
isc::db::StatementTagEqual
Equality function for StatementMap keys.
Definition: cql_connection.h:99
isc::db::CqlConnection::force_consistency_
bool force_consistency_
CQL consistency enabled.
Definition: cql_connection.h:209
isc::db::CqlTaggedStatement::name_
StatementTag name_
Short description of the query.
Definition: cql_connection.h:62
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::db::CqlConnection::commit
virtual void commit()
Commit Transactions.
Definition: cql_connection.cc:357
isc::db::CqlConnection
Common CQL connector pool.
Definition: cql_connection.h:119
isc::db::CQL_DRIVER_VERSION_MAJOR
constexpr uint32_t CQL_DRIVER_VERSION_MAJOR
Define CQL backend version.
Definition: cql_connection.h:48
isc::db::StatementMapEntry
std::pair< StatementTag, CqlTaggedStatement > StatementMapEntry
A type for a single entry on the statements map.
Definition: cql_connection.h:110
isc::db::StatementTag
char const *const StatementTag
Statement index representing the statement name.
Definition: cql_connection.h:43
isc::db::CqlConnection::rollback
virtual void rollback()
Rollback Transactions.
Definition: cql_connection.cc:362
isc::db::CqlConnection::startTransaction
void startTransaction()
Start transaction.
Definition: cql_connection.cc:352
isc::db::CqlConnection::statements_
StatementMap statements_
Pointer to external array of tagged statements containing statement name, array of names of bind para...
Definition: cql_connection.h:191
isc::db::CqlConnection::prepareStatements
void prepareStatements(StatementMap &statements)
Prepare statements.
Definition: cql_connection.cc:315
isc::db::CqlConnection::setConsistency
void setConsistency(bool force, CassConsistency consistency)
Set consistency.
Definition: cql_connection.cc:346
isc::db::CqlTaggedStatement::prepared_statement_
const CassPrepared * prepared_statement_
Internal Cassandra object representing the prepared statement.
Definition: cql_connection.h:68
isc::db::CqlTaggedStatement::text_
char const *const text_
Text representation of the actual query.
Definition: cql_connection.h:65
isc::db::CQL_DRIVER_VERSION_MINOR
constexpr uint32_t CQL_DRIVER_VERSION_MINOR
Definition: cql_connection.h:49
database_connection.h
isc::db::CqlTaggedStatement::is_raw_
bool is_raw_
Should the statement be executed raw or with binds?
Definition: cql_connection.h:71
isc::db::CqlConnection::checkFutureError
static const std::string checkFutureError(const std::string &what, CassFuture *future, StatementTag statement_tag=NULL)
Check for errors.
Definition: cql_connection.cc:367
isc::db::StatementMap
std::unordered_map< StatementTag, CqlTaggedStatement, StatementTagHash, StatementTagEqual > StatementMap
A container for all statements.
Definition: cql_connection.h:107
isc::db::CqlConnection::session_
CassSession * session_
CQL session handle.
Definition: cql_connection.h:197
isc::db::StatementTagEqual::operator()
bool operator()(StatementTag const &lhs, StatementTag const &rhs) const
Definition: cql_connection.h:100
isc::db::CqlConnectionPtr
std::shared_ptr< CqlConnection > CqlConnectionPtr
Definition: cql_connection.h:212
isc::db::DatabaseConnection
Common database connection class.
Definition: database_connection.h:140
isc::db::CQL_SCHEMA_VERSION_MAJOR
constexpr uint32_t CQL_SCHEMA_VERSION_MAJOR
Define CQL schema version: 3.0.
Definition: cql_connection.h:54
isc::db::CqlTaggedStatement::CqlTaggedStatement
CqlTaggedStatement(StatementTag name, char const *const text)
Constructor.
Definition: cql_connection.h:76
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition: database_connection.h:152
isc::db::CQL_SCHEMA_VERSION_MINOR
constexpr uint32_t CQL_SCHEMA_VERSION_MINOR
Definition: cql_connection.h:55
isc::db::CqlConnection::consistency_
CassConsistency consistency_
CQL consistency.
Definition: cql_connection.h:200
isc::db::CqlConnection::CqlConnection
CqlConnection(const ParameterMap &parameters)
Constructor.
Definition: cql_connection.cc:31
isc::db::CqlConnection::keyspace_meta_
const CassKeyspaceMeta * keyspace_meta_
Keyspace meta information, used for UDTs.
Definition: cql_connection.h:206
isc::db::CqlConnection::~CqlConnection
virtual ~CqlConnection()
Destructor.
Definition: cql_connection.cc:37