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
16 #define MLOG_TAG "TabFaCardPhotosEventHandler"
17
18 #include "tab_facard_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 TabFaCardPhotosTableEventHandler::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->CreateIndex(store);
39 MEDIA_INFO_LOG("OnCreate end create %{public}s table", this->TABLE_NAME.c_str());
40 return NativeRdb::E_OK;
41 }
42
43 /**
44 * @brief execute sql while database is upgraded
45 * @param store: rdb store
46 */
OnUpgrade(NativeRdb::RdbStore & store,int oldVersion,int newVersion)47 int32_t TabFaCardPhotosTableEventHandler::OnUpgrade(NativeRdb::RdbStore &store, int oldVersion, int newVersion)
48 {
49 return 0;
50 }
51
CreateTable(NativeRdb::RdbStore & store)52 int32_t TabFaCardPhotosTableEventHandler::CreateTable(NativeRdb::RdbStore &store)
53 {
54 int32_t ret = store.ExecuteSql(this->CREATE_TABLE_SQL);
55 if (ret != NativeRdb::E_OK) {
56 MEDIA_ERR_LOG("Media_Library: CreateTable failed, ret=%{public}d, sql=%{public}s", ret,
57 this->CREATE_TABLE_SQL.c_str());
58 return NativeRdb::E_ERROR;
59 }
60 return NativeRdb::E_OK;
61 }
62
CreateIndex(NativeRdb::RdbStore & store)63 int32_t TabFaCardPhotosTableEventHandler::CreateIndex(NativeRdb::RdbStore &store)
64 {
65 for (const std::string &sql : this->CREATE_INDEX_SQLS) {
66 int32_t ret = store.ExecuteSql(sql);
67 if (ret != NativeRdb::E_OK) {
68 MEDIA_ERR_LOG("Media_Library: CreateIndex failed, ret=%{public}d, sql=%{public}s", ret, sql.c_str());
69 return NativeRdb::E_ERROR;
70 }
71 }
72 return NativeRdb::E_OK;
73 }
74 } // namespace OHOS::Media