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 
16 #ifndef DISTRIBUTEDDB_QUERY_EXPRESSION_H
17 #define DISTRIBUTEDDB_QUERY_EXPRESSION_H
18 
19 #include <list>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "types_export.h"
25 
26 namespace DistributedDB {
27 enum class QueryValueType: int32_t {
28     VALUE_TYPE_INVALID = -1,
29     VALUE_TYPE_NULL,
30     VALUE_TYPE_BOOL,
31     VALUE_TYPE_INTEGER,
32     VALUE_TYPE_LONG,
33     VALUE_TYPE_DOUBLE,
34     VALUE_TYPE_STRING,
35 };
36 
37 // value will const, it will influence query object id
38 // use high pos bit to distinguish operator type
39 enum class QueryObjType : uint32_t {
40     OPER_ILLEGAL = 0x0000,
41     EQUALTO = 0x0101,
42     NOT_EQUALTO,
43     GREATER_THAN,
44     LESS_THAN,
45     GREATER_THAN_OR_EQUALTO,
46     LESS_THAN_OR_EQUALTO,
47     LIKE = 0x0201,
48     NOT_LIKE,
49     IS_NULL,
50     IS_NOT_NULL,
51     IN = 0x0301,
52     NOT_IN,
53     QUERY_BY_KEY_PREFIX = 0x0401,
54     BEGIN_GROUP = 0x0501,
55     END_GROUP,
56     AND = 0x0601,
57     OR,
58     LIMIT = 0x0701,
59     ORDERBY,
60     SUGGEST_INDEX = 0x0801,
61     IN_KEYS = 0x0901,
62     KEY_RANGE = 0x1001,
63 };
64 
65 struct QueryObjNode {
66     QueryObjType operFlag = QueryObjType::OPER_ILLEGAL;
67     std::string fieldName {};
68     QueryValueType type = QueryValueType::VALUE_TYPE_INVALID;
69     std::vector<FieldValue> fieldValue = {};
IsValidQueryObjNode70     bool IsValid()
71     {
72         return operFlag != QueryObjType::OPER_ILLEGAL &&
73             type != QueryValueType::VALUE_TYPE_INVALID;
74     }
75 };
76 
77 enum class WriteTimeSort : int32_t {
78     TIMESTAMP_ASC = 1,
79     TIMESTAMP_DESC
80 };
81 
82 class QueryExpression final {
83 public:
84     DB_SYMBOL QueryExpression();
~QueryExpression()85     DB_SYMBOL ~QueryExpression() {};
86 
87     void EqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
88 
89     void NotEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
90 
91     void GreaterThan(const std::string &field, const QueryValueType type, const FieldValue &value);
92 
93     void LessThan(const std::string &field, const QueryValueType type, const FieldValue &value);
94 
95     void GreaterThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
96 
97     void LessThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
98 
99     void OrderBy(const std::string &field, bool isAsc);
100 
101     void Limit(int number, int offset);
102 
103     void Like(const std::string &field, const std::string &value);
104     void NotLike(const std::string &field, const std::string &value);
105 
106     void In(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
107     void NotIn(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
108 
109     void IsNull(const std::string &field);
110     void IsNotNull(const std::string &field);
111 
112     void And();
113 
114     void Or();
115 
116     void BeginGroup();
117 
118     void EndGroup();
119 
120     void Reset();
121 
122     void QueryByPrefixKey(const std::vector<uint8_t> &key);
123 
124     void QueryBySuggestIndex(const std::string &indexName);
125 
126     void QueryByKeyRange(const std::vector<uint8_t> &keyBegin, const std::vector<uint8_t> &keyEnd);
127 
128     std::vector<uint8_t> GetPreFixKey() const;
129 
130     std::vector<uint8_t> GetBeginKey() const;
131 
132     std::vector<uint8_t> GetEndKey() const;
133 
134     void SetTableName(const std::string &tableName);
135     const std::string &GetTableName();
136     bool IsTableNameSpecified() const;
137 
138     std::string GetSuggestIndex() const;
139 
140     const std::set<Key> &GetKeys() const;
141     void InKeys(const std::set<Key> &keys);
142 
143     const std::list<QueryObjNode> &GetQueryExpression();
144 
145     void SetErrFlag(bool flag);
146     bool GetErrFlag();
147 
148     int GetSortType() const;
149     void SetSortType(bool isAsc);
150 
151     std::vector<std::string> GetTables();
152     void SetTables(const std::vector<std::string> &tableNames);
153 
154     void From(const std::string &tableName);
155     int GetExpressionStatus() const;
156     std::vector<QueryExpression> GetQueryExpressions() const;
157     int RangeParamCheck() const;
158 private:
159     void AssemblyQueryInfo(const QueryObjType queryOperType, const std::string &field,
160         const QueryValueType type, const std::vector<FieldValue> &value, bool isNeedFieldPath);
161 
162     void SetNotSupportIfFromTables();
163 
164     void SetNotSupportIfCondition();
165 
166     void SetNotSupportIfNeed(QueryObjType type);
167 
168     std::list<QueryObjNode> queryInfo_;
169     bool errFlag_ = true;
170     std::vector<uint8_t> prefixKey_;
171     std::vector<uint8_t> beginKey_;
172     std::vector<uint8_t> endKey_;
173     std::string suggestIndex_;
174     std::string tableName_;
175     bool isTableNameSpecified_;
176     std::set<Key> keys_;
177     int sortType_ = 0;
178     std::vector<std::string> tables_;
179 
180     bool useFromTable_ = false;
181     std::string fromTable_;
182     std::list<std::string> tableSequence_;
183     std::map<std::string, QueryExpression> expressions_;
184     int validStatus_ = 0;
185 };
186 
187 // specialize for double
188 class GetQueryValueType {
189 public:
GetFieldTypeAndValue(const double & queryValue,FieldValue & fieldValue)190     static QueryValueType GetFieldTypeAndValue(const double &queryValue, FieldValue &fieldValue)
191     {
192         fieldValue.doubleValue = queryValue;
193         return QueryValueType::VALUE_TYPE_DOUBLE;
194     }
GetFieldTypeAndValue(const int & queryValue,FieldValue & fieldValue)195     static QueryValueType GetFieldTypeAndValue(const int &queryValue, FieldValue &fieldValue)
196     {
197         fieldValue.integerValue = queryValue;
198         return QueryValueType::VALUE_TYPE_INTEGER;
199     }
GetFieldTypeAndValue(const int64_t & queryValue,FieldValue & fieldValue)200     static QueryValueType GetFieldTypeAndValue(const int64_t &queryValue, FieldValue &fieldValue)
201     {
202         fieldValue.longValue = queryValue;
203         return QueryValueType::VALUE_TYPE_LONG;
204     }
GetFieldTypeAndValue(const bool & queryValue,FieldValue & fieldValue)205     static QueryValueType GetFieldTypeAndValue(const bool &queryValue, FieldValue &fieldValue)
206     {
207         fieldValue.boolValue = queryValue;
208         return QueryValueType::VALUE_TYPE_BOOL;
209     }
GetFieldTypeAndValue(const std::string & queryValue,FieldValue & fieldValue)210     static QueryValueType GetFieldTypeAndValue(const std::string &queryValue, FieldValue &fieldValue)
211     {
212         fieldValue.stringValue = queryValue;
213         return QueryValueType::VALUE_TYPE_STRING;
214     }
GetFieldTypeAndValue(const char * queryValue,FieldValue & fieldValue)215     static QueryValueType GetFieldTypeAndValue(const char *queryValue, FieldValue &fieldValue)
216     {
217         if (queryValue == nullptr) {
218             return QueryValueType::VALUE_TYPE_STRING;
219         }
220         fieldValue.stringValue = queryValue;
221         return QueryValueType::VALUE_TYPE_STRING;
222     }
223 };
224 } // DistributedDB
225 #endif // DISTRIBUTEDDB_QUERY_EXPRESSION_H