1 /*
2 * Copyright (C) 2025 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 "database_utils.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "media_log.h"
21 #include "result_set_utils.h"
22 #include "media_file_utils.h"
23
24 namespace OHOS::Media {
25 /**
26 * @brief Check table column exists or not.
27 */
IsColumnExists(std::shared_ptr<MediaLibraryRdbStore> store,const std::string & tableName,const std::string & columnName)28 bool DatabaseUtils::IsColumnExists(
29 std::shared_ptr<MediaLibraryRdbStore> store, const std::string &tableName, const std::string &columnName)
30 {
31 CHECK_AND_RETURN_RET_LOG(store != nullptr, false, "store is null");
32 std::string querySql = this->SQL_PRAGMA_TABLE_INFO_QUERY;
33 std::vector<NativeRdb::ValueObject> bindArgs = {tableName, columnName};
34 int64_t startTime = MediaFileUtils::UTCTimeMilliSeconds();
35 auto resultSet = store->QuerySql(querySql, bindArgs);
36 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr,
37 false,
38 "Query resultSql is null. tableName: %{public}s, columnName: %{public}s is not exists.",
39 tableName.c_str(),
40 columnName.c_str());
41 int rowCount = 0;
42 bool isExists = !resultSet->GetRowCount(rowCount) && rowCount > 0;
43 int64_t endTime = MediaFileUtils::UTCTimeMilliSeconds();
44 int64_t costTime = endTime - startTime;
45 MEDIA_INFO_LOG("tableName=%{public}s, columnName=%{public}s, isExists:%{public}d, costTime:%{public}" PRId64,
46 tableName.c_str(),
47 columnName.c_str(),
48 isExists,
49 costTime);
50 return isExists;
51 }
52
GetAllTriggers(std::shared_ptr<MediaLibraryRdbStore> store,const std::string & tableName)53 std::vector<std::string> DatabaseUtils::GetAllTriggers(
54 std::shared_ptr<MediaLibraryRdbStore> store, const std::string &tableName)
55 {
56 std::string querySql = this->SQL_SQLITE_MASTER_QUERY_TRIGGER;
57 std::vector<NativeRdb::ValueObject> bindArgs = {tableName};
58 auto resultSet = store->QuerySql(querySql, bindArgs);
59 std::vector<std::string> result;
60 CHECK_AND_RETURN_RET_WARN_LOG(resultSet != nullptr,
61 result,
62 "Query resultSql is null. tableName: %{public}s does not have any triggers.",
63 tableName.c_str());
64 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
65 std::string triggerName = GetStringVal("name", resultSet);
66 result.emplace_back(triggerName);
67 }
68 return result;
69 }
70
IsTriggerExists(std::shared_ptr<MediaLibraryRdbStore> store,const std::string & tableName,const std::string & triggerName)71 bool DatabaseUtils::IsTriggerExists(
72 std::shared_ptr<MediaLibraryRdbStore> store, const std::string &tableName, const std::string &triggerName)
73 {
74 std::vector<std::string> triggers = this->GetAllTriggers(store, tableName);
75 return std::find(triggers.begin(), triggers.end(), triggerName) != triggers.end();
76 }
77 } // namespace OHOS::Media