• 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 <string>
20 #include <vector>
21 #include <list>
22 #include <set>
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 class QueryExpression final {
77 public:
78     DB_SYMBOL QueryExpression();
~QueryExpression()79     DB_SYMBOL ~QueryExpression() {};
80 
81     void EqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
82 
83     void NotEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
84 
85     void GreaterThan(const std::string &field, const QueryValueType type, const FieldValue &value);
86 
87     void LessThan(const std::string &field, const QueryValueType type, const FieldValue &value);
88 
89     void GreaterThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
90 
91     void LessThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value);
92 
93     void OrderBy(const std::string &field, bool isAsc);
94 
95     void Limit(int number, int offset);
96 
97     void Like(const std::string &field, const std::string &value);
98     void NotLike(const std::string &field, const std::string &value);
99 
100     void In(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
101     void NotIn(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values);
102 
103     void IsNull(const std::string &field);
104     void IsNotNull(const std::string &field);
105 
106     void And();
107 
108     void Or();
109 
110     void BeginGroup();
111 
112     void EndGroup();
113 
114     void Reset();
115 
116     void QueryByPrefixKey(const std::vector<uint8_t> &key);
117 
118     void QueryBySuggestIndex(const std::string &indexName);
119 
120     std::vector<uint8_t> GetPreFixKey() const;
121 
122     void SetTableName(const std::string &tableName);
123     const std::string &GetTableName();
124     bool IsTableNameSpecified() const;
125 
126     std::string GetSuggestIndex() const;
127 
128     const std::set<Key> &GetKeys() const;
129     void InKeys(const std::set<Key> &keys);
130 
131     const std::list<QueryObjNode> &GetQueryExpression();
132 
133     void SetErrFlag(bool flag);
134     bool GetErrFlag();
135 
136 private:
137     void AssemblyQueryInfo(const QueryObjType queryOperType, const std::string &field,
138         const QueryValueType type, const std::vector<FieldValue> &value, bool isNeedFieldPath);
139 
140     std::list<QueryObjNode> queryInfo_;
141     bool errFlag_ = true;
142     std::vector<uint8_t> prefixKey_;
143     std::string suggestIndex_;
144     std::string tableName_;
145     bool isTableNameSpecified_;
146     std::set<Key> keys_;
147 };
148 
149 // specialize for double
150 class GetQueryValueType {
151 public:
GetFieldTypeAndValue(const double & queryValue,FieldValue & fieldValue)152     static QueryValueType GetFieldTypeAndValue(const double &queryValue, FieldValue &fieldValue)
153     {
154         fieldValue.doubleValue = queryValue;
155         return QueryValueType::VALUE_TYPE_DOUBLE;
156     }
GetFieldTypeAndValue(const int & queryValue,FieldValue & fieldValue)157     static QueryValueType GetFieldTypeAndValue(const int &queryValue, FieldValue &fieldValue)
158     {
159         fieldValue.integerValue = queryValue;
160         return QueryValueType::VALUE_TYPE_INTEGER;
161     }
GetFieldTypeAndValue(const int64_t & queryValue,FieldValue & fieldValue)162     static QueryValueType GetFieldTypeAndValue(const int64_t &queryValue, FieldValue &fieldValue)
163     {
164         fieldValue.longValue = queryValue;
165         return QueryValueType::VALUE_TYPE_LONG;
166     }
GetFieldTypeAndValue(const bool & queryValue,FieldValue & fieldValue)167     static QueryValueType GetFieldTypeAndValue(const bool &queryValue, FieldValue &fieldValue)
168     {
169         fieldValue.boolValue = queryValue;
170         return QueryValueType::VALUE_TYPE_BOOL;
171     }
GetFieldTypeAndValue(const std::string & queryValue,FieldValue & fieldValue)172     static QueryValueType GetFieldTypeAndValue(const std::string &queryValue, FieldValue &fieldValue)
173     {
174         fieldValue.stringValue = queryValue;
175         return QueryValueType::VALUE_TYPE_STRING;
176     }
GetFieldTypeAndValue(const char * queryValue,FieldValue & fieldValue)177     static QueryValueType GetFieldTypeAndValue(const char *queryValue, FieldValue &fieldValue)
178     {
179         if (queryValue == nullptr) {
180             return QueryValueType::VALUE_TYPE_STRING;
181         }
182         fieldValue.stringValue = queryValue;
183         return QueryValueType::VALUE_TYPE_STRING;
184     }
185 };
186 }
187 #endif