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 "gallery_media_dao.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 {
SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)25 void GalleryMediaDao::SetGalleryRdb(std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
26 {
27 this->galleryRdb_ = galleryRdb;
28 }
29
30 /**
31 * @brief Get the gallery_media to restore to Photos.
32 */
GetGalleryMedia(int32_t offset,int pageSize,bool shouldIncludeSd,bool hasLowQualityImage)33 std::shared_ptr<NativeRdb::ResultSet> GalleryMediaDao::GetGalleryMedia(
34 int32_t offset, int pageSize, bool shouldIncludeSd, bool hasLowQualityImage)
35 {
36 int32_t shouldIncludeSdFlag = shouldIncludeSd == true ? 1 : 0;
37 int32_t hasLowQualityImageFlag = hasLowQualityImage == true ? 1 : 0;
38 std::vector<NativeRdb::ValueObject> params = {hasLowQualityImageFlag, shouldIncludeSdFlag, offset, pageSize};
39 if (this->galleryRdb_ == nullptr) {
40 MEDIA_ERR_LOG("Media_Restore: galleryRdb_ is null.");
41 return nullptr;
42 }
43 return this->galleryRdb_->QuerySql(this->SQL_GALLERY_MEDIA_QUERY_FOR_RESTORE, params);
44 }
45
46 /**
47 * @brief Get the row count of gallery_media.
48 */
GetGalleryMediaCount(bool shouldIncludeSd,bool hasLowQualityImage)49 int32_t GalleryMediaDao::GetGalleryMediaCount(bool shouldIncludeSd, bool hasLowQualityImage)
50 {
51 int32_t shouldIncludeSdFlag = shouldIncludeSd == true ? 1 : 0;
52 int32_t hasLowQualityImageFlag = hasLowQualityImage == true ? 1 : 0;
53 std::vector<NativeRdb::ValueObject> params = {hasLowQualityImageFlag, shouldIncludeSdFlag};
54 if (this->galleryRdb_ == nullptr) {
55 MEDIA_ERR_LOG("Media_Restore: galleryRdb_ is null.");
56 return 0;
57 }
58 auto resultSet = this->galleryRdb_->QuerySql(this->SQL_GALLERY_MEDIA_QUERY_COUNT, params);
59 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
60 return 0;
61 }
62 return GetInt32Val("count", resultSet);
63 }
64
65 /**
66 * @brief Get the row count of gallery_media no need migrate count.
67 */
GetNoNeedMigrateCount(bool shouldIncludeSd)68 int32_t GalleryMediaDao::GetNoNeedMigrateCount(bool shouldIncludeSd)
69 {
70 int32_t shouldIncludeSdFlag = shouldIncludeSd == true ? 1 : 0;
71 std::vector<NativeRdb::ValueObject> params = {shouldIncludeSdFlag};
72 if (this->galleryRdb_ == nullptr) {
73 MEDIA_ERR_LOG("Media_Restore: galleryRdb_ is null.");
74 return 0;
75 }
76 auto resultSet = this->galleryRdb_->QuerySql(this->SQL_GALLERY_MEDIA_QUERY_NO_NEED_MIGRATE_COUNT, params);
77 if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
78 return 0;
79 }
80 return GetInt32Val("count", resultSet);
81 }
82 } // namespace OHOS::Media