Kea  1.9.9-git
cql_connection.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 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 = 5u;
55 constexpr uint32_t CQL_SCHEMA_VERSION_MINOR = 0u;
57 
60 
62  StatementTag name_;
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  static std::pair<uint32_t, uint32_t>
141  getVersion(const ParameterMap& parameters);
142 
153  void prepareStatements(StatementMap& statements);
154 
175  void openDatabase();
176 
178  void setConsistency(bool force,
179  CassConsistency consistency,
180  CassConsistency serial_consistency);
181 
183  void startTransaction();
184 
186  virtual void commit();
187 
189  virtual void rollback();
190 
192  static CassConsistency parseConsistency(std::string value);
193 
204  static const std::string
205  checkFutureError(const std::string& what,
206  CassFuture* future,
207  StatementTag statement_tag = NULL);
208 
211  StatementMap statements_;
212 
214  CassCluster* cluster_;
215 
217  CassSession* session_;
218 
220  CassConsistency consistency_;
221 
223  CassConsistency serial_consistency_;
224 
225  // @brief Schema meta information, used for UDTs
226  const CassSchemaMeta* schema_meta_;
227 
229  const CassKeyspaceMeta* keyspace_meta_;
230 
233 };
234 
235 typedef std::shared_ptr<CqlConnection> CqlConnectionPtr;
236 
237 } // namespace db
238 } // namespace isc
239 
240 #endif // CQL_CONNECTION_H
bool operator()(StatementTag const &lhs, StatementTag const &rhs) const
StatementMap statements_
Pointer to external array of tagged statements containing statement name, array of names of bind para...
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap &parameters)
Get the schema version.
constexpr uint32_t CQL_DRIVER_VERSION_MAJOR
Define CQL backend version.
StatementTag name_
Short description of the query.
virtual void commit()
Commit Transactions.
const CassPrepared * prepared_statement_
Internal Cassandra object representing the prepared statement.
bool force_consistency_
CQL consistency enabled.
void startTransaction()
Start transaction.
void prepareStatements(StatementMap &statements)
Prepare statements.
void openDatabase()
Open database.
constexpr uint32_t CQL_DRIVER_VERSION_MINOR
virtual void rollback()
Rollback Transactions.
Common database connection class.
virtual ~CqlConnection()
Destructor.
char const *const StatementTag
Statement index representing the statement name.
size_t operator()(StatementTag const &key) const
CqlTaggedStatement(StatementTag name, char const *const text, bool const &is_raw)
Constructor.
const CassKeyspaceMeta * keyspace_meta_
Keyspace meta information, used for UDTs.
void setConsistency(bool force, CassConsistency consistency, CassConsistency serial_consistency)
Set consistency.
Defines a single statement or query.
CassConsistency consistency_
CQL consistency.
CassSession * session_
CQL session handle.
static CassConsistency parseConsistency(std::string value)
parse Consistency value
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
CassCluster * cluster_
CQL connection handle.
constexpr uint32_t CQL_SCHEMA_VERSION_MAJOR
Define CQL schema version: 5.0.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Equality function for StatementMap keys.
CqlTaggedStatement(StatementTag name, char const *const text)
Constructor.
std::shared_ptr< CqlConnection > CqlConnectionPtr
Hash function for StatementMap keys.
std::unordered_map< StatementTag, CqlTaggedStatement, StatementTagHash, StatementTagEqual > StatementMap
A container for all statements.
CassConsistency serial_consistency_
CQL serial consistency.
constexpr uint32_t CQL_SCHEMA_VERSION_MINOR
const CassSchemaMeta * schema_meta_
Common CQL connector pool.
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.
bool is_raw_
Should the statement be executed raw or with binds?
CqlConnection(const ParameterMap &parameters)
Constructor.
char const *const text_
Text representation of the actual query.