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 #ifdef RELATIONAL_STORE
70     int SetSchema(const RelationalSchemaObject &schemaObj);  // The interface can only be used in relational query.
71 #endif
72 
73     // For continue token, once sync may not get all sync data, use AddOffset to continue last query
74     void SetLimit(int limit, int offset);
75 protected:
76     explicit QueryObject(const QueryExpression &queryExpression);
77     static std::vector<QueryExpression> GetQueryExpressions(const Query &query);
78     std::list<QueryObjNode> queryObjNodes_;
79     std::vector<uint8_t> prefixKey_;
80     std::string tableName_ = "sync_data";
81     std::string suggestIndex_;
82     std::set<Key> keys_;
83 
84     bool isValid_ = true;
85 
86     bool initialized_ = false; // use function need after init
87     bool isTableNameSpecified_ = false;
88     std::vector<std::string> tables_;
89     int validStatus = E_OK;
90 
91 private:
92     int Parse();
93     int ParseNode(const std::list<QueryObjNode>::iterator &iter);
94     int ParseNodeByOperFlag(const std::list<QueryObjNode>::iterator &iter);
95     int CheckEqualFormat(const std::list<QueryObjNode>::iterator &iter) const;
96     int CheckLinkerFormat(const std::list<QueryObjNode>::iterator &iter) const;
97     int CheckSuggestIndexFormat(const std::list<QueryObjNode>::iterator &iter) const;
98     int CheckOrderByFormat(const std::list<QueryObjNode>::iterator &iter);
99     int CheckLimitFormat(const std::list<QueryObjNode>::iterator &iter) const;
100     int CheckLinkerBefore(const std::list<QueryObjNode>::iterator &iter) const;
101     void ClearNodesFlag();
102     void SetAttrWithQueryObjNodes();
103     int CheckInKeys() const;
104 
105     SchemaObject schema_; // used to check and parse schema filed
106     int limit_;
107     int offset_;
108     bool hasOrderBy_;
109     bool hasLimit_;
110     bool hasPrefixKey_;
111     bool hasInKeys_;
112     int orderByCounts_;
113     SortType sortType_ = SortType::NONE;
114 };
115 }
116 #endif
117