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_PHOTO_ALBUM_CLONE
16 #define OHOS_MEDIA_PHOTO_ALBUM_CLONE
17 
18 #include <string>
19 
20 #include "rdb_store.h"
21 #include "photo_album_dao.h"
22 #include "backup_const.h"
23 
24 namespace OHOS::Media {
25 class PhotoAlbumClone {
26 public:
27     /**
28      * @brief Restore Start Event Handler.
29      */
OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb,std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb)30     void OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb,
31         std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb)
32     {
33         this->mediaLibraryOriginalRdb_ = mediaLibraryOriginalRdb;
34         this->mediaLibraryTargetRdb_ = mediaLibraryTargetRdb;
35         this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryTargetRdb);
36     }
37 
38     int32_t GetPhotoAlbumCountInOriginalDb();
39     std::shared_ptr<NativeRdb::ResultSet> GetPhotoAlbumInOriginalDb(int32_t offset, int32_t pageSize);
40 
HasSameAlbum(const std::string & lPath)41     bool HasSameAlbum(const std::string &lPath)
42     {
43         // Do not allow albums with empty lPath to be created.
44         if (lPath.empty()) {
45             return true;
46         }
47         PhotoAlbumDao::PhotoAlbumRowData albumInfo = this->photoAlbumDao_.GetPhotoAlbum(lPath);
48         return !albumInfo.lPath.empty();
49     }
50     void TRACE_LOG(const std::string &tableName, std::vector<AlbumInfo> &albumInfos);
51     void TRACE_LOG(std::vector<PhotoAlbumDao::PhotoAlbumRowData> &albumInfos);
52 
53 private:
54     std::string ToString(const std::vector<NativeRdb::ValueObject> &bindArgs);
55 
56 private:
57     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryTargetRdb_;
58     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryOriginalRdb_;
59     PhotoAlbumDao photoAlbumDao_;
60 
61 private:
62     const std::string SQL_PHOTO_ALBUM_COUNT_FOR_CLONE = "\
63         SELECT COUNT(DISTINCT PhotoAlbum.album_id) AS count \
64         FROM PhotoAlbum \
65             LEFT JOIN PhotoMap \
66             ON PhotoAlbum.album_id = PhotoMap.map_album \
67             LEFT JOIN Photos AS P1 \
68             ON PhotoMap.map_asset=P1.file_id \
69             LEFT JOIN Photos AS P2 \
70             ON PhotoAlbum.album_id=P2.owner_album_id \
71         WHERE P1.file_id IS NOT NULL AND P1.position IN (1, 3) OR \
72             P2.file_id IS NOT NULL AND P2.position IN (1, 3) ;";
73     const std::string SQL_PHOTO_ALBUM_SELECT_FOR_CLONE = "\
74         SELECT DISTINCT PhotoAlbum.* \
75         FROM PhotoAlbum \
76             LEFT JOIN PhotoMap \
77             ON PhotoAlbum.album_id = PhotoMap.map_album \
78             LEFT JOIN Photos AS P1 \
79             ON PhotoMap.map_asset=P1.file_id \
80             LEFT JOIN Photos AS P2 \
81             ON PhotoAlbum.album_id=P2.owner_album_id \
82         WHERE P1.file_id IS NOT NULL AND P1.position IN (1, 3) OR \
83             P2.file_id IS NOT NULL AND P2.position IN (1, 3) \
84         ORDER BY PhotoAlbum.album_id \
85         LIMIT ?, ? ;";
86 };
87 }  // namespace OHOS::Media
88 
89 #endif  // OHOS_MEDIA_PHOTO_ALBUM_CLONE