1 /*
2 * Copyright (c) 2021 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 "collaboration_log_table_manager.h"
17
18 namespace DistributedDB {
IsCollaborationWithoutKey(const TableInfo & table)19 bool CollaborationLogTableManager::IsCollaborationWithoutKey(const TableInfo &table)
20 {
21 return ((table.GetIdentifyKey().size() == 1u && table.GetIdentifyKey().at(0) == "rowid") ||
22 table.GetAutoIncrement());
23 }
24
CalcPrimaryKeyHash(const std::string & references,const TableInfo & table,const std::string & identity)25 std::string CollaborationLogTableManager::CalcPrimaryKeyHash(const std::string &references, const TableInfo &table,
26 const std::string &identity)
27 {
28 std::string sql;
29 if (IsCollaborationWithoutKey(table)) {
30 sql = "calc_hash('" + identity + "'||calc_hash(" + references + "rowid))";
31 } else {
32 if (table.GetIdentifyKey().size() == 1) {
33 sql = "calc_hash(" + references + table.GetIdentifyKey().at(0) + ")";
34 } else {
35 sql = "calc_hash(";
36 for (const auto &it : table.GetIdentifyKey()) {
37 sql += "calc_hash(" + references + it + ")||";
38 }
39 sql.pop_back();
40 sql.pop_back();
41 sql += ")";
42 }
43 }
44 return sql;
45 }
46
GetInsertTrigger(const TableInfo & table,const std::string & identity)47 std::string CollaborationLogTableManager::GetInsertTrigger(const TableInfo &table, const std::string &identity)
48 {
49 std::string logTblName = DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
50 std::string insertTrigger = "CREATE TRIGGER IF NOT EXISTS ";
51 insertTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_INSERT AFTER INSERT \n";
52 insertTrigger += "ON " + table.GetTableName() + "\n";
53 insertTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata ";
54 insertTrigger += "WHERE key = 'log_trigger_switch' AND value = 'true')\n";
55 insertTrigger += "BEGIN\n";
56 insertTrigger += "\t INSERT OR REPLACE INTO " + logTblName;
57 insertTrigger += " (data_key, device, ori_device, timestamp, wtimestamp, flag, hash_key)";
58 insertTrigger += " VALUES (new.rowid, '', '',";
59 insertTrigger += " get_sys_time(0), get_sys_time(0),";
60 insertTrigger += " CASE WHEN (SELECT count(*)<>0 FROM " + logTblName + " WHERE hash_key=" +
61 CalcPrimaryKeyHash("NEW.", table, identity) + " AND flag&0x02=0x02) THEN 0x22 ELSE 0x02 END,";
62 insertTrigger += CalcPrimaryKeyHash("NEW.", table, identity) + ");\n";
63 insertTrigger += "END;";
64 return insertTrigger;
65 }
66
GetUpdateTrigger(const TableInfo & table,const std::string & identity)67 std::string CollaborationLogTableManager::GetUpdateTrigger(const TableInfo &table, const std::string &identity)
68 {
69 std::string logTblName = DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
70 std::string updateTrigger = "CREATE TRIGGER IF NOT EXISTS ";
71 updateTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_UPDATE AFTER UPDATE \n";
72 updateTrigger += "ON " + table.GetTableName() + "\n";
73 updateTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata ";
74 updateTrigger += "WHERE key = 'log_trigger_switch' AND value = 'true')\n";
75 updateTrigger += "BEGIN\n";
76 if (table.GetIdentifyKey().size() == 1 && table.GetIdentifyKey().at(0) == "rowid") {
77 updateTrigger += "\t UPDATE " + DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
78 updateTrigger += " SET timestamp=get_sys_time(0), device='', flag=0x22";
79 updateTrigger += " WHERE data_key = OLD.rowid;";
80 } else {
81 updateTrigger += "\t UPDATE " + logTblName;
82 updateTrigger += " SET data_key=-1, timestamp=get_sys_time(0), device='', flag=0x03";
83 updateTrigger += " WHERE data_key = OLD.rowid;\n";
84 updateTrigger += "\t INSERT OR REPLACE INTO " + logTblName + " VALUES (NEW.rowid, '', '', get_sys_time(0), "
85 "get_sys_time(0), CASE WHEN (" + CalcPrimaryKeyHash("NEW.", table, identity) + " != " +
86 CalcPrimaryKeyHash("NEW.", table, identity) + ") THEN 0x02 ELSE 0x22 END, " +
87 CalcPrimaryKeyHash("NEW.", table, identity) + ");\n";
88 }
89 updateTrigger += "END;";
90 return updateTrigger;
91 }
92
GetDeleteTrigger(const TableInfo & table,const std::string & identity)93 std::string CollaborationLogTableManager::GetDeleteTrigger(const TableInfo &table, const std::string &identity)
94 {
95 (void)identity;
96 std::string deleteTrigger = "CREATE TRIGGER IF NOT EXISTS ";
97 deleteTrigger += "naturalbase_rdb_" + table.GetTableName() + "_ON_DELETE BEFORE DELETE \n";
98 deleteTrigger += "ON " + table.GetTableName() + "\n";
99 deleteTrigger += "WHEN (SELECT count(*) from " + DBConstant::RELATIONAL_PREFIX + "metadata ";
100 deleteTrigger += "WHERE key = 'log_trigger_switch' AND VALUE = 'true')\n";
101 deleteTrigger += "BEGIN\n";
102 deleteTrigger += "\t UPDATE " + DBConstant::RELATIONAL_PREFIX + table.GetTableName() + "_log";
103 deleteTrigger += " SET data_key=-1,flag=0x03,timestamp=get_sys_time(0)";
104 deleteTrigger += " WHERE data_key = OLD.rowid;";
105 deleteTrigger += "END;";
106 return deleteTrigger;
107 }
108
GetPrimaryKeySql(const TableInfo & table)109 std::string CollaborationLogTableManager::GetPrimaryKeySql(const TableInfo &table)
110 {
111 return "PRIMARY KEY(hash_key)";
112 }
113
GetIndexSql(const TableInfo & table,std::vector<std::string> & schema)114 void CollaborationLogTableManager::GetIndexSql(const TableInfo &table, std::vector<std::string> &schema)
115 {
116 SqliteLogTableManager::GetIndexSql(table, schema);
117 std::string dataKeyIndex = "CREATE INDEX IF NOT EXISTS " + DBConstant::RELATIONAL_PREFIX + "datakey_index ON " +
118 GetLogTableName(table) + "(data_key);";
119 schema.emplace_back(dataKeyIndex);
120 }
121 }