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