• 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," \
48         "cloud_gid   TEXT," +
49         primaryKey + ");";
50     std::vector<std::string> logTableSchema;
51     logTableSchema.emplace_back(createTableSql);
52     GetIndexSql(table, logTableSchema);
53 
54     for (const auto &sql : logTableSchema) {
55         int errCode = SQLiteUtils::ExecuteRawSQL(db, sql);
56         if (errCode != E_OK) {
57             LOGE("[LogTableManager] execute create log table schema failed, errCode=%d", errCode);
58             return errCode;
59         }
60     }
61     return E_OK;
62 }
63 
GetIndexSql(const TableInfo & table,std::vector<std::string> & schema)64 void SqliteLogTableManager::GetIndexSql(const TableInfo &table, std::vector<std::string> &schema)
65 {
66     const std::string tableName = GetLogTableName(table);
67 
68     std::string indexTimestampFlag = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX +
69         "time_flag_index ON " + tableName + "(timestamp, flag);";
70     schema.emplace_back(indexTimestampFlag);
71 
72     std::string indexHashkey = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX +
73         "hashkey_index ON " + tableName + "(hash_key);";
74     schema.emplace_back(indexHashkey);
75 }
76 
GetLogTableName(const TableInfo & table) const77 std::string SqliteLogTableManager::GetLogTableName(const TableInfo &table) const
78 {
79     return DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
80 }
81 }