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