• 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 &OrderByWriteTime(bool isAsc = true);
93 
94     DB_API Query &Limit(int number, int offset = 0);
95 
96     DB_API Query &Like(const std::string &field, const std::string &value);
97 
98     DB_API Query &NotLike(const std::string &field, const std::string &value);
99 
100     template<typename T>
In(const std::string & field,const std::vector<T> & values)101     DB_API Query &In(const std::string &field, const std::vector<T> &values)
102     {
103         std::vector<FieldValue> fieldValues;
104         QueryValueType type = QueryValueType::VALUE_TYPE_NULL;
105         for (const auto &value : values) {
106             FieldValue fieldValue;
107             type = GetFieldTypeAndValue(value, fieldValue);
108             fieldValues.push_back(fieldValue);
109         }
110 
111         ExecuteCompareOperation(QueryObjType::IN, field, type, fieldValues);
112         return *this;
113     }
114 
115     template<typename T>
NotIn(const std::string & field,const std::vector<T> & values)116     DB_API Query &NotIn(const std::string &field, const std::vector<T> &values)
117     {
118         std::vector<FieldValue> fieldValues;
119         QueryValueType type = QueryValueType::VALUE_TYPE_NULL;
120         for (const auto &value : values) {
121             FieldValue fieldValue;
122             type = GetFieldTypeAndValue(value, fieldValue);
123             fieldValues.push_back(fieldValue);
124         }
125 
126         ExecuteCompareOperation(QueryObjType::NOT_IN, field, type, fieldValues);
127         return *this;
128     }
129 
130     DB_API Query &IsNull(const std::string &field);
131 
132     DB_API Query &And();
133 
134     DB_API Query &Or();
135 
136     DB_API Query &IsNotNull(const std::string &field);
137 
138     DB_API Query &BeginGroup();
139 
140     DB_API Query &EndGroup();
141 
142     DB_API Query &PrefixKey(const std::vector<uint8_t> &key);
143 
144     DB_API Query &SuggestIndex(const std::string &indexName);
145 
146     DB_API Query &InKeys(const std::set<Key> &keys);
147 
148     friend class GetQueryInfo;
149     DB_API ~Query() = default;
150     DB_API Query() = default;
151 private:
152     explicit Query(const std::string &tableName);
153 
154     DB_SYMBOL void ExecuteCompareOperation(QueryObjType operType, const std::string &field,
155         const QueryValueType type, const FieldValue &fieldValue);
156     DB_SYMBOL void ExecuteCompareOperation(QueryObjType operType, const std::string &field,
157         const QueryValueType type, const std::vector<FieldValue> &fieldValue);
158 
159     template<typename T>
GetFieldTypeAndValue(const T & queryValue,FieldValue & fieldValue)160     QueryValueType GetFieldTypeAndValue(const T &queryValue, FieldValue &fieldValue)
161     {
162         return GetQueryValueType::GetFieldTypeAndValue(queryValue, fieldValue);
163     }
164 
165     QueryExpression queryExpression_;
166 };
167 } // namespace DistributedDB
168 #endif // DISTRIBUTEDDB_QUERY_H
169