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