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 #define MLOG_TAG "Media_Cloud_Dao"
17
18 #include "cloud_media_common_dao.h"
19
20 #include "abs_rdb_predicates.h"
21 #include "photo_map_column.h"
22 #include "media_log.h"
23 #include "medialibrary_rdbstore.h"
24 #include "cloud_media_operation_code.h"
25 #include "medialibrary_unistore_manager.h"
26 #include "result_set.h"
27 #include "result_set_utils.h"
28 #include "photos_po_writer.h"
29 #include "result_set_reader.h"
30
31 namespace OHOS::Media::CloudSync {
QueryLocalByCloudId(const std::vector<std::string> & cloudIds,const std::vector<std::string> & columns,std::vector<PhotosPo> & result)32 int32_t CloudMediaCommonDao::QueryLocalByCloudId(
33 const std::vector<std::string> &cloudIds, const std::vector<std::string> &columns, std::vector<PhotosPo> &result)
34 {
35 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
36 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_RDB_STORE_NULL, "QueryLocalByCloudId Failed to get rdbStore.");
37
38 NativeRdb::AbsRdbPredicates predicates = NativeRdb::AbsRdbPredicates(PhotoColumn::PHOTOS_TABLE);
39 predicates.In(PhotoColumn::PHOTO_CLOUD_ID, cloudIds);
40
41 auto resultSet = rdbStore->Query(predicates, columns);
42 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_RESULT_SET_NULL, "QueryLocalByCloudId Failed to query.");
43 return ResultSetReader<PhotosPoWriter, PhotosPo>(resultSet).ReadRecords(result);
44 }
45
QueryLocalMap(const int32_t & fileId,std::map<int32_t,int32_t> & localMapIds)46 int32_t CloudMediaCommonDao::QueryLocalMap(const int32_t &fileId, std::map<int32_t, int32_t> &localMapIds)
47 {
48 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
49 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_RDB_STORE_NULL, "Failed to get rdbStore.");
50 NativeRdb::AbsRdbPredicates predicates = NativeRdb::AbsRdbPredicates(PhotoMap::TABLE);
51 predicates.EqualTo(PhotoMap::ASSET_ID, std::to_string(fileId));
52 auto resultSet = rdbStore->Query(predicates, {PhotoMap::ALBUM_ID, PhotoMap::DIRTY});
53 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, E_RESULT_SET_NULL, "QueryLocalMap Failed to query.");
54 int32_t albumId;
55 int32_t dirty;
56 while (resultSet->GoToNextRow() == E_OK) {
57 albumId = GetInt32Val(PhotoMap::ALBUM_ID, resultSet);
58 dirty = GetInt32Val(PhotoMap::DIRTY, resultSet);
59 localMapIds[albumId] = dirty;
60 }
61 return E_OK;
62 }
63 } // namespace OHOS::Media::CloudSync