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 #include "query.h"
16 namespace DistributedDB {
Query(const std::string & tableName)17 Query::Query(const std::string &tableName)
18 {
19 queryExpression_.SetTableName(tableName);
20 }
Select()21 Query Query::Select()
22 {
23 Query query;
24 return query;
25 }
26
Select(const std::string & tableName)27 Query Query::Select(const std::string &tableName)
28 {
29 Query query(tableName);
30 return query;
31 }
32
33
FromTable(const std::vector<std::string> & tableNames)34 Query &Query::FromTable(const std::vector<std::string> &tableNames)
35 {
36 queryExpression_.SetTables(tableNames);
37 return *this;
38 }
39
From(const std::string & tableName)40 Query &Query::From(const std::string &tableName)
41 {
42 queryExpression_.From(tableName);
43 return *this;
44 }
45
BeginGroup()46 Query &Query::BeginGroup()
47 {
48 queryExpression_.BeginGroup();
49 return *this;
50 }
51
EndGroup()52 Query &Query::EndGroup()
53 {
54 queryExpression_.EndGroup();
55 return *this;
56 }
57
IsNotNull(const std::string & field)58 Query &Query::IsNotNull(const std::string &field)
59 {
60 queryExpression_.IsNotNull(field);
61 return *this;
62 }
63
PrefixKey(const std::vector<uint8_t> & key)64 Query &Query::PrefixKey(const std::vector<uint8_t> &key)
65 {
66 queryExpression_.QueryByPrefixKey(key);
67 return *this;
68 }
69
Range(const std::vector<uint8_t> & keyBegin,const std::vector<uint8_t> & keyEnd)70 Query &Query::Range(const std::vector<uint8_t> &keyBegin, const std::vector<uint8_t> &keyEnd)
71 {
72 queryExpression_.QueryByKeyRange(keyBegin, keyEnd);
73 return *this;
74 }
75
SuggestIndex(const std::string & indexName)76 Query &Query::SuggestIndex(const std::string &indexName)
77 {
78 queryExpression_.QueryBySuggestIndex(indexName);
79 return *this;
80 }
81
InKeys(const std::set<Key> & keys)82 Query &Query::InKeys(const std::set<Key> &keys)
83 {
84 queryExpression_.InKeys(keys);
85 return *this;
86 }
87
OrderBy(const std::string & field,bool isAsc)88 Query &Query::OrderBy(const std::string &field, bool isAsc)
89 {
90 queryExpression_.OrderBy(field, isAsc);
91 return *this;
92 }
93
OrderByWriteTime(bool isAsc)94 Query &Query::OrderByWriteTime(bool isAsc)
95 {
96 queryExpression_.SetSortType(isAsc);
97 return *this;
98 }
99
Limit(int number,int offset)100 Query &Query::Limit(int number, int offset)
101 {
102 queryExpression_.Limit(number, offset);
103 return *this;
104 }
105
Like(const std::string & field,const std::string & value)106 Query &Query::Like(const std::string &field, const std::string &value)
107 {
108 queryExpression_.Like(field, value);
109 return *this;
110 }
111
NotLike(const std::string & field,const std::string & value)112 Query &Query::NotLike(const std::string &field, const std::string &value)
113 {
114 queryExpression_.NotLike(field, value);
115 return *this;
116 }
117
IsNull(const std::string & field)118 Query &Query::IsNull(const std::string &field)
119 {
120 queryExpression_.IsNull(field);
121 return *this;
122 }
123
And()124 Query &Query::And()
125 {
126 queryExpression_.And();
127 return *this;
128 }
129
Or()130 Query &Query::Or()
131 {
132 queryExpression_.Or();
133 return *this;
134 }
135
ExecuteCompareOperation(QueryObjType operType,const std::string & field,const QueryValueType type,const FieldValue & fieldValue)136 void Query::ExecuteCompareOperation(QueryObjType operType, const std::string &field, const QueryValueType type,
137 const FieldValue &fieldValue)
138 {
139 switch (operType) {
140 case QueryObjType::EQUALTO:
141 queryExpression_.EqualTo(field, type, fieldValue);
142 break;
143 case QueryObjType::NOT_EQUALTO:
144 queryExpression_.NotEqualTo(field, type, fieldValue);
145 break;
146 case QueryObjType::GREATER_THAN:
147 queryExpression_.GreaterThan(field, type, fieldValue);
148 break;
149 case QueryObjType::LESS_THAN:
150 queryExpression_.LessThan(field, type, fieldValue);
151 break;
152 case QueryObjType::GREATER_THAN_OR_EQUALTO:
153 queryExpression_.GreaterThanOrEqualTo(field, type, fieldValue);
154 break;
155 case QueryObjType::LESS_THAN_OR_EQUALTO:
156 queryExpression_.LessThanOrEqualTo(field, type, fieldValue);
157 break;
158 default:
159 return;
160 }
161 }
162
ExecuteCompareOperation(QueryObjType operType,const std::string & field,const QueryValueType type,const std::vector<FieldValue> & fieldValues)163 void Query::ExecuteCompareOperation(QueryObjType operType, const std::string &field, const QueryValueType type,
164 const std::vector<FieldValue> &fieldValues)
165 {
166 switch (operType) {
167 case QueryObjType::IN:
168 queryExpression_.In(field, type, fieldValues);
169 break;
170 case QueryObjType::NOT_IN:
171 queryExpression_.NotIn(field, type, fieldValues);
172 break;
173 default:
174 return;
175 }
176 }
177 } // namespace DistributedDB