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
16 #define MLOG_TAG "TabOldPhotosEventHandler"
17
18 #include "tab_old_photos_table_event_handler.h"
19
20 #include <string>
21
22 #include "rdb_store.h"
23 #include "rdb_errno.h"
24 #include "media_log.h"
25
26 namespace OHOS::Media {
27 /**
28 * @brief execute sql while database is created
29 * @param store: rdb store
30 */
OnCreate(NativeRdb::RdbStore & store)31 int32_t TabOldPhotosTableEventHandler::OnCreate(NativeRdb::RdbStore &store)
32 {
33 MEDIA_INFO_LOG("OnCreate begin create %{public}s table", this->TABLE_NAME.c_str());
34 int32_t ret = this->CreateTable(store);
35 if (ret != NativeRdb::E_OK) {
36 return NativeRdb::E_ERROR;
37 }
38 this->CreateTrigger(store);
39 this->CreateIndex(store);
40 MEDIA_INFO_LOG("OnCreate end create %{public}s table", this->TABLE_NAME.c_str());
41 return NativeRdb::E_OK;
42 }
43
44 /**
45 * @brief execute sql while database is upgraded
46 * @param store: rdb store
47 */
OnUpgrade(NativeRdb::RdbStore & store,int oldVersion,int newVersion)48 int32_t TabOldPhotosTableEventHandler::OnUpgrade(NativeRdb::RdbStore &store, int oldVersion, int newVersion)
49 {
50 return 0;
51 }
52
CreateTable(NativeRdb::RdbStore & store)53 int32_t TabOldPhotosTableEventHandler::CreateTable(NativeRdb::RdbStore &store)
54 {
55 int32_t ret = store.ExecuteSql(this->CREATE_TABLE_SQL);
56 if (ret != NativeRdb::E_OK) {
57 MEDIA_ERR_LOG("Media_Library: CreateTable failed, ret=%{public}d, sql=%{public}s", ret,
58 this->CREATE_TABLE_SQL.c_str());
59 return NativeRdb::E_ERROR;
60 }
61 return NativeRdb::E_OK;
62 }
63
CreateIndex(NativeRdb::RdbStore & store)64 int32_t TabOldPhotosTableEventHandler::CreateIndex(NativeRdb::RdbStore &store)
65 {
66 for (const std::string &sql : this->CREATE_INDEX_SQLS) {
67 int32_t ret = store.ExecuteSql(sql);
68 if (ret != NativeRdb::E_OK) {
69 MEDIA_ERR_LOG("Media_Library: CreateIndex failed, ret=%{public}d, sql=%{public}s", ret, sql.c_str());
70 return NativeRdb::E_ERROR;
71 }
72 }
73 return NativeRdb::E_OK;
74 }
75
CreateTrigger(NativeRdb::RdbStore & store)76 int32_t TabOldPhotosTableEventHandler::CreateTrigger(NativeRdb::RdbStore &store)
77 {
78 std::string sql = this->TRIGGER_DELETE_CLEAR_TAB_OLD_PHOTOS;
79 int32_t ret = store.ExecuteSql(sql);
80 if (ret != NativeRdb::E_OK) {
81 MEDIA_ERR_LOG("Media_Library: CreateTrigger failed, ret=%{public}d, sql=%{public}s", ret, sql.c_str());
82 return NativeRdb::E_ERROR;
83 }
84 return NativeRdb::E_OK;
85 }
86 } // namespace OHOS::Media