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 #ifndef OHOS_MEDIA_BACKUP_GALLERY_MEDIA_DAO_H
16 #define OHOS_MEDIA_BACKUP_GALLERY_MEDIA_DAO_H
17 
18 #include <string>
19 #include <vector>
20 
21 #include "rdb_store.h"
22 
23 namespace OHOS::Media {
24 class GalleryMediaDao {
25 public:
26     GalleryMediaDao() = default;
GalleryMediaDao(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)27     GalleryMediaDao(std::shared_ptr<NativeRdb::RdbStore> galleryRdb) : galleryRdb_(galleryRdb)
28     {}
29     void SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb);
30     std::shared_ptr<NativeRdb::ResultSet> GetGalleryMedia(
31         int32_t offset, int pageSize, bool shouldIncludeSd, bool hasLowQualityImage);
32     int32_t GetGalleryMediaCount(bool shouldIncludeSd, bool hasLowQualityImage);
33     int32_t GetNoNeedMigrateCount(bool shouldIncludeSd);
34 
35 private:
36     std::shared_ptr<NativeRdb::RdbStore> galleryRdb_;
37 
38 private:
39     const std::string SQL_GALLERY_MEDIA_QUERY_COUNT = "\
40         SELECT COUNT(1) AS count \
41         FROM gallery_media \
42             LEFT JOIN gallery_album \
43             ON gallery_media.albumId=gallery_album.albumId \
44             LEFT JOIN gallery_album AS album_v2 \
45             ON gallery_media.relative_bucket_id = album_v2.relativeBucketId \
46         WHERE (local_media_id != -1) AND \
47             (relative_bucket_id IS NULL OR \
48                 relative_bucket_id NOT IN ( \
49                     SELECT DISTINCT relative_bucket_id \
50                     FROM garbage_album \
51                     WHERE type = 1 \
52                 ) \
53             ) AND \
54             (_size > 0 OR (1 = ? AND _size = 0 AND photo_quality = 0)) AND \
55             _data NOT LIKE '/storage/emulated/0/Pictures/cloud/Imports%' AND \
56             COALESCE(_data, '') <> '' AND \
57             (1 = ? OR storage_id IN (0, 65537) ) \
58         ORDER BY _id ASC ;";
59     const std::string SQL_GALLERY_MEDIA_QUERY_FOR_RESTORE = "\
60         SELECT \
61             _id, \
62             local_media_id, \
63             _data, \
64             _display_name, \
65             description, \
66             is_hw_favorite, \
67             recycledTime, \
68             _size, \
69             duration, \
70             media_type, \
71             showDateToken, \
72             height, \
73             width, \
74             title, \
75             orientation, \
76             date_modified, \
77             relative_bucket_id, \
78             sourcePath, \
79             is_hw_burst, \
80             recycleFlag, \
81             hash, \
82             special_file_type, \
83             first_update_time, \
84             datetaken, \
85             detail_time, \
86             photo_quality, \
87             CASE WHEN COALESCE(gallery_album.lPath, '') <> '' \
88                 THEN gallery_album.lPath \
89                 ELSE album_v2.lPath \
90             END AS lPath \
91         FROM gallery_media \
92             LEFT JOIN gallery_album \
93             ON gallery_media.albumId=gallery_album.albumId \
94             LEFT JOIN gallery_album AS album_v2 \
95             ON gallery_media.relative_bucket_id = album_v2.relativeBucketId \
96         WHERE (local_media_id != -1) AND \
97             (relative_bucket_id IS NULL OR \
98                 relative_bucket_id NOT IN ( \
99                     SELECT DISTINCT relative_bucket_id \
100                     FROM garbage_album \
101                     WHERE type = 1 \
102                 ) \
103             ) AND \
104             (_size > 0 OR (1 = ? AND _size = 0 AND photo_quality = 0)) AND \
105             _data NOT LIKE '/storage/emulated/0/Pictures/cloud/Imports%' AND \
106             COALESCE(_data, '') <> '' AND \
107             (1 = ? OR storage_id IN (0, 65537) ) \
108         ORDER BY _id ASC \
109         LIMIT ?, ?;";
110     const std::string SQL_GALLERY_MEDIA_QUERY_NO_NEED_MIGRATE_COUNT = "\
111         SELECT COUNT(1) AS count \
112         FROM gallery_media \
113         WHERE (local_media_id = -1) OR \
114             _data LIKE '/storage/emulated/0/Pictures/cloud/Imports%' OR \
115             (0 = ? AND storage_id NOT IN (0, 65537));";
116 };
117 }  // namespace OHOS::Media
118 #endif  // OHOS_MEDIA_PHOTO_ALBUM_DAO_H