• 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 #include <string>
16 #include <vector>
17 
18 #include "photos_dao.h"
19 #include "rdb_store.h"
20 #include "result_set_utils.h"
21 #include "userfile_manager_types.h"
22 #include "backup_const.h"
23 
24 namespace OHOS::Media {
25 /**
26  * @brief Find FileInfo related PhotoAlbu, by lPath, displayName, fileSize and orientation.
27  */
FindSameFileInAlbum(const FileInfo & fileInfo,int32_t maxFileId)28 PhotosDao::PhotosRowData PhotosDao::FindSameFileInAlbum(const FileInfo &fileInfo, int32_t maxFileId)
29 {
30     PhotosDao::PhotosRowData rowData;
31     if (maxFileId <= 0) {
32         return rowData;
33     }
34     // pictureFlag: 0 for video, 1 for photo; Only search for photo in this case.
35     int pictureFlag = fileInfo.fileType == MEDIA_TYPE_VIDEO ? 0 : 1;
36     const std::vector<NativeRdb::ValueObject> params = {
37         fileInfo.lPath, maxFileId, fileInfo.displayName, fileInfo.fileSize, pictureFlag, fileInfo.orientation};
38     std::string querySql = this->SQL_PHOTOS_FIND_SAME_FILE_IN_ALBUM;
39     if (this->mediaLibraryRdb_ == nullptr) {
40         MEDIA_ERR_LOG("Media_Restore: mediaLibraryRdb_ is null.");
41         return rowData;
42     }
43     auto resultSet = this->mediaLibraryRdb_->QuerySql(querySql, params);
44     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
45         return rowData;
46     }
47     rowData.fileId = GetInt32Val("file_id", resultSet);
48     rowData.data = GetStringVal("data", resultSet);
49     return rowData;
50 }
51 
52 /**
53  * @brief Find FileInfo, which is not related PhotoAlbum, by displayName, fileSize and orientation.
54  */
FindSameFileWithoutAlbum(const FileInfo & fileInfo,int32_t maxFileId)55 PhotosDao::PhotosRowData PhotosDao::FindSameFileWithoutAlbum(const FileInfo &fileInfo, int32_t maxFileId)
56 {
57     PhotosDao::PhotosRowData rowData;
58     if (maxFileId <= 0) {
59         return rowData;
60     }
61     // pictureFlag: 0 for video, 1 for photo; Only search for photo in this case.
62     int pictureFlag = fileInfo.fileType == MEDIA_TYPE_VIDEO ? 0 : 1;
63     const std::vector<NativeRdb::ValueObject> params = {
64         maxFileId, fileInfo.displayName, fileInfo.fileSize, pictureFlag, fileInfo.orientation};
65     std::string querySql = this->SQL_PHOTOS_FIND_SAME_FILE_WITHOUT_ALBUM;
66     if (this->mediaLibraryRdb_ == nullptr) {
67         MEDIA_ERR_LOG("Media_Restore: mediaLibraryRdb_ is null.");
68         return rowData;
69     }
70     auto resultSet = this->mediaLibraryRdb_->QuerySql(querySql, params);
71     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
72         return rowData;
73     }
74     rowData.fileId = GetInt32Val("file_id", resultSet);
75     rowData.data = GetStringVal("data", resultSet);
76     return rowData;
77 }
78 
79 /**
80  * @brief Get basic information of the Photos.
81  */
GetBasicInfo()82 PhotosDao::PhotosBasicInfo PhotosDao::GetBasicInfo()
83 {
84     PhotosDao::PhotosBasicInfo basicInfo = {0, 0};
85     std::string querySql = this->SQL_PHOTOS_BASIC_INFO;
86     if (this->mediaLibraryRdb_ == nullptr) {
87         MEDIA_ERR_LOG("Media_Restore: mediaLibraryRdb_ is null.");
88         return basicInfo;
89     }
90     auto resultSet = this->mediaLibraryRdb_->QuerySql(querySql);
91     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
92         MEDIA_WARN_LOG("Media_Restore: GetBasicInfo resultSet is null. querySql: %{public}s", querySql.c_str());
93         return basicInfo;
94     }
95     basicInfo.maxFileId = GetInt32Val("max_file_id", resultSet);
96     basicInfo.count = GetInt32Val("count", resultSet);
97     MEDIA_INFO_LOG("Media_Restore: max_file_id: %{public}d, count: %{public}d", basicInfo.maxFileId, basicInfo.count);
98     return basicInfo;
99 }
100 }  // namespace OHOS::Media