1 /* 2 * Copyright (c) 2023 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 16 #ifndef RELATIONAL_SYNC_DATA_INSERTER_H 17 #define RELATIONAL_SYNC_DATA_INSERTER_H 18 19 #include <vector> 20 #include "db_types.h" 21 #include "query_object.h" 22 #include "relational_schema_object.h" 23 24 namespace DistributedDB { 25 26 struct SaveSyncDataStmt { 27 sqlite3_stmt *insertDataStmt = nullptr; 28 sqlite3_stmt *updateDataStmt = nullptr; 29 sqlite3_stmt *saveLogStmt = nullptr; 30 sqlite3_stmt *queryStmt = nullptr; 31 sqlite3_stmt *rmDataStmt = nullptr; 32 sqlite3_stmt *rmLogStmt = nullptr; SaveSyncDataStmtSaveSyncDataStmt33 SaveSyncDataStmt() {} ~SaveSyncDataStmtSaveSyncDataStmt34 ~SaveSyncDataStmt() 35 { 36 ResetStatements(true); 37 } 38 39 int ResetStatements(bool isNeedFinalize); 40 }; 41 42 class RelationalSyncDataInserter { 43 public: 44 RelationalSyncDataInserter() = default; 45 ~RelationalSyncDataInserter() = default; 46 47 static RelationalSyncDataInserter CreateInserter(const std::string &deviceName, const QueryObject &query, 48 const RelationalSchemaObject &localSchema, const std::vector<FieldInfo> &remoteFields, 49 const StoreInfo &info); 50 51 void SetHashDevId(const std::string &hashDevId); 52 // Set remote fields in cid order 53 void SetRemoteFields(std::vector<FieldInfo> remoteFields); 54 void SetEntries(std::vector<DataItem> entries); 55 56 void SetLocalTable(TableInfo localTable); 57 const TableInfo &GetLocalTable() const; 58 59 void SetQuery(QueryObject query); 60 void SetInsertTableName(std::string tableName); 61 62 void SetTableMode(DistributedTableMode mode); 63 64 int Iterate(const std::function<int (DataItem &)> &); 65 66 int SaveData(bool isUpdate, const DataItem &dataItem, SaveSyncDataStmt &saveSyncDataStmt, 67 std::map<std::string, Type> &pkVals); 68 int BindSaveDataStatement(bool isExist, const DataItem &dataItem, const std::set<std::string> &filterSet, 69 sqlite3_stmt *stmt, std::map<std::string, Type> &pkVals); 70 71 int PrepareStatement(sqlite3 *db, SaveSyncDataStmt &stmt); 72 int GetDeleteLogStmt(sqlite3 *db, sqlite3_stmt *&stmt); 73 int GetDeleteSyncDataStmt(sqlite3 *db, sqlite3_stmt *&stmt); 74 75 int BindHashKeyAndDev(const DataItem &dataItem, sqlite3_stmt *stmt, int beginIndex); 76 77 int SaveSyncLog(sqlite3 *db, sqlite3_stmt *statement, sqlite3_stmt *queryStmt, const DataItem &dataItem, 78 int64_t rowid); 79 80 ChangedData &GetChangedData(); 81 private: 82 83 int GetInsertStatement(sqlite3 *db, sqlite3_stmt *&stmt); 84 85 int GetSaveLogStatement(sqlite3 *db, sqlite3_stmt *&logStmt, sqlite3_stmt *&queryStmt); 86 87 int GetUpdateStatement(sqlite3 *db, sqlite3_stmt *&stmt); 88 89 std::string hashDevId_; 90 std::vector<FieldInfo> remoteFields_; 91 std::vector<DataItem> entries_; 92 TableInfo localTable_; 93 QueryObject query_; 94 std::string insertTableName_; // table name to save sync data 95 DistributedTableMode mode_ = DistributedTableMode::SPLIT_BY_DEVICE; 96 ChangedData data_; 97 }; 98 } 99 #endif // RELATIONAL_SYNC_DATA_INSERTER_H