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_EXPRESSION_H 17 #define DISTRIBUTEDDB_QUERY_EXPRESSION_H 18 19 #include <string> 20 #include <vector> 21 #include <list> 22 #include <set> 23 24 #include "types_export.h" 25 26 namespace DistributedDB { 27 enum class QueryValueType: int32_t { 28 VALUE_TYPE_INVALID = -1, 29 VALUE_TYPE_NULL, 30 VALUE_TYPE_BOOL, 31 VALUE_TYPE_INTEGER, 32 VALUE_TYPE_LONG, 33 VALUE_TYPE_DOUBLE, 34 VALUE_TYPE_STRING, 35 }; 36 37 // value will const, it will influence query object id 38 // use high pos bit to distinguish operator type 39 enum class QueryObjType : uint32_t { 40 OPER_ILLEGAL = 0x0000, 41 EQUALTO = 0x0101, 42 NOT_EQUALTO, 43 GREATER_THAN, 44 LESS_THAN, 45 GREATER_THAN_OR_EQUALTO, 46 LESS_THAN_OR_EQUALTO, 47 LIKE = 0x0201, 48 NOT_LIKE, 49 IS_NULL, 50 IS_NOT_NULL, 51 IN = 0x0301, 52 NOT_IN, 53 QUERY_BY_KEY_PREFIX = 0x0401, 54 BEGIN_GROUP = 0x0501, 55 END_GROUP, 56 AND = 0x0601, 57 OR, 58 LIMIT = 0x0701, 59 ORDERBY, 60 SUGGEST_INDEX = 0x0801, 61 IN_KEYS = 0x0901, 62 }; 63 64 struct QueryObjNode { 65 QueryObjType operFlag = QueryObjType::OPER_ILLEGAL; 66 std::string fieldName {}; 67 QueryValueType type = QueryValueType::VALUE_TYPE_INVALID; 68 std::vector<FieldValue> fieldValue = {}; IsValidQueryObjNode69 bool IsValid() 70 { 71 return operFlag != QueryObjType::OPER_ILLEGAL && 72 type != QueryValueType::VALUE_TYPE_INVALID; 73 } 74 }; 75 76 enum class WriteTimeSort : int32_t { 77 TIMESTAMP_ASC = 1, 78 TIMESTAMP_DESC 79 }; 80 81 class QueryExpression final { 82 public: 83 DB_SYMBOL QueryExpression(); ~QueryExpression()84 DB_SYMBOL ~QueryExpression() {}; 85 86 void EqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 87 88 void NotEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 89 90 void GreaterThan(const std::string &field, const QueryValueType type, const FieldValue &value); 91 92 void LessThan(const std::string &field, const QueryValueType type, const FieldValue &value); 93 94 void GreaterThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 95 96 void LessThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 97 98 void OrderBy(const std::string &field, bool isAsc); 99 100 void Limit(int number, int offset); 101 102 void Like(const std::string &field, const std::string &value); 103 void NotLike(const std::string &field, const std::string &value); 104 105 void In(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values); 106 void NotIn(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values); 107 108 void IsNull(const std::string &field); 109 void IsNotNull(const std::string &field); 110 111 void And(); 112 113 void Or(); 114 115 void BeginGroup(); 116 117 void EndGroup(); 118 119 void Reset(); 120 121 void QueryByPrefixKey(const std::vector<uint8_t> &key); 122 123 void QueryBySuggestIndex(const std::string &indexName); 124 125 std::vector<uint8_t> GetPreFixKey() const; 126 127 void SetTableName(const std::string &tableName); 128 const std::string &GetTableName(); 129 bool IsTableNameSpecified() const; 130 131 std::string GetSuggestIndex() const; 132 133 const std::set<Key> &GetKeys() const; 134 void InKeys(const std::set<Key> &keys); 135 136 const std::list<QueryObjNode> &GetQueryExpression(); 137 138 void SetErrFlag(bool flag); 139 bool GetErrFlag(); 140 141 int GetSortType() const; 142 void SetSortType(bool isAsc); 143 144 private: 145 void AssemblyQueryInfo(const QueryObjType queryOperType, const std::string &field, 146 const QueryValueType type, const std::vector<FieldValue> &value, bool isNeedFieldPath); 147 148 std::list<QueryObjNode> queryInfo_; 149 bool errFlag_ = true; 150 std::vector<uint8_t> prefixKey_; 151 std::string suggestIndex_; 152 std::string tableName_; 153 bool isTableNameSpecified_; 154 std::set<Key> keys_; 155 int sortType_ = 0; 156 }; 157 158 // specialize for double 159 class GetQueryValueType { 160 public: GetFieldTypeAndValue(const double & queryValue,FieldValue & fieldValue)161 static QueryValueType GetFieldTypeAndValue(const double &queryValue, FieldValue &fieldValue) 162 { 163 fieldValue.doubleValue = queryValue; 164 return QueryValueType::VALUE_TYPE_DOUBLE; 165 } GetFieldTypeAndValue(const int & queryValue,FieldValue & fieldValue)166 static QueryValueType GetFieldTypeAndValue(const int &queryValue, FieldValue &fieldValue) 167 { 168 fieldValue.integerValue = queryValue; 169 return QueryValueType::VALUE_TYPE_INTEGER; 170 } GetFieldTypeAndValue(const int64_t & queryValue,FieldValue & fieldValue)171 static QueryValueType GetFieldTypeAndValue(const int64_t &queryValue, FieldValue &fieldValue) 172 { 173 fieldValue.longValue = queryValue; 174 return QueryValueType::VALUE_TYPE_LONG; 175 } GetFieldTypeAndValue(const bool & queryValue,FieldValue & fieldValue)176 static QueryValueType GetFieldTypeAndValue(const bool &queryValue, FieldValue &fieldValue) 177 { 178 fieldValue.boolValue = queryValue; 179 return QueryValueType::VALUE_TYPE_BOOL; 180 } GetFieldTypeAndValue(const std::string & queryValue,FieldValue & fieldValue)181 static QueryValueType GetFieldTypeAndValue(const std::string &queryValue, FieldValue &fieldValue) 182 { 183 fieldValue.stringValue = queryValue; 184 return QueryValueType::VALUE_TYPE_STRING; 185 } GetFieldTypeAndValue(const char * queryValue,FieldValue & fieldValue)186 static QueryValueType GetFieldTypeAndValue(const char *queryValue, FieldValue &fieldValue) 187 { 188 if (queryValue == nullptr) { 189 return QueryValueType::VALUE_TYPE_STRING; 190 } 191 fieldValue.stringValue = queryValue; 192 return QueryValueType::VALUE_TYPE_STRING; 193 } 194 }; 195 } 196 #endif