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 DeleteDistributedDeviceTableLog(const std::string &device, const std::string &tableName, 70 const std::map<std::string, TableInfo> &tables); 71 72 int DeleteDistributedLogTable(const std::string &tableName); 73 74 int CheckAndCleanDistributedTable(const std::vector<std::string> &tableNames, 75 std::vector<std::string> &missingTables); 76 77 int CreateDistributedDeviceTable(const std::string &device, const TableInfo &baseTbl); 78 79 int CheckQueryObjectLegal(const TableInfo &table, QueryObject &query, const std::string &schemaVersion); 80 81 int GetMaxTimestamp(const std::vector<std::string> &tablesName, Timestamp &maxTimestamp) const; 82 83 int ExecuteQueryBySqlStmt(const std::string &sql, const std::vector<std::string> &bindArgs, int packetSize, 84 std::vector<std::string> &colNames, std::vector<RelationalRowData *> &data); 85 86 int CheckEncryptedOrCorrupted() const; 87 88 private: 89 struct SaveSyncDataStmt { 90 sqlite3_stmt *saveDataStmt = nullptr; 91 sqlite3_stmt *saveLogStmt = nullptr; 92 sqlite3_stmt *queryStmt = nullptr; 93 sqlite3_stmt *rmDataStmt = nullptr; 94 sqlite3_stmt *rmLogStmt = nullptr; 95 96 int ResetStatements(bool isNeedFinalize); 97 }; 98 99 int PrepareForSyncDataByTime(Timestamp begin, Timestamp end, 100 sqlite3_stmt *&statement, bool getDeletedData) const; 101 102 int GetDataItemForSync(sqlite3_stmt *statement, DataItem &dataItem, bool isGettingDeletedData) const; 103 104 int GetSyncDataPre(const DataItem &dataItem, DataItem &itemGet); 105 106 int CheckDataConflictDefeated(const DataItem &item, bool &isDefeated); 107 108 int SaveSyncDataItem(const std::vector<FieldInfo> &fieldInfos, const std::string &deviceName, DataItem &item); 109 110 int SaveSyncDataItems(const QueryObject &object, std::vector<DataItem> &dataItems, const std::string &deviceName); 111 int SaveSyncDataItem(const DataItem &dataItem, sqlite3_stmt *&saveDataStmt, sqlite3_stmt *&rmDataStmt, 112 const std::vector<FieldInfo> &fieldInfos, int64_t &rowid); 113 114 int DeleteSyncDataItem(const DataItem &dataItem, sqlite3_stmt *&rmDataStmt); 115 116 int SaveSyncLog(sqlite3_stmt *statement, sqlite3_stmt *queryStmt, const DataItem &dataItem, int64_t rowid); 117 int PrepareForSavingData(const QueryObject &object, sqlite3_stmt *&statement) const; 118 int PrepareForSavingLog(const QueryObject &object, const std::string &deviceName, 119 sqlite3_stmt *&statement, sqlite3_stmt *&queryStmt) const; 120 121 int AlterAuxTableForUpgrade(const TableInfo &oldTableInfo, const TableInfo &newTableInfo); 122 123 int DeleteSyncLog(const DataItem &item, sqlite3_stmt *&rmLogStmt); 124 int ProcessMissQueryData(const DataItem &item, sqlite3_stmt *&rmDataStmt, sqlite3_stmt *&rmLogStmt); 125 int GetMissQueryData(sqlite3_stmt *fullStmt, DataItem &item); 126 int GetQueryDataAndStepNext(bool isFirstTime, bool isGettingDeletedData, sqlite3_stmt *queryStmt, DataItem &item, 127 Timestamp &queryTime); 128 int GetMissQueryDataAndStepNext(sqlite3_stmt *fullStmt, DataItem &item, Timestamp &missQueryTime); 129 130 int SetLogTriggerStatus(bool status); 131 132 void SetTableInfo(const TableInfo &tableInfo); // When put or get sync data, must call the func first. 133 134 int GeneLogInfoForExistedData(sqlite3 *db, const std::string &tableName, const TableInfo &table, 135 const std::string &calPrimaryKeyHash); 136 137 std::string baseTblName_; 138 TableInfo table_; // Always operating table, user table when get, device table when put. 139 140 SaveSyncDataStmt saveStmt_; 141 142 DistributedTableMode mode_; 143 }; 144 } // namespace DistributedDB 145 #endif 146 #endif // SQLITE_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H