• 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 #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     int32_t GetDirtyFilesCount();
54     std::vector<PhotosRowData> GetDirtyFiles(int32_t offset);
55 
56 private:
57     PhotosRowData FindSameFileWithoutAlbum(const FileInfo &fileInfo, int32_t maxFileId);
58     PhotosRowData FindSameFileInAlbum(const FileInfo &fileInfo, int32_t maxFileId);
59     PhotosRowData FindSameFileBySourcePath(const FileInfo &fileInfo, int32_t maxFileId);
60     std::string ToString(const FileInfo &fileInfo);
61     std::string ToString(const PhotosRowData &rowData);
62     std::string ToLower(const std::string &str);
63 
64 private:
65     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
66 
67 private:
68     const std::string SOURCE_PATH_PREFIX = "/storage/emulated/0";
69     const std::string SQL_PHOTOS_BASIC_INFO = "\
70         SELECT \
71             MAX(file_id) AS max_file_id, \
72             COUNT(1) AS count \
73         FROM Photos; \
74     ";
75     const std::string SQL_PHOTOS_FIND_SAME_FILE_IN_ALBUM = "\
76         SELECT \
77             p.file_id, \
78             p.data, \
79             p.clean_flag, \
80             p.position \
81         FROM \
82         ( \
83             SELECT album_id \
84             FROM PhotoAlbum \
85             WHERE LOWER(lpath) = LOWER(?) \
86         ) \
87         AS a \
88         INNER JOIN \
89         ( \
90             SELECT \
91                 file_id, \
92                 data, \
93                 clean_flag, \
94                 position, \
95                 size, \
96                 orientation, \
97                 owner_album_id \
98             FROM Photos \
99             WHERE file_id <= ? AND \
100                 display_name = ? AND \
101                 size = ? AND \
102                 ( 1 <> ? OR orientation= ? ) \
103         ) \
104         AS p \
105         ON a.album_id = p.owner_album_id \
106         LIMIT 1; ";
107     const std::string SQL_PHOTOS_FIND_SAME_FILE_WITHOUT_ALBUM = "\
108         SELECT \
109             P.file_id, \
110             P.data, \
111             P.clean_flag, \
112             P.position \
113         FROM Photos AS P \
114         WHERE file_id <= ? AND \
115             display_name = ? AND \
116             size = ? AND \
117             (owner_album_id IS NULL OR owner_album_id = 0) AND \
118             (1 <> ? OR orientation = ?) \
119         LIMIT 1;";
120     const std::string SQL_PHOTOS_FIND_SAME_FILE_BY_SOURCE_PATH = "\
121         SELECT \
122             file_id, \
123             data, \
124             clean_flag, \
125             position \
126         FROM \
127         ( \
128             SELECT file_id, \
129                 data, \
130                 clean_flag, \
131                 position, \
132                 display_name, \
133                 size, \
134                 orientation, \
135                 hidden, \
136                 date_trashed, \
137                 source_path \
138             FROM Photos \
139                 LEFT JOIN PhotoAlbum \
140                 ON Photos.owner_album_id = PhotoAlbum.album_id \
141             WHERE PhotoAlbum.album_id IS NULL AND \
142                 COALESCE(Photos.source_path, '') <> '' AND \
143                 ( \
144                     COALESCE(Photos.hidden, 0) = 1 OR \
145                     COALESCE(Photos.date_trashed, 0) <> 0 \
146                 ) \
147         ) AS MISS \
148         LEFT JOIN \
149         ( \
150             SELECT \
151                 ? AS source_path, \
152                 ? AS max_file_id, \
153                 ? AS display_name, \
154                 ? AS size, \
155                 ? AS picture_flag, \
156                 ? AS orientation \
157         ) AS INPUT \
158         ON 1 = 1 \
159         WHERE MISS.file_id <= INPUT.max_file_id AND \
160             MISS.display_name = INPUT.display_name AND \
161             MISS.size = INPUT.size AND \
162             ( 1 <> INPUT.picture_flag OR MISS.orientation = INPUT.orientation ) AND \
163             LOWER(MISS.source_path) = LOWER(INPUT.source_path) \
164         LIMIT 1;";
165     const std::string SQL_PHOTOS_GET_DIRTY_FILES_COUNT =
166         "SELECT count(1) as count FROM Photos WHERE sync_status = ?";
167     const std::string SQL_PHOTOS_GET_DIRTY_FILES =
168         "SELECT file_id, data FROM Photos WHERE sync_status = ? LIMIT ?, ?";
169 };
170 }  // namespace OHOS::Media
171 #endif  // OHOS_MEDIA_PHOTOS_DAO