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 CHECK_AND_RETURN_RET_WARN_LOG(galleryRdbPtr != nullptr, -1,
30 "galleryRdbPtr is nullptr, Maybe init failed, skip gallery db upgrade.");
31 return this->OnUpgrade(*galleryRdbPtr);
32 }
33
34 /**
35 * @brief Upgrade the database, before data restore or clone.
36 */
OnUpgrade(NativeRdb::RdbStore & store)37 int32_t GalleryDbUpgrade::OnUpgrade(NativeRdb::RdbStore &store)
38 {
39 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade start.");
40 AlbumPluginTableEventHandler handler;
41 int32_t ret = handler.OnUpgrade(store, 0, 0);
42 MEDIA_INFO_LOG("GalleryDbUpgrade::OnUpgrade end, ret: %{public}d", ret);
43 this->AddPhotoQualityOfGalleryMedia(store);
44 this->AddResolutionOfGalleryMedia(store);
45 this->AddRelativeBucketIdOfGalleryAlbum(store);
46 this->GarbageAlbumUpgrade(store);
47 this->AddIndexOfGalleryAlbum(store);
48 this->AddIndexAlbumIdOfGalleryAlbum(store);
49 this->AddIndexOfAlbumPlugin(store);
50 this->AddStoryChosenOfGalleryMedia(store);
51 this->CreateRelativeAlbumOfGalleryAlbum(store);
52 this->AddRelativeBucketIdColumn(store);
53 return NativeRdb::E_OK;
54 }
55
56 /**
57 * @brief Add photo_quality of gallery_media table in gallery.db.
58 */
AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore & store)59 int32_t GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore &store)
60 {
61 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "photo_quality");
62 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
63 std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_PHOTO_QUALITY;
64 int32_t ret = store.ExecuteSql(sql);
65 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia failed,"
66 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
67 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia success");
68 return ret;
69 }
70
71 /**
72 * @brief Add resolution of gallery_media table in gallery.db.
73 */
AddResolutionOfGalleryMedia(NativeRdb::RdbStore & store)74 int32_t GalleryDbUpgrade::AddResolutionOfGalleryMedia(NativeRdb::RdbStore &store)
75 {
76 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "resolution");
77 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
78 std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_RESOLUTION;
79 int32_t ret = store.ExecuteSql(sql);
80 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddResolutionOfGalleryMedia failed,"
81 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
82 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddResolutionOfGalleryMedia success");
83 return ret;
84 }
85
86 /**
87 * @brief Add relativeBucketId of gallery_album table in gallery.db if not exists.
88 */
AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore & store)89 int32_t GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore &store)
90 {
91 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "gallery_album", "relativeBucketId");
92 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
93 std::string sql = this->SQL_GALLERY_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
94 int32_t ret = store.ExecuteSql(sql);
95 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK,
96 "Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum failed,"
97 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
98 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum success");
99 return ret;
100 }
101
GarbageAlbumUpgrade(NativeRdb::RdbStore & store)102 int32_t GalleryDbUpgrade::GarbageAlbumUpgrade(NativeRdb::RdbStore &store)
103 {
104 this->GarbageAlbumCheckOrAddRelativeBucketId(store);
105 this->GarbageAlbumCheckOrAddType(store);
106 return NativeRdb::E_OK;
107 }
108
GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore & store)109 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore &store)
110 {
111 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "relative_bucket_id");
112 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
113 std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
114 int32_t ret = store.ExecuteSql(sql);
115 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId failed,"
116 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
117 MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId success");
118 return ret;
119 }
120
GarbageAlbumCheckOrAddType(NativeRdb::RdbStore & store)121 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddType(NativeRdb::RdbStore &store)
122 {
123 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "type");
124 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
125 std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_TYPE;
126 int32_t ret = store.ExecuteSql(sql);
127 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GarbageAlbumCheckOrAddType failed,"
128 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
129 MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddType success");
130 return ret;
131 }
132
AddIndexOfGalleryAlbum(NativeRdb::RdbStore & store)133 int32_t GalleryDbUpgrade::AddIndexOfGalleryAlbum(NativeRdb::RdbStore &store)
134 {
135 std::string sql = this->SQL_GALLERY_ALBUM_INDEX_RELATIVE_BUCKET_ID;
136 int32_t ret = store.ExecuteSql(sql);
137 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum failed,"
138 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
139 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum success");
140 return ret;
141 }
142
AddIndexAlbumIdOfGalleryAlbum(NativeRdb::RdbStore & store)143 int32_t GalleryDbUpgrade::AddIndexAlbumIdOfGalleryAlbum(NativeRdb::RdbStore &store)
144 {
145 std::string sql = this->SQL_GALLERY_ALBUM_INDEX_ALBUM_ID;
146 int32_t ret = store.ExecuteSql(sql);
147 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddIndexAlbumIdOfGalleryAlbum failed,"
148 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
149 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexAlbumIdOfGalleryAlbum success");
150 return ret;
151 }
152
AddIndexOfAlbumPlugin(NativeRdb::RdbStore & store)153 int32_t GalleryDbUpgrade::AddIndexOfAlbumPlugin(NativeRdb::RdbStore &store)
154 {
155 std::string sql = this->SQL_ALBUM_PLUGIN_INDEX_ALBUM_NAME;
156 int32_t ret = store.ExecuteSql(sql);
157 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin failed,"
158 " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
159 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin success");
160 return ret;
161 }
162
163 /**
164 * @brief Add story_chosen of gallery_media table in gallery.db.
165 */
AddStoryChosenOfGalleryMedia(NativeRdb::RdbStore & store)166 int32_t GalleryDbUpgrade::AddStoryChosenOfGalleryMedia(NativeRdb::RdbStore &store)
167 {
168 bool cond = this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "story_chosen");
169 CHECK_AND_RETURN_RET(!cond, NativeRdb::E_OK);
170 std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_STORY_CHOSEN;
171 int32_t ret = store.ExecuteSql(sql);
172 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddStoryChosenOfGalleryMedia failed,"
173 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
174 MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddStoryChosenOfGalleryMedia success");
175 return ret;
176 }
177
178 /**
179 * @brief Create relative_album table in gallery.db.
180 */
CreateRelativeAlbumOfGalleryAlbum(NativeRdb::RdbStore & store)181 int32_t GalleryDbUpgrade::CreateRelativeAlbumOfGalleryAlbum(NativeRdb::RdbStore &store)
182 {
183 std::string sql = this->CREATE_RELATE_ALBUM_TBL_SQL;
184 int32_t ret = store.ExecuteSql(sql);
185 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Add RelativeAlbum Of Gallery Album failed,"
186 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
187
188 std::string insertSql = this->INSERT_RELATE_ALBUM_TBL_SQL;
189 ret = store.ExecuteSql(insertSql);
190 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Create RelativeAlbum Of Gallery Album failed,"
191 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
192 MEDIA_INFO_LOG("Create Relative Album Of Gallery Album success");
193 return ret;
194 }
195
196 /**
197 * @brief Add relative_bucket_id column for gallery_cover_cache table in gallery.db.
198 */
AddRelativeBucketIdColumn(NativeRdb::RdbStore & store)199 int32_t GalleryDbUpgrade::AddRelativeBucketIdColumn(NativeRdb::RdbStore &store)
200 {
201 std::string sql = "ALTER TABLE gallery_cover_cache ADD COLUMN relative_bucket_id TEXT default ''";
202 int32_t ret = store.ExecuteSql(sql);
203 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Add relative_bucket_id column for gallery_cover_cache table failed,"
204 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
205
206 std::string prefix = "/gallery/album/entity/preferential/*/%";
207 size_t prefixLen = prefix.size();
208 std::string updateSql = "UPDATE gallery_cover_cache SET relative_bucket_id = SUBSTR(column_value, " +
209 std::to_string(prefixLen) + ") where column_name = 'relative_bucket_id' AND from_user = 1 AND "
210 "column_value LIKE'" + prefix + "'";
211 ret = store.ExecuteSql(updateSql);
212 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "update gallery_cover_cache Album failed,"
213 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
214
215 prefix = "/gallery/album/entity/common/1/%";
216 prefixLen = prefix.size();
217 updateSql = "UPDATE gallery_cover_cache SET relative_bucket_id = SUBSTR(column_value, " +
218 std::to_string(prefixLen) +
219 ") where column_name = 'relative_bucket_id' AND from_user = 1 AND column_value LIKE'" + prefix + "'";
220 ret = store.ExecuteSql(updateSql);
221 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "update gallery_cover_cache Album failed,"
222 "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
223
224 MEDIA_INFO_LOG("Add and update relative_bucket_id column for gallery_cover_cache table success");
225 return ret;
226 }
227 } // namespace DataTransfer
228 } // namespace OHOS::Media