1 /*
2 * Copyright (C) 2023 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
16 #include "photo_album_column.h"
17
18 #include <string>
19 #include <unordered_map>
20 #include "location_column.h"
21 #include "media_column.h"
22 #include "media_log.h"
23 #include "medialibrary_type_const.h"
24 #include "photo_map_column.h"
25 #include "vision_column.h"
26 #include "vision_face_tag_column.h"
27
28 namespace OHOS::Media {
29 using namespace std;
30 using namespace NativeRdb;
31
32 // PhotoAlbum table
33 const string PhotoAlbumColumns::TABLE = "PhotoAlbum";
34 const string PhotoAlbumColumns::ALBUM_ID = "album_id";
35 const string PhotoAlbumColumns::ALBUM_TYPE = "album_type";
36 const string PhotoAlbumColumns::ALBUM_SUBTYPE = "album_subtype";
37 const string PhotoAlbumColumns::ALBUM_NAME = "album_name";
38 const string PhotoAlbumColumns::ALBUM_COVER_URI = "cover_uri";
39 const string PhotoAlbumColumns::ALBUM_COUNT = "count";
40 const string PhotoAlbumColumns::ALBUM_DATE_MODIFIED = "date_modified";
41 const string PhotoAlbumColumns::ALBUM_DIRTY = "dirty";
42 const string PhotoAlbumColumns::ALBUM_CLOUD_ID = "cloud_id";
43 const string PhotoAlbumColumns::ALBUM_IMAGE_COUNT = "image_count";
44 const string PhotoAlbumColumns::ALBUM_VIDEO_COUNT = "video_count";
45 const string PhotoAlbumColumns::ALBUM_LATITUDE = "latitude";
46 const string PhotoAlbumColumns::ALBUM_LONGITUDE = "longitude";
47 const string PhotoAlbumColumns::ALBUM_BUNDLE_NAME = "bundle_name";
48 const string PhotoAlbumColumns::ALBUM_LOCAL_LANGUAGE = "local_language";
49 const string PhotoAlbumColumns::ALBUM_IS_LOCAL = "is_local";
50 const string PhotoAlbumColumns::ALBUM_DATE_ADDED = "date_added";
51 const string PhotoAlbumColumns::ALBUM_PRIORITY = "priority";
52 const string PhotoAlbumColumns::ALBUM_LPATH = "lpath";
53 const string PhotoAlbumColumns::ALBUM_CHECK_FLAG = "check_flag";
54 const string PhotoAlbumColumns::COVER_URI_SOURCE = "cover_uri_source";
55 const string PhotoAlbumColumns::COVER_CLOUD_ID = "cover_cloud_id";
56
57 // For api9 compatibility
58 const string PhotoAlbumColumns::ALBUM_RELATIVE_PATH = "relative_path";
59
60 const string PhotoAlbumColumns::CONTAINS_HIDDEN = "contains_hidden";
61 const string PhotoAlbumColumns::HIDDEN_COUNT = "hidden_count";
62 const string PhotoAlbumColumns::HIDDEN_COVER = "hidden_cover";
63
64 // For sorting albums
65 const string PhotoAlbumColumns::ALBUM_ORDER = "album_order";
66 const string PhotoAlbumColumns::REFERENCE_ALBUM_ID = "reference_album_id";
67 // For api20 sorting albums
68 const string PhotoAlbumColumns::ALBUMS_ORDER = "albums_order";
69 const string PhotoAlbumColumns::ORDER_SECTION = "order_section";
70 const string PhotoAlbumColumns::ORDER_TYPE = "order_type";
71 const string PhotoAlbumColumns::ORDER_STATUS = "order_status";
72 const string PhotoAlbumColumns::STYLE2_ALBUMS_ORDER = "style2_albums_order";
73 const string PhotoAlbumColumns::STYLE2_ORDER_SECTION = "style2_order_section";
74 const string PhotoAlbumColumns::STYLE2_ORDER_TYPE = "style2_order_type";
75 const string PhotoAlbumColumns::STYLE2_ORDER_STATUS = "style2_order_status";
76
77 const vector<std::string> PhotoAlbumColumns::ALBUM_ORDER_COLUMNS = {
78 PhotoAlbumColumns::ALBUMS_ORDER, PhotoAlbumColumns::STYLE2_ALBUMS_ORDER
79 };
80 const vector<std::string> PhotoAlbumColumns::ALBUM_ORDER_SECTION_COLUMNS = {
81 PhotoAlbumColumns::ORDER_SECTION, PhotoAlbumColumns::STYLE2_ORDER_SECTION
82 };
83 const vector<std::string> PhotoAlbumColumns::ALBUM_ORDER_TYPE_COLUMNS = {
84 PhotoAlbumColumns::ORDER_TYPE, PhotoAlbumColumns::STYLE2_ORDER_TYPE
85 };
86 const vector<std::string> PhotoAlbumColumns::ALBUM_ORDER_STATUS_COLUMNS = {
87 PhotoAlbumColumns::ORDER_STATUS, PhotoAlbumColumns::STYLE2_ORDER_STATUS
88 };
89
90 const unordered_map<AlbumOrderParam, vector<std::string>> PhotoAlbumColumns::ORDER_COLUMN_STYLE_MAP = {
91 {AlbumOrderParam::ALBUM_ORDER, PhotoAlbumColumns::ALBUM_ORDER_COLUMNS},
92 {AlbumOrderParam::ORDER_SECTION, PhotoAlbumColumns::ALBUM_ORDER_SECTION_COLUMNS},
93 {AlbumOrderParam::ORDER_TYPE, PhotoAlbumColumns::ALBUM_ORDER_TYPE_COLUMNS},
94 {AlbumOrderParam::ORDER_STATUS, PhotoAlbumColumns::ALBUM_ORDER_STATUS_COLUMNS}
95 };
96 // For accurate refresh
97 const string PhotoAlbumColumns::COVER_DATE_TIME = "cover_date_time";
98 const string PhotoAlbumColumns::HIDDEN_COVER_DATE_TIME = "hidden_cover_date_time";
99
100 // location album result
101 const std::string LOCATION_ALBUM_ID = MediaColumn::MEDIA_ID + " AS " + ALBUM_ID;
102 const std::string LOCATION_ALBUM_TYPE = std::to_string(PhotoAlbumType::SMART) + " AS " + ALBUM_TYPE;
103 const std::string LOCATION_ALBUM_SUBTYPE = std::to_string(PhotoAlbumSubType::GEOGRAPHY_LOCATION) +
104 " AS " + ALBUM_SUBTYPE;
105 const std::string LOCATION_COUNT = "COUNT(*) AS " + COUNT;
106 const std::string LOCATION_DATE_MODIFIED = "MAX(" + MediaColumn::MEDIA_DATE_ADDED + ") AS " + DATE_MODIFIED;
107 const std::string CITY_ALBUM_NAME = CITY_NAME + " AS " + ALBUM_NAME;
108 const std::string LOCATION_COVER_URI =
109 " (SELECT '" + PhotoColumn::PHOTO_URI_PREFIX + "'||" + MediaColumn::MEDIA_ID + "||" +
110 "(SELECT SUBSTR(" + MediaColumn::MEDIA_FILE_PATH +
111 ", (SELECT LENGTH(" + MediaColumn::MEDIA_FILE_PATH +
112 ") - INSTR(reverseStr, '/') + 1) , (SELECT (SELECT LENGTH(" +
113 MediaColumn::MEDIA_FILE_PATH + ") - INSTR(reverseStr, '.')) - (SELECT LENGTH(" +
114 MediaColumn::MEDIA_FILE_PATH + ") - INSTR(reverseStr, '/')))) from (select " +
115 " (WITH RECURSIVE reverse_string(str, revstr) AS ( SELECT " +
116 MediaColumn::MEDIA_FILE_PATH + ", '' UNION ALL SELECT SUBSTR(str, 1, LENGTH(str) - 1), " +
117 "revstr || SUBSTR(str, LENGTH(str), 1) FROM reverse_string WHERE LENGTH(str) > 1 ) " +
118 " SELECT revstr || str FROM reverse_string WHERE LENGTH(str) = 1) as reverseStr)) ||'/'||" +
119 MediaColumn::MEDIA_NAME + ") AS " + COVER_URI;
120
121 // default fetch columns
122 const set<string> PhotoAlbumColumns::DEFAULT_FETCH_COLUMNS = {
123 ALBUM_ID, ALBUM_TYPE, ALBUM_SUBTYPE, ALBUM_NAME, ALBUM_COVER_URI, ALBUM_COUNT, ALBUM_DATE_MODIFIED
124 };
125
126 // default fetch albumOrder columns
127 const set<string> PhotoAlbumColumns::DEFAULT_FETCH_ORDER_COLUMNS_STYLE1 = {
128 PhotoAlbumColumns::ALBUM_ID, PhotoAlbumColumns::ALBUMS_ORDER, PhotoAlbumColumns::ORDER_SECTION,
129 PhotoAlbumColumns::ORDER_TYPE, PhotoAlbumColumns::ORDER_STATUS
130 };
131 const set<string> PhotoAlbumColumns::DEFAULT_FETCH_ORDER_COLUMNS_STYLE2 = {
132 PhotoAlbumColumns::ALBUM_ID, PhotoAlbumColumns::STYLE2_ALBUMS_ORDER, PhotoAlbumColumns::STYLE2_ORDER_SECTION,
133 PhotoAlbumColumns::STYLE2_ORDER_TYPE, PhotoAlbumColumns::STYLE2_ORDER_STATUS
134 };
135
136 // location default fetch columns
137 const vector<string> PhotoAlbumColumns::LOCATION_DEFAULT_FETCH_COLUMNS = {
138 LATITUDE, LONGITUDE, LOCATION_ALBUM_TYPE, LOCATION_ALBUM_SUBTYPE, LOCATION_COUNT,
139 LOCATION_DATE_MODIFIED, LOCATION_COVER_URI, LOCATION_ALBUM_ID
140 };
141
142 // city default fetch columns
143 const vector<string> PhotoAlbumColumns::CITY_DEFAULT_FETCH_COLUMNS = {
144 ALBUM_ID, ALBUM_TYPE, ALBUM_SUBTYPE, CITY_ALBUM_NAME, ALBUM_COVER_URI, ALBUM_COUNT, ALBUM_DATE_MODIFIED
145 };
146
147 const string PhotoAlbumColumns::ALBUM_URI_PREFIX = "file://media/PhotoAlbum/";
148 const string PhotoAlbumColumns::DEFAULT_PHOTO_ALBUM_URI = "file://media/PhotoAlbum";
149 const string PhotoAlbumColumns::HIDDEN_ALBUM_URI_PREFIX = "file://media/HiddenAlbum/";
150 const string PhotoAlbumColumns::DEFAULT_HIDDEN_ALBUM_URI = "file://media/HiddenAlbum";
151 const string PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX = "file://media/AnalysisAlbum/";
152 const string PhotoAlbumColumns::TRASHED_ALBUM_URI_PREFIX = "file://media/trashedAlbum/";
153 const string PhotoAlbumColumns::ALBUM_CLOUD_URI_PREFIX = "file://cloudsync/PhotoAlbum/";
154 const string PhotoAlbumColumns::ALBUM_GALLERY_CLOUD_URI_PREFIX = "file://cloudsync/gallery/PhotoAlbum/";
155 const string PhotoAlbumColumns::PHOTO_GALLERY_CLOUD_SYNC_INFO_URI_PREFIX = "file://cloudsync/gallery/cloudSyncInfo/";
156 const string PhotoAlbumColumns::PHOTO_GALLERY_DOWNLOAD_URI_PREFIX = "file://cloudsync/gallery/download/";
157
158 // Create tables
159 const string PhotoAlbumColumns::CREATE_TABLE = CreateTable() +
160 TABLE + " (" +
161 ALBUM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
162 ALBUM_TYPE + " INT, " +
163 ALBUM_SUBTYPE + " INT, " +
164 ALBUM_NAME + " TEXT COLLATE NOCASE, " +
165 ALBUM_COVER_URI + " TEXT, " +
166 ALBUM_COUNT + " INT DEFAULT 0, " +
167 ALBUM_DATE_MODIFIED + " BIGINT DEFAULT 0, " +
168 ALBUM_DIRTY + " INT DEFAULT " + std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_NEW)) + ", " +
169 ALBUM_CLOUD_ID + " TEXT, " +
170 ALBUM_RELATIVE_PATH + " TEXT, " +
171 CONTAINS_HIDDEN + " INT DEFAULT 0, " +
172 HIDDEN_COUNT + " INT DEFAULT 0, " +
173 HIDDEN_COVER + " TEXT DEFAULT '', " +
174 ALBUM_ORDER + " INT," +
175 ALBUM_IMAGE_COUNT + " INT DEFAULT 0, " +
176 ALBUM_VIDEO_COUNT + " INT DEFAULT 0, " +
177 ALBUM_BUNDLE_NAME + " TEXT, " +
178 ALBUM_LOCAL_LANGUAGE + " TEXT, " +
179 ALBUM_IS_LOCAL + " INT, " +
180 ALBUM_DATE_ADDED + " BIGINT DEFAULT 0, " +
181 ALBUM_LPATH + " TEXT, " +
182 ALBUM_PRIORITY + " INT, " +
183 ALBUM_CHECK_FLAG + " INT DEFAULT 0, " +
184 PhotoAlbumColumns::COVER_URI_SOURCE + " INT DEFAULT 0, " +
185 COVER_DATE_TIME + " BIGINT DEFAULT 0, " +
186 HIDDEN_COVER_DATE_TIME + " BIGINT DEFAULT 0, " +
187 ALBUMS_ORDER + " INT DEFAULT -1, " +
188 ORDER_SECTION + " INT DEFAULT -1, " +
189 ORDER_TYPE + " INT DEFAULT -1, " +
190 ORDER_STATUS + " INT DEFAULT 0, " +
191 STYLE2_ALBUMS_ORDER + " INT DEFAULT -1, " +
192 STYLE2_ORDER_SECTION + " INT DEFAULT -1, " +
193 STYLE2_ORDER_TYPE + " INT DEFAULT -1, " +
194 STYLE2_ORDER_STATUS + " INT DEFAULT 0)";
195
196 // Create indexes
197 const string PhotoAlbumColumns::INDEX_ALBUM_TYPES = CreateIndex() + "photo_album_types" + " ON " + TABLE +
198 " (" + ALBUM_TYPE + "," + ALBUM_SUBTYPE + ");";
199
200 // Create triggers
201 const std::string PhotoAlbumColumns::CREATE_ALBUM_INSERT_TRIGGER =
202 " CREATE TRIGGER IF NOT EXISTS album_insert_cloud_sync_trigger AFTER INSERT ON " + TABLE +
203 " BEGIN SELECT cloud_sync_func(); END;";
204
205 const std::string PhotoAlbumColumns::CREATE_ALBUM_DELETE_TRIGGER =
206 "CREATE TRIGGER IF NOT EXISTS album_delete_trigger AFTER UPDATE ON " + TABLE +
207 " FOR EACH ROW WHEN new." + ALBUM_DIRTY + " = " +
208 std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_DELETED)) +
209 " AND old." + ALBUM_DIRTY + " = " + std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_NEW)) +
210 " AND is_caller_self_func() = 'true' BEGIN DELETE FROM " + TABLE +
211 " WHERE " + ALBUM_ID + " = old." + ALBUM_ID + "; SELECT cloud_sync_func();" +
212 " END;";
213
214 const std::string PhotoAlbumColumns::CREATE_ALBUM_MDIRTY_TRIGGER =
215 "CREATE TRIGGER IF NOT EXISTS album_modify_trigger AFTER UPDATE ON " + TABLE +
216 " FOR EACH ROW WHEN old." + ALBUM_DIRTY + " = " +
217 std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_SYNCED)) +
218 " AND old." + ALBUM_DIRTY + " = " + "new." + ALBUM_DIRTY +
219 " AND is_caller_self_func() = 'true'" +
220 " AND " + PhotoAlbumColumns::CheckUploadPhotoAlbumColumns() +
221 " BEGIN UPDATE " + TABLE + " SET dirty = " +
222 std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_MDIRTY)) +
223 " WHERE " + ALBUM_ID + " = old." + ALBUM_ID + "; SELECT cloud_sync_func();" +
224 " END;";
225
226 const std::string PhotoAlbumColumns::ALBUM_DELETE_ORDER_TRIGGER =
227 " CREATE TRIGGER IF NOT EXISTS update_order_trigger AFTER DELETE ON " + PhotoAlbumColumns::TABLE +
228 " FOR EACH ROW " +
229 " BEGIN " +
230 " UPDATE " + PhotoAlbumColumns::TABLE + " SET album_order = album_order - 1" +
231 " WHERE album_order > old.album_order; " +
232 " END";
233
234 const std::string PhotoAlbumColumns::ALBUM_INSERT_ORDER_TRIGGER =
235 " CREATE TRIGGER IF NOT EXISTS insert_order_trigger AFTER INSERT ON " + PhotoAlbumColumns::TABLE +
236 " BEGIN " +
237 " UPDATE " + PhotoAlbumColumns::TABLE + " SET album_order = (" +
238 " SELECT COALESCE(MAX(album_order), 0) + 1 FROM " + PhotoAlbumColumns::TABLE +
239 ") WHERE rowid = new.rowid;" +
240 " END";
241
IsPhotoAlbumColumn(const string & columnName)242 bool PhotoAlbumColumns::IsPhotoAlbumColumn(const string &columnName)
243 {
244 static const set<string> PHOTO_ALBUM_COLUMNS = {
245 PhotoAlbumColumns::ALBUM_ID, PhotoAlbumColumns::ALBUM_TYPE, PhotoAlbumColumns::ALBUM_SUBTYPE,
246 PhotoAlbumColumns::ALBUM_NAME, PhotoAlbumColumns::ALBUM_COVER_URI, PhotoAlbumColumns::ALBUM_COUNT,
247 PhotoAlbumColumns::ALBUM_RELATIVE_PATH, CONTAINS_HIDDEN, HIDDEN_COUNT, HIDDEN_COVER, ALBUM_LPATH,
248 PhotoAlbumColumns::COVER_URI_SOURCE,
249 };
250 return PHOTO_ALBUM_COLUMNS.find(columnName) != PHOTO_ALBUM_COLUMNS.end();
251 }
252
SetDefaultPredicatesCondition(RdbPredicates & predicates,const int32_t dateTrashed,const bool isHidden,const int32_t timePending,const bool isTemp)253 inline void SetDefaultPredicatesCondition(RdbPredicates &predicates, const int32_t dateTrashed,
254 const bool isHidden, const int32_t timePending, const bool isTemp)
255 {
256 predicates.EqualTo(MediaColumn::MEDIA_DATE_TRASHED, to_string(dateTrashed));
257 predicates.EqualTo(MediaColumn::MEDIA_HIDDEN, to_string(isHidden));
258 predicates.EqualTo(MediaColumn::MEDIA_TIME_PENDING, to_string(timePending));
259 predicates.EqualTo(PhotoColumn::PHOTO_IS_TEMP, to_string(isTemp));
260 predicates.EqualTo(PhotoColumn::PHOTO_BURST_COVER_LEVEL,
261 to_string(static_cast<int32_t>(BurstCoverLevelType::COVER)));
262 }
263
GetUserAlbumPredicates(const int32_t albumId,RdbPredicates & predicates,const bool hiddenState)264 void PhotoAlbumColumns::GetUserAlbumPredicates(const int32_t albumId, RdbPredicates &predicates, const bool hiddenState)
265 {
266 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
267 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
268 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
269 predicates.EqualTo(PhotoColumn::PHOTO_OWNER_ALBUM_ID, to_string(albumId));
270 }
271
GetPortraitAlbumPredicates(const int32_t albumId,RdbPredicates & predicates)272 void PhotoAlbumColumns::GetPortraitAlbumPredicates(const int32_t albumId, RdbPredicates &predicates)
273 {
274 string onClause = MediaColumn::MEDIA_ID + " = " + PhotoMap::ASSET_ID;
275 vector<string> clauses = { onClause };
276 predicates.InnerJoin(ANALYSIS_PHOTO_MAP_TABLE)->On(clauses);
277 onClause = ALBUM_ID + " = " + PhotoMap::ALBUM_ID;
278 clauses = { onClause };
279 predicates.InnerJoin(ANALYSIS_ALBUM_TABLE)->On(clauses);
280 string tempTable = "(SELECT " + GROUP_TAG + " FROM " + ANALYSIS_ALBUM_TABLE + " WHERE " + ALBUM_ID + " = " +
281 to_string(albumId) + ") ag";
282 onClause = "ag." + GROUP_TAG + " = " + ANALYSIS_ALBUM_TABLE + "." + GROUP_TAG;
283 clauses = { onClause };
284 predicates.InnerJoin(tempTable)->On(clauses);
285 SetDefaultPredicatesCondition(predicates, 0, 0, 0, false);
286 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
287 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
288 predicates.Distinct();
289 return;
290 }
291
GetAnalysisPhotoMapPredicates(const int32_t albumId,RdbPredicates & predicates,const bool hiddenState)292 void PhotoAlbumColumns::GetAnalysisPhotoMapPredicates(const int32_t albumId,
293 RdbPredicates &predicates, const bool hiddenState)
294 {
295 string onClause = MediaColumn::MEDIA_ID + " = " + PhotoMap::ASSET_ID;
296 predicates.InnerJoin(ANALYSIS_PHOTO_MAP_TABLE)->On({ onClause });
297 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
298 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
299 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
300 predicates.EqualTo(PhotoMap::ALBUM_ID, to_string(albumId));
301 }
302
GetFavoritePredicates(RdbPredicates & predicates,const bool hiddenState)303 static void GetFavoritePredicates(RdbPredicates &predicates, const bool hiddenState)
304 {
305 predicates.BeginWrap();
306 constexpr int32_t isFavorite = 1;
307 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
308 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
309 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
310 predicates.EqualTo(MediaColumn::MEDIA_IS_FAV, to_string(isFavorite));
311 predicates.EndWrap();
312 }
313
GetVideoPredicates(RdbPredicates & predicates,const bool hiddenState)314 static void GetVideoPredicates(RdbPredicates &predicates, const bool hiddenState)
315 {
316 predicates.BeginWrap();
317 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
318 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
319 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
320 predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_VIDEO));
321 predicates.EndWrap();
322 }
323
GetHiddenPredicates(RdbPredicates & predicates)324 static void GetHiddenPredicates(RdbPredicates &predicates)
325 {
326 predicates.BeginWrap();
327 constexpr int32_t isHidden = 1;
328 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
329 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
330 SetDefaultPredicatesCondition(predicates, 0, isHidden, 0, false);
331 predicates.EndWrap();
332 }
333
GetTrashPredicates(RdbPredicates & predicates)334 static void GetTrashPredicates(RdbPredicates &predicates)
335 {
336 predicates.BeginWrap();
337 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
338 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
339 predicates.GreaterThan(MediaColumn::MEDIA_DATE_TRASHED, to_string(0));
340 predicates.EqualTo(MediaColumn::MEDIA_TIME_PENDING, to_string(0));
341 predicates.EqualTo(PhotoColumn::PHOTO_IS_TEMP, to_string(0));
342 predicates.EqualTo(PhotoColumn::PHOTO_BURST_COVER_LEVEL,
343 to_string(static_cast<int32_t>(BurstCoverLevelType::COVER)));
344 predicates.EndWrap();
345 }
346
GetScreenshotPredicates(RdbPredicates & predicates,const bool hiddenState)347 static void GetScreenshotPredicates(RdbPredicates &predicates, const bool hiddenState)
348 {
349 predicates.BeginWrap();
350 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
351 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
352 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
353 predicates.EqualTo(PhotoColumn::PHOTO_SUBTYPE, to_string(static_cast<int32_t>(PhotoSubType::SCREENSHOT)));
354 predicates.EndWrap();
355 }
356
GetCameraPredicates(RdbPredicates & predicates,const bool hiddenState)357 static void GetCameraPredicates(RdbPredicates &predicates, const bool hiddenState)
358 {
359 predicates.BeginWrap();
360 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
361 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
362 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
363 predicates.EqualTo(PhotoColumn::PHOTO_SUBTYPE, to_string(static_cast<int32_t>(PhotoSubType::CAMERA)));
364 predicates.EndWrap();
365 }
366
GetAllImagesPredicates(RdbPredicates & predicates,const bool hiddenState)367 static void GetAllImagesPredicates(RdbPredicates &predicates, const bool hiddenState)
368 {
369 predicates.BeginWrap();
370 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
371 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
372 predicates.EqualTo(MediaColumn::MEDIA_DATE_TRASHED, to_string(0));
373 predicates.EqualTo(MediaColumn::MEDIA_HIDDEN, to_string(hiddenState));
374 predicates.EqualTo(MediaColumn::MEDIA_TIME_PENDING, to_string(0));
375 predicates.EqualTo(PhotoColumn::PHOTO_IS_TEMP, to_string(false));
376 // order need adapt PHOTO_SCHPT_MEDIA_TYPE_INDEX
377 predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_IMAGE));
378 predicates.EqualTo(PhotoColumn::PHOTO_BURST_COVER_LEVEL,
379 to_string(static_cast<int32_t>(BurstCoverLevelType::COVER)));
380 predicates.EndWrap();
381 }
382
GetCloudEnhancementPredicates(RdbPredicates & predicates,const bool hiddenState)383 static void GetCloudEnhancementPredicates(RdbPredicates &predicates, const bool hiddenState)
384 {
385 predicates.BeginWrap();
386 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
387 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
388 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
389 predicates.EqualTo(PhotoColumn::PHOTO_STRONG_ASSOCIATION,
390 to_string(static_cast<int32_t>(StrongAssociationType::CLOUD_ENHANCEMENT)));
391 predicates.EqualTo(MediaColumn::MEDIA_TYPE, to_string(MEDIA_TYPE_IMAGE));
392 predicates.EndWrap();
393 }
394
GetSourceAlbumPredicates(const int32_t albumId,RdbPredicates & predicates,const bool hiddenState)395 void PhotoAlbumColumns::GetSourceAlbumPredicates(const int32_t albumId, RdbPredicates &predicates,
396 const bool hiddenState)
397 {
398 predicates.EqualTo(PhotoColumn::PHOTO_SYNC_STATUS, to_string(static_cast<int32_t>(SyncStatusType::TYPE_VISIBLE)));
399 predicates.EqualTo(PhotoColumn::PHOTO_CLEAN_FLAG, to_string(static_cast<int32_t>(CleanType::TYPE_NOT_CLEAN)));
400 SetDefaultPredicatesCondition(predicates, 0, hiddenState, 0, false);
401 predicates.EqualTo(PhotoColumn::PHOTO_OWNER_ALBUM_ID, to_string(albumId));
402 }
403
GetSystemAlbumPredicates(const PhotoAlbumSubType subtype,RdbPredicates & predicates,const bool hiddenState)404 bool PhotoAlbumColumns::GetSystemAlbumPredicates(const PhotoAlbumSubType subtype, RdbPredicates &predicates,
405 const bool hiddenState)
406 {
407 switch (subtype) {
408 case PhotoAlbumSubType::FAVORITE: {
409 GetFavoritePredicates(predicates, hiddenState);
410 return true;
411 }
412 case PhotoAlbumSubType::VIDEO: {
413 GetVideoPredicates(predicates, hiddenState);
414 return true;
415 }
416 case PhotoAlbumSubType::HIDDEN: {
417 GetHiddenPredicates(predicates);
418 return true;
419 }
420 case PhotoAlbumSubType::TRASH: {
421 GetTrashPredicates(predicates);
422 return true;
423 }
424 case PhotoAlbumSubType::SCREENSHOT: {
425 GetScreenshotPredicates(predicates, hiddenState);
426 return true;
427 }
428 case PhotoAlbumSubType::CAMERA: {
429 GetCameraPredicates(predicates, hiddenState);
430 return true;
431 }
432 case PhotoAlbumSubType::IMAGE: {
433 GetAllImagesPredicates(predicates, hiddenState);
434 return true;
435 }
436 case PhotoAlbumSubType::CLOUD_ENHANCEMENT: {
437 GetCloudEnhancementPredicates(predicates, hiddenState);
438 return true;
439 }
440 default: {
441 predicates.EqualTo(PhotoColumn::MEDIA_ID, to_string(0));
442 MEDIA_ERR_LOG("Unsupported system album subtype: %{public}d", subtype);
443 return false;
444 }
445 }
446 }
447
CheckUploadPhotoAlbumColumns()448 std::string PhotoAlbumColumns::CheckUploadPhotoAlbumColumns()
449 {
450 // Since date_modified has been checked in mdirty and fdirty, omit it here.
451 const std::vector<std::string> uploadPhotoAlbumColumns = {
452 ALBUM_NAME,
453 ALBUM_DATE_MODIFIED,
454 };
455
456 std::string result = "(";
457 size_t size = uploadPhotoAlbumColumns.size();
458 for (size_t i = 0; i < size; i++) {
459 std::string column = uploadPhotoAlbumColumns[i];
460 if (i != size - 1) {
461 result += "new." + column + " <> old." + column + " OR ";
462 } else {
463 result += "new." + column + " <> old." + column + ")";
464 }
465 }
466 return result;
467 }
468 } // namespace OHOS::Media
469