• 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 #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 
AssetsOnly(const AssetsMap & assets)76 Query &Query::AssetsOnly(const AssetsMap &assets)
77 {
78     queryExpression_.QueryAssetsOnly(assets);
79     return *this;
80 }
81 
SuggestIndex(const std::string & indexName)82 Query &Query::SuggestIndex(const std::string &indexName)
83 {
84     queryExpression_.QueryBySuggestIndex(indexName);
85     return *this;
86 }
87 
InKeys(const std::set<Key> & keys)88 Query &Query::InKeys(const std::set<Key> &keys)
89 {
90     queryExpression_.InKeys(keys);
91     return *this;
92 }
93 
OrderBy(const std::string & field,bool isAsc)94 Query &Query::OrderBy(const std::string &field, bool isAsc)
95 {
96     queryExpression_.OrderBy(field, isAsc);
97     return *this;
98 }
99 
OrderByWriteTime(bool isAsc)100 Query &Query::OrderByWriteTime(bool isAsc)
101 {
102     queryExpression_.SetSortType(isAsc);
103     return *this;
104 }
105 
Limit(int number,int offset)106 Query &Query::Limit(int number, int offset)
107 {
108     queryExpression_.Limit(number, offset);
109     return *this;
110 }
111 
Like(const std::string & field,const std::string & value)112 Query &Query::Like(const std::string &field, const std::string &value)
113 {
114     queryExpression_.Like(field, value);
115     return *this;
116 }
117 
NotLike(const std::string & field,const std::string & value)118 Query &Query::NotLike(const std::string &field, const std::string &value)
119 {
120     queryExpression_.NotLike(field, value);
121     return *this;
122 }
123 
IsNull(const std::string & field)124 Query &Query::IsNull(const std::string &field)
125 {
126     queryExpression_.IsNull(field);
127     return *this;
128 }
129 
And()130 Query &Query::And()
131 {
132     queryExpression_.And();
133     return *this;
134 }
135 
Or()136 Query &Query::Or()
137 {
138     queryExpression_.Or();
139     return *this;
140 }
141 
ExecuteCompareOperation(QueryObjType operType,const std::string & field,const QueryValueType type,const FieldValue & fieldValue)142 void Query::ExecuteCompareOperation(QueryObjType operType, const std::string &field, const QueryValueType type,
143     const FieldValue &fieldValue)
144 {
145     switch (operType) {
146         case QueryObjType::EQUALTO:
147             queryExpression_.EqualTo(field, type, fieldValue);
148             break;
149         case QueryObjType::NOT_EQUALTO:
150             queryExpression_.NotEqualTo(field, type, fieldValue);
151             break;
152         case QueryObjType::GREATER_THAN:
153             queryExpression_.GreaterThan(field, type, fieldValue);
154             break;
155         case QueryObjType::LESS_THAN:
156             queryExpression_.LessThan(field, type, fieldValue);
157             break;
158         case QueryObjType::GREATER_THAN_OR_EQUALTO:
159             queryExpression_.GreaterThanOrEqualTo(field, type, fieldValue);
160             break;
161         case QueryObjType::LESS_THAN_OR_EQUALTO:
162             queryExpression_.LessThanOrEqualTo(field, type, fieldValue);
163             break;
164         default:
165             return;
166     }
167 }
168 
ExecuteCompareOperation(QueryObjType operType,const std::string & field,const QueryValueType type,const std::vector<FieldValue> & fieldValues)169 void Query::ExecuteCompareOperation(QueryObjType operType, const std::string &field, const QueryValueType type,
170     const std::vector<FieldValue> &fieldValues)
171 {
172     switch (operType) {
173         case QueryObjType::IN:
174             queryExpression_.In(field, type, fieldValues);
175             break;
176         case QueryObjType::NOT_IN:
177             queryExpression_.NotIn(field, type, fieldValues);
178             break;
179         default:
180             return;
181     }
182 }
183 }  // namespace DistributedDB