Line data Source code
1 : /*
2 : * Header:
3 : * libsnapwebsites/src/libdbproxy/predicate.h
4 : *
5 : * Description:
6 : * Handling of Cassandra Predicates to retrieve a set of columns
7 : * all at once.
8 : *
9 : * Documentation:
10 : * See the corresponding .cpp file.
11 : *
12 : * License:
13 : * Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
14 : *
15 : * https://snapwebsites.org/
16 : * contact@m2osw.com
17 : *
18 : * Permission is hereby granted, free of charge, to any person obtaining a
19 : * copy of this software and associated documentation files (the
20 : * "Software"), to deal in the Software without restriction, including
21 : * without limitation the rights to use, copy, modify, merge, publish,
22 : * distribute, sublicense, and/or sell copies of the Software, and to
23 : * permit persons to whom the Software is furnished to do so, subject to
24 : * the following conditions:
25 : *
26 : * The above copyright notice and this permission notice shall be included
27 : * in all copies or substantial portions of the Software.
28 : *
29 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 : */
37 : #pragma once
38 :
39 : #include "libdbproxy/consistency_level.h"
40 : #include "libdbproxy/order.h"
41 :
42 : #include <QByteArray>
43 : #include <QRegExp>
44 : #include <QString>
45 :
46 : #include <memory>
47 :
48 : namespace libdbproxy
49 : {
50 :
51 : typedef int32_t cassandra_count_t; // usually defaults to 100
52 :
53 : class predicate
54 : {
55 : public:
56 : typedef std::shared_ptr<predicate> pointer_t;
57 :
58 0 : predicate() : f_count(100), f_consistency_level(CONSISTENCY_LEVEL_DEFAULT) {}
59 0 : virtual ~predicate() {}
60 :
61 0 : int32_t count() const { return f_count; }
62 0 : void setCount( const int32_t val = 100 ) { f_count = val; }
63 :
64 0 : bool allowFiltering() const { return f_allow_filtering; }
65 : void setAllowFiltering(bool allow_filtering) { f_allow_filtering = allow_filtering; }
66 :
67 0 : consistency_level_t consistencyLevel() const { return f_consistency_level; }
68 : void setConsistencyLevel( consistency_level_t level ) { f_consistency_level = level; }
69 :
70 : protected:
71 : virtual void appendQuery( QString& query, int& bind_count ) = 0;
72 : virtual void bindOrder( order& order ) = 0;
73 :
74 : cassandra_count_t f_count = 100;
75 : consistency_level_t f_consistency_level = CONSISTENCY_LEVEL_DEFAULT;
76 : bool f_allow_filtering = true; // this should probably be false by default, but at this point we do not have time to test which orders would need to set it to true...
77 : };
78 :
79 :
80 : class cell_predicate
81 : : public predicate
82 : {
83 : public:
84 : typedef std::shared_ptr<cell_predicate> pointer_t;
85 :
86 : // The name predicates can have any character from \0 to \uFFFD
87 : // (although in full Unicode, you may want to use \U10FFFD but at this
88 : // point I limit the code to \uFFFD because QChar uses a ushort)
89 : //
90 : // Note: Qt strings use UTF-16, but in a QChar, I'm not too sure we
91 : // can put a value more than 0xFFFF... so we'd need the last_char
92 : // to be a QString to support the max. character in Unicode!
93 : static const QChar first_char;
94 : static const QChar last_char;
95 :
96 0 : cell_predicate() {}
97 0 : virtual ~cell_predicate() {}
98 :
99 : protected:
100 : friend class row_predicate;
101 : friend class row_key_predicate;
102 : friend class row_range_predicate;
103 :
104 0 : virtual void appendQuery( QString& /*query*/, int& /*bind_count*/ ) {}
105 0 : virtual void bindOrder( order& /*order*/ ) {}
106 : };
107 :
108 :
109 0 : class cell_key_predicate
110 : : public cell_predicate
111 : {
112 : public:
113 : typedef std::shared_ptr<cell_key_predicate> pointer_t;
114 :
115 0 : cell_key_predicate() {}
116 :
117 : const QByteArray& cellKey() const { return f_cellKey; }
118 0 : void setCellKey(const QByteArray& cell_key) { f_cellKey = cell_key; }
119 :
120 : protected:
121 : virtual void appendQuery( QString& query, int& bind_count );
122 : virtual void bindOrder( order& order );
123 :
124 : QByteArray f_cellKey = QByteArray();
125 : };
126 :
127 :
128 0 : class cell_range_predicate
129 : : public cell_predicate
130 : {
131 : public:
132 : typedef std::shared_ptr<cell_range_predicate> pointer_t;
133 :
134 0 : cell_range_predicate() {}
135 :
136 : const QByteArray& startCellKey() const { return f_startCellKey; }
137 : void setStartCellKey(const char* cell_key) { setStartCellKey(QByteArray(cell_key,qstrlen(cell_key))); }
138 : void setStartCellKey(const QString& cell_key) { setStartCellKey(cell_key.toUtf8()); }
139 : void setStartCellKey(const QByteArray& cell_key) { f_startCellKey = cell_key; }
140 :
141 : const QByteArray& endCellKey() const { return f_endCellKey; }
142 : void setEndCellKey(const char* cell_key) { setEndCellKey(QByteArray(cell_key,qstrlen(cell_key))); }
143 : void setEndCellKey(const QString& cell_key) { setEndCellKey(cell_key.toUtf8()); }
144 : void setEndCellKey(const QByteArray& cell_key) { f_endCellKey = cell_key; }
145 :
146 : bool reversed() const { return f_reversed; }
147 : void setReversed( bool val = true ) { f_reversed = val; }
148 :
149 : bool index() const { return f_index; }
150 0 : void setIndex( bool val = true ) { f_index = val; }
151 :
152 : protected:
153 : virtual void appendQuery( QString& query, int& bind_count );
154 : virtual void bindOrder( order& order );
155 :
156 : QByteArray f_startCellKey = QByteArray();
157 : QByteArray f_endCellKey = QByteArray();
158 : bool f_reversed = false;
159 : bool f_index = false; // whether predicate is used as an index
160 : };
161 :
162 :
163 : class row_predicate
164 : : public predicate
165 : {
166 : public:
167 : typedef std::shared_ptr<row_predicate> pointer_t;
168 :
169 0 : row_predicate() : f_cell_pred( new cell_predicate ) {}
170 0 : virtual ~row_predicate() {}
171 :
172 0 : QRegExp rowNameMatch() const { return f_row_name_match; }
173 : void setRowNameMatch(QRegExp const& re) { f_row_name_match = re; }
174 :
175 0 : cell_predicate::pointer_t cellPredicate() const { return f_cell_pred; }
176 0 : void setCellPredicate( cell_predicate::pointer_t pred ) { f_cell_pred = pred; }
177 :
178 0 : virtual void appendQuery( QString& /*query*/, int& /*bind_count*/ ) {}
179 0 : virtual void bindOrder( order& /*order*/ ) {}
180 :
181 : protected:
182 : cell_predicate::pointer_t f_cell_pred = cell_predicate::pointer_t();
183 : QRegExp f_row_name_match = QRegExp();
184 : };
185 :
186 :
187 : class row_key_predicate
188 : : public row_predicate
189 : {
190 : public:
191 : typedef std::shared_ptr<row_key_predicate> pointer_t;
192 :
193 0 : row_key_predicate() {}
194 0 : virtual ~row_key_predicate() {}
195 :
196 : const QByteArray& rowKey() const { return f_rowKey; }
197 0 : void setRowKey(const QByteArray& row_key) { f_rowKey = row_key; }
198 :
199 : virtual void appendQuery( QString& query, int& bind_count );
200 : virtual void bindOrder( order& order );
201 :
202 : protected:
203 : QByteArray f_rowKey = QByteArray();
204 : };
205 :
206 :
207 0 : class row_range_predicate
208 : : public row_predicate
209 : {
210 : public:
211 : typedef std::shared_ptr<row_range_predicate> pointer_t;
212 :
213 : row_range_predicate() {}
214 :
215 : const QByteArray& startRowKey() const { return f_startRowKey; }
216 : void setStartRowKey(const QByteArray& row_key) { f_startRowKey = row_key; }
217 :
218 : const QByteArray& endRowKey() const { return f_endRowKey; }
219 : void setEndRowKey(const QByteArray& row_key) { f_endRowKey = row_key; }
220 :
221 : virtual void appendQuery( QString& query, int& bind_count );
222 : virtual void bindOrder( order& order );
223 :
224 : protected:
225 : QByteArray f_startRowKey = QByteArray();
226 : QByteArray f_endRowKey = QByteArray();
227 : };
228 :
229 :
230 : } // namespace libdbproxy
231 :
232 : // vim: ts=4 sw=4 et
|