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 <list> 20 #include <set> 21 #include <string> 22 #include <vector> 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 KEY_RANGE = 0x1001, 63 }; 64 65 struct QueryObjNode { 66 QueryObjType operFlag = QueryObjType::OPER_ILLEGAL; 67 std::string fieldName {}; 68 QueryValueType type = QueryValueType::VALUE_TYPE_INVALID; 69 std::vector<FieldValue> fieldValue = {}; IsValidQueryObjNode70 bool IsValid() 71 { 72 return operFlag != QueryObjType::OPER_ILLEGAL && 73 type != QueryValueType::VALUE_TYPE_INVALID; 74 } 75 }; 76 77 enum class WriteTimeSort : int32_t { 78 TIMESTAMP_ASC = 1, 79 TIMESTAMP_DESC 80 }; 81 82 class QueryExpression final { 83 public: 84 DB_SYMBOL QueryExpression(); ~QueryExpression()85 DB_SYMBOL ~QueryExpression() {}; 86 87 void EqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 88 89 void NotEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 90 91 void GreaterThan(const std::string &field, const QueryValueType type, const FieldValue &value); 92 93 void LessThan(const std::string &field, const QueryValueType type, const FieldValue &value); 94 95 void GreaterThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 96 97 void LessThanOrEqualTo(const std::string &field, const QueryValueType type, const FieldValue &value); 98 99 void OrderBy(const std::string &field, bool isAsc); 100 101 void Limit(int number, int offset); 102 103 void Like(const std::string &field, const std::string &value); 104 void NotLike(const std::string &field, const std::string &value); 105 106 void In(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values); 107 void NotIn(const std::string &field, const QueryValueType type, const std::vector<FieldValue> &values); 108 109 void IsNull(const std::string &field); 110 void IsNotNull(const std::string &field); 111 112 void And(); 113 114 void Or(); 115 116 void BeginGroup(); 117 118 void EndGroup(); 119 120 void Reset(); 121 122 void QueryByPrefixKey(const std::vector<uint8_t> &key); 123 124 void QueryBySuggestIndex(const std::string &indexName); 125 126 void QueryByKeyRange(const std::vector<uint8_t> &keyBegin, const std::vector<uint8_t> &keyEnd); 127 128 void QueryAssetsOnly(const AssetsMap &assets); 129 130 std::vector<uint8_t> GetPreFixKey() const; 131 132 std::vector<uint8_t> GetBeginKey() const; 133 134 std::vector<uint8_t> GetEndKey() const; 135 136 void SetTableName(const std::string &tableName); 137 const std::string &GetTableName(); 138 bool IsTableNameSpecified() const; 139 140 std::string GetSuggestIndex() const; 141 142 const std::set<Key> &GetKeys() const; 143 void InKeys(const std::set<Key> &keys); 144 145 const std::list<QueryObjNode> &GetQueryExpression(); 146 147 void SetErrFlag(bool flag); 148 bool GetErrFlag(); 149 150 int GetSortType() const; 151 void SetSortType(bool isAsc); 152 153 std::vector<std::string> GetTables(); 154 void SetTables(const std::vector<std::string> &tableNames); 155 156 void From(const std::string &tableName); 157 int GetExpressionStatus() const; 158 std::vector<QueryExpression> GetQueryExpressions() const; 159 int RangeParamCheck() const; 160 161 bool IsAssetsOnly() const; 162 AssetsGroupMap GetAssetsOnlyGroupMap() const; 163 uint32_t GetGroupNum() const; 164 int GetExpressionStatusForAssetsOnly() const; 165 166 bool IsUseFromTables() const; 167 168 private: 169 void AssemblyQueryInfo(const QueryObjType queryOperType, const std::string &field, 170 const QueryValueType type, const std::vector<FieldValue> &value, bool isNeedFieldPath); 171 172 void SetNotSupportIfFromTables(); 173 174 void SetNotSupportIfCondition(); 175 176 void SetNotSupportIfNeed(QueryObjType type); 177 178 void SetAssetsOnlyValidStatusIfNeed(int status); 179 180 std::list<QueryObjNode> queryInfo_; 181 bool errFlag_ = true; 182 std::vector<uint8_t> prefixKey_; 183 std::vector<uint8_t> beginKey_; 184 std::vector<uint8_t> endKey_; 185 std::string suggestIndex_; 186 std::string tableName_; 187 bool isTableNameSpecified_; 188 std::set<Key> keys_; 189 int sortType_ = 0; 190 std::vector<std::string> tables_; 191 192 bool useFromTable_ = false; 193 bool isUseFromTables_ = false; 194 std::string fromTable_; 195 std::list<std::string> tableSequence_; 196 std::map<std::string, QueryExpression> expressions_; 197 int validStatus_ = 0; 198 uint32_t groupNum_ = 0; 199 bool isAssetsOnly_ = false; 200 AssetsGroupMap assetsGroupMap_; 201 int validStatusForAssetsOnly_ = 0; 202 }; 203 204 // specialize for double 205 class GetQueryValueType { 206 public: GetFieldTypeAndValue(const double & queryValue,FieldValue & fieldValue)207 static QueryValueType GetFieldTypeAndValue(const double &queryValue, FieldValue &fieldValue) 208 { 209 fieldValue.doubleValue = queryValue; 210 return QueryValueType::VALUE_TYPE_DOUBLE; 211 } GetFieldTypeAndValue(const int & queryValue,FieldValue & fieldValue)212 static QueryValueType GetFieldTypeAndValue(const int &queryValue, FieldValue &fieldValue) 213 { 214 fieldValue.integerValue = queryValue; 215 return QueryValueType::VALUE_TYPE_INTEGER; 216 } GetFieldTypeAndValue(const int64_t & queryValue,FieldValue & fieldValue)217 static QueryValueType GetFieldTypeAndValue(const int64_t &queryValue, FieldValue &fieldValue) 218 { 219 fieldValue.longValue = queryValue; 220 return QueryValueType::VALUE_TYPE_LONG; 221 } GetFieldTypeAndValue(const bool & queryValue,FieldValue & fieldValue)222 static QueryValueType GetFieldTypeAndValue(const bool &queryValue, FieldValue &fieldValue) 223 { 224 fieldValue.boolValue = queryValue; 225 return QueryValueType::VALUE_TYPE_BOOL; 226 } GetFieldTypeAndValue(const std::string & queryValue,FieldValue & fieldValue)227 static QueryValueType GetFieldTypeAndValue(const std::string &queryValue, FieldValue &fieldValue) 228 { 229 fieldValue.stringValue = queryValue; 230 return QueryValueType::VALUE_TYPE_STRING; 231 } GetFieldTypeAndValue(const char * queryValue,FieldValue & fieldValue)232 static QueryValueType GetFieldTypeAndValue(const char *queryValue, FieldValue &fieldValue) 233 { 234 if (queryValue == nullptr) { 235 return QueryValueType::VALUE_TYPE_STRING; 236 } 237 fieldValue.stringValue = queryValue; 238 return QueryValueType::VALUE_TYPE_STRING; 239 } 240 }; 241 } // DistributedDB 242 #endif // DISTRIBUTEDDB_QUERY_EXPRESSION_H