• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38     void CheckAndCreateTrigger(sqlite3 *db, const TableInfo &table, const std::string &identity);
39 
40     std::string GetCreateRelationalLogTableSql(const TableInfo &table, const std::string &extendStr = "");
41 
42     virtual std::string GetConflictPkSql(const TableInfo &table);
43 
44     static std::string CalcPkHash(const std::string &references, const std::vector<std::string> &pk);
45 protected:
46     virtual void GetIndexSql(const TableInfo &table, std::vector<std::string> &schema);
47     std::string GetLogTableName(const TableInfo &table) const;
48 
49     virtual std::string GetUpdatePkTrigger(const TableInfo &table, const std::string &identity);
50 
51     static std::string GetUpdateTimestamp(const TableInfo &table, bool defaultNewTime);
52 
53     static std::string GetUpdateWithAssignSql(const TableInfo &table, const std::string &emptyValue,
54         const std::string &matchValue, const std::string &missMatchValue);
55 private:
56     virtual std::string GetInsertTrigger(const TableInfo &table, const std::string &identity) = 0;
57     virtual std::string GetUpdateTrigger(const TableInfo &table, const std::string &identity) = 0;
58     virtual std::string GetDeleteTrigger(const TableInfo &table, const std::string &identity) = 0;
59     virtual std::vector<std::string> GetDropTriggers(const TableInfo &table) = 0;
60 
61     virtual std::string GetPrimaryKeySql(const TableInfo &table) = 0;
62 
63     static int UpgradeKvSyncLogTable(const std::string &tableName, sqlite3 *db);
64     static int CreateKvCloudFlagIndex(const std::string &tableName, sqlite3 *db);
65 
66     static std::string GetUpdateSqlWhenFieldExceedsLimit(const std::vector<std::string> &syncFields,
67         const std::string &matchValue, const std::string &missMatchValue);
68 };
69 }
70 #endif // SQLITE_LOG_TABLE_MANAGER_H