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(std::shared_ptr<NativeRdb::RdbStore> galleryRdbPtr)27 int32_t GalleryDbUpgrade::OnUpgrade(std::shared_ptr<NativeRdb::RdbStore> galleryRdbPtr)
28 {
29 if (galleryRdbPtr == nullptr) {
30 MEDIA_WARN_LOG("galleryRdbPtr is nullptr, Maybe init failed, skip gallery db upgrade.");
31 return -1;
32 }
33 return this->OnUpgrade(*galleryRdbPtr);
34 }
35
36 /**
37 * @brief Upgrade the database, before data restore or clone.
38 */
OnUpgrade(NativeRdb::RdbStore & store)39 int32_t GalleryDbUpgrade::OnUpgrade(NativeRdb::RdbStore &store)
40 {
41 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade start.");
42 AlbumPluginTableEventHandler handler;
43 int32_t ret = handler.OnUpgrade(store, 0, 0);
44 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade end, ret: %{public}d", ret);
45 this->AddPhotoQualityOfGalleryMedia(store);
46 this->AddRelativeBucketIdOfGalleryAlbum(store);
47 this->GarbageAlbumUpgrade(store);
48 this->AddIndexOfGalleryAlbum(store);
49 this->AddIndexOfAlbumPlugin(store);
50 return NativeRdb::E_OK;
51 }
52
53 /**
54 * @brief Add photo_quality of gallery_media table in gallery.db.
55 */
AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore & store)56 int32_t GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore &store)
57 {
58 if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "photo_quality")) {
59 return NativeRdb::E_OK;
60 }
61 std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_PHOTO_QUALITY;
62 int32_t ret = store.ExecuteSql(sql);
63 if (ret != NativeRdb::E_OK) {
64 MEDIA_ERR_LOG(
65 "Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia failed, ret=%{public}d, sql=%{public}s",
66 ret,
67 sql.c_str());
68 }
69 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia success");
70 return ret;
71 }
72
73 /**
74 * @brief Add relativeBucketId of gallery_album table in gallery.db if not exists.
75 */
AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore & store)76 int32_t GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore &store)
77 {
78 if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_album", "relativeBucketId")) {
79 return NativeRdb::E_OK;
80 }
81 std::string sql = this->SQL_GALLERY_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
82 int32_t ret = store.ExecuteSql(sql);
83 if (ret != NativeRdb::E_OK) {
84 MEDIA_ERR_LOG(
85 "Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum failed, ret=%{public}d, sql=%{public}s",
86 ret,
87 sql.c_str());
88 }
89 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum success");
90 return ret;
91 }
92
GarbageAlbumUpgrade(NativeRdb::RdbStore & store)93 int32_t GalleryDbUpgrade::GarbageAlbumUpgrade(NativeRdb::RdbStore &store)
94 {
95 this->GarbageAlbumCheckOrAddRelativeBucketId(store);
96 this->GarbageAlbumCheckOrAddType(store);
97 return NativeRdb::E_OK;
98 }
99
GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore & store)100 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore &store)
101 {
102 if (this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "relative_bucket_id")) {
103 return NativeRdb::E_OK;
104 }
105 std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
106 int32_t ret = store.ExecuteSql(sql);
107 if (ret != NativeRdb::E_OK) {
108 MEDIA_ERR_LOG("Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId failed, ret=%{public}d, sql=%{public}s",
109 ret,
110 sql.c_str());
111 }
112 MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId success");
113 return ret;
114 }
115
GarbageAlbumCheckOrAddType(NativeRdb::RdbStore & store)116 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddType(NativeRdb::RdbStore &store)
117 {
118 if (this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "type")) {
119 return NativeRdb::E_OK;
120 }
121 std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_TYPE;
122 int32_t ret = store.ExecuteSql(sql);
123 if (ret != NativeRdb::E_OK) {
124 MEDIA_ERR_LOG(
125 "Media_Restore: GarbageAlbumCheckOrAddType failed, ret=%{public}d, sql=%{public}s", ret, sql.c_str());
126 }
127 MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddType success");
128 return ret;
129 }
130
AddIndexOfGalleryAlbum(NativeRdb::RdbStore & store)131 int32_t GalleryDbUpgrade::AddIndexOfGalleryAlbum(NativeRdb::RdbStore &store)
132 {
133 std::string sql = this->SQL_GALLERY_ALBUM_INDEX_RELATIVE_BUCKET_ID;
134 int32_t ret = store.ExecuteSql(sql);
135 if (ret != NativeRdb::E_OK) {
136 MEDIA_ERR_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum failed, ret=%{public}d, sql=%{public}s",
137 ret,
138 sql.c_str());
139 }
140 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum success");
141 return ret;
142 }
143
AddIndexOfAlbumPlugin(NativeRdb::RdbStore & store)144 int32_t GalleryDbUpgrade::AddIndexOfAlbumPlugin(NativeRdb::RdbStore &store)
145 {
146 std::string sql = this->SQL_ALBUM_PLUGIN_INDEX_ALBUM_NAME;
147 int32_t ret = store.ExecuteSql(sql);
148 if (ret != NativeRdb::E_OK) {
149 MEDIA_ERR_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin failed, ret=%{public}d, sql=%{public}s",
150 ret,
151 sql.c_str());
152 }
153 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin success");
154 return ret;
155 }
156 } // namespace DataTransfer
157 } // namespace OHOS::Media