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 "media_column.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "base_column.h"
22 #include "medialibrary_db_const.h"
23 #include "userfile_manager_types.h"
24 
25 namespace OHOS {
26 namespace Media {
27 const std::string MediaColumn::MEDIA_ID = "file_id";
28 const std::string MediaColumn::MEDIA_FILE_PATH = "data";
29 const std::string MediaColumn::MEDIA_SIZE = "size";
30 const std::string MediaColumn::MEDIA_TITLE = "title";
31 const std::string MediaColumn::MEDIA_NAME = "display_name";
32 const std::string MediaColumn::MEDIA_TYPE = "media_type";
33 const std::string MediaColumn::MEDIA_MIME_TYPE = "mime_type";
34 const std::string MediaColumn::MEDIA_OWNER_PACKAGE = "owner_package";
35 const std::string MediaColumn::MEDIA_OWNER_APPID = "owner_appid";
36 const std::string MediaColumn::MEDIA_PACKAGE_NAME = "package_name";
37 const std::string MediaColumn::MEDIA_DEVICE_NAME = "device_name";
38 const std::string MediaColumn::MEDIA_DATE_MODIFIED = "date_modified";
39 const std::string MediaColumn::MEDIA_DATE_ADDED = "date_added";
40 const std::string MediaColumn::MEDIA_DATE_TAKEN = "date_taken";
41 const std::string MediaColumn::MEDIA_DURATION = "duration";
42 const std::string MediaColumn::MEDIA_TIME_PENDING = "time_pending";
43 const std::string MediaColumn::MEDIA_IS_FAV = "is_favorite";
44 const std::string MediaColumn::MEDIA_DATE_TRASHED = "date_trashed";
45 const std::string MediaColumn::MEDIA_DATE_DELETED = "date_deleted";
46 const std::string MediaColumn::MEDIA_HIDDEN = "hidden";
47 const std::string MediaColumn::MEDIA_PARENT_ID = "parent";
48 const std::string MediaColumn::MEDIA_RELATIVE_PATH = "relative_path";
49 const std::string MediaColumn::MEDIA_VIRTURL_PATH = "virtual_path";
50 const std::set<std::string> MediaColumn::MEDIA_COLUMNS = {
51     MEDIA_ID, MEDIA_FILE_PATH, MEDIA_SIZE, MEDIA_TITLE, MEDIA_NAME, MEDIA_TYPE, MEDIA_MIME_TYPE,
52     MEDIA_OWNER_PACKAGE, MEDIA_OWNER_APPID, MEDIA_PACKAGE_NAME, MEDIA_DEVICE_NAME, MEDIA_DATE_MODIFIED,
53     MEDIA_DATE_ADDED, MEDIA_DATE_TAKEN, MEDIA_DURATION, MEDIA_TIME_PENDING, MEDIA_IS_FAV, MEDIA_DATE_TRASHED,
54     MEDIA_DATE_DELETED, MEDIA_HIDDEN, MEDIA_PARENT_ID, MEDIA_RELATIVE_PATH, MEDIA_VIRTURL_PATH
55 };
56 const std::set<std::string> MediaColumn::DEFAULT_FETCH_COLUMNS = {
57     MEDIA_ID, MEDIA_FILE_PATH, MEDIA_NAME, MEDIA_TYPE
58 };
59 
60 const std::string PhotoColumn::PHOTO_DIRTY = "dirty";
61 const std::string PhotoColumn::PHOTO_CLOUD_ID = "cloud_id";
62 const std::string PhotoColumn::PHOTO_META_DATE_MODIFIED = "meta_date_modified";
63 const std::string PhotoColumn::PHOTO_SYNC_STATUS = "sync_status";
64 const std::string PhotoColumn::PHOTO_CLOUD_VERSION = "cloud_version";
65 const std::string PhotoColumn::PHOTO_ORIENTATION = "orientation";
66 const std::string PhotoColumn::PHOTO_LATITUDE = "latitude";
67 const std::string PhotoColumn::PHOTO_LONGITUDE = "longitude";
68 const std::string PhotoColumn::PHOTO_HEIGHT = "height";
69 const std::string PhotoColumn::PHOTO_WIDTH = "width";
70 const std::string PhotoColumn::PHOTO_LCD_VISIT_TIME = "lcd_visit_time";
71 const std::string PhotoColumn::PHOTO_EDIT_TIME = "edit_time";
72 const std::string PhotoColumn::PHOTO_POSITION = "position";
73 const std::string PhotoColumn::PHOTO_SUBTYPE = "subtype";
74 const std::string PhotoColumn::CAMERA_SHOT_KEY = "camera_shot_key";
75 const std::string PhotoColumn::PHOTO_USER_COMMENT = "user_comment";
76 const std::string PhotoColumn::PHOTO_SHOOTING_MODE = "shooting_mode";
77 const std::string PhotoColumn::PHOTO_SHOOTING_MODE_TAG = "shooting_mode_tag";
78 const std::string PhotoColumn::PHOTO_ALL_EXIF = "all_exif";
79 const std::string PhotoColumn::PHOTO_DATE_YEAR = "date_year";
80 const std::string PhotoColumn::PHOTO_DATE_MONTH = "date_month";
81 const std::string PhotoColumn::PHOTO_DATE_DAY = "date_day";
82 const std::string PhotoColumn::PHOTO_LAST_VISIT_TIME = "last_visit_time";
83 const std::string PhotoColumn::PHOTO_HIDDEN_TIME = "hidden_time";
84 const std::string PhotoColumn::PHOTO_THUMB_STATUS = "thumb_status";
85 const std::string PhotoColumn::PHOTO_CLEAN_FLAG = "clean_flag";
86 const std::string PhotoColumn::PHOTO_ID = "photo_id";
87 const std::string PhotoColumn::PHOTO_QUALITY = "photo_quality";
88 const std::string PhotoColumn::PHOTO_FIRST_VISIT_TIME = "first_visit_time";
89 const std::string PhotoColumn::PHOTO_DEFERRED_PROC_TYPE = "deferred_proc_type";
90 const std::string PhotoColumn::PHOTO_DYNAMIC_RANGE_TYPE = "dynamic_range_type";
91 const std::string PhotoColumn::MOVING_PHOTO_EFFECT_MODE = "moving_photo_effect_mode";
92 const std::string PhotoColumn::PHOTO_LCD_SIZE = "lcd_size";
93 const std::string PhotoColumn::PHOTO_THUMB_SIZE = "thumb_size";
94 const std::string PhotoColumn::PHOTO_HAS_ASTC = "has_astc"; // This attribute has been replaced by "thumbnail_ready"
95 const std::string PhotoColumn::PHOTO_IS_TEMP = "is_temp";
96 const std::string PhotoColumn::PHOTO_THUMBNAIL_READY = "thumbnail_ready";
97 const std::string PhotoColumn::PHOTO_COVER_POSITION = "cover_position";
98 const std::string PhotoColumn::PHOTO_THUMBNAIL_VISIBLE = "thumbnail_visible";
99 const std::string PhotoColumn::PHOTO_FRONT_CAMERA = "front_camera";
100 const std::string PhotoColumn::PHOTO_BURST_COVER_LEVEL = "burst_cover_level";
101 const std::string PhotoColumn::PHOTO_BURST_KEY = "burst_key";
102 const std::string PhotoColumn::PHOTO_ORIGINAL_SUBTYPE = "original_subtype";
103 
104 const std::string PhotoColumn::PHOTO_CE_AVAILABLE = "ce_available";
105 const std::string PhotoColumn::PHOTO_CE_STATUS_CODE = "ce_status_code";
106 const std::string PhotoColumn::PHOTO_STRONG_ASSOCIATION = "strong_association";
107 const std::string PhotoColumn::PHOTO_ASSOCIATE_FILE_ID = "associate_file_id";
108 const std::string PhotoColumn::PHOTO_HAS_CLOUD_WATERMARK = "has_cloud_watermark";
109 const std::string PhotoColumn::PHOTO_DETAIL_TIME = "detail_time";
110 
111 const std::string PhotoColumn::PHOTO_OWNER_ALBUM_ID = "owner_album_id";
112 const std::string PhotoColumn::PHOTO_ORIGINAL_ASSET_CLOUD_ID = "original_asset_cloud_id";
113 const std::string PhotoColumn::PHOTO_SOURCE_PATH = "source_path";
114 const std::string PhotoColumn::PHOTO_CLOUD_ID_INDEX = "cloud_id_index";
115 const std::string PhotoColumn::PHOTO_DATE_YEAR_INDEX = "date_year_index";
116 const std::string PhotoColumn::PHOTO_DATE_MONTH_INDEX = "date_month_index";
117 const std::string PhotoColumn::PHOTO_DATE_DAY_INDEX = "date_day_index";
118 const std::string PhotoColumn::PHOTO_SCHPT_ADDED_INDEX = "idx_schpt_date_added";
119 const std::string PhotoColumn::PHOTO_SCHPT_ADDED_ALBUM_INDEX = "idx_schpt_date_added_album";
120 const std::string PhotoColumn::PHOTO_SCHPT_MEDIA_TYPE_INDEX = "idx_schpt_media_type";
121 const std::string PhotoColumn::PHOTO_SCHPT_DAY_INDEX = "idx_schpt_date_day";
122 const std::string PhotoColumn::PHOTO_HIDDEN_TIME_INDEX = "hidden_time_index";
123 const std::string PhotoColumn::PHOTO_SCHPT_HIDDEN_TIME_INDEX = "idx_schpt_hidden_time";
124 const std::string PhotoColumn::PHOTO_FAVORITE_INDEX = "idx_photo_is_favorite";
125 const std::string PhotoColumn::PHOTO_DISPLAYNAME_INDEX = "idx_display_name";
126 const std::string PhotoColumn::PHOTO_SCHPT_READY_INDEX = "idx_schpt_thumbnail_ready";
127 const std::string PhotoColumn::PHOTO_BURSTKEY_INDEX = "idx_burstkey";
128 const std::string PhotoColumn::PHOTO_SCHPT_MEDIA_TYPE_COUNT_READY_INDEX = "idx_schpt_media_type_ready";
129 const std::string PhotoColumn::PHOTO_SCHPT_DATE_YEAR_COUNT_READY_INDEX = "idx_schpt_date_year_ready";
130 const std::string PhotoColumn::PHOTO_SCHPT_DATE_MONTH_COUNT_READY_INDEX = "idx_schpt_date_month_ready";
131 const std::string PhotoColumn::SUPPORTED_WATERMARK_TYPE = "supported_watermark_type";
132 
133 const std::string PhotoColumn::PHOTO_DATE_YEAR_FORMAT = "%Y";
134 const std::string PhotoColumn::PHOTO_DATE_MONTH_FORMAT = "%Y%m";
135 const std::string PhotoColumn::PHOTO_DATE_DAY_FORMAT = "%Y%m%d";
136 const std::string PhotoColumn::PHOTO_DETAIL_TIME_FORMAT = "%Y:%m:%d %H:%M:%S";
137 
138 const std::string PhotoColumn::PHOTOS_TABLE = "Photos";
139 
140 const std::string PhotoColumn::PHOTO_URI_PREFIX = "file://media/Photo/";
141 const std::string PhotoColumn::DEFAULT_PHOTO_URI = "file://media/Photo";
142 const std::string PhotoColumn::PHOTO_CACHE_URI_PREFIX = "file://media/Photo/cache/";
143 const std::string PhotoColumn::PHOTO_TYPE_URI = "/Photo";
144 const std::string PhotoColumn::HIGHTLIGHT_COVER_URI = "/highlight";
145 const std::string PhotoColumn::HIGHTLIGHT_URI = "/highlight/video";
146 
147 const std::string PhotoColumn::PHOTO_CLOUD_URI_PREFIX = "file://cloudsync/Photo/";
148 
149 const std::string PhotoColumn::PHOTO_HEIGHT_ERROR_URI_PREFIX = "file://cloudsync/Photo/HeightError/";
150 const std::string PhotoColumn::PHOTO_DOWNLOAD_SUCCEED_URI_PREFIX = "file://cloudsync/Photo/DownloadSucceed/";
151 
152 const std::string PhotoColumn::PHOTO_REQUEST_PICTURE = "file://media/Photo/picture/";
153 const std::string PhotoColumn::PHOTO_REQUEST_PICTURE_BUFFER = "file://media/Photo/pictureBuffer/";
154 
155 const std::set<std::string> PhotoColumn::DEFAULT_FETCH_COLUMNS = {
156     PHOTO_SUBTYPE, PHOTO_BURST_KEY,
157 };
158 
159 const std::string PhotoColumn::CREATE_PHOTO_TABLE = "CREATE TABLE IF NOT EXISTS " +
160     PHOTOS_TABLE + " (" +
161     MEDIA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
162     MEDIA_FILE_PATH + " TEXT, " +
163     MEDIA_SIZE + " BIGINT, " +
164     MEDIA_TITLE + " TEXT, " +
165     MEDIA_NAME + " TEXT, " +
166     MEDIA_TYPE + " INT, " +
167     MEDIA_MIME_TYPE + " TEXT, " +
168     MEDIA_OWNER_PACKAGE + " TEXT, " +
169     MEDIA_OWNER_APPID + " TEXT, " +
170     MEDIA_PACKAGE_NAME + " TEXT, " +
171     MEDIA_DEVICE_NAME + " TEXT, " +
172     MEDIA_DATE_ADDED + " BIGINT, " +
173     MEDIA_DATE_MODIFIED + " BIGINT, " +
174     MEDIA_DATE_TAKEN + " BIGINT DEFAULT 0, " +
175     MEDIA_DURATION + " INT, " +
176     MEDIA_TIME_PENDING + " BIGINT DEFAULT 0, " +
177     MEDIA_IS_FAV + " INT DEFAULT 0, " +
178     MEDIA_DATE_TRASHED + " BIGINT DEFAULT 0, " +
179     MEDIA_DATE_DELETED + " BIGINT DEFAULT 0, " +
180     MEDIA_HIDDEN + " INT DEFAULT 0, " +
181     MEDIA_PARENT_ID + " INT DEFAULT 0, " +
182     MEDIA_RELATIVE_PATH + " TEXT, " +
183     MEDIA_VIRTURL_PATH + " TEXT UNIQUE, " +
184     PHOTO_DIRTY + " INT DEFAULT 1, " +
185     PHOTO_CLOUD_ID + " TEXT, " +
186     PHOTO_META_DATE_MODIFIED + "  BIGINT DEFAULT 0, " +
187     PHOTO_SYNC_STATUS + "  INT DEFAULT 0, " +
188     PHOTO_CLOUD_VERSION + " BIGINT DEFAULT 0, " +
189     PHOTO_ORIENTATION + " INT DEFAULT 0, " +
190     PHOTO_LATITUDE + " DOUBLE DEFAULT 0, " +
191     PHOTO_LONGITUDE + " DOUBLE DEFAULT 0, " +
192     PHOTO_HEIGHT + " INT, " +
193     PHOTO_WIDTH + " INT, " +
194     PHOTO_EDIT_TIME + " BIGINT DEFAULT 0, " +
195     PHOTO_LCD_VISIT_TIME + " BIGINT DEFAULT 0, " +
196     PHOTO_POSITION + " INT DEFAULT 1, " +
197     PHOTO_SUBTYPE + " INT DEFAULT 0, " +
198     PHOTO_ORIGINAL_SUBTYPE + " INT," +
199     CAMERA_SHOT_KEY + " TEXT, " +
200     PHOTO_USER_COMMENT + " TEXT, " +
201     PHOTO_ALL_EXIF  + " TEXT, " +
202     PHOTO_DATE_YEAR + " TEXT, " +
203     PHOTO_DATE_MONTH + " TEXT, " +
204     PHOTO_DATE_DAY + " TEXT, " +
205     PHOTO_SHOOTING_MODE + " TEXT, " +
206     PHOTO_SHOOTING_MODE_TAG + " TEXT, " +
207     PHOTO_LAST_VISIT_TIME + " BIGINT DEFAULT 0, " +
208     PHOTO_HIDDEN_TIME + " BIGINT DEFAULT 0, " +
209     PHOTO_THUMB_STATUS + " INT DEFAULT 0, " +
210     PHOTO_CLEAN_FLAG + " INT DEFAULT 0, " +
211     PHOTO_ID + " TEXT, " +
212     PHOTO_QUALITY + " INT, " +
213     PHOTO_FIRST_VISIT_TIME + " BIGINT DEFAULT 0, " +
214     PHOTO_DEFERRED_PROC_TYPE + " INT DEFAULT 0, " +
215     PHOTO_DYNAMIC_RANGE_TYPE + " INT DEFAULT 0, " +
216     MOVING_PHOTO_EFFECT_MODE + " INT DEFAULT 0, " +
217     PHOTO_COVER_POSITION + " BIGINT DEFAULT 0, " +
218     PHOTO_THUMBNAIL_READY + " BIGINT DEFAULT 0, " +
219     PHOTO_LCD_SIZE + " TEXT, " +
220     PHOTO_THUMB_SIZE + " TEXT," +
221     PHOTO_FRONT_CAMERA + " TEXT, " +
222     PHOTO_IS_TEMP + " INT DEFAULT 0," +
223     PHOTO_BURST_COVER_LEVEL + " INT DEFAULT 1, " +
224     PHOTO_BURST_KEY + " TEXT, " +
225     PHOTO_CE_AVAILABLE + " INT DEFAULT 0, " +
226     PHOTO_CE_STATUS_CODE + " INT, " +
227     PHOTO_STRONG_ASSOCIATION + " INT DEFAULT 0, " +
228     PHOTO_ASSOCIATE_FILE_ID + " INT DEFAULT 0, " +
229     PHOTO_HAS_CLOUD_WATERMARK + " INT DEFAULT 0, " +
230     PHOTO_DETAIL_TIME + " TEXT, " +
231     PHOTO_OWNER_ALBUM_ID + " INT DEFAULT 0, " +
232     PHOTO_ORIGINAL_ASSET_CLOUD_ID + " TEXT, " +
233     PHOTO_THUMBNAIL_VISIBLE + " INT DEFAULT 0, " +
234     PHOTO_SOURCE_PATH + " TEXT, " +
235     SUPPORTED_WATERMARK_TYPE + " INT) ";
236 
237 const std::string PhotoColumn::CREATE_CLOUD_ID_INDEX = BaseColumn::CreateIndex() +
238     PHOTO_CLOUD_ID_INDEX + " ON " + PHOTOS_TABLE + " (" + PHOTO_CLOUD_ID + " DESC)";
239 
240 const std::string PhotoColumn::CREATE_YEAR_INDEX = BaseColumn::CreateIndex() +
241     PHOTO_DATE_YEAR_INDEX + " ON " + PHOTOS_TABLE + " (" + PHOTO_DATE_YEAR + " DESC)";
242 
243 const std::string PhotoColumn::CREATE_MONTH_INDEX = BaseColumn::CreateIndex() +
244     PHOTO_DATE_MONTH_INDEX + " ON " + PHOTOS_TABLE + " (" + PHOTO_DATE_MONTH + " DESC)";
245 
246 const std::string PhotoColumn::CREATE_DAY_INDEX = BaseColumn::CreateIndex() +
247     PHOTO_DATE_DAY_INDEX + " ON " + PHOTOS_TABLE + " (" + PHOTO_DATE_DAY + " DESC)";
248 
249 const std::string PhotoColumn::CREATE_SCHPT_DAY_INDEX = BaseColumn::CreateIndex() + PHOTO_SCHPT_DAY_INDEX + " ON " +
250     PHOTOS_TABLE + " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + PHOTO_THUMBNAIL_VISIBLE + "," +
251     MEDIA_DATE_TRASHED + "," + MEDIA_TIME_PENDING + "," + MEDIA_HIDDEN + "," + PHOTO_IS_TEMP + "," +
252     PHOTO_BURST_COVER_LEVEL + "," + PHOTO_DATE_DAY + " DESC);";
253 
254 const std::string PhotoColumn::DROP_SCHPT_DAY_INDEX = BaseColumn::DropIndex() + PHOTO_SCHPT_DAY_INDEX;
255 
256 const std::string PhotoColumn::DROP_SCHPT_MEDIA_TYPE_INDEX = "DROP INDEX IF EXISTS " + PHOTO_SCHPT_MEDIA_TYPE_INDEX;
257 
258 const std::string PhotoColumn::CREATE_SCHPT_MEDIA_TYPE_INDEX = BaseColumn::CreateIndex() +
259     PHOTO_SCHPT_MEDIA_TYPE_INDEX + " ON " + PHOTOS_TABLE +
260     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + MEDIA_DATE_TRASHED + "," + MEDIA_HIDDEN +
261     "," + MEDIA_TIME_PENDING + ", " + PHOTO_IS_TEMP + "," + MEDIA_TYPE + "," + PHOTO_BURST_COVER_LEVEL +
262     "," + MEDIA_DATE_ADDED + " DESC);";
263 
264 const std::string PhotoColumn::CREATE_SCHPT_YEAR_COUNT_READY_INDEX = BaseColumn::CreateIndex() +
265     PHOTO_SCHPT_DATE_YEAR_COUNT_READY_INDEX + " ON " + PHOTOS_TABLE +
266     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + PHOTO_THUMBNAIL_VISIBLE + "," + MEDIA_DATE_TRASHED +
267     "," + MEDIA_TIME_PENDING + "," + MEDIA_HIDDEN + "," + PHOTO_IS_TEMP + "," +
268     PHOTO_BURST_COVER_LEVEL + "," + PHOTO_DATE_YEAR + " DESC);";
269 
270 const std::string PhotoColumn::CREATE_SCHPT_MONTH_COUNT_READY_INDEX = BaseColumn::CreateIndex() +
271     PHOTO_SCHPT_DATE_MONTH_COUNT_READY_INDEX + " ON " + PHOTOS_TABLE +
272     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + PHOTO_THUMBNAIL_VISIBLE + "," + MEDIA_DATE_TRASHED +
273     "," + MEDIA_TIME_PENDING + "," + MEDIA_HIDDEN + "," + PHOTO_IS_TEMP + "," +
274     PHOTO_BURST_COVER_LEVEL + "," + PHOTO_DATE_MONTH + " DESC);";
275 
276 const std::string PhotoColumn::CREATE_SCHPT_MEDIA_TYPE_COUNT_READY_INDEX = BaseColumn::CreateIndex() +
277     PHOTO_SCHPT_MEDIA_TYPE_COUNT_READY_INDEX + " ON " + PHOTOS_TABLE +
278     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + PHOTO_THUMBNAIL_VISIBLE + "," + MEDIA_DATE_TRASHED +
279     "," + MEDIA_TIME_PENDING + "," + MEDIA_HIDDEN + ", " + PHOTO_IS_TEMP + "," + PHOTO_BURST_COVER_LEVEL +
280     "," + MEDIA_TYPE + ");";
281 
282 const std::string PhotoColumn::DROP_SCHPT_YEAR_COUNT_READY_INDEX = "DROP INDEX IF EXISTS " +
283     PHOTO_SCHPT_DATE_YEAR_COUNT_READY_INDEX;
284 
285 const std::string PhotoColumn::DROP_SCHPT_MONTH_COUNT_READY_INDEX = "DROP INDEX IF EXISTS " +
286     PHOTO_SCHPT_DATE_MONTH_COUNT_READY_INDEX;
287 
288 const std::string PhotoColumn::DROP_SCHPT_MEDIA_TYPE_COUNT_READY_INDEX = "DROP INDEX IF EXISTS " +
289     PHOTO_SCHPT_MEDIA_TYPE_COUNT_READY_INDEX;
290 
291 const std::string PhotoColumn::CREATE_HIDDEN_TIME_INDEX = BaseColumn::CreateIndex() +
292     PHOTO_HIDDEN_TIME_INDEX + " ON " + PHOTOS_TABLE + " (" + PHOTO_HIDDEN_TIME + " DESC)";
293 
294 const std::string PhotoColumn::CREATE_SCHPT_HIDDEN_TIME_INDEX =
295     BaseColumn::CreateIndex() + PHOTO_SCHPT_HIDDEN_TIME_INDEX + " ON " + PHOTOS_TABLE +
296     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + MEDIA_HIDDEN + "," + MEDIA_TIME_PENDING +
297     "," + MEDIA_DATE_TRASHED + "," + PHOTO_IS_TEMP + "," + PHOTO_BURST_COVER_LEVEL +
298     "," + PHOTO_HIDDEN_TIME + " DESC);";
299 
300 const std::string PhotoColumn::DROP_SCHPT_HIDDEN_TIME_INDEX = BaseColumn::DropIndex() + PHOTO_SCHPT_HIDDEN_TIME_INDEX;
301 
302 const std::string PhotoColumn::CREATE_PHOTO_FAVORITE_INDEX =
303     BaseColumn::CreateIndex() + PHOTO_FAVORITE_INDEX + " ON " + PHOTOS_TABLE +
304     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + MEDIA_HIDDEN + "," + MEDIA_TIME_PENDING +
305     "," + MEDIA_DATE_TRASHED + "," + PHOTO_IS_TEMP + "," + MEDIA_IS_FAV + "," + PHOTO_BURST_COVER_LEVEL +
306     "," + MEDIA_DATE_TAKEN + " DESC);";
307 
308 const std::string PhotoColumn::DROP_PHOTO_FAVORITE_INDEX = BaseColumn::DropIndex() + PHOTO_FAVORITE_INDEX;
309 
310 const std::string PhotoColumn::CREATE_PHOTO_DISPLAYNAME_INDEX = BaseColumn::CreateIndex() +
311     PHOTO_DISPLAYNAME_INDEX + " ON " + PHOTOS_TABLE + " (" + MediaColumn::MEDIA_NAME + ")";
312 
313 const std::string PhotoColumn::CREATE_PHOTO_BURSTKEY_INDEX = BaseColumn::CreateIndex() + PHOTO_BURSTKEY_INDEX +
314     " ON " + PHOTOS_TABLE + " (" + PHOTO_BURST_KEY + "," + MEDIA_TIME_PENDING  + "," +
315     MediaColumn::MEDIA_NAME + " ASC);";
316 
317 const std::string PhotoColumn::QUERY_MEDIA_VOLUME = "SELECT sum(" + MediaColumn::MEDIA_SIZE + ") AS " +
318     MediaColumn::MEDIA_SIZE + "," +
319     MediaColumn::MEDIA_TYPE + " FROM " +
320     PhotoColumn::PHOTOS_TABLE + " WHERE " +
321     "(" + MediaColumn::MEDIA_TYPE + " = " + std::to_string(MEDIA_TYPE_IMAGE) + " OR " +
322     MediaColumn::MEDIA_TYPE + " = " + std::to_string(MEDIA_TYPE_VIDEO) + ") AND " +
323     PhotoColumn::PHOTO_POSITION + " != 2" + " GROUP BY " +
324     MediaColumn::MEDIA_TYPE;
325 
326 // Create indexes
327 const std::string PhotoColumn::INDEX_SCTHP_ADDTIME =
328     BaseColumn::CreateIndex() + PHOTO_SCHPT_ADDED_INDEX + " ON " + PHOTOS_TABLE +
329     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + MEDIA_DATE_TRASHED + "," + MEDIA_HIDDEN + "," +
330     MEDIA_TIME_PENDING + "," + PHOTO_IS_TEMP + "," + PHOTO_BURST_COVER_LEVEL + "," + MEDIA_DATE_TAKEN + " DESC, " +
331     MEDIA_ID + " DESC);";
332 
333 const std::string PhotoColumn::DROP_INDEX_SCTHP_ADDTIME = BaseColumn::DropIndex() + PHOTO_SCHPT_ADDED_INDEX;
334 
335 const std::string PhotoColumn::DROP_INDEX_SCHPT_ADDTIME_ALBUM = BaseColumn::DropIndex() + PHOTO_SCHPT_ADDED_ALBUM_INDEX;
336 
337 const std::string PhotoColumn::INDEX_CAMERA_SHOT_KEY =
338     BaseColumn::CreateIndex() + "idx_camera_shot_key" + " ON " + PHOTOS_TABLE +
339     " (" + CAMERA_SHOT_KEY + ");";
340 
341 const std::string PhotoColumn::INDEX_SCHPT_READY =
342     BaseColumn::CreateIndex() + PHOTO_SCHPT_READY_INDEX + " ON " + PHOTOS_TABLE +
343     " (" + PHOTO_SYNC_STATUS + "," + PHOTO_CLEAN_FLAG + "," + PHOTO_THUMBNAIL_VISIBLE + "," + MEDIA_DATE_TRASHED +
344     "," + MEDIA_TIME_PENDING + ", " + MEDIA_HIDDEN + "," + PHOTO_IS_TEMP + "," + PHOTO_BURST_COVER_LEVEL +
345     "," + MEDIA_DATE_TAKEN + " DESC, " + MEDIA_ID + " DESC);";
346 
347 const std::string PhotoColumn::DROP_INDEX_SCHPT_READY = BaseColumn::DropIndex() + PHOTO_SCHPT_READY_INDEX;
348 
349 const std::string PhotoColumn::CREATE_PHOTOS_DELETE_TRIGGER =
350                         "CREATE TRIGGER IF NOT EXISTS photos_delete_trigger AFTER UPDATE ON " +
351                         PhotoColumn::PHOTOS_TABLE + " FOR EACH ROW WHEN new." + PhotoColumn::PHOTO_DIRTY +
352                         " = " + std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_DELETED)) +
353                         " AND OLD." + PhotoColumn::PHOTO_CLOUD_ID + " is NULL AND is_caller_self_func() = 'true'" +
354                         " BEGIN DELETE FROM " + PhotoColumn::PHOTOS_TABLE +
355                         " WHERE " + PhotoColumn::MEDIA_ID + " = old." + PhotoColumn::MEDIA_ID + "; END;";
356 
357 const std::string PhotoColumn::CREATE_PHOTOS_FDIRTY_TRIGGER =
358                         "CREATE TRIGGER IF NOT EXISTS photos_fdirty_trigger AFTER UPDATE ON " +
359                         PhotoColumn::PHOTOS_TABLE + " FOR EACH ROW WHEN OLD.cloud_id IS NOT NULL AND" +
360                         " new.date_modified <> old.date_modified " +
361                         " AND new.dirty = old.dirty AND is_caller_self_func() = 'true'" +
362                         " BEGIN " +
363                         " UPDATE " + PhotoColumn::PHOTOS_TABLE + " SET dirty = " +
364                         std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_FDIRTY)) +
365                         " WHERE file_id = old.file_id;" +
366                         " SELECT cloud_sync_func(); " +
367                         " END;";
368 
369 const std::string PhotoColumn::CREATE_PHOTOS_MDIRTY_TRIGGER =
370                         "CREATE TRIGGER IF NOT EXISTS photos_mdirty_trigger AFTER UPDATE ON " +
371                         PhotoColumn::PHOTOS_TABLE + " FOR EACH ROW WHEN OLD.cloud_id IS NOT NULL" +
372                         " AND new.date_modified = old.date_modified AND ( old.dirty = " +
373                         std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_SYNCED)) + " OR old.dirty =" +
374                         std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_SDIRTY)) +
375                         ") AND new.dirty = old.dirty AND is_caller_self_func() = 'true'" +
376                         " AND " + PhotoColumn::CheckUploadPhotoColumns() +
377                         " BEGIN " +
378                         " UPDATE " + PhotoColumn::PHOTOS_TABLE + " SET dirty = " +
379                         std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_MDIRTY)) +
380                         " WHERE file_id = old.file_id;" +
381                         " SELECT cloud_sync_func(); " +
382                         " END;";
383 
384 const std::string  PhotoColumn::CREATE_PHOTOS_INSERT_CLOUD_SYNC =
385                         " CREATE TRIGGER IF NOT EXISTS photo_insert_cloud_sync_trigger AFTER INSERT ON " +
386                         PhotoColumn::PHOTOS_TABLE + " BEGIN SELECT cloud_sync_func(); END;";
387 
388 const std::string PhotoColumn::CREATE_PHOTOS_UPDATE_CLOUD_SYNC =
389                         " CREATE TRIGGER IF NOT EXISTS photo_update_cloud_sync_trigger AFTER UPDATE ON " +
390                         PhotoColumn::PHOTOS_TABLE + " FOR EACH ROW WHEN OLD.dirty IN (1,2,3,5) AND new.dirty != " +
391                         std::to_string(static_cast<int32_t>(DirtyTypes::TYPE_SYNCED)) +
392                         " BEGIN SELECT cloud_sync_func(); END;";
393 
394 const std::string PhotoColumn::UPDATE_READY_ON_THUMBNAIL_UPGRADE =
395                         " UPDATE " + PhotoColumn::PHOTOS_TABLE + " SET " + PhotoColumn::PHOTO_THUMBNAIL_READY +
396                         " = 6 " + " WHERE " + PhotoColumn::PHOTO_THUMBNAIL_READY + " != 0; END;";
397 
398 const std::string PhotoColumn::UPDATA_PHOTOS_DATA_UNIQUE = "CREATE UNIQUE INDEX IF NOT EXISTS photo_data_index ON " +
399     PhotoColumn::PHOTOS_TABLE + " (" + MEDIA_FILE_PATH + ");";
400 
401 const std::string PhotoColumn::UPDATE_LCD_STATUS_NOT_UPLOADED =
402                         " UPDATE " + PhotoColumn::PHOTOS_TABLE + " SET " + PhotoColumn::PHOTO_LCD_VISIT_TIME +
403                         " = 0 " + " WHERE " + PhotoColumn::PHOTO_DIRTY + " = 1; END;";
404 
405 const std::set<std::string> PhotoColumn::PHOTO_COLUMNS = {
406     PhotoColumn::PHOTO_ORIENTATION, PhotoColumn::PHOTO_LATITUDE, PhotoColumn::PHOTO_LONGITUDE,
407     PhotoColumn::PHOTO_HEIGHT, PhotoColumn::PHOTO_WIDTH, PhotoColumn::PHOTO_LCD_VISIT_TIME, PhotoColumn::PHOTO_POSITION,
408     PhotoColumn::PHOTO_DIRTY, PhotoColumn::PHOTO_CLOUD_ID, PhotoColumn::CAMERA_SHOT_KEY, PhotoColumn::PHOTO_ALL_EXIF,
409     PhotoColumn::PHOTO_USER_COMMENT, PhotoColumn::PHOTO_DATE_YEAR, PhotoColumn::PHOTO_DATE_MONTH,
410     PhotoColumn::PHOTO_DATE_DAY, PhotoColumn::PHOTO_EDIT_TIME, PhotoColumn::PHOTO_CLEAN_FLAG,
411     PhotoColumn::PHOTO_SHOOTING_MODE, PhotoColumn::PHOTO_SHOOTING_MODE_TAG, PhotoColumn::PHOTO_THUMB_STATUS,
412     PhotoColumn::PHOTO_SUBTYPE, PhotoColumn::PHOTO_DYNAMIC_RANGE_TYPE, PhotoColumn::PHOTO_LCD_SIZE,
413     PhotoColumn::PHOTO_THUMB_SIZE, PhotoColumn::MOVING_PHOTO_EFFECT_MODE, PhotoColumn::PHOTO_COVER_POSITION,
414     PhotoColumn::PHOTO_FRONT_CAMERA, PhotoColumn::PHOTO_BURST_COVER_LEVEL, PhotoColumn::PHOTO_BURST_KEY,
415     PhotoColumn::PHOTO_THUMBNAIL_READY, PhotoColumn::PHOTO_ORIGINAL_SUBTYPE, PhotoColumn::PHOTO_CE_AVAILABLE,
416     PhotoColumn::PHOTO_DETAIL_TIME, PhotoColumn::PHOTO_OWNER_ALBUM_ID, PhotoColumn::PHOTO_THUMBNAIL_VISIBLE,
417     PhotoColumn::SUPPORTED_WATERMARK_TYPE,
418 };
419 
IsPhotoColumn(const std::string & columnName)420 bool PhotoColumn::IsPhotoColumn(const std::string &columnName)
421 {
422     if (columnName == "count(*)") {
423         return true;
424     }
425     return (PHOTO_COLUMNS.find(columnName) != PHOTO_COLUMNS.end()) ||
426         (MEDIA_COLUMNS.find(columnName) != MEDIA_COLUMNS.end());
427 }
428 
CheckUploadPhotoColumns()429 std::string PhotoColumn::CheckUploadPhotoColumns()
430 {
431     // Since date_modified has been checked in mdirty and fdirty, omit it here.
432     const std::vector<std::string> uploadPhotoColumns = {
433         MEDIA_FILE_PATH,
434         MEDIA_SIZE,
435         MEDIA_NAME,
436         MEDIA_TYPE,
437         MEDIA_MIME_TYPE,
438         MEDIA_OWNER_PACKAGE,
439         MEDIA_OWNER_APPID,
440         MEDIA_DEVICE_NAME,
441         MEDIA_DATE_ADDED,
442         MEDIA_DATE_TAKEN,
443         MEDIA_DURATION,
444         MEDIA_IS_FAV,
445         MEDIA_DATE_TRASHED,
446         MEDIA_DATE_DELETED,
447         MEDIA_HIDDEN,
448         PHOTO_META_DATE_MODIFIED,
449         PHOTO_ORIENTATION,
450         PHOTO_LATITUDE,
451         PHOTO_LONGITUDE,
452         PHOTO_HEIGHT,
453         PHOTO_WIDTH,
454         PHOTO_SUBTYPE,
455         PHOTO_USER_COMMENT,
456         PHOTO_DATE_YEAR,
457         PHOTO_DATE_MONTH,
458         PHOTO_DATE_DAY,
459         PHOTO_SHOOTING_MODE,
460         PHOTO_SHOOTING_MODE_TAG,
461         MOVING_PHOTO_EFFECT_MODE,
462         PHOTO_COVER_POSITION,
463         PHOTO_ORIGINAL_SUBTYPE,
464         PHOTO_OWNER_ALBUM_ID,
465         PHOTO_SOURCE_PATH,
466     };
467 
468     std::string result = "(";
469     size_t size = uploadPhotoColumns.size();
470     for (size_t i = 0; i < size; i++) {
471         std::string column = uploadPhotoColumns[i];
472         if (i != size - 1) {
473             result += "new." + column + " <> old." + column + " OR ";
474         } else {
475             result += "new." + column + " <> old." + column + ")";
476         }
477     }
478     return result;
479 }
480 
481 const std::string AudioColumn::AUDIO_ALBUM = "audio_album";
482 const std::string AudioColumn::AUDIO_ARTIST = "artist";
483 
484 const std::string AudioColumn::AUDIOS_TABLE = "Audios";
485 
486 const std::string AudioColumn::AUDIO_URI_PREFIX = "file://media/Audio/";
487 const std::string AudioColumn::DEFAULT_AUDIO_URI = "file://media/Audio";
488 const std::string AudioColumn::AUDIO_TYPE_URI = "/Audio";
489 
490 const std::string AudioColumn::CREATE_AUDIO_TABLE = "CREATE TABLE IF NOT EXISTS " +
491     AUDIOS_TABLE + " (" +
492     MEDIA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
493     MEDIA_FILE_PATH + " TEXT, " +
494     MEDIA_SIZE + " BIGINT, " +
495     MEDIA_TITLE + " TEXT, " +
496     MEDIA_NAME + " TEXT, " +
497     MEDIA_TYPE + " INT, " +
498     MEDIA_MIME_TYPE + " TEXT, " +
499     MEDIA_OWNER_PACKAGE + " TEXT, " +
500     MEDIA_OWNER_APPID + " TEXT, " +
501     MEDIA_PACKAGE_NAME + " TEXT, " +
502     MEDIA_DEVICE_NAME + " TEXT, " +
503     AUDIO_ARTIST + " TEXT, " +
504     MEDIA_DATE_ADDED + " BIGINT, " +
505     MEDIA_DATE_MODIFIED + " BIGINT, " +
506     MEDIA_DATE_TAKEN + " BIGINT DEFAULT 0, " +
507     MEDIA_DURATION + " INT, " +
508     MEDIA_TIME_PENDING + " BIGINT DEFAULT 0, " +
509     MEDIA_IS_FAV + " INT DEFAULT 0, " +
510     MEDIA_DATE_TRASHED + " BIGINT DEFAULT 0, " +
511     MEDIA_DATE_DELETED + " BIGINT DEFAULT 0, " +
512     MEDIA_PARENT_ID + " INT DEFAULT 0, " +
513     MEDIA_RELATIVE_PATH + " TEXT, " +
514     MEDIA_VIRTURL_PATH + " TEXT UNIQUE, " +
515     AUDIO_ALBUM + " TEXT)";
516 
517 const std::string AudioColumn::QUERY_MEDIA_VOLUME = "SELECT sum(" + MediaColumn::MEDIA_SIZE + ") AS " +
518     MediaColumn::MEDIA_SIZE + "," +
519     MediaColumn::MEDIA_TYPE + " FROM " +
520     AudioColumn::AUDIOS_TABLE + " WHERE " +
521     MediaColumn::MEDIA_TYPE + " = " + std::to_string(MEDIA_TYPE_AUDIO) + " GROUP BY " +
522     MediaColumn::MEDIA_TYPE;
523 
524 const std::set<std::string> AudioColumn::AUDIO_COLUMNS = {
525     AudioColumn::AUDIO_ALBUM, AudioColumn::AUDIO_ARTIST
526 };
527 
IsAudioColumn(const std::string & columnName)528 bool AudioColumn::IsAudioColumn(const std::string &columnName)
529 {
530     return (AUDIO_COLUMNS.find(columnName) != AUDIO_COLUMNS.end()) ||
531         (MEDIA_COLUMNS.find(columnName) != MEDIA_COLUMNS.end());
532 }
533 
534 const std::string MediaColumn::ASSETS_QUERY_FILTER =
535     PhotoColumn::PHOTO_SYNC_STATUS + " = 0" + " AND " +
536     MediaColumn::MEDIA_DATE_TRASHED + " = 0" + " AND " +
537     MediaColumn::MEDIA_HIDDEN + " = 0" + " AND " +
538     MediaColumn::MEDIA_TIME_PENDING + " = 0 ";
539 
540 const std::string PhotoExtColumn::PHOTOS_EXT_TABLE = "tab_photos_ext";
541 
542 const std::string PhotoExtColumn::PHOTO_ID = "photo_id";
543 const std::string PhotoExtColumn::THUMBNAIL_SIZE = "thumbnail_size";
544 
545 const std::string PhotoExtColumn::CREATE_PHOTO_EXT_TABLE =
546     "CREATE TABLE IF NOT EXISTS " +
547     PHOTOS_EXT_TABLE + " (" +
548     PHOTO_ID + " INTEGER PRIMARY KEY, " +
549     THUMBNAIL_SIZE + " BIGINT DEFAULT 0)";
550 
551 // For Photos table query filter
552 const std::string PhotoColumn::PHOTOS_QUERY_FILTER =
553     MediaColumn::MEDIA_DATE_TRASHED + " = 0" + " AND " +
554     MediaColumn::MEDIA_HIDDEN + " = 0" + " AND " +
555     MediaColumn::MEDIA_TIME_PENDING + " = 0" + " AND " +
556     PhotoColumn::PHOTO_IS_TEMP + " = 0" + " AND " +
557     PhotoColumn::PHOTO_BURST_COVER_LEVEL + " = 1 ";
558 }  // namespace Media
559 }  // namespace OHOS
560