• 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 
16 #include "sqlite_log_table_manager.h"
17 
18 namespace DistributedDB {
AddRelationalLogTableTrigger(sqlite3 * db,const TableInfo & table,const std::string & identity)19 int SqliteLogTableManager::AddRelationalLogTableTrigger(sqlite3 *db, const TableInfo &table,
20     const std::string &identity)
21 {
22     std::vector<std::string> sqls = { GetInsertTrigger(table, identity), GetUpdateTrigger(table, identity),
23         GetDeleteTrigger(table, identity) };
24     // add insert,update,delete trigger
25     for (const auto &sql : sqls) {
26         int errCode = SQLiteUtils::ExecuteRawSQL(db, sql);
27         if (errCode != E_OK) {
28             LOGE("[LogTableManager] execute create log trigger sql failed, errCode=%d", errCode);
29             return errCode;
30         }
31     }
32     return E_OK;
33 }
34 
CreateRelationalLogTable(sqlite3 * db,const TableInfo & table)35 int SqliteLogTableManager::CreateRelationalLogTable(sqlite3 *db, const TableInfo &table)
36 {
37     const std::string tableName = GetLogTableName(table);
38     std::string primaryKey = GetPrimaryKeySql(table);
39 
40     std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + "(" \
41         "data_key    INT NOT NULL," \
42         "device      BLOB," \
43         "ori_device  BLOB," \
44         "timestamp   INT  NOT NULL," \
45         "wtimestamp  INT  NOT NULL," \
46         "flag        INT  NOT NULL," \
47         "hash_key    BLOB NOT NULL," + primaryKey + ");";
48     std::vector<std::string> logTableSchema;
49     logTableSchema.emplace_back(createTableSql);
50     GetIndexSql(table, logTableSchema);
51 
52     for (const auto &sql : logTableSchema) {
53         int errCode = SQLiteUtils::ExecuteRawSQL(db, sql);
54         if (errCode != E_OK) {
55             LOGE("[LogTableManager] execute create log table schema failed, errCode=%d", errCode);
56             return errCode;
57         }
58     }
59     return E_OK;
60 }
61 
GetIndexSql(const TableInfo & table,std::vector<std::string> & schema)62 void SqliteLogTableManager::GetIndexSql(const TableInfo &table, std::vector<std::string> &schema)
63 {
64     const std::string tableName = GetLogTableName(table);
65 
66     std::string indexTimestampFlag = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX +
67         "time_flag_index ON " + tableName + "(timestamp, flag);";
68     schema.emplace_back(indexTimestampFlag);
69 
70     std::string indexHashkey = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX +
71         "hashkey_index ON " + tableName + "(hash_key);";
72     schema.emplace_back(indexHashkey);
73 }
74 
GetLogTableName(const TableInfo & table) const75 std::string SqliteLogTableManager::GetLogTableName(const TableInfo &table) const
76 {
77     return DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
78 }
79 }