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 "data_transformer.h" 21 #include "db_types.h" 22 #include "query_object.h" 23 #include "relational_schema_object.h" 24 25 namespace DistributedDB { 26 27 struct SaveSyncDataStmt { 28 sqlite3_stmt *insertDataStmt = nullptr; 29 sqlite3_stmt *updateDataStmt = nullptr; 30 sqlite3_stmt *saveLogStmt = nullptr; 31 sqlite3_stmt *queryStmt = nullptr; 32 sqlite3_stmt *rmDataStmt = nullptr; 33 sqlite3_stmt *rmLogStmt = nullptr; 34 sqlite3_stmt *queryByFieldStmt = nullptr; SaveSyncDataStmtSaveSyncDataStmt35 SaveSyncDataStmt() {} ~SaveSyncDataStmtSaveSyncDataStmt36 ~SaveSyncDataStmt() 37 { 38 ResetStatements(true); 39 } 40 41 int ResetStatements(bool isNeedFinalize); 42 }; 43 44 struct SchemaInfo { 45 const RelationalSchemaObject &localSchema; 46 const RelationalSchemaObject &trackerSchema; 47 }; 48 49 class RelationalSyncDataInserter { 50 public: 51 RelationalSyncDataInserter() = default; 52 ~RelationalSyncDataInserter() = default; 53 54 static RelationalSyncDataInserter CreateInserter(const std::string &deviceName, const QueryObject &query, 55 const SchemaInfo &schemaInfo, const std::vector<FieldInfo> &remoteFields, const StoreInfo &info); 56 57 void SetHashDevId(const std::string &hashDevId); 58 // Set remote fields in cid order 59 void SetRemoteFields(std::vector<FieldInfo> remoteFields); 60 void SetEntries(std::vector<DataItem> entries); 61 62 void SetLocalTable(TableInfo localTable); 63 const TableInfo &GetLocalTable() const; 64 65 void SetQuery(QueryObject query); 66 void SetInsertTableName(std::string tableName); 67 68 void SetTableMode(DistributedTableMode mode); 69 70 int Iterate(const std::function<int (DataItem &)> &); 71 72 int GetObserverDataByRowId(sqlite3 *db, int64_t rowid, ChangeType type); 73 74 int SaveData(bool isUpdate, const DataItem &dataItem, SaveSyncDataStmt &saveSyncDataStmt, 75 std::map<std::string, Type> &saveVals); 76 int BindSaveDataStatement(bool isExist, const DataItem &dataItem, const std::set<std::string> &filterSet, 77 sqlite3_stmt *stmt, std::map<std::string, Type> &saveVals); 78 79 int PrepareStatement(sqlite3 *db, SaveSyncDataStmt &stmt); 80 int GetDeleteLogStmt(sqlite3 *db, sqlite3_stmt *&stmt); 81 int GetDeleteSyncDataStmt(sqlite3 *db, sqlite3_stmt *&stmt); 82 83 int BindHashKeyAndDev(const DataItem &dataItem, sqlite3_stmt *stmt, int beginIndex); 84 85 int SaveSyncLog(sqlite3 *db, const DataItem &dataItem, const DeviceSyncSaveDataInfo &deviceSyncSaveDataInfo, 86 std::map<std::string, Type> &saveVals, SaveSyncDataStmt &saveStmt); 87 88 ChangedData &GetChangedData(); 89 90 std::vector<FieldInfo> GetRemoteFields() const; 91 private: 92 93 int GetInsertStatement(sqlite3 *db, sqlite3_stmt *&stmt); 94 95 int GetSaveLogStatement(sqlite3 *db, sqlite3_stmt *&logStmt, sqlite3_stmt *&queryStmt); 96 97 int GetUpdateStatement(sqlite3 *db, sqlite3_stmt *&stmt); 98 99 int GetQueryLogByFieldStmt(sqlite3 *db, sqlite3_stmt *&stmt); 100 int GetDbValueByRowId(sqlite3 *db, const std::vector<std::string> &fieldList, 101 const int64_t rowid, std::vector<Type> &values); 102 void BindExtendFieldOrRowid(sqlite3_stmt *&stmt, std::map<std::string, Type> &saveVals, int bindIndex); 103 104 std::string hashDevId_; 105 std::vector<FieldInfo> remoteFields_; 106 std::vector<DataItem> entries_; 107 TableInfo localTable_; 108 QueryObject query_; 109 std::string insertTableName_; // table name to save sync data 110 DistributedTableMode mode_ = DistributedTableMode::SPLIT_BY_DEVICE; 111 ChangedData data_; 112 }; 113 } 114 #endif // RELATIONAL_SYNC_DATA_INSERTER_H