1 /*
2 * Copyright (C) 2024-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 #define MLOG_TAG "AlbumPluginTableEventHandler"
16
17 #include <string>
18 #include <vector>
19
20 #include "album_plugin_table_event_handler.h"
21 #include "album_plugin_config.h"
22 #include "rdb_store.h"
23 #include "rdb_errno.h"
24 #include "result_set_utils.h"
25 #include "media_log.h"
26 #include "medialibrary_rdb_transaction.h"
27
28 namespace OHOS::Media {
29
IsTableCreated(NativeRdb::RdbStore & store,const std::string & tableName)30 bool AlbumPluginTableEventHandler::IsTableCreated(NativeRdb::RdbStore &store, const std::string &tableName)
31 {
32 std::string sql = "SELECT COUNT(1) AS count FROM sqlite_master WHERE type='table' AND name= ?;";
33 const std::vector<NativeRdb::ValueObject> params = {NativeRdb::ValueObject(tableName)};
34 auto resultSet = store.QuerySql(sql, params);
35 if (resultSet == nullptr) {
36 MEDIA_ERR_LOG("resultSet is null!");
37 return 0;
38 }
39 if (resultSet->GoToFirstRow() != NativeRdb::E_OK) {
40 MEDIA_ERR_LOG("go to first row failed");
41 return 0;
42 }
43 int32_t count = get<int32_t>(ResultSetUtils::GetValFromColumn("count", resultSet, TYPE_INT32));
44 if (count < 0) {
45 MEDIA_ERR_LOG(
46 "Check sqlite_master count error, tableName: %{public}s, count is %{public}d", tableName.c_str(), count);
47 return 0;
48 }
49 return count > 0;
50 }
51
InitiateData(NativeRdb::RdbStore & store)52 int32_t AlbumPluginTableEventHandler::InitiateData(NativeRdb::RdbStore &store)
53 {
54 int32_t err = NativeRdb::E_OK;
55 MEDIA_INFO_LOG("InitiateData begin initiate %{public}s table data.", TABLE_NAME.c_str());
56 auto [errCode, trans] = store.CreateTransaction(OHOS::NativeRdb::Transaction::DEFERRED);
57 if (errCode != NativeRdb::E_OK || trans == nullptr) {
58 MEDIA_ERR_LOG("transaction failed, err:%{public}d", errCode);
59 return errCode;
60 }
61 for (const AlbumPlugin::AlbumPluginRowData &data : AlbumPlugin::ALBUM_PLUGIN_DATA) {
62 std::vector<NativeRdb::ValueObject> bindArgs = {
63 data.lpath,
64 data.album_name,
65 data.album_name_en,
66 data.bundle_name,
67 data.cloud_id,
68 data.dual_album_name,
69 data.priority
70 };
71 auto res = trans->Execute(this->INSERT_DATA_SQL, bindArgs);
72 err = res.first;
73 if (err != NativeRdb::E_OK) {
74 trans->Rollback();
75 return err;
76 }
77 }
78 err = trans->Commit();
79 if (err != NativeRdb::E_OK) {
80 MEDIA_ERR_LOG("InitiateData tans finish fail!, ret:%{public}d", err);
81 }
82 MEDIA_INFO_LOG("InitiateData end initiate %{public}s table data %{public}d.",
83 TABLE_NAME.c_str(),
84 static_cast<int32_t>(AlbumPlugin::ALBUM_PLUGIN_DATA.size()));
85 return NativeRdb::E_OK;
86 }
87
GetAlbumPluginDataCount(NativeRdb::RdbStore & store)88 int32_t AlbumPluginTableEventHandler::GetAlbumPluginDataCount(NativeRdb::RdbStore &store)
89 {
90 std::string querySql = this->SQL_SELECT_DATA_COUNT;
91 auto resultSet = store.QuerySql(querySql);
92 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
93 MEDIA_WARN_LOG("resultSet is null! querySql = %{public}s", querySql.c_str());
94 return 0;
95 }
96 return GetInt32Val("count", resultSet);
97 }
98
99 /**
100 * @brief execute sql while database created
101 * @param store rdb store
102 */
OnCreate(NativeRdb::RdbStore & store)103 int32_t AlbumPluginTableEventHandler::OnCreate(NativeRdb::RdbStore &store)
104 {
105 MEDIA_INFO_LOG("OnCreate begin create %{public}s table.", TABLE_NAME.c_str());
106 if (store.ExecuteSql(this->CREATE_TABLE_SQL) != NativeRdb::E_OK) {
107 return NativeRdb::E_ERROR;
108 }
109 if (InitiateData(store) != NativeRdb::E_OK) {
110 return NativeRdb::E_ERROR;
111 }
112 MEDIA_INFO_LOG("OnCreate end create %{public}s table.", TABLE_NAME.c_str());
113 return NativeRdb::E_OK;
114 }
115
116 /**
117 * @brief execute sql while database upgraded
118 * @param store rdb store
119 */
OnUpgrade(NativeRdb::RdbStore & store,int oldVersion,int newVersion)120 int32_t AlbumPluginTableEventHandler::OnUpgrade(NativeRdb::RdbStore &store, int oldVersion, int newVersion)
121 {
122 MEDIA_INFO_LOG("OnUpgrade begin upgrade %{public}s table.", TABLE_NAME.c_str());
123 // if table is exists and has data, do not need to create again
124 if (this->IsTableCreated(store, TABLE_NAME)) {
125 int32_t count = this->GetAlbumPluginDataCount(store);
126 if (count > 0) {
127 MEDIA_INFO_LOG("OnUpgrade check table %{public}s is exists, and has data %{public}d, "
128 "no need to create again.",
129 TABLE_NAME.c_str(),
130 count);
131 return NativeRdb::E_OK;
132 }
133 }
134 int32_t ret = OnCreate(store);
135 MEDIA_INFO_LOG("OnUpgrade end upgrade %{public}s table.", TABLE_NAME.c_str());
136 return ret;
137 }
138 } // namespace OHOS::Media