1 /*
2 * Copyright (C) 2025 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 #include "photo_query_filter.h"
17
18 #include "base_column.h"
19 #include "datashare_predicates.h"
20 #include "media_log.h"
21
22 namespace OHOS {
23 namespace Media {
24 using namespace std;
25
GetSqlWhereClause(const PhotoQueryFilter::Option option)26 std::string PhotoQueryFilter::GetSqlWhereClause(const PhotoQueryFilter::Option option)
27 {
28 PhotoQueryFilter::Config config {};
29 switch (option) {
30 case PhotoQueryFilter::Option::FILTER_VISIBLE:
31 return GetSqlWhereClause(config);
32 break;
33 case PhotoQueryFilter::Option::FILTER_HIDDEN:
34 config.hiddenConfig = PhotoQueryFilter::ConfigType::INCLUDE;
35 return GetSqlWhereClause(config);
36 break;
37 case PhotoQueryFilter::Option::FILTER_TRASHED:
38 config.trashedConfig = PhotoQueryFilter::ConfigType::INCLUDE;
39 return GetSqlWhereClause(config);
40 break;
41 default:
42 MEDIA_ERR_LOG("Invalid option: %{public}d", static_cast<int>(option));
43 return "";
44 break;
45 }
46 }
47
GetSqlWhereClause(const PhotoQueryFilter::Config & config)48 std::string PhotoQueryFilter::GetSqlWhereClause(const PhotoQueryFilter::Config& config)
49 {
50 string whereClause = "";
51 if (config.syncStatusConfig != ConfigType::IGNORE) {
52 whereClause += "sync_status = " + string(config.syncStatusConfig == ConfigType::INCLUDE ? "1" : "0");
53 }
54 if (config.cleanFlagConfig != ConfigType::IGNORE) {
55 whereClause += " AND clean_flag = " + string(config.cleanFlagConfig == ConfigType::INCLUDE ? "1" : "0");
56 }
57 if (config.pendingConfig != ConfigType::IGNORE) {
58 if (config.pendingConfig == ConfigType::INCLUDE) {
59 whereClause += " AND time_pending > 0";
60 } else {
61 whereClause += " AND time_pending = 0";
62 }
63 }
64 if (config.tempConfig != ConfigType::IGNORE) {
65 whereClause += " AND is_temp = " + string(config.tempConfig == ConfigType::INCLUDE ? "1" : "0");
66 }
67 if (config.hiddenConfig != ConfigType::IGNORE) {
68 whereClause += " AND hidden = " + string(config.hiddenConfig == ConfigType::INCLUDE ? "1" : "0");
69 }
70 if (config.trashedConfig != ConfigType::IGNORE) {
71 if (config.trashedConfig == ConfigType::INCLUDE) {
72 whereClause += " AND date_trashed > 0";
73 } else {
74 whereClause += " AND date_trashed = 0";
75 }
76 }
77 if (config.burstCoverOnly != ConfigType::IGNORE) {
78 whereClause += " AND burst_cover_level = " +
79 string(config.burstCoverOnly == ConfigType::INCLUDE ? "1" : "0");
80 }
81
82 return whereClause;
83 }
84
85 template <class T>
ModifyPredicate(const PhotoQueryFilter::Option option,T & predicate)86 void PhotoQueryFilter::ModifyPredicate(const PhotoQueryFilter::Option option, T& predicate)
87 {
88 PhotoQueryFilter::Config config {};
89 switch (option) {
90 case PhotoQueryFilter::Option::FILTER_VISIBLE:
91 ModifyPredicate(config, predicate);
92 break;
93 case PhotoQueryFilter::Option::FILTER_HIDDEN:
94 config.hiddenConfig = PhotoQueryFilter::ConfigType::INCLUDE;
95 ModifyPredicate(config, predicate);
96 break;
97 case PhotoQueryFilter::Option::FILTER_TRASHED:
98 config.trashedConfig = PhotoQueryFilter::ConfigType::INCLUDE;
99 ModifyPredicate(config, predicate);
100 break;
101 default:
102 MEDIA_ERR_LOG("Invalid option: %{public}d", static_cast<int>(option));
103 break;
104 }
105 }
106
107 template <class T>
ModifyPredicate(const PhotoQueryFilter::Config & config,T & predicates)108 void PhotoQueryFilter::ModifyPredicate(const PhotoQueryFilter::Config& config, T& predicates)
109 {
110 if (config.syncStatusConfig != ConfigType::IGNORE) {
111 predicates.EqualTo("sync_status", config.syncStatusConfig == ConfigType::INCLUDE ? 1 : 0);
112 }
113 if (config.cleanFlagConfig != ConfigType::IGNORE) {
114 predicates.EqualTo("clean_flag", config.cleanFlagConfig == ConfigType::INCLUDE ? 1 : 0);
115 }
116 if (config.pendingConfig != ConfigType::IGNORE) {
117 if (config.pendingConfig == ConfigType::INCLUDE) {
118 predicates.GreaterThan("time_pending", 0);
119 } else {
120 predicates.EqualTo("time_pending", 0);
121 }
122 }
123 if (config.tempConfig != ConfigType::IGNORE) {
124 predicates.EqualTo("is_temp", config.tempConfig == ConfigType::INCLUDE ? 1 : 0);
125 }
126 if (config.hiddenConfig != ConfigType::IGNORE) {
127 predicates.EqualTo("hidden", config.hiddenConfig == ConfigType::INCLUDE ? 1 : 0);
128 }
129 if (config.trashedConfig != ConfigType::IGNORE) {
130 if (config.trashedConfig == ConfigType::INCLUDE) {
131 predicates.GreaterThan("date_trashed", 0);
132 } else {
133 predicates.EqualTo("date_trashed", 0);
134 }
135 }
136 if (config.burstCoverOnly != ConfigType::IGNORE) {
137 predicates.EqualTo("burst_cover_level", config.burstCoverOnly == ConfigType::INCLUDE ? 1 : 0);
138 }
139 }
140
141 template void PhotoQueryFilter::ModifyPredicate<DataShare::DataSharePredicates>(const PhotoQueryFilter::Option option,
142 DataShare::DataSharePredicates& predicate);
143
144 template void PhotoQueryFilter::ModifyPredicate<NativeRdb::RdbPredicates>(const PhotoQueryFilter::Option option,
145 NativeRdb::RdbPredicates& predicate);
146
147 template void PhotoQueryFilter::ModifyPredicate<DataShare::DataSharePredicates>(const PhotoQueryFilter::Config& config,
148 DataShare::DataSharePredicates& predicate);
149
150 template void PhotoQueryFilter::ModifyPredicate<NativeRdb::RdbPredicates>(const PhotoQueryFilter::Config& config,
151 NativeRdb::RdbPredicates& predicate);
152
153 } // namespace Media
154 } // namespace OHOS