Line data Source code
1 : /*
2 : * Text:
3 : * libsnapwebsites/src/libdbproxy/predicate.cpp
4 : *
5 : * Description:
6 : * Handling of CQL query string manipulation.
7 : *
8 : * Documentation:
9 : * See the corresponding .cpp file.
10 : *
11 : * License:
12 : * Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
13 : *
14 : * https://snapwebsites.org/
15 : * contact@m2osw.com
16 : *
17 : * Permission is hereby granted, free of charge, to any person obtaining a
18 : * copy of this software and associated documentation files (the
19 : * "Software"), to deal in the Software without restriction, including
20 : * without limitation the rights to use, copy, modify, merge, publish,
21 : * distribute, sublicense, and/or sell copies of the Software, and to
22 : * permit persons to whom the Software is furnished to do so, subject to
23 : * the following conditions:
24 : *
25 : * The above copyright notice and this permission notice shall be included
26 : * in all copies or substantial portions of the Software.
27 : *
28 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
33 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
34 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 : */
36 :
37 : #include "libdbproxy/predicate.h"
38 :
39 : #include <iostream>
40 :
41 : namespace libdbproxy
42 : {
43 :
44 : /** \brief Define the first possible character in a column key.
45 : *
46 : * This character can be used to define the very first character
47 : * in a column key. Note though that it is rarely used because
48 : * the empty string serves the purpose and is more likely what
49 : * you want.
50 : *
51 : * The first character is '\0'.
52 : */
53 : const QChar cell_predicate::first_char = QChar('\0');
54 :
55 :
56 : /** \brief Define the last possible character in a column key.
57 : *
58 : * This character can be used to define the very last character
59 : * in a column key.
60 : *
61 : * The last character is '\\uFFFD'.
62 : *
63 : * \note
64 : * This character can also be used in row predicates.
65 : */
66 : const QChar cell_predicate::last_char = QChar(L'\uFFFD');
67 :
68 :
69 : /// \brief Cell predicate query handlers
70 0 : void cell_key_predicate::appendQuery( QString& query, int& bind_count )
71 : {
72 0 : query += " AND column1 == ?";
73 0 : bind_count++;
74 0 : }
75 :
76 :
77 0 : void cell_key_predicate::bindOrder( order& order )
78 : {
79 0 : order.addParameter( f_cellKey );
80 0 : }
81 :
82 :
83 : /// \brief Cell range predicate query handlers
84 0 : void cell_range_predicate::appendQuery( QString& query, int& bind_count )
85 : {
86 0 : if(!f_startCellKey.isEmpty())
87 : {
88 0 : query += " AND column1>=?";
89 0 : bind_count += 1;
90 : }
91 :
92 0 : if(!f_endCellKey.isEmpty())
93 : {
94 : // The end boundary is NEVER included in the results
95 0 : query += " AND column1<?";
96 0 : bind_count += 1;
97 : }
98 :
99 0 : if(f_reversed)
100 : {
101 0 : query += " ORDER BY column1 DESC";
102 : }
103 0 : }
104 :
105 :
106 0 : void cell_range_predicate::bindOrder( order& order )
107 : {
108 0 : if(!f_startCellKey.isEmpty())
109 : {
110 0 : order.addParameter( f_startCellKey );
111 : }
112 :
113 0 : if(!f_endCellKey.isEmpty())
114 : {
115 0 : order.addParameter( f_endCellKey );
116 : }
117 0 : }
118 :
119 :
120 : /// \brief Row key predicate query handlers
121 0 : void row_key_predicate::appendQuery( QString& query, int& bind_count )
122 : {
123 0 : query += " WHERE key=?";
124 0 : ++bind_count;
125 0 : f_cell_pred->appendQuery( query, bind_count );
126 0 : }
127 :
128 :
129 0 : void row_key_predicate::bindOrder( order& order)
130 : {
131 0 : order.addParameter( f_rowKey );
132 0 : f_cell_pred->bindOrder( order );
133 0 : }
134 :
135 :
136 : /// \brief Row range predicate query handlers
137 0 : void row_range_predicate::appendQuery( QString& query, int& bind_count )
138 : {
139 0 : query += " WHERE token(key) >= token(?) AND token(key) <= token(?)";
140 0 : bind_count += 2;
141 0 : f_cell_pred->appendQuery( query, bind_count );
142 0 : }
143 :
144 :
145 0 : void row_range_predicate::bindOrder( order& order )
146 : {
147 0 : order.addParameter( f_startRowKey );
148 0 : order.addParameter( f_endRowKey );
149 0 : f_cell_pred->bindOrder( order );
150 0 : }
151 :
152 :
153 6 : } // namespace libdbproxy
154 :
155 : // vim: ts=4 sw=4 et
|