1 /*
2  * Copyright (c) 2023 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 #define MLOG_TAG "SearchOperation"
16 
17 #include "medialibrary_search_operations.h"
18 
19 #include "media_log.h"
20 #include "medialibrary_data_manager_utils.h"
21 #include "medialibrary_db_const.h"
22 #include "medialibrary_errno.h"
23 #include "medialibrary_unistore_manager.h"
24 #include "search_column.h"
25 
26 using namespace std;
27 using namespace OHOS::NativeRdb;
28 
29 namespace OHOS {
30 namespace Media {
31 const std::string notTrashedAndHiddenCondition = MediaColumn::MEDIA_DATE_TRASHED + " = 0 AND " +
32         MediaColumn::MEDIA_HIDDEN + " = 0 AND " + MediaColumn::MEDIA_TIME_PENDING + " = 0 AND " +
33         PhotoColumn::PHOTO_CLEAN_FLAG + " = 0 AND " + PhotoColumn::PHOTO_BURST_COVER_LEVEL + " = 1 AND ";
34 const std::string analysisCompleteCondition = TBL_SEARCH_CV_STATUS + " = 1 AND (" + TBL_SEARCH_GEO_STATUS +
35     " = 1 OR (" + PhotoColumn::PHOTOS_TABLE + "." + PhotoColumn::PHOTO_LATITUDE + " = 0 AND " +
36     PhotoColumn::PHOTOS_TABLE + "." + PhotoColumn::PHOTO_LONGITUDE + " = 0)) ";
37 const std::string selectAnalysisCompletedPhoto = "SELECT COUNT(case when " + notTrashedAndHiddenCondition +
38     TBL_SEARCH_PHOTO_STATUS + " > 0 AND " + MediaColumn::MEDIA_TYPE + " = 1 AND " + analysisCompleteCondition +
39     " then 1 end) as " + PHOTO_COMPLETE_NUM + ",";
40 const std::string mediaPhotoTotal = "COUNT(case when " + notTrashedAndHiddenCondition + MediaColumn::MEDIA_TYPE +
41     " = 1 then 1 end) as " + PHOTO_TOTAL_NUM + ",";
42 const std::string selectAnalysisCompletedVideo = "COUNT(case when " + notTrashedAndHiddenCondition +
43     TBL_SEARCH_PHOTO_STATUS + " > 0 AND " + MediaColumn::MEDIA_TYPE + " = 2 AND " + analysisCompleteCondition +
44     " then 1 end) as " + VIDEO_COMPLETE_NUM + ",";
45 const std::string mediaVideoTotal = "COUNT(case when " + notTrashedAndHiddenCondition + MediaColumn::MEDIA_TYPE +
46     " = 2 then 1 end) as " + VIDEO_TOTAL_NUM;
47 const std::string mediaPhotosQuery = selectAnalysisCompletedPhoto + mediaPhotoTotal + selectAnalysisCompletedVideo +
48     mediaVideoTotal + " FROM " + PhotoColumn::PHOTOS_TABLE + " Inner JOIN " + SEARCH_TOTAL_TABLE + " ON " +
49     PhotoColumn::PHOTOS_TABLE + "." + MediaColumn::MEDIA_ID + "=" + SEARCH_TOTAL_TABLE + "." + TBL_SEARCH_FILE_ID;
50 
InsertOperation(MediaLibraryCommand & cmd)51 int32_t MediaLibrarySearchOperations::InsertOperation(MediaLibraryCommand &cmd)
52 {
53     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
54     if (rdbStore == nullptr) {
55         return E_HAS_DB_ERROR;
56     }
57     int64_t outRowId = -1;
58     int32_t errCode = rdbStore->Insert(cmd, outRowId);
59     if (errCode != NativeRdb::E_OK || outRowId < 0) {
60         MEDIA_ERR_LOG("Insert into db failed, errCode = %{public}d", errCode);
61         return E_HAS_DB_ERROR;
62     }
63     return static_cast<int32_t>(outRowId);
64 }
65 
UpdateOperation(MediaLibraryCommand & cmd)66 int32_t MediaLibrarySearchOperations::UpdateOperation(MediaLibraryCommand &cmd)
67 {
68     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
69     if (rdbStore == nullptr) {
70         return E_HAS_DB_ERROR;
71     }
72     int32_t updateRows = -1;
73     int32_t errCode = rdbStore->Update(cmd, updateRows);
74     if (errCode != NativeRdb::E_OK || updateRows < 0) {
75         MEDIA_ERR_LOG("Update db failed, errCode = %{public}d", errCode);
76         return E_HAS_DB_ERROR;
77     }
78     return static_cast<int32_t>(updateRows);
79 }
80 
DeleteOperation(MediaLibraryCommand & cmd)81 int32_t MediaLibrarySearchOperations::DeleteOperation(MediaLibraryCommand &cmd)
82 {
83     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
84     if (rdbStore == nullptr) {
85         return E_HAS_DB_ERROR;
86     }
87     int32_t deleteRows = -1;
88     int32_t errCode = rdbStore->Delete(cmd, deleteRows);
89     if (errCode != NativeRdb::E_OK || deleteRows < 0) {
90         MEDIA_ERR_LOG("Delete db failed, errCode = %{public}d", errCode);
91         return E_HAS_DB_ERROR;
92     }
93     return static_cast<int32_t>(deleteRows);
94 }
95 
QueryOperation(MediaLibraryCommand & cmd,const std::vector<std::string> & columns)96 shared_ptr<NativeRdb::ResultSet> MediaLibrarySearchOperations::QueryOperation(MediaLibraryCommand &cmd,
97     const std::vector<std::string> &columns)
98 {
99     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
100     if (rdbStore == nullptr) {
101         return nullptr;
102     }
103     return rdbStore->Query(cmd, columns);
104 }
105 
QueryIndexConstructProgress()106 shared_ptr<ResultSet> MediaLibrarySearchOperations::QueryIndexConstructProgress()
107 {
108     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
109     if (rdbStore == nullptr) {
110         MEDIA_ERR_LOG("rdbStore is nullptr!");
111         return nullptr;
112     }
113 
114     return rdbStore->QuerySql(mediaPhotosQuery);
115 }
116 }
117 }
118