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 #define MLOG_TAG "Media_Upgrade"
16
17 #include <string>
18 #include <vector>
19
20 #include "photo_map_table_event_handler.h"
21
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 #include "medialibrary_rdbstore.h"
28 #include "media_file_utils.h"
29 #include "medialibrary_db_const.h"
30
31 namespace OHOS::Media {
32 /**
33 * @brief execute sql while database created
34 * @param store rdb store
35 */
OnCreate(std::shared_ptr<MediaLibraryRdbStore> store)36 int32_t PhotoMapTableEventHandler::OnCreate(std::shared_ptr<MediaLibraryRdbStore> store)
37 {
38 return NativeRdb::E_OK;
39 }
40
41 /**
42 * @brief execute sql while database upgraded
43 * @param store rdb store
44 */
OnUpgrade(std::shared_ptr<MediaLibraryRdbStore> store,int32_t oldVersion,int32_t newVersion)45 int32_t PhotoMapTableEventHandler::OnUpgrade(
46 std::shared_ptr<MediaLibraryRdbStore> store, int32_t oldVersion, int32_t newVersion)
47 {
48 int32_t ret = NativeRdb::E_OK;
49 int64_t startTime = MediaFileUtils::UTCTimeMilliSeconds();
50 for (auto &it : this->upgradeHandles_) {
51 ret = (this->*it)(store, oldVersion, newVersion);
52 }
53 int64_t endTime = MediaFileUtils::UTCTimeMilliSeconds();
54 int64_t costTime = endTime - startTime;
55 MEDIA_INFO_LOG(
56 "OnUpgrade end upgrade %{public}s table, costTime: %{public}" PRId64 "ms.", PhotoMap::TABLE.c_str(), costTime);
57 return ret;
58 }
59
DropAllTriggers(std::shared_ptr<MediaLibraryRdbStore> store,int32_t oldVersion,int32_t newVersion)60 int32_t PhotoMapTableEventHandler::DropAllTriggers(
61 std::shared_ptr<MediaLibraryRdbStore> store, int32_t oldVersion, int32_t newVersion)
62 {
63 bool conn = oldVersion < VERSION_CLOUD_MEDIA_UPGRADE;
64 CHECK_AND_RETURN_RET(conn, NativeRdb::E_OK);
65 CHECK_AND_RETURN_RET_LOG(store != nullptr, -1, "store is null");
66 int64_t startTime = MediaFileUtils::UTCTimeMilliSeconds();
67 const std::vector<std::string> sqls = {
68 SQL_DROP_NEW_TRIGGER,
69 SQL_DROP_DELETE_TRIGGER,
70 SQL_DROP_INSERT_SEARCH_TRIGGER,
71 SQL_DROP_DELETE_SEARCH_TRIGGER,
72 };
73 int32_t ret = NativeRdb::E_OK;
74 for (auto &sql : sqls) {
75 ret = store->ExecuteSql(sql);
76 CHECK_AND_CONTINUE_ERR_LOG(ret == NativeRdb::E_OK, "execute sql failed");
77 }
78 int64_t endTime = MediaFileUtils::UTCTimeMilliSeconds();
79 int64_t costTime = endTime - startTime;
80 MEDIA_INFO_LOG("DropAllTriggers in %{public}s table, ret: %{public}d, costTime: %{public}" PRId64 "ms.",
81 PhotoMap::TABLE.c_str(),
82 ret,
83 costTime);
84 return ret;
85 }
86 } // namespace OHOS::Media