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 #include "photo_album_clone.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "rdb_store.h"
21 #include "result_set_utils.h"
22 #include "media_log.h"
23
24 namespace OHOS::Media {
ToString(const std::vector<NativeRdb::ValueObject> & bindArgs)25 std::string PhotoAlbumClone::ToString(const std::vector<NativeRdb::ValueObject> &bindArgs)
26 {
27 std::string args;
28 for (auto &arg : bindArgs) {
29 std::string tempStr;
30 arg.GetString(tempStr);
31 args += tempStr + ", ";
32 }
33 return args;
34 }
35
36 /**
37 * @brief Get Total Count of PhotoAlbum, for clone.
38 */
GetPhotoAlbumCountInOriginalDb()39 int32_t PhotoAlbumClone::GetPhotoAlbumCountInOriginalDb()
40 {
41 std::string querySql = this->SQL_PHOTO_ALBUM_COUNT_FOR_CLONE;
42 if (this->mediaLibraryOriginalRdb_ == nullptr) {
43 MEDIA_ERR_LOG("Media_Restore: mediaLibraryOriginalRdb_ is null.");
44 return 0;
45 }
46 auto resultSet = this->mediaLibraryOriginalRdb_->QuerySql(querySql);
47 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
48 MEDIA_ERR_LOG("Failed to query album! querySql = %{public}s", querySql.c_str());
49 return 0;
50 }
51 return GetInt32Val("count", resultSet);
52 }
53
54 /**
55 * @brief Get Row Data of PhotoAlbum, for clone.
56 */
GetPhotoAlbumInOriginalDb(int32_t offset,int32_t pageSize)57 std::shared_ptr<NativeRdb::ResultSet> PhotoAlbumClone::GetPhotoAlbumInOriginalDb(int32_t offset, int32_t pageSize)
58 {
59 std::string querySql = this->SQL_PHOTO_ALBUM_SELECT_FOR_CLONE;
60 std::vector<NativeRdb::ValueObject> bindArgs = {offset, pageSize};
61 if (this->mediaLibraryOriginalRdb_ == nullptr) {
62 MEDIA_ERR_LOG("Media_Restore: mediaLibraryOriginalRdb_ is null.");
63 return nullptr;
64 }
65 auto resultSet = this->mediaLibraryOriginalRdb_->QuerySql(querySql, bindArgs);
66 if (resultSet == nullptr) {
67 MEDIA_ERR_LOG("Failed to query album! querySql = %{public}s, bindArgs = %{public}s",
68 querySql.c_str(),
69 this->ToString(bindArgs).c_str());
70 }
71 return resultSet;
72 }
73
TRACE_LOG(std::vector<PhotoAlbumDao::PhotoAlbumRowData> & albumInfos)74 void PhotoAlbumClone::TRACE_LOG(std::vector<PhotoAlbumDao::PhotoAlbumRowData> &albumInfos)
75 {
76 MEDIA_INFO_LOG("Media_Restore: albumInfos size : %{public}d", static_cast<int32_t>(albumInfos.size()));
77 for (auto &info : albumInfos) {
78 MEDIA_INFO_LOG("Media_Restore: restore album info: albumId = %{public}d, \
79 albumName = %{public}s, \
80 albumType = %{public}d, \
81 albumSubType = %{public}d, \
82 lPath = %{public}s, \
83 bundleName = %{public}s, \
84 priority = %{public}d",
85 info.albumId,
86 info.albumName.c_str(),
87 info.albumType,
88 info.albumSubType,
89 info.lPath.c_str(),
90 info.bundleName.c_str(),
91 info.priority);
92 }
93 }
94
TRACE_LOG(const std::string & tableName,vector<AlbumInfo> & albumInfos)95 void PhotoAlbumClone::TRACE_LOG(const std::string &tableName, vector<AlbumInfo> &albumInfos)
96 {
97 for (auto &albumInfo : albumInfos) {
98 MEDIA_INFO_LOG("Media_Restore: tableName %{public}s, \
99 albumInfo.albumName = %{public}s, \
100 albumInfo.albumBundleName = %{public}s, \
101 albumInfo.albumType = %{public}d, \
102 albumInfo.albumSubType = %{public}d, \
103 albumInfo.lPath = %{public}s",
104 tableName.c_str(),
105 albumInfo.albumName.c_str(),
106 albumInfo.albumBundleName.c_str(),
107 static_cast<int32_t>(albumInfo.albumType),
108 static_cast<int32_t>(albumInfo.albumSubType),
109 albumInfo.lPath.c_str());
110 }
111 // fetch all albums from mediaLibraryRdb
112 std::vector<PhotoAlbumDao::PhotoAlbumRowData> targetAlbumInfos = this->photoAlbumDao_.GetPhotoAlbums();
113 this->TRACE_LOG(targetAlbumInfos);
114 }
115 } // namespace OHOS::Media