1 /*
2 * Copyright (C) 2024 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 #include "db_upgrade_utils.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "rdb_store.h"
21 #include "media_log.h"
22 #include "result_set_utils.h"
23
24 namespace OHOS::Media {
25 namespace DataTransfer {
26 /**
27 * @brief Check table exists or not.
28 */
IsTableExists(NativeRdb::RdbStore & store,const std::string & tableName)29 bool DbUpgradeUtils::IsTableExists(NativeRdb::RdbStore &store, const std::string &tableName)
30 {
31 std::string querySql = this->SQL_SQLITE_MASTER_QUERY;
32 std::vector<NativeRdb::ValueObject> bindArgs = {tableName};
33 auto resultSet = store.QuerySql(querySql, bindArgs);
34 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr, false,
35 "Query resultSql is null. tableName: %{public}s is not exists.", tableName.c_str());
36 int rowCount = 0;
37 bool isExists = !resultSet->GetRowCount(rowCount) && rowCount > 0;
38 MEDIA_INFO_LOG("Media_Restore: tableName=%{public}s, isExists:%{public}d", tableName.c_str(), isExists);
39 resultSet->Close();
40 return isExists;
41 }
42
43 /**
44 * @brief Check table column exists or not.
45 */
IsColumnExists(NativeRdb::RdbStore & store,const std::string & tableName,const std::string & columnName)46 bool DbUpgradeUtils::IsColumnExists(
47 NativeRdb::RdbStore &store, const std::string &tableName, const std::string &columnName)
48 {
49 std::string querySql = this->SQL_PRAGMA_TABLE_INFO_QUERY;
50 std::vector<NativeRdb::ValueObject> bindArgs = {tableName, columnName};
51 auto resultSet = store.QuerySql(querySql, bindArgs);
52 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr, false,
53 "Query resultSql is null. tableName: %{public}s, columnName: %{public}s is not exists.",
54 tableName.c_str(), columnName.c_str());
55 int rowCount = 0;
56 bool isExists = !resultSet->GetRowCount(rowCount) && rowCount > 0;
57 MEDIA_INFO_LOG("Media_Restore: tableName=%{public}s, columnName=%{public}s, isExists:%{public}d",
58 tableName.c_str(),
59 columnName.c_str(),
60 isExists);
61 resultSet->Close();
62 return isExists;
63 }
64
GetAllTriggers(NativeRdb::RdbStore & store,const std::string & tableName)65 std::vector<std::string> DbUpgradeUtils::GetAllTriggers(NativeRdb::RdbStore &store, const std::string &tableName)
66 {
67 std::string querySql = this->SQL_SQLITE_MASTER_QUERY_TRIGGER;
68 std::vector<NativeRdb::ValueObject> bindArgs = {tableName};
69 auto resultSet = store.QuerySql(querySql, bindArgs);
70 std::vector<std::string> result;
71 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr, result,
72 "Query resultSql is null. tableName: %{public}s does not have any triggers.", tableName.c_str());
73 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
74 std::string triggerName = GetStringVal("name", resultSet);
75 result.emplace_back(triggerName);
76 }
77 resultSet->Close();
78 return result;
79 }
80
DropAllTriggers(NativeRdb::RdbStore & store,const std::string & tableName)81 int32_t DbUpgradeUtils::DropAllTriggers(NativeRdb::RdbStore &store, const std::string &tableName)
82 {
83 const std::vector<std::string> triggerNames = this->GetAllTriggers(store, tableName);
84 std::string prefix = "DROP TRIGGER IF EXISTS ";
85 for (auto &triggerName : triggerNames) {
86 std::string deleteTriggerSql = prefix + triggerName + ";";
87 int32_t ret = store.ExecuteSql(deleteTriggerSql);
88 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK,
89 "Media_Restore: Drop trigger failed, triggerName=%{public}s", triggerName.c_str());
90 }
91 return NativeRdb::E_OK;
92 }
GetAllUniqueIndex(NativeRdb::RdbStore & store,const std::string & tableName)93 std::vector<std::string> DbUpgradeUtils::GetAllUniqueIndex(NativeRdb::RdbStore &store, const std::string &tableName)
94 {
95 std::string querySql = this->SQL_SQLITE_MASTER_QUERY_UNIQUE_INDEX;
96 std::vector<NativeRdb::ValueObject> bindArgs = {tableName};
97 auto resultSet = store.QuerySql(querySql, bindArgs);
98 std::vector<std::string> result;
99 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr, result,
100 "resultSet is null. tableName: %{public}s does not have any unique index.", tableName.c_str());
101 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
102 std::string uniqueIndexName = GetStringVal("name", resultSet);
103 result.emplace_back(uniqueIndexName);
104 }
105 resultSet->Close();
106 return result;
107 }
108
DropAllUniqueIndex(NativeRdb::RdbStore & store,const std::string & tableName)109 int32_t DbUpgradeUtils::DropAllUniqueIndex(NativeRdb::RdbStore &store, const std::string &tableName)
110 {
111 const std::vector<std::string> uniqueIndexNames = this->GetAllUniqueIndex(store, tableName);
112 std::string prefix = "DROP INDEX IF EXISTS ";
113 for (auto &indexName : uniqueIndexNames) {
114 std::string deleteIndexSql = prefix + indexName + ";";
115 int32_t ret = store.ExecuteSql(deleteIndexSql);
116 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK,
117 "Media_Restore: Drop trigger failed, indexName=%{public}s", indexName.c_str());
118 }
119 return NativeRdb::E_OK;
120 }
121 } // namespace DataTransfer
122 } // namespace OHOS::Media