• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "query_expression.h"
16 #include "log_print.h"
17 #include "schema_utils.h"
18 #include "db_errno.h"
19 
20 namespace DistributedDB {
21 namespace {
22     const int MAX_OPR_TIMES = 256;
23 } // namespace
24 
AssemblyQueryInfo(const QueryObjType queryOperType,const std::string & field,const QueryValueType type,const std::vector<FieldValue> & values,bool isNeedFieldPath=true)25 void QueryExpression::AssemblyQueryInfo(const QueryObjType queryOperType, const std::string& field,
26     const QueryValueType type, const std::vector<FieldValue> &values, bool isNeedFieldPath = true)
27 {
28     if (queryInfo_.size() > MAX_OPR_TIMES) {
29         SetErrFlag(false);
30         LOGE("Operate too much times!");
31         return;
32     }
33 
34     if (!GetErrFlag()) {
35         LOGE("Illegal data node!");
36         return;
37     }
38 
39     FieldPath outPath;
40     if (isNeedFieldPath) {
41         if (SchemaUtils::ParseAndCheckFieldPath(field, outPath) != E_OK) {
42             SetErrFlag(false);
43             LOGE("Field path illegal!");
44             return;
45         }
46     }
47     std::string formatedField;
48     if (isTableNameSpecified_) { // remove '$.' prefix in relational query
49         for (auto it = outPath.begin(); it < outPath.end(); ++it) {
50             if (it != outPath.begin()) {
51                 formatedField += ".";
52             }
53             formatedField += *it;
54         }
55     } else {
56         formatedField = field;
57     }
58     queryInfo_.emplace_back(QueryObjNode{queryOperType, formatedField, type, values});
59 }
60 
QueryExpression()61 QueryExpression::QueryExpression()
62     : errFlag_(true),
63       tableName_("sync_data"), // default kv type store table name
64       isTableNameSpecified_(false) // default no specify for kv type store table name
65 {}
66 
EqualTo(const std::string & field,const QueryValueType type,const FieldValue & value)67 void QueryExpression::EqualTo(const std::string& field, const QueryValueType type, const FieldValue &value)
68 {
69     std::vector<FieldValue> fieldValues{value};
70     AssemblyQueryInfo(QueryObjType::EQUALTO, field, type, fieldValues);
71 }
72 
NotEqualTo(const std::string & field,const QueryValueType type,const FieldValue & value)73 void QueryExpression::NotEqualTo(const std::string& field, const QueryValueType type, const FieldValue &value)
74 {
75     std::vector<FieldValue> fieldValues{value};
76     AssemblyQueryInfo(QueryObjType::NOT_EQUALTO, field, type, fieldValues);
77 }
78 
GreaterThan(const std::string & field,const QueryValueType type,const FieldValue & value)79 void QueryExpression::GreaterThan(const std::string& field, const QueryValueType type, const FieldValue &value)
80 {
81     if (type == QueryValueType::VALUE_TYPE_BOOL) {
82         LOGD("Prohibit the use of bool for comparison!");
83         SetErrFlag(false);
84     }
85     std::vector<FieldValue> fieldValues{value};
86     AssemblyQueryInfo(QueryObjType::GREATER_THAN, field, type, fieldValues);
87 }
88 
LessThan(const std::string & field,const QueryValueType type,const FieldValue & value)89 void QueryExpression::LessThan(const std::string& field, const QueryValueType type, const FieldValue &value)
90 {
91     if (type == QueryValueType::VALUE_TYPE_BOOL) {
92         LOGD("Prohibit the use of bool for comparison!");
93         SetErrFlag(false);
94     }
95     std::vector<FieldValue> fieldValues{value};
96     AssemblyQueryInfo(QueryObjType::LESS_THAN, field, type, fieldValues);
97 }
98 
GreaterThanOrEqualTo(const std::string & field,const QueryValueType type,const FieldValue & value)99 void QueryExpression::GreaterThanOrEqualTo(const std::string& field, const QueryValueType type, const FieldValue &value)
100 {
101     if (type == QueryValueType::VALUE_TYPE_BOOL) {
102         LOGD("Prohibit the use of bool for comparison!");
103         SetErrFlag(false);
104     }
105     std::vector<FieldValue> fieldValues{value};
106     AssemblyQueryInfo(QueryObjType::GREATER_THAN_OR_EQUALTO, field, type, fieldValues);
107 }
108 
LessThanOrEqualTo(const std::string & field,const QueryValueType type,const FieldValue & value)109 void QueryExpression::LessThanOrEqualTo(const std::string& field, const QueryValueType type, const FieldValue &value)
110 {
111     if (type == QueryValueType::VALUE_TYPE_BOOL) {
112         LOGD("Prohibit the use of bool for comparison!");
113         SetErrFlag(false);
114     }
115     std::vector<FieldValue> fieldValues{value};
116     AssemblyQueryInfo(QueryObjType::LESS_THAN_OR_EQUALTO, field, type, fieldValues);
117 }
118 
OrderBy(const std::string & field,bool isAsc)119 void QueryExpression::OrderBy(const std::string& field, bool isAsc)
120 {
121     FieldValue fieldValue;
122     fieldValue.boolValue = isAsc;
123     std::vector<FieldValue> fieldValues{fieldValue};
124     AssemblyQueryInfo(QueryObjType::ORDERBY, field, QueryValueType::VALUE_TYPE_BOOL, fieldValues);
125 }
126 
Like(const std::string & field,const std::string & value)127 void QueryExpression::Like(const std::string& field, const std::string &value)
128 {
129     FieldValue fieldValue;
130     fieldValue.stringValue = value;
131     std::vector<FieldValue> fieldValues{fieldValue};
132     AssemblyQueryInfo(QueryObjType::LIKE, field, QueryValueType::VALUE_TYPE_STRING, fieldValues);
133 }
134 
NotLike(const std::string & field,const std::string & value)135 void QueryExpression::NotLike(const std::string& field, const std::string &value)
136 {
137     FieldValue fieldValue;
138     fieldValue.stringValue = value;
139     std::vector<FieldValue> fieldValues{fieldValue};
140     AssemblyQueryInfo(QueryObjType::NOT_LIKE, field, QueryValueType::VALUE_TYPE_STRING, fieldValues);
141 }
142 
Limit(int number,int offset)143 void QueryExpression::Limit(int number, int offset)
144 {
145     FieldValue fieldNumber;
146     fieldNumber.integerValue = number;
147     FieldValue fieldOffset;
148     fieldOffset.integerValue = offset;
149     std::vector<FieldValue> fieldValues{fieldNumber, fieldOffset};
150     AssemblyQueryInfo(QueryObjType::LIMIT, std::string(), QueryValueType::VALUE_TYPE_INTEGER, fieldValues, false);
151 }
152 
IsNull(const std::string & field)153 void QueryExpression::IsNull(const std::string& field)
154 {
155     AssemblyQueryInfo(QueryObjType::IS_NULL, field, QueryValueType::VALUE_TYPE_NULL, std::vector<FieldValue>());
156 }
157 
IsNotNull(const std::string & field)158 void QueryExpression::IsNotNull(const std::string& field)
159 {
160     AssemblyQueryInfo(QueryObjType::IS_NOT_NULL, field, QueryValueType::VALUE_TYPE_NULL, std::vector<FieldValue>());
161 }
162 
In(const std::string & field,const QueryValueType type,const std::vector<FieldValue> & values)163 void QueryExpression::In(const std::string& field, const QueryValueType type, const std::vector<FieldValue> &values)
164 {
165     AssemblyQueryInfo(QueryObjType::IN, field, type, values);
166 }
167 
NotIn(const std::string & field,const QueryValueType type,const std::vector<FieldValue> & values)168 void QueryExpression::NotIn(const std::string& field, const QueryValueType type, const std::vector<FieldValue> &values)
169 {
170     AssemblyQueryInfo(QueryObjType::NOT_IN, field, type, values);
171 }
172 
And()173 void QueryExpression::And()
174 {
175     AssemblyQueryInfo(QueryObjType::AND, std::string(), QueryValueType::VALUE_TYPE_NULL,
176         std::vector<FieldValue>(), false);
177 }
178 
Or()179 void QueryExpression::Or()
180 {
181     AssemblyQueryInfo(QueryObjType::OR, std::string(), QueryValueType::VALUE_TYPE_NULL,
182         std::vector<FieldValue>(), false);
183 }
184 
QueryByPrefixKey(const std::vector<uint8_t> & key)185 void QueryExpression::QueryByPrefixKey(const std::vector<uint8_t> &key)
186 {
187     queryInfo_.emplace_front(QueryObjNode{QueryObjType::QUERY_BY_KEY_PREFIX, std::string(),
188         QueryValueType::VALUE_TYPE_NULL, std::vector<FieldValue>()});
189     prefixKey_ = key;
190 }
191 
QueryBySuggestIndex(const std::string & indexName)192 void QueryExpression::QueryBySuggestIndex(const std::string &indexName)
193 {
194     queryInfo_.emplace_back(QueryObjNode{QueryObjType::SUGGEST_INDEX, indexName,
195         QueryValueType::VALUE_TYPE_STRING, std::vector<FieldValue>()});
196     suggestIndex_ = indexName;
197 }
198 
InKeys(const std::set<Key> & keys)199 void QueryExpression::InKeys(const std::set<Key> &keys)
200 {
201     queryInfo_.emplace_front(QueryObjNode{QueryObjType::IN_KEYS, std::string(), QueryValueType::VALUE_TYPE_NULL,
202         std::vector<FieldValue>()});
203     keys_ = keys;
204 }
205 
GetQueryExpression()206 const std::list<QueryObjNode> &QueryExpression::GetQueryExpression()
207 {
208     if (!GetErrFlag()) {
209         queryInfo_.clear();
210         queryInfo_.emplace_back(QueryObjNode{QueryObjType::OPER_ILLEGAL});
211         LOGE("Query operate illegal!");
212     }
213     return queryInfo_;
214 }
215 
GetPreFixKey() const216 std::vector<uint8_t> QueryExpression::GetPreFixKey() const
217 {
218     return prefixKey_;
219 }
220 
SetTableName(const std::string & tableName)221 void QueryExpression::SetTableName(const std::string &tableName)
222 {
223     tableName_ = tableName;
224     isTableNameSpecified_ = true;
225 }
226 
GetTableName()227 const std::string &QueryExpression::GetTableName()
228 {
229     return tableName_;
230 }
231 
IsTableNameSpecified() const232 bool QueryExpression::IsTableNameSpecified() const
233 {
234     return isTableNameSpecified_;
235 }
236 
GetSuggestIndex() const237 std::string QueryExpression::GetSuggestIndex() const
238 {
239     return suggestIndex_;
240 }
241 
GetKeys() const242 const std::set<Key> &QueryExpression::GetKeys() const
243 {
244     return keys_;
245 }
246 
BeginGroup()247 void QueryExpression::BeginGroup()
248 {
249     queryInfo_.emplace_back(QueryObjNode{QueryObjType::BEGIN_GROUP, std::string(),
250         QueryValueType::VALUE_TYPE_NULL, std::vector<FieldValue>()});
251 }
252 
EndGroup()253 void QueryExpression::EndGroup()
254 {
255     queryInfo_.emplace_back(QueryObjNode{QueryObjType::END_GROUP, std::string(),
256         QueryValueType::VALUE_TYPE_NULL, std::vector<FieldValue>()});
257 }
258 
Reset()259 void QueryExpression::Reset()
260 {
261     errFlag_ = true;
262     queryInfo_.clear();
263     prefixKey_.clear();
264     prefixKey_.shrink_to_fit();
265     suggestIndex_.clear();
266     keys_.clear();
267 }
268 
SetErrFlag(bool flag)269 void QueryExpression::SetErrFlag(bool flag)
270 {
271     errFlag_ = flag;
272 }
273 
GetErrFlag()274 bool QueryExpression::GetErrFlag()
275 {
276     return errFlag_;
277 }
278 
GetSortType() const279 int QueryExpression::GetSortType() const
280 {
281     return sortType_;
282 }
283 
SetSortType(bool isAsc)284 void QueryExpression::SetSortType(bool isAsc)
285 {
286     WriteTimeSort sortType = isAsc ? WriteTimeSort::TIMESTAMP_ASC : WriteTimeSort::TIMESTAMP_DESC;
287     sortType_ = static_cast<int>(sortType);
288 }
289 } // namespace DistributedDB
290