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
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 = 3u;
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 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
212typedef std::shared_ptr<CqlConnection> CqlConnectionPtr;
213
214} // namespace db
215} // namespace isc
216
217#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...
virtual void commit()
Commit Transactions.
void startTransaction()
Start transaction.
virtual void rollback()
Rollback Transactions.
CassSession * session_
CQL session handle.
bool force_consistency_
CQL consistency enabled.
const CassKeyspaceMeta * keyspace_meta_
Keyspace meta information, used for UDTs.
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.
void prepareStatements(StatementMap &statements)
Prepare statements.
void setConsistency(bool force, CassConsistency consistency)
Set consistency.
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: 3.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-dhcp-ddns.
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