Kea 2.0.1
cql_connection.h
Go to the documentation of this file.
1// Copyright (C) 2018-2021 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
34namespace isc {
35namespace db {
36
40typedef std::pair<uint32_t, uint32_t> VersionPair;
41
43typedef char const* const StatementTag;
44
48constexpr uint32_t CQL_DRIVER_VERSION_MAJOR = CASS_VERSION_MAJOR;
49constexpr uint32_t CQL_DRIVER_VERSION_MINOR = CASS_VERSION_MINOR;
51
54constexpr uint32_t CQL_SCHEMA_VERSION_MAJOR = 5u;
55constexpr 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
106typedef std::unordered_map<StatementTag, CqlTaggedStatement,
108
110typedef std::pair<StatementTag, CqlTaggedStatement> StatementMapEntry;
111
120public:
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
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
235typedef std::shared_ptr<CqlConnection> CqlConnectionPtr;
236
237} // namespace db
238} // namespace isc
239
240#endif // CQL_CONNECTION_H
Common CQL connector pool.
virtual ~CqlConnection()
Destructor.
StatementMap statements_
Pointer to external array of tagged statements containing statement name, array of names of bind para...
CqlConnection(const ParameterMap &parameters)
Constructor.
CassConsistency serial_consistency_
CQL serial consistency.
virtual void commit()
Commit Transactions.
void startTransaction()
Start transaction.
virtual void rollback()
Rollback Transactions.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap &parameters)
Get the schema version.
CassSession * session_
CQL session handle.
bool force_consistency_
CQL consistency enabled.
const CassKeyspaceMeta * keyspace_meta_
Keyspace meta information, used for UDTs.
void setConsistency(bool force, CassConsistency consistency, CassConsistency serial_consistency)
Set consistency.
CassCluster * cluster_
CQL connection handle.
void openDatabase()
Open database.
CassConsistency consistency_
CQL consistency.
const CassSchemaMeta * schema_meta_
static const std::string checkFutureError(const std::string &what, CassFuture *future, StatementTag statement_tag=NULL)
Check for errors.
static CassConsistency parseConsistency(std::string value)
parse Consistency value
void prepareStatements(StatementMap &statements)
Prepare statements.
Common database connection class.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
char const *const StatementTag
Statement index representing the statement name.
std::pair< StatementTag, CqlTaggedStatement > StatementMapEntry
A type for a single entry on the statements map.
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
std::shared_ptr< CqlConnection > CqlConnectionPtr
constexpr uint32_t CQL_SCHEMA_VERSION_MINOR
constexpr uint32_t CQL_SCHEMA_VERSION_MAJOR
Define CQL schema version: 5.0.
constexpr uint32_t CQL_DRIVER_VERSION_MAJOR
Define CQL backend version.
std::unordered_map< StatementTag, CqlTaggedStatement, StatementTagHash, StatementTagEqual > StatementMap
A container for all statements.
constexpr uint32_t CQL_DRIVER_VERSION_MINOR
Defines the logger used by the top-level component of kea-lfc.
Defines a single statement or query.
StatementTag name_
Short description of the query.
CqlTaggedStatement(StatementTag name, char const *const text)
Constructor.
char const *const text_
Text representation of the actual query.
CqlTaggedStatement(StatementTag name, char const *const text, bool const &is_raw)
Constructor.
bool is_raw_
Should the statement be executed raw or with binds?
const CassPrepared * prepared_statement_
Internal Cassandra object representing the prepared statement.
Equality function for StatementMap keys.
bool operator()(StatementTag const &lhs, StatementTag const &rhs) const
Hash function for StatementMap keys.
size_t operator()(StatementTag const &key) const