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 50 void SetTableName(const std::string &tableName); 51 52 const std::string &GetTableName() const; 53 54 bool HasOrderBy() const; 55 56 int ParseQueryObjNodes(); 57 58 bool Empty() const; 59 60 bool HasInKeys() const; 61 62 void SetSortType(SortType sortType); 63 64 SortType GetSortType() const; 65 66 #ifdef RELATIONAL_STORE 67 int SetSchema(const RelationalSchemaObject &schemaObj); // The interface can only be used in relational query. 68 #endif 69 70 // For continue token, once sync may not get all sync data, use AddOffset to continue last query 71 void SetLimit(int limit, int offset); 72 protected: 73 std::list<QueryObjNode> queryObjNodes_; 74 std::vector<uint8_t> prefixKey_; 75 std::string tableName_ = "sync_data"; 76 std::string suggestIndex_; 77 std::set<Key> keys_; 78 79 bool isValid_ = true; 80 81 bool initialized_ = false; // use function need after init 82 bool isTableNameSpecified_ = false; 83 std::vector<std::string> tables_; 84 bool isWithDeviceSyncQuery_ = true; 85 86 private: 87 int Parse(); 88 int ParseNode(const std::list<QueryObjNode>::iterator &iter); 89 int ParseNodeByOperFlag(const std::list<QueryObjNode>::iterator &iter); 90 int CheckEqualFormat(const std::list<QueryObjNode>::iterator &iter) const; 91 int CheckLinkerFormat(const std::list<QueryObjNode>::iterator &iter) const; 92 int CheckSuggestIndexFormat(const std::list<QueryObjNode>::iterator &iter) const; 93 int CheckOrderByFormat(const std::list<QueryObjNode>::iterator &iter); 94 int CheckLimitFormat(const std::list<QueryObjNode>::iterator &iter) const; 95 int CheckLinkerBefore(const std::list<QueryObjNode>::iterator &iter) const; 96 void ClearNodesFlag(); 97 void SetAttrWithQueryObjNodes(); 98 int CheckInKeys() const; 99 100 SchemaObject schema_; // used to check and parse schema filed 101 int limit_; 102 int offset_; 103 bool hasOrderBy_; 104 bool hasLimit_; 105 bool hasPrefixKey_; 106 bool hasInKeys_; 107 int orderByCounts_; 108 SortType sortType_ = SortType::NONE; 109 }; 110 } 111 #endif 112