• 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_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H
16 #define SQLITE_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H
17 #ifdef RELATIONAL_STORE
18 
19 #include "data_transformer.h"
20 #include "db_types.h"
21 #include "macro_utils.h"
22 #include "sqlite_utils.h"
23 #include "sqlite_storage_executor.h"
24 #include "relational_row_data.h"
25 #include "relational_store_delegate.h"
26 #include "query_object.h"
27 
28 namespace DistributedDB {
29 class SQLiteSingleVerRelationalStorageExecutor : public SQLiteStorageExecutor {
30 public:
31     SQLiteSingleVerRelationalStorageExecutor(sqlite3 *dbHandle, bool writable, DistributedTableMode mode);
32     ~SQLiteSingleVerRelationalStorageExecutor() override = default;
33 
34     // Delete the copy and assign constructors
35     DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerRelationalStorageExecutor);
36 
37     // The parameter "identity" is a hash string that identifies a device
38     int CreateDistributedTable(const std::string &tableName, DistributedTableMode mode, bool isUpgraded,
39         const std::string &identity, TableInfo &table);
40 
41     int UpgradeDistributedTable(const std::string &tableName, DistributedTableMode mode, bool &schemaChanged,
42         RelationalSchemaObject &schema);
43 
44     int StartTransaction(TransactType type);
45     int Commit();
46     int Rollback();
47 
48     // For Get sync data
49     int GetSyncDataByQuery(std::vector<DataItem> &dataItems, size_t appendLength, const DataSizeSpecInfo &sizeInfo,
50         std::function<int(sqlite3 *, sqlite3_stmt *&, sqlite3_stmt *&, bool &)> getStmt, const TableInfo &tableInfo);
51 
52     // operation of meta data
53     int GetKvData(const Key &key, Value &value) const;
54     int PutKvData(const Key &key, const Value &value) const;
55     int DeleteMetaData(const std::vector<Key> &keys) const;
56     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const;
57     int GetAllMetaKeys(std::vector<Key> &keys) const;
58 
59     // For Put sync data
60     int SaveSyncItems(const QueryObject &object, std::vector<DataItem> &dataItems,
61         const std::string &deviceName, const TableInfo &table, bool useTrans = true);
62 
63     int AnalysisRelationalSchema(const std::string &tableName, TableInfo &tableInfo);
64 
65     int CheckDBModeForRelational();
66 
67     int DeleteDistributedDeviceTable(const std::string &device, const std::string &tableName);
68 
69     int DeleteDistributedAllDeviceTableLog(const std::string &tableName);
70 
71     int DeleteDistributedDeviceTableLog(const std::string &device, const std::string &tableName);
72 
73     int DeleteDistributedLogTable(const std::string &tableName);
74 
75     int CheckAndCleanDistributedTable(const std::vector<std::string> &tableNames,
76         std::vector<std::string> &missingTables);
77 
78     int CreateDistributedDeviceTable(const std::string &device, const TableInfo &baseTbl, const StoreInfo &info);
79 
80     int CheckQueryObjectLegal(const TableInfo &table, QueryObject &query, const std::string &schemaVersion);
81 
82     int GetMaxTimestamp(const std::vector<std::string> &tablesName, Timestamp &maxTimestamp) const;
83 
84     int ExecuteQueryBySqlStmt(const std::string &sql, const std::vector<std::string> &bindArgs, int packetSize,
85         std::vector<std::string> &colNames, std::vector<RelationalRowData *> &data);
86 
87     int CheckEncryptedOrCorrupted() const;
88 
89     int GetExistsDeviceList(std::set<std::string> &devices) const;
90 
91 private:
92     struct SaveSyncDataStmt {
93         sqlite3_stmt *saveDataStmt = nullptr;
94         sqlite3_stmt *saveLogStmt = nullptr;
95         sqlite3_stmt *queryStmt = nullptr;
96         sqlite3_stmt *rmDataStmt = nullptr;
97         sqlite3_stmt *rmLogStmt = nullptr;
98 
99         int ResetStatements(bool isNeedFinalize);
100     };
101 
102     int PrepareForSyncDataByTime(Timestamp begin, Timestamp end,
103         sqlite3_stmt *&statement, bool getDeletedData) const;
104 
105     int GetDataItemForSync(sqlite3_stmt *statement, DataItem &dataItem, bool isGettingDeletedData) const;
106 
107     int GetSyncDataPre(const DataItem &dataItem, DataItem &itemGet);
108 
109     int CheckDataConflictDefeated(const DataItem &item, bool &isDefeated);
110 
111     int SaveSyncDataItem(const std::vector<FieldInfo> &fieldInfos, const std::string &deviceName, DataItem &item);
112 
113     int SaveSyncDataItems(const QueryObject &object, std::vector<DataItem> &dataItems, const std::string &deviceName);
114     int SaveSyncDataItem(const DataItem &dataItem, sqlite3_stmt *&saveDataStmt, sqlite3_stmt *&rmDataStmt,
115         const std::vector<FieldInfo> &fieldInfos, int64_t &rowid);
116 
117     int DeleteSyncDataItem(const DataItem &dataItem, sqlite3_stmt *&rmDataStmt);
118 
119     int SaveSyncLog(sqlite3_stmt *statement, sqlite3_stmt *queryStmt, const DataItem &dataItem, int64_t rowid);
120     int PrepareForSavingData(const QueryObject &object, sqlite3_stmt *&statement) const;
121     int PrepareForSavingLog(const QueryObject &object, const std::string &deviceName,
122         sqlite3_stmt *&statement,  sqlite3_stmt *&queryStmt) const;
123 
124     int AlterAuxTableForUpgrade(const TableInfo &oldTableInfo, const TableInfo &newTableInfo);
125 
126     int DeleteSyncLog(const DataItem &item, sqlite3_stmt *&rmLogStmt);
127     int ProcessMissQueryData(const DataItem &item, sqlite3_stmt *&rmDataStmt, sqlite3_stmt *&rmLogStmt);
128     int GetMissQueryData(sqlite3_stmt *fullStmt, DataItem &item);
129     int GetQueryDataAndStepNext(bool isFirstTime, bool isGettingDeletedData, sqlite3_stmt *queryStmt, DataItem &item,
130         Timestamp &queryTime);
131     int GetMissQueryDataAndStepNext(sqlite3_stmt *fullStmt, DataItem &item, Timestamp &missQueryTime);
132 
133     int SetLogTriggerStatus(bool status);
134 
135     void SetTableInfo(const TableInfo &tableInfo);  // When put or get sync data, must call the func first.
136 
137     int GeneLogInfoForExistedData(sqlite3 *db, const std::string &tableName, const TableInfo &table,
138         const std::string &calPrimaryKeyHash);
139 
140     std::string baseTblName_;
141     TableInfo table_;  // Always operating table, user table when get, device table when put.
142 
143     SaveSyncDataStmt saveStmt_;
144 
145     DistributedTableMode mode_;
146 };
147 } // namespace DistributedDB
148 #endif
149 #endif // SQLITE_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H