• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IsQueryByRange() const;
49     bool IsQueryForRelationalDB() const;
50 
51     void SetTableName(const std::string &tableName);
52 
53     const std::string &GetTableName() const;
54 
55     bool HasOrderBy() const;
56 
57     int ParseQueryObjNodes();
58 
59     bool Empty() const;
60 
61     bool HasInKeys() const;
62 
63     void SetSortType(SortType sortType);
64 
65     SortType GetSortType() const;
66 
67     int CheckPrimaryKey(const std::map<int, FieldName> &primaryKeyMap) const;
68 
69     bool IsAssetsOnly() const;
70 
71     uint32_t GetGroupNum() const;
72 
73     AssetsGroupMap GetAssetsOnlyGroupMap() const;
74 
75     int AssetsOnlyErrFlag() const;
76 
77 #ifdef RELATIONAL_STORE
78     int SetSchema(const RelationalSchemaObject &schemaObj);  // The interface can only be used in relational query.
79 #endif
80 
81     // For continue token, once sync may not get all sync data, use AddOffset to continue last query
82     void SetLimit(int limit, int offset);
83 
84     void SetUseLocalSchema(bool isUse);
85     bool IsUseLocalSchema() const;
86 
87     void SetRemoteDev(const std::string &dev);
88     std::string GetRemoteDev() const;
89 
90     bool IsUseFromTables() const;
91 protected:
92     explicit QueryObject(const QueryExpression &queryExpression);
93     static std::vector<QueryExpression> GetQueryExpressions(const Query &query);
94     std::list<QueryObjNode> queryObjNodes_;
95     std::vector<uint8_t> prefixKey_;
96     std::string tableName_ = "sync_data";
97     std::string suggestIndex_;
98     std::set<Key> keys_;
99 
100     bool isValid_ = true;
101 
102     bool initialized_ = false; // use function need after init
103     bool isTableNameSpecified_ = false;
104     std::vector<std::string> tables_;
105     int validStatus = E_OK;
106     uint32_t groupNum_ = 0;
107     bool isAssetsOnly_ = false;
108     AssetsGroupMap assetsGroupMap_;
109     int assetsOnlyErrFlag_ = E_OK;
110     bool isUseFromTables_ = false;
111 
112 private:
113     int Parse();
114     int ParseNode(const std::list<QueryObjNode>::iterator &iter);
115     int ParseNodeByOperFlag(const std::list<QueryObjNode>::iterator &iter);
116     int CheckEqualFormat(const std::list<QueryObjNode>::iterator &iter) const;
117     int CheckLinkerFormat(const std::list<QueryObjNode>::iterator &iter) const;
118     int CheckSuggestIndexFormat(const std::list<QueryObjNode>::iterator &iter) const;
119     int CheckOrderByFormat(const std::list<QueryObjNode>::iterator &iter);
120     int CheckLimitFormat(const std::list<QueryObjNode>::iterator &iter) const;
121     int CheckLinkerBefore(const std::list<QueryObjNode>::iterator &iter) const;
122     void ClearNodesFlag();
123     void SetAttrWithQueryObjNodes();
124     int CheckInKeys() const;
125 
126     SchemaObject schema_; // used to check and parse schema filed
127     int limit_;
128     int offset_;
129     bool hasOrderBy_;
130     bool hasLimit_;
131     bool hasPrefixKey_;
132     bool hasInKeys_;
133     int orderByCounts_;
134     bool isUseLocalSchema_;
135     SortType sortType_ = SortType::NONE;
136     std::string remoteDev_;
137 };
138 }
139 #endif
140