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 #include "gallery_db_upgrade.h"
16
17 #include "rdb_store.h"
18 #include "album_plugin_table_event_handler.h"
19 #include "media_log.h"
20 #include "db_upgrade_utils.h"
21
22 namespace OHOS::Media {
23 namespace DataTransfer {
24 /**
25 * @brief Upgrade the database, before data restore or clone.
26 */
OnUpgrade(NativeRdb::RdbStore & store)27 int32_t GalleryDbUpgrade::OnUpgrade(NativeRdb::RdbStore &store)
28 {
29 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade start.");
30 AlbumPluginTableEventHandler handler;
31 int32_t ret = handler.OnUpgrade(store, 0, 0);
32 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade end, ret: %{public}d", ret);
33 this->AddPhotoQualityOfGalleryMedia(store);
34 return this->AddRelativeBucketIdOfGalleryAlbum(store);
35 }
36
37 /**
38 * @brief Add photo_quality of gallery_media table in gallery.db.
39 */
AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore & store)40 int32_t GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore &store)
41 {
42 if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "photo_quality")) {
43 return NativeRdb::E_OK;
44 }
45 std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_PHOTO_QUALITY;
46 int32_t ret = store.ExecuteSql(sql);
47 if (ret != NativeRdb::E_OK) {
48 MEDIA_ERR_LOG(
49 "Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia failed, ret=%{public}d, sql=%{public}s",
50 ret,
51 sql.c_str());
52 }
53 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia success");
54 return ret;
55 }
56
57 /**
58 * @brief Add relativeBucketId of gallery_album table in gallery.db if not exists.
59 */
AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore & store)60 int32_t GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore &store)
61 {
62 if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_album", "relativeBucketId")) {
63 return NativeRdb::E_OK;
64 }
65 std::string sql = this->SQL_GALLERY_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
66 int32_t ret = store.ExecuteSql(sql);
67 if (ret != NativeRdb::E_OK) {
68 MEDIA_ERR_LOG(
69 "Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum failed, ret=%{public}d, sql=%{public}s",
70 ret,
71 sql.c_str());
72 }
73 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum success");
74 return ret;
75 }
76 } // namespace DataTransfer
77 } // namespace OHOS::Media