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 *saveDataStmt = nullptr;
28     sqlite3_stmt *saveLogStmt = nullptr;
29     sqlite3_stmt *queryStmt = nullptr;
30     sqlite3_stmt *rmDataStmt = nullptr;
31     sqlite3_stmt *rmLogStmt = nullptr;
SaveSyncDataStmtSaveSyncDataStmt32     SaveSyncDataStmt() {}
~SaveSyncDataStmtSaveSyncDataStmt33     ~SaveSyncDataStmt()
34     {
35         ResetStatements(true);
36     }
37 
38     int ResetStatements(bool isNeedFinalize);
39 };
40 
41 class RelationalSyncDataInserter {
42 public:
43     RelationalSyncDataInserter();
44     ~RelationalSyncDataInserter();
45 
46     static RelationalSyncDataInserter CreateInserter(const std::string &deviceName, const QueryObject &query,
47         const RelationalSchemaObject &localSchema, const std::vector<FieldInfo> &remoteFields,
48         const StoreInfo &info);
49 
50     void SetHashDevId(const std::string &hashDevId);
51     // Set remote fields in cid order
52     void SetRemoteFields(std::vector<FieldInfo> remoteFields);
53     void SetEntries(std::vector<DataItem> entries);
54 
55     void SetLocalTable(TableInfo localTable);
56     const TableInfo &GetLocalTable() const;
57 
58     void SetQuery(QueryObject query);
59     void SetInsertTableName(std::string tableName);
60 
61     void SetTableMode(DistributedTableMode mode);
62 
63     int Iterate(const std::function<int (DataItem &)> &);
64     int BindInsertStatement(sqlite3_stmt *stmt, const DataItem &dataItem);
65 
66     int PrepareStatement(sqlite3 *db, SaveSyncDataStmt &stmt);
67     int GetDeleteLogStmt(sqlite3 *db, sqlite3_stmt *&stmt);
68     int GetDeleteSyncDataStmt(sqlite3 *db, sqlite3_stmt *&stmt);
69 
70 private:
71 
72     int GetInsertStatement(sqlite3 *db, sqlite3_stmt *&stmt);
73 
74     int GetSaveLogStatement(sqlite3 *db, sqlite3_stmt *&logStmt, sqlite3_stmt *&queryStmt);
75 
76     std::string hashDevId_;
77     std::vector<FieldInfo> remoteFields_;
78     std::vector<DataItem> entries_;
79     TableInfo localTable_;
80     QueryObject query_;
81     std::string insertTableName_; // table name to save sync data
82     DistributedTableMode mode_ = DistributedTableMode::SPLIT_BY_DEVICE;
83 };
84 }
85 #endif // RELATIONAL_SYNC_DATA_INSERTER_H