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 #ifndef QUERY_OBJECT_H 16 #define QUERY_OBJECT_H 17 18 #include <string> 19 20 #include "db_types.h" 21 #include "query.h" 22 #include "relational_schema_object.h" 23 #include "schema_object.h" 24 #include "sqlite_query_helper.h" 25 26 namespace DistributedDB { 27 class QueryObject { 28 public: 29 QueryObject(); 30 explicit QueryObject(const Query &query); 31 // for query sync 32 QueryObject(const std::list<QueryObjNode> &queryObjNodes, const std::vector<uint8_t> &prefixKey, 33 const std::set<Key> &keys); 34 virtual ~QueryObject(); 35 int Init(); 36 SqliteQueryHelper GetQueryHelper(int &errCode); 37 38 // suggest: get those attributes after init or GetQueryHelper to parsed query 39 bool IsValid(); 40 bool HasLimit() const; 41 void GetLimitVal(int &limit, int &offset) const; 42 bool IsCountValid() const; 43 44 const std::vector<uint8_t> &GetPrefixKey() const; 45 void SetSchema(const SchemaObject &schema); 46 47 bool IsQueryOnlyByKey() const; 48 bool IsQueryForRelationalDB() const; 49 SetTableName(const std::string & tableName)50 void SetTableName(const std::string &tableName) 51 { 52 tableName_ = tableName; 53 isTableNameSpecified_ = true; 54 } 55 GetTableName()56 const std::string &GetTableName() const 57 { 58 return tableName_; 59 } 60 61 bool HasOrderBy() const; 62 63 int ParseQueryObjNodes(); 64 65 bool Empty() const; 66 HasInKeys()67 bool HasInKeys() const 68 { 69 return hasInKeys_; 70 } 71 SetSortType(SortType sortType)72 void SetSortType(SortType sortType) 73 { 74 sortType_ = sortType; 75 } 76 GetSortType()77 SortType GetSortType() const 78 { 79 return sortType_; 80 } 81 82 #ifdef RELATIONAL_STORE 83 int SetSchema(const RelationalSchemaObject &schemaObj); // The interface can only be used in relational query. 84 #endif 85 86 protected: 87 std::list<QueryObjNode> queryObjNodes_; 88 std::vector<uint8_t> prefixKey_; 89 std::string tableName_ = "sync_data"; 90 std::string suggestIndex_; 91 std::set<Key> keys_; 92 93 bool isValid_ = true; 94 95 bool initialized_ = false; // use function need after init 96 bool isTableNameSpecified_ = false; 97 98 private: 99 int Parse(); 100 int ParseNode(const std::list<QueryObjNode>::iterator &iter); 101 int ParseNodeByOperFlag(const std::list<QueryObjNode>::iterator &iter); 102 int CheckEqualFormat(const std::list<QueryObjNode>::iterator &iter) const; 103 int CheckLinkerFormat(const std::list<QueryObjNode>::iterator &iter) const; 104 int CheckSuggestIndexFormat(const std::list<QueryObjNode>::iterator &iter) const; 105 int CheckOrderByFormat(const std::list<QueryObjNode>::iterator &iter); 106 int CheckLimitFormat(const std::list<QueryObjNode>::iterator &iter) const; 107 int CheckLinkerBefore(const std::list<QueryObjNode>::iterator &iter) const; 108 void ClearNodesFlag(); 109 void GetAttrFromQueryObjNodes(); 110 int CheckInKeys() const; 111 bool IsRelationalQuery() const; 112 113 SchemaObject schema_; // used to check and parse schema filed 114 int limit_; 115 int offset_; 116 bool hasOrderBy_; 117 bool hasLimit_; 118 bool hasPrefixKey_; 119 bool hasInKeys_; 120 int orderByCounts_; 121 SortType sortType_ = SortType::NONE; 122 }; 123 } 124 #endif 125