1 /*
2  * Copyright (C) 2024-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 
16 #ifndef OHOS_MEDIA_PHOTO_ALBUM_MERGE_OPERATION_H
17 #define OHOS_MEDIA_PHOTO_ALBUM_MERGE_OPERATION_H
18 
19 #include <string>
20 #include <vector>
21 #include <sstream>
22 
23 #include "medialibrary_rdbstore.h"
24 
25 namespace OHOS::Media {
26 class PhotoAlbumMergeOperation {
27 public:
28     PhotoAlbumMergeOperation &SetRdbStore(const std::shared_ptr<MediaLibraryRdbStore> &rdbStorePtr);
29     int32_t MergeAlbum(const int32_t &oldAlbumId, const int32_t &newAlbumId);
30 
31 private:
32     std::string ToString(const std::vector<NativeRdb::ValueObject> &values);
33     int32_t DeleteDuplicateRelationshipInPhotoMap(const int32_t &oldAlbumId, const int32_t &newAlbumId);
34     int32_t UpdateRelationshipInPhotoMap(const int32_t &oldAlbumId, const int32_t &newAlbumId);
35     int32_t UpdateRelationshipInPhotos(const int32_t &oldAlbumId, const int32_t &newAlbumId);
36     int32_t DeleteOldAlbum(const int32_t &oldAlbumId);
37 
38 private:
39     std::shared_ptr<MediaLibraryRdbStore> rdbStorePtr_;
40 
41 private:
42     const std::string SQL_PHOTO_MAP_DUPLICATE_RELATIONSHIP_DELETE = "\
43         DELETE FROM PhotoMap \
44         WHERE map_album = ? AND \
45             map_asset IN \
46             ( \
47                 SELECT DISTINCT map_asset \
48                 FROM PhotoMap \
49                 WHERE map_album = ? \
50             ); ";
51     const std::string SQL_PHOTO_MAP_MOVE_RELATIONSHIP_UPDATE = "\
52         UPDATE PhotoMap \
53         SET map_album = ? \
54         WHERE map_album = ? AND \
55             ? IN ( \
56                 SELECT album_id \
57                 FROM PhotoAlbum \
58                 WHERE album_type IN (0, 2048) \
59             );";
60     const std::string SQL_PHOTOS_MOVE_RELATIONSHIP_UPDATE = "\
61         UPDATE Photos \
62         SET owner_album_id = ? \
63         WHERE owner_album_id = ? AND \
64             ? IN ( \
65                 SELECT album_id \
66                 FROM PhotoAlbum \
67                 WHERE album_type IN (0, 2048) \
68             );";
69     const std::string SQL_PHOTO_ALBUM_DELETE = "\
70         DELETE FROM PhotoAlbum \
71         WHERE album_id = ? AND \
72             album_id NOT IN ( \
73                 SELECT DISTINCT owner_album_id \
74                 FROM Photos \
75             ) AND \
76             album_id NOT IN ( \
77                 SELECT DISTINCT map_album \
78                 FROM PhotoMap \
79             );";
80 };
81 }  // namespace OHOS::Media
82 #endif  // OHOS_MEDIA_PHOTO_ALBUM_MERGE_OPERATION_H