Kea 2.0.1
cql_exchange.h
Go to the documentation of this file.
1// Copyright (C) 2018,2021 Internet Systems Consortium, Inc. ("ISC")
2// Copyright (C) 2016-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_EXCHANGE_H
20#define CQL_EXCHANGE_H
21
22#include <cql/cql_connection.h>
23#include <cql/sql_common.h>
24
25#include <boost/any.hpp> // for boost::any
26
27#include <string>
28#include <typeinfo> // for std::type_info
29#include <unordered_map> // for std::unordered_map
30#include <utility>
31#include <vector>
32
33namespace isc {
34namespace db {
35
37typedef std::vector<cass_byte_t> CassBlob;
38
40class CqlExchange;
41
50class AnyArray : public std::vector<boost::any> {
51public:
53 void add(const boost::any& value);
54
57 void remove(const size_t& index);
58};
59
60// @brief Representation of a Cassandra User Defined Type
61class Udt : public AnyArray {
62public:
64 Udt(const CqlConnection& connection, const std::string& name);
65
67 ~Udt();
68
71
74
77
79 const std::string name_;
80
83 const CassDataType* cass_data_type_;
84
86 CassUserType* cass_user_type_;
87};
88
91
97typedef CassError (*CqlBindFunction)(const boost::any& value,
98 const size_t& index,
99 CassStatement* statement);
100
105typedef CassError (*CqlUdtSetFunction)(const boost::any& value,
106 const size_t& index,
107 CassUserType* cass_user_type);
108
112typedef CassError (*CqlCollectionAppendFunction)(const boost::any& value,
113 CassCollection* collection);
114
120typedef CassError (*CqlGetFunction)(const boost::any& data,
121 const CassValue* value);
122
135};
136
142class CqlExchange : public virtual SqlExchange {
143public:
147 CqlExchange();
148
150 virtual ~CqlExchange();
151
159 static void convertToDatabaseTime(const time_t& cltt,
160 const uint32_t& valid_lifetime,
161 cass_int64_t& expire);
162
167 static void convertFromDatabaseTime(const cass_int64_t& expire,
168 const cass_int64_t& valid_lifetime,
169 time_t& cltt);
171
179 virtual void createBindForSelect(AnyArray& data,
180 StatementTag statement_tag = NULL) = 0;
181
195 AnyArray executeSelect(const CqlConnection& connection,
196 const AnyArray& where_values,
197 StatementTag statement_tag,
198 const bool& single = false);
199
208 void executeMutation(const CqlConnection& connection,
209 const AnyArray& assigned_values,
210 StatementTag statement_tag);
211
222 bool statementApplied(CassFuture* future, size_t* row_count = NULL,
223 size_t* column_count = NULL);
224
232 virtual boost::any retrieve() = 0;
233};
234
236class CqlVersionExchange : public virtual CqlExchange {
237public:
242
244 virtual ~CqlVersionExchange();
245
253 virtual void
255 StatementTag statement_tag = NULL) override;
256
264 virtual VersionPair retrieveVersion(const CqlConnection& connection);
265
272 virtual boost::any retrieve() override;
273
276 static constexpr StatementTag GET_VERSION = "GET_VERSION";
278
281
282private:
284 cass_int32_t version_;
286 cass_int32_t minor_;
288 VersionPair pair_;
289};
290
293public:
302 static void bindData(const AnyArray& data, CassStatement* statement);
303
311 static void getData(const CassRow* row, AnyArray& data);
312};
313
316exchangeType(const boost::any& object);
317
320exchangeType(const CassValueType& type);
321
322} // namespace db
323} // namespace isc
324
325#endif // CQL_EXCHANGE_H
Structure used to bind C++ input values to dynamic CQL parameters.
Definition: cql_exchange.h:50
void add(const boost::any &value)
Add a value at the end of the vector.
void remove(const size_t &index)
Remove the void pointer to the data value from a specified position inside the vector.
Common operations in Cassandra exchanges.
Definition: cql_exchange.h:292
static void bindData(const AnyArray &data, CassStatement *statement)
Assigns values to every column of an INSERT or an UPDATE statement.
static void getData(const CassRow *row, AnyArray &data)
Retrieves data returned by Cassandra.
Common CQL connector pool.
Cassandra Exchange.
Definition: cql_exchange.h:142
CqlExchange()
Constructor.
AnyArray executeSelect(const CqlConnection &connection, const AnyArray &where_values, StatementTag statement_tag, const bool &single=false)
Executes SELECT statements.
void executeMutation(const CqlConnection &connection, const AnyArray &assigned_values, StatementTag statement_tag)
Executes INSERT, UPDATE or DELETE statements.
virtual boost::any retrieve()=0
Copy received data into the derived class' object.
virtual ~CqlExchange()
Destructor.
virtual void createBindForSelect(AnyArray &data, StatementTag statement_tag=NULL)=0
Create BIND array to receive C++ data.
bool statementApplied(CassFuture *future, size_t *row_count=NULL, size_t *column_count=NULL)
Check if CQL statement has been applied.
static void convertFromDatabaseTime(const cass_int64_t &expire, const cass_int64_t &valid_lifetime, time_t &cltt)
Converts time from Cassandra format.
static void convertToDatabaseTime(const time_t &cltt, const uint32_t &valid_lifetime, cass_int64_t &expire)
Exchange used to retrieve schema version from the keyspace.
Definition: cql_exchange.h:236
virtual VersionPair retrieveVersion(const CqlConnection &connection)
Standalone method used to retrieve schema version.
static StatementMap tagged_statements_
Cassandra statements.
Definition: cql_exchange.h:280
virtual ~CqlVersionExchange()
Destructor.
static constexpr StatementTag GET_VERSION
Statement tags definitions.
Definition: cql_exchange.h:276
CqlVersionExchange()
Constructor.
virtual boost::any retrieve() override
Copy received data into the <version,minor> pair.
virtual void createBindForSelect(AnyArray &data, StatementTag statement_tag=NULL) override
Create BIND array to receive C++ data.
Base class for backend exchanges.
Definition: sql_common.h:42
Udt(const CqlConnection &connection, const std::string &name)
Parameterized constructor.
const CassDataType * cass_data_type_
Internal Cassandra driver object representing a Cassandra data type.
Definition: cql_exchange.h:83
void newUserType()
Creates the underlying container.
const CqlConnection & connection_
Connection to the Cassandra database.
Definition: cql_exchange.h:76
CassUserType * cass_user_type_
Internal Cassandra driver object representing a user defined type.
Definition: cql_exchange.h:86
void freeUserType()
Frees the underlying container.
~Udt()
Destructor.
const std::string name_
Name of the UDT in the schema: CREATE TYPE ___ { ... }.
Definition: cql_exchange.h:79
AnyArray AnyCollection
Defines an array of arbitrary objects (used by Cassandra backend)
Definition: cql_exchange.h:90
char const *const StatementTag
Statement index representing the statement name.
CassError(* CqlGetFunction)(const boost::any &data, const CassValue *value)
Converts a single Cassandra column value to a C++ object.
Definition: cql_exchange.h:120
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
CassError(* CqlBindFunction)(const boost::any &value, const size_t &index, CassStatement *statement)
Binds a C++ object to a Cassandra statement's parameter.
Definition: cql_exchange.h:97
ExchangeDataType
Used to map server data types with internal backend storage data types.
Definition: sql_common.h:26
std::vector< cass_byte_t > CassBlob
Host identifier converted to Cassandra data type.
Definition: cql_exchange.h:37
CassError(* CqlCollectionAppendFunction)(const boost::any &value, CassCollection *collection)
Sets an item in a collection.
Definition: cql_exchange.h:112
std::unordered_map< StatementTag, CqlTaggedStatement, StatementTagHash, StatementTagEqual > StatementMap
A container for all statements.
ExchangeDataType exchangeType(const boost::any &object)
Determine exchange type based on boost::any type.
CassError(* CqlUdtSetFunction)(const boost::any &value, const size_t &index, CassUserType *cass_user_type)
Sets a member in a UDT.
Definition: cql_exchange.h:105
Defines the logger used by the top-level component of kea-lfc.
Wrapper over the bind and get functions that interface with Cassandra.
Definition: cql_exchange.h:124
CqlUdtSetFunction cqlUdtSetFunction_
Sets a member in a UDT. Used in INSERT & UPDATE statements.
Definition: cql_exchange.h:129
CqlGetFunction cqlGetFunction_
Converts a single Cassandra column value to a C++ object.
Definition: cql_exchange.h:134
CqlCollectionAppendFunction cqlCollectionAppendFunction_
Sets an item in a collection. Used in INSERT & UPDATE statements.
Definition: cql_exchange.h:131
CqlBindFunction cqlBindFunction_
Binds a C++ object to a Cassandra statement's parameter.
Definition: cql_exchange.h:127