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 #define MLOG_TAG "CustomRecordOperations"
16 #include "custom_record_operations.h"
17 #include "medialibrary_errno.h"
18 #include "media_log.h"
19 #include "custom_records_column.h"
20
21 namespace OHOS::Media {
22
InsertCustomRecord(std::shared_ptr<MediaLibraryRdbStore> & rdbStore,MediaLibraryCommand & cmd)23 int32_t CustomRecordOperations::InsertCustomRecord(std::shared_ptr<MediaLibraryRdbStore> &rdbStore,
24 MediaLibraryCommand &cmd)
25 {
26 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_HAS_DB_ERROR, "custom record insert rdbStore is nullptr");
27 int64_t outRowId = -1;
28 int32_t errCode = rdbStore->Insert(cmd, outRowId);
29 if (errCode != NativeRdb::E_OK || outRowId < 0) {
30 MEDIA_ERR_LOG("custom record Insert into db failed, errCode = %{public}d", errCode);
31 return E_HAS_DB_ERROR;
32 }
33 return static_cast<int32_t>(outRowId);
34 }
35
BatchAddCustomRecords(MediaLibraryCommand & cmd,const std::vector<DataShare::DataShareValuesBucket> & values)36 int32_t CustomRecordOperations::BatchAddCustomRecords(MediaLibraryCommand &cmd,
37 const std::vector<DataShare::DataShareValuesBucket> &values)
38 {
39 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
40 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_HAS_DB_ERROR, "custom record insert rdbStore is nullptr");
41 int32_t errCode = -1;
42 for (auto &value : values) {
43 NativeRdb::ValuesBucket rdbValue = RdbDataShareAdapter::RdbUtils::ToValuesBucket(value);
44 cmd.SetValueBucket(rdbValue);
45 errCode = InsertCustomRecord(rdbStore, cmd);
46 CHECK_AND_RETURN_RET_LOG(errCode >= 0, E_HAS_DB_ERROR, "custom record insert fail");
47 }
48 return errCode;
49 }
50 }