• 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 SQLITE_QUERY_HELPER_H
16 #define SQLITE_QUERY_HELPER_H
17 
18 #include <list>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include "query_expression.h"
24 #include "schema_utils.h"
25 #include "sqlite_import.h"
26 
27 namespace DistributedDB {
28 namespace TriggerMode {
29 enum class TriggerModeEnum;
30 }
31 struct QueryObjInfo {
32     SchemaObject schema_;
33     std::list<QueryObjNode> queryObjNodes_;
34     std::vector<uint8_t> prefixKey_;
35     std::string suggestIndex_;
36     std::set<Key> keys_;
37     int orderByCounts_ = 0; // Record processing to which orderBy node
38     bool isValid_ = true;
39     bool hasOrderBy_ = false;
40     bool hasLimit_ = false;
41     bool hasPrefixKey_ = false;
42     std::string tableName_;
43     bool isRelationalQuery_ = false;
44     SortType sortType_ = SortType::NONE;
45 };
46 
47 enum SymbolType : uint32_t {
48     INVALID_SYMBOL = 0x0000,
49     COMPARE_SYMBOL = 0x0100, // relation symbol use to compare
50     RELATIONAL_SYMBOL = 0x0200,
51     RANGE_SYMBOL = 0x0300,
52     PREFIXKEY_SYMBOL = 0x0400,
53     LOGIC_SYMBOL = 0x0500,
54     LINK_SYMBOL = 0x0600, // use to link relatonal symbol
55     SPECIAL_SYMBOL = 0x0700, // need special precess and need at the last
56     SUGGEST_INDEX_SYMBOL = 0x0800,
57     IN_KEYS_SYMBOL = 0x0900,
58 };
59 
60 class SqliteQueryHelper final {
61 public:
62     explicit SqliteQueryHelper(const QueryObjInfo &info);
63 
64     // forbidden move constructor.
65     SqliteQueryHelper(SqliteQueryHelper &&) = delete;
66     SqliteQueryHelper &operator=(SqliteQueryHelper &&) = delete;
67     // forbidden copy constructor.
68     SqliteQueryHelper(const SqliteQueryHelper &) = delete;
69     SqliteQueryHelper &operator=(const SqliteQueryHelper &) = delete;
70 
71     ~SqliteQueryHelper() = default;
72 
73     int GetQuerySqlStatement(sqlite3 *dbHandle, bool onlyRowid, sqlite3_stmt *&statement);
74     int GetQuerySqlStatement(sqlite3 *dbHandle, const std::string &sql, sqlite3_stmt *&statement);
75     int GetCountSqlStatement(sqlite3 *dbHandle, sqlite3_stmt *&countStmt);
76 
77     // For query Sync
78     int GetQuerySyncStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, sqlite3_stmt *&statement);
79     int GetSyncDataCheckSql(std::string &sql);
80     int BindSyncDataCheckStmt(sqlite3_stmt *statement, const Key &hashKey) const;
81 
82     int GetSubscribeSql(const std::string &subscribeId, TriggerMode::TriggerModeEnum mode,
83         std::string &subscribeCondition);
84 
85     static SymbolType GetSymbolType(const QueryObjType &queryObjType);
86 
87     // public for unit test
88     int GetQuerySql(std::string &sql, bool onlyRowid);
89     int GetCountQuerySql(std::string &sql);
90 
GetTableName()91     const std::string &GetTableName()
92     {
93         return tableName_;
94     }
95 
96     int GetRelationalMissQuerySql(const std::vector<std::string> &fieldNames, std::string &sql);
97     int GetRelationalMissQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime,
98         const std::vector<std::string> &fieldNames, sqlite3_stmt *&statement);
99     int GetRelationalSyncDataQuerySql(std::string &sql, bool hasSubQuery, const std::vector<std::string> &fieldNames);
100     int GetRelationalSyncDataQuerySqlWithLimit(const std::vector<std::string> &fieldNames, std::string &sql);
101     int GetRelationalQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime,
102         const std::vector<std::string> &fieldNames, sqlite3_stmt *&statement);
103     int GetRelationalCloudQueryStatement(sqlite3 *dbHandle, uint64_t beginTime,
104         const std::vector<Field> &fields, const bool &isCloudForcePush, sqlite3_stmt *&statement);
105 
106 private:
107     int ToQuerySql();
108     int ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias = false);
109     int ToGetCountSql();
110     int ParseQueryExpression(const QueryObjNode &queryNode, std::string &querySql,
111         const std::string &accessStr = "", bool placeholder = true);
112     std::string MapRelationalSymbolToSql(const QueryObjNode &queryNode, bool placeholder = false) const;
113     std::string MapKeywordSymbolToSql(const QueryObjNode &queryNode);
114     std::string MapLogicSymbolToSql(const QueryObjNode &queryNode) const;
115     std::string MapValueToSql(const QueryObjNode &queryNode, bool placeholder) const;
116     std::string MapCastFuncSql(const QueryObjNode &queryNode, const std::string &accessStr = "");
117     std::string MapCastTypeSql(const FieldType &type) const;
118     int BindFieldValue(sqlite3_stmt *statement, const QueryObjNode &queryNode, int &index) const;
119     bool FilterSymbolToAddBracketLink(std::string &querySql, bool isNeedLink = true) const;
120     std::string AssembleSqlForSuggestIndex(const std::string &baseSql, const std::string &filter) const;
121     std::string CheckAndFormatSuggestIndex() const;
122     int GetSyncDataQuerySql(std::string &sql, bool hasSubQuery);
123     int ParseQueryObjNodeToSQL(bool isQueryForSync);
124     int BindTimeRange(sqlite3_stmt *&statement, int &index, uint64_t beginTime, uint64_t endTime) const;
125     int BindObjNodes(sqlite3_stmt *&statement, int &index) const;
126     int GetSubscribeCondition(const std::string &accessStr, std::string &conditionStr);
127     std::string MapKeysInToSql(size_t keysNum) const;
128     int BindKeysToStmt(const std::set<Key> &keys, sqlite3_stmt *&countStmt, int &index) const;
129 
130     std::string MapKeysInSubCondition(const std::string &accessStr) const;  // For InKeys.
131     // Return the left string of symbol in compare clause.
132     std::string GetFieldShape(const QueryObjNode &queryNode, const std::string &accessStr = "");
133 
134     SchemaObject schema_;
135     std::list<QueryObjNode> queryObjNodes_;
136     std::vector<uint8_t> prefixKey_;
137     std::string suggestIndex_;
138     std::string tableName_;
139     std::set<Key> keys_;
140 
141     std::string querySql_;
142     std::string countSql_;
143 
144     int orderByCounts_; // Record processing to which orderBy node
145     bool isValid_;
146     bool transformed_;
147     bool hasOrderBy_;
148     bool hasLimit_;
149     bool isOrderByAppeared_;
150     bool hasPrefixKey_;
151     bool isNeedOrderbyKey_;  // The tag field is used for prefix query filtering key sorting
152     bool isRelationalQuery_;
153     SortType sortType_ = SortType::NONE;
154 };
155 }
156 #endif