• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->AddResolutionOfGalleryMedia(store);
47     this->AddRelativeBucketIdOfGalleryAlbum(store);
48     this->GarbageAlbumUpgrade(store);
49     this->AddIndexOfGalleryAlbum(store);
50     this->AddIndexOfAlbumPlugin(store);
51     this->AddStoryChosenOfGalleryMedia(store);
52     return NativeRdb::E_OK;
53 }
54 
55 /**
56  * @brief Add photo_quality of gallery_media table in gallery.db.
57  */
AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore & store)58 int32_t GalleryDbUpgrade::AddPhotoQualityOfGalleryMedia(NativeRdb::RdbStore &store)
59 {
60     if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "photo_quality")) {
61         return NativeRdb::E_OK;
62     }
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     if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "resolution")) {
77         return NativeRdb::E_OK;
78     }
79     std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_RESOLUTION;
80     int32_t ret = store.ExecuteSql(sql);
81     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddResolutionOfGalleryMedia failed,"
82          "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
83     MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddResolutionOfGalleryMedia success");
84     return ret;
85 }
86 
87 /**
88  * @brief Add relativeBucketId of gallery_album table in gallery.db if not exists.
89  */
AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore & store)90 int32_t GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum(NativeRdb::RdbStore &store)
91 {
92     if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_album", "relativeBucketId")) {
93         return NativeRdb::E_OK;
94     }
95     std::string sql = this->SQL_GALLERY_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
96     int32_t ret = store.ExecuteSql(sql);
97     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK,
98         "Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum failed,"
99         " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
100     MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddRelativeBucketIdOfGalleryAlbum success");
101     return ret;
102 }
103 
GarbageAlbumUpgrade(NativeRdb::RdbStore & store)104 int32_t GalleryDbUpgrade::GarbageAlbumUpgrade(NativeRdb::RdbStore &store)
105 {
106     this->GarbageAlbumCheckOrAddRelativeBucketId(store);
107     this->GarbageAlbumCheckOrAddType(store);
108     return NativeRdb::E_OK;
109 }
110 
GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore & store)111 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddRelativeBucketId(NativeRdb::RdbStore &store)
112 {
113     if (this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "relative_bucket_id")) {
114         return NativeRdb::E_OK;
115     }
116     std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_RELATIVE_BUCKET_ID;
117     int32_t ret = store.ExecuteSql(sql);
118     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId failed,"
119         " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
120     MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddRelativeBucketId success");
121     return ret;
122 }
123 
GarbageAlbumCheckOrAddType(NativeRdb::RdbStore & store)124 int32_t GalleryDbUpgrade::GarbageAlbumCheckOrAddType(NativeRdb::RdbStore &store)
125 {
126     if (this->dbUpgradeUtils_.IsColumnExists(store, "garbage_album", "type")) {
127         return NativeRdb::E_OK;
128     }
129     std::string sql = this->SQL_GARBAGE_ALBUM_TABLE_ADD_TYPE;
130     int32_t ret = store.ExecuteSql(sql);
131     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GarbageAlbumCheckOrAddType failed,"
132         " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
133     MEDIA_INFO_LOG("Media_Restore: GarbageAlbumCheckOrAddType success");
134     return ret;
135 }
136 
AddIndexOfGalleryAlbum(NativeRdb::RdbStore & store)137 int32_t GalleryDbUpgrade::AddIndexOfGalleryAlbum(NativeRdb::RdbStore &store)
138 {
139     std::string sql = this->SQL_GALLERY_ALBUM_INDEX_RELATIVE_BUCKET_ID;
140     int32_t ret = store.ExecuteSql(sql);
141     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum failed,"
142         " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
143     MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfGalleryAlbum success");
144     return ret;
145 }
146 
AddIndexOfAlbumPlugin(NativeRdb::RdbStore & store)147 int32_t GalleryDbUpgrade::AddIndexOfAlbumPlugin(NativeRdb::RdbStore &store)
148 {
149     std::string sql = this->SQL_ALBUM_PLUGIN_INDEX_ALBUM_NAME;
150     int32_t ret = store.ExecuteSql(sql);
151     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin failed,"
152         " ret=%{public}d, sql=%{public}s", ret, sql.c_str());
153     MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddIndexOfAlbumPlugin success");
154     return ret;
155 }
156 
157 /**
158  * @brief Add story_chosen of gallery_media table in gallery.db.
159  */
AddStoryChosenOfGalleryMedia(NativeRdb::RdbStore & store)160  int32_t GalleryDbUpgrade::AddStoryChosenOfGalleryMedia(NativeRdb::RdbStore &store)
161 {
162     if (this->dbUpgradeUtils_.IsColumnExists(store, "gallery_media", "story_chosen")) {
163         return NativeRdb::E_OK;
164     }
165     std::string sql = this->SQL_GALLERY_MEDIA_TABLE_ADD_STORY_CHOSEN;
166     int32_t ret = store.ExecuteSql(sql);
167     CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK, "Media_Restore: GalleryDbUpgrade::AddStoryChosenOfGalleryMedia failed,"
168          "ret=%{public}d, sql=%{public}s", ret, sql.c_str());
169     MEDIA_INFO_LOG("Media_Restore: GalleryDbUpgrade::AddStoryChosenOfGalleryMedia success");
170     return ret;
171 }
172 }  // namespace DataTransfer
173 }  // namespace OHOS::Media