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