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_PHOTOS_DAO
16 #define OHOS_MEDIA_PHOTOS_DAO
17 
18 #include <string>
19 
20 #include "backup_const.h"
21 #include "rdb_store.h"
22 #include "media_log.h"
23 
24 namespace OHOS::Media {
25 class PhotosDao {
26 public:
27     struct PhotosRowData {
28         int32_t fileId;
29         std::string data;
30         int32_t ownerAlbumId;
31         std::string burstKey;
32         int32_t cleanFlag;
33         int32_t position;
34     };
35 
36     struct PhotosBasicInfo {
37         int32_t maxFileId;
38         int32_t count;
39     };
40 
41 public:
SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)42     void SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)
43     {
44         this->mediaLibraryRdb_ = mediaLibraryRdb;
45     }
46     /**
47      * @brief Find same file info by lPath, displayName, size, orientation.
48      * lPath - if original fileInfo's lPath is empty, it will be ignored.
49      * orientation - if original fileInfo's fileType is not Video(2), it will be ignored.
50      */
51     PhotosRowData FindSameFile(const FileInfo &fileInfo, int32_t maxFileId);
52     PhotosBasicInfo GetBasicInfo();
53 
54 private:
55     PhotosRowData FindSameFileWithoutAlbum(const FileInfo &fileInfo, int32_t maxFileId);
56     PhotosRowData FindSameFileInAlbum(const FileInfo &fileInfo, int32_t maxFileId);
57     PhotosRowData FindSameFileBySourcePath(const FileInfo &fileInfo, int32_t maxFileId);
58     std::string ToString(const FileInfo &fileInfo);
59     std::string ToString(const PhotosRowData &rowData);
60     std::string ToLower(const std::string &str);
61 
62 private:
63     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
64 
65 private:
66     const std::string SOURCE_PATH_PREFIX = "/storage/emulated/0";
67     const std::string SQL_PHOTOS_BASIC_INFO = "\
68         SELECT \
69             MAX(file_id) AS max_file_id, \
70             COUNT(1) AS count \
71         FROM Photos; \
72     ";
73     const std::string SQL_PHOTOS_FIND_SAME_FILE_IN_ALBUM = "\
74         SELECT \
75             p.file_id, \
76             p.data, \
77             p.clean_flag, \
78             p.position \
79         FROM \
80         ( \
81             SELECT album_id \
82             FROM PhotoAlbum \
83             WHERE LOWER(lpath) = LOWER(?) \
84         ) \
85         AS a \
86         INNER JOIN \
87         ( \
88             SELECT \
89                 file_id, \
90                 data, \
91                 clean_flag, \
92                 position, \
93                 size, \
94                 orientation, \
95                 owner_album_id \
96             FROM Photos \
97             WHERE file_id <= ? AND \
98                 display_name = ? AND \
99                 size = ? AND \
100                 ( 1 <> ? OR orientation= ? ) \
101         ) \
102         AS p \
103         ON a.album_id = p.owner_album_id \
104         LIMIT 1; ";
105     const std::string SQL_PHOTOS_FIND_SAME_FILE_WITHOUT_ALBUM = "\
106         SELECT \
107             P.file_id, \
108             P.data, \
109             P.clean_flag, \
110             P.position \
111         FROM Photos AS P \
112         WHERE file_id <= ? AND \
113             display_name = ? AND \
114             size = ? AND \
115             (owner_album_id IS NULL OR owner_album_id = 0) AND \
116             (1 <> ? OR orientation = ?) \
117         LIMIT 1;";
118     const std::string SQL_PHOTOS_FIND_SAME_FILE_BY_SOURCE_PATH = "\
119         SELECT \
120             file_id, \
121             data, \
122             clean_flag, \
123             position \
124         FROM \
125         ( \
126             SELECT file_id, \
127                 data, \
128                 clean_flag, \
129                 position, \
130                 display_name, \
131                 size, \
132                 orientation, \
133                 hidden, \
134                 date_trashed, \
135                 source_path \
136             FROM Photos \
137                 LEFT JOIN PhotoAlbum \
138                 ON Photos.owner_album_id = PhotoAlbum.album_id \
139             WHERE PhotoAlbum.album_id IS NULL AND \
140                 COALESCE(Photos.source_path, '') <> '' AND \
141                 ( \
142                     COALESCE(Photos.hidden, 0) = 1 OR \
143                     COALESCE(Photos.date_trashed, 0) <> 0 \
144                 ) \
145         ) AS MISS \
146         LEFT JOIN \
147         ( \
148             SELECT \
149                 ? AS source_path, \
150                 ? AS max_file_id, \
151                 ? AS display_name, \
152                 ? AS size, \
153                 ? AS picture_flag, \
154                 ? AS orientation \
155         ) AS INPUT \
156         ON 1 = 1 \
157         WHERE MISS.file_id <= INPUT.max_file_id AND \
158             MISS.display_name = INPUT.display_name AND \
159             MISS.size = INPUT.size AND \
160             ( 1 <> INPUT.picture_flag OR MISS.orientation = INPUT.orientation ) AND \
161             LOWER(MISS.source_path) = LOWER(INPUT.source_path) \
162         LIMIT 1;";
163 };
164 }  // namespace OHOS::Media
165 #endif  // OHOS_MEDIA_PHOTOS_DAO