• 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_H
17 #define DISTRIBUTEDDB_QUERY_H
18 
19 #include <string>
20 #include <vector>
21 #include <list>
22 #include <set>
23 
24 #include "query_expression.h"
25 #include "types_export.h"
26 
27 namespace DistributedDB {
28 class GetQueryInfo;
29 class Query {
30 public:
31 
32     // Do not support concurrent use of query objects
33     DB_API static Query Select();
34     DB_API static Query Select(const std::string &tableName);
35 
36     template<typename T>
EqualTo(const std::string & field,const T & value)37     DB_API Query &EqualTo(const std::string &field, const T &value)
38     {
39         FieldValue fieldValue;
40         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
41         ExecuteCompareOperation(QueryObjType::EQUALTO, field, type, fieldValue);
42         return *this;
43     }
44 
45     template<typename T>
NotEqualTo(const std::string & field,const T & value)46     DB_API Query &NotEqualTo(const std::string &field, const T &value)
47     {
48         FieldValue fieldValue;
49         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
50         ExecuteCompareOperation(QueryObjType::NOT_EQUALTO, field, type, fieldValue);
51         return *this;
52     }
53 
54     template<typename T>
GreaterThan(const std::string & field,const T & value)55     DB_API Query &GreaterThan(const std::string &field, const T &value)
56     {
57         FieldValue fieldValue;
58         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
59         ExecuteCompareOperation(QueryObjType::GREATER_THAN, field, type, fieldValue);
60         return *this;
61     }
62 
63     template<typename T>
LessThan(const std::string & field,const T & value)64     DB_API Query &LessThan(const std::string &field, const T &value)
65     {
66         FieldValue fieldValue;
67         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
68         ExecuteCompareOperation(QueryObjType::LESS_THAN, field, type, fieldValue);
69         return *this;
70     }
71 
72     template<typename T>
GreaterThanOrEqualTo(const std::string & field,const T & value)73     DB_API Query &GreaterThanOrEqualTo(const std::string &field, const T &value)
74     {
75         FieldValue fieldValue;
76         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
77         ExecuteCompareOperation(QueryObjType::GREATER_THAN_OR_EQUALTO, field, type, fieldValue);
78         return *this;
79     }
80 
81     template<typename T>
LessThanOrEqualTo(const std::string & field,const T & value)82     DB_API Query &LessThanOrEqualTo(const std::string &field, const T &value)
83     {
84         FieldValue fieldValue;
85         QueryValueType type = GetFieldTypeAndValue(value, fieldValue);
86         ExecuteCompareOperation(QueryObjType::LESS_THAN_OR_EQUALTO, field, type, fieldValue);
87         return *this;
88     }
89 
90     DB_API Query &OrderBy(const std::string &field, bool isAsc = true);
91 
92     DB_API Query &Limit(int number, int offset = 0);
93 
94     DB_API Query &Like(const std::string &field, const std::string &value);
95 
96     DB_API Query &NotLike(const std::string &field, const std::string &value);
97 
98     template<typename T>
In(const std::string & field,const std::vector<T> & values)99     DB_API Query &In(const std::string &field, const std::vector<T> &values)
100     {
101         std::vector<FieldValue> fieldValues;
102         QueryValueType type;
103         for (const auto &value : values) {
104             FieldValue fieldValue;
105             type = GetFieldTypeAndValue(value, fieldValue);
106             fieldValues.push_back(fieldValue);
107         }
108 
109         ExecuteCompareOperation(QueryObjType::IN, field, type, fieldValues);
110         return *this;
111     }
112 
113     template<typename T>
NotIn(const std::string & field,const std::vector<T> & values)114     DB_API Query &NotIn(const std::string &field, const std::vector<T> &values)
115     {
116         std::vector<FieldValue> fieldValues;
117         QueryValueType type;
118         for (const auto &value : values) {
119             FieldValue fieldValue;
120             type = GetFieldTypeAndValue(value, fieldValue);
121             fieldValues.push_back(fieldValue);
122         }
123 
124         ExecuteCompareOperation(QueryObjType::NOT_IN, field, type, fieldValues);
125         return *this;
126     }
127 
128     DB_API Query &IsNull(const std::string &field);
129 
130     DB_API Query &And();
131 
132     DB_API Query &Or();
133 
134     DB_API Query &IsNotNull(const std::string &field);
135 
136     DB_API Query &BeginGroup();
137 
138     DB_API Query &EndGroup();
139 
140     DB_API Query &PrefixKey(const std::vector<uint8_t> &key);
141 
142     DB_API Query &SuggestIndex(const std::string &indexName);
143 
144     DB_API Query &InKeys(const std::set<Key> &keys);
145 
146     friend class GetQueryInfo;
147     ~Query() = default;
148 
149 private:
150     Query() = default;
151     Query(const std::string &tableName);
152 
153     DB_SYMBOL void ExecuteCompareOperation(QueryObjType operType, const std::string &field,
154         const QueryValueType type, const FieldValue &fieldValue);
155     DB_SYMBOL void ExecuteCompareOperation(QueryObjType operType, const std::string &field,
156         const QueryValueType type, const std::vector<FieldValue> &fieldValue);
157 
158     template<typename T>
GetFieldTypeAndValue(const T & queryValue,FieldValue & fieldValue)159     QueryValueType GetFieldTypeAndValue(const T &queryValue, FieldValue &fieldValue)
160     {
161         return GetQueryValueType::GetFieldTypeAndValue(queryValue, fieldValue);
162     }
163 
164     QueryExpression queryExpression_;
165 };
166 } // namespace DistributedDB
167 #endif // DISTRIBUTEDDB_QUERY_H
168