• 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 
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 };
63 
64 struct QueryObjNode {
65     QueryObjType operFlag = QueryObjType::OPER_ILLEGAL;
66     std::string fieldName {};
67     QueryValueType type = QueryValueType::VALUE_TYPE_INVALID;
68     std::vector<FieldValue> fieldValue = {};
IsValidQueryObjNode69     bool IsValid()
70     {
71         return operFlag != QueryObjType::OPER_ILLEGAL &&
72             type != QueryValueType::VALUE_TYPE_INVALID;
73     }
74 };
75 
76 enum class WriteTimeSort : int32_t {
77     TIMESTAMP_ASC = 1,
78     TIMESTAMP_DESC
79 };
80 
81 class QueryExpression final {
82 public:
83     DB_SYMBOL QueryExpression();
~QueryExpression()84     DB_SYMBOL ~QueryExpression() {};
85 
86     void EqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
87 
88     void NotEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
89 
90     void GreaterThan(const std::string &field, const QueryValueType type, const FieldValue &value);
91 
92     void LessThan(const std::string &field, const QueryValueType type, const FieldValue &value);
93 
94     void GreaterThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
95 
96     void LessThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
97 
98     void OrderBy(const std::string &field, bool isAsc);
99 
100     void Limit(int number, int offset);
101 
102     void Like(const std::string &field, const std::string &value);
103     void NotLike(const std::string &field, const std::string &value);
104 
105     void In(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
106     void NotIn(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
107 
108     void IsNull(const std::string &field);
109     void IsNotNull(const std::string &field);
110 
111     void And();
112 
113     void Or();
114 
115     void BeginGroup();
116 
117     void EndGroup();
118 
119     void Reset();
120 
121     void QueryByPrefixKey(const std::vector<uint8_t> &key);
122 
123     void QueryBySuggestIndex(const std::string &indexName);
124 
125     std::vector<uint8_t> GetPreFixKey() const;
126 
127     void SetTableName(const std::string &tableName);
128     const std::string &GetTableName();
129     bool IsTableNameSpecified() const;
130 
131     std::string GetSuggestIndex() const;
132 
133     const std::set<Key> &GetKeys() const;
134     void InKeys(const std::set<Key> &keys);
135 
136     const std::list<QueryObjNode> &GetQueryExpression();
137 
138     void SetErrFlag(bool flag);
139     bool GetErrFlag();
140 
141     int GetSortType() const;
142     void SetSortType(bool isAsc);
143 
144     std::vector<std::string> GetTables();
145     void SetTables(const std::vector<std::string> &tableNames);
146 
147     void SetIsDeviceSyncQuery(bool isDeviceSync = true);
148     bool GetIsDeviceSyncQuery() const;
149 
150 private:
151     void AssemblyQueryInfo(const QueryObjType queryOperType, const std::string &field,
152         const QueryValueType type, const std::vector<FieldValue> &value, bool isNeedFieldPath);
153 
154     std::list<QueryObjNode> queryInfo_;
155     bool errFlag_ = true;
156     std::vector<uint8_t> prefixKey_;
157     std::string suggestIndex_;
158     std::string tableName_;
159     bool isTableNameSpecified_;
160     std::set<Key> keys_;
161     int sortType_ = 0;
162     std::vector<std::string> tables_;
163     bool isWithDeviceSyncQuery_ = false;
164 };
165 
166 // specialize for double
167 class GetQueryValueType {
168 public:
GetFieldTypeAndValue(const double & queryValue,FieldValue & fieldValue)169     static QueryValueType GetFieldTypeAndValue(const double &queryValue, FieldValue &fieldValue)
170     {
171         fieldValue.doubleValue = queryValue;
172         return QueryValueType::VALUE_TYPE_DOUBLE;
173     }
GetFieldTypeAndValue(const int & queryValue,FieldValue & fieldValue)174     static QueryValueType GetFieldTypeAndValue(const int &queryValue, FieldValue &fieldValue)
175     {
176         fieldValue.integerValue = queryValue;
177         return QueryValueType::VALUE_TYPE_INTEGER;
178     }
GetFieldTypeAndValue(const int64_t & queryValue,FieldValue & fieldValue)179     static QueryValueType GetFieldTypeAndValue(const int64_t &queryValue, FieldValue &fieldValue)
180     {
181         fieldValue.longValue = queryValue;
182         return QueryValueType::VALUE_TYPE_LONG;
183     }
GetFieldTypeAndValue(const bool & queryValue,FieldValue & fieldValue)184     static QueryValueType GetFieldTypeAndValue(const bool &queryValue, FieldValue &fieldValue)
185     {
186         fieldValue.boolValue = queryValue;
187         return QueryValueType::VALUE_TYPE_BOOL;
188     }
GetFieldTypeAndValue(const std::string & queryValue,FieldValue & fieldValue)189     static QueryValueType GetFieldTypeAndValue(const std::string &queryValue, FieldValue &fieldValue)
190     {
191         fieldValue.stringValue = queryValue;
192         return QueryValueType::VALUE_TYPE_STRING;
193     }
GetFieldTypeAndValue(const char * queryValue,FieldValue & fieldValue)194     static QueryValueType GetFieldTypeAndValue(const char *queryValue, FieldValue &fieldValue)
195     {
196         if (queryValue == nullptr) {
197             return QueryValueType::VALUE_TYPE_STRING;
198         }
199         fieldValue.stringValue = queryValue;
200         return QueryValueType::VALUE_TYPE_STRING;
201     }
202 };
203 } // DistributedDB
204 #endif // DISTRIBUTEDDB_QUERY_EXPRESSION_H