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