1 /*
2  * Copyright (c) 2022 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_LOG_TABLE_MANAGER_H
16 #define SQLITE_LOG_TABLE_MANAGER_H
17 #include <mutex>
18 
19 #include "relational_schema_object.h"
20 #include "sqlite_utils.h"
21 
22 namespace DistributedDB {
23 class SqliteLogTableManager {
24 public:
25     SqliteLogTableManager() = default;
26     virtual ~SqliteLogTableManager() = default;
27 
28     virtual std::string CalcPrimaryKeyHash(const std::string &references, const TableInfo &table,
29         const std::string &identity) = 0;
30 
31     // The parameter "identity" is a hash string that identifies a device
32     int AddRelationalLogTableTrigger(sqlite3 *db, const TableInfo &table, const std::string &identity);
33 
34     int CreateRelationalLogTable(sqlite3 *db, const TableInfo &table);
35 
36     static int CreateKvSyncLogTable(sqlite3 *db);
37 protected:
38     virtual void GetIndexSql(const TableInfo &table, std::vector<std::string> &schema);
39     std::string GetLogTableName(const TableInfo &table) const;
40 
41 private:
42     virtual std::string GetInsertTrigger(const TableInfo &table, const std::string &identity) = 0;
43     virtual std::string GetUpdateTrigger(const TableInfo &table, const std::string &identity) = 0;
44     virtual std::string GetDeleteTrigger(const TableInfo &table, const std::string &identity) = 0;
45     virtual std::vector<std::string> GetDropTriggers(const TableInfo &table) = 0;
46 
47     virtual std::string GetPrimaryKeySql(const TableInfo &table) = 0;
48 
49     static int UpgradeKvSyncLogTable(const std::string &tableName, sqlite3 *db);
50     static int CreateKvCloudFlagIndex(const std::string &tableName, sqlite3 *db);
51 };
52 }
53 #endif // SQLITE_LOG_TABLE_MANAGER_H