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