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