1 /*
2 * Copyright (C) 2021 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 "Distributed"
16
17 #include "medialibrary_device_db.h"
18 #include "media_log.h"
19 #include "medialibrary_errno.h"
20
21 using namespace std;
22 using namespace OHOS::NativeRdb;
23
24 namespace OHOS {
25 namespace Media {
26 static const std::string DEVICE_DB_COND = DEVICE_DB_UDID + " = ?";
27
InsertDeviceInfo(const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)28 int64_t MediaLibraryDeviceDb::InsertDeviceInfo(const ValuesBucket &values, const shared_ptr<RdbStore> &rdbStore)
29 {
30 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_DEVICE_OPER_ERR, "Invalid RDB store");
31 int64_t outRowId(0);
32 int32_t insertResult = rdbStore->Insert(outRowId, DEVICE_TABLE, values);
33 CHECK_AND_RETURN_RET_LOG(insertResult == E_OK, E_DEVICE_OPER_ERR, "Insert failed");
34
35 return outRowId;
36 }
37
DeleteDeviceInfo(const std::string & udid,const shared_ptr<RdbStore> & rdbStore)38 int32_t MediaLibraryDeviceDb::DeleteDeviceInfo(const std::string &udid,
39 const shared_ptr<RdbStore> &rdbStore)
40 {
41 CHECK_AND_RETURN_RET_LOG((rdbStore != nullptr) && (!udid.empty()), E_DEVICE_OPER_ERR, "Invalid input");
42
43 int32_t deletedRows(E_DEVICE_OPER_ERR);
44 vector<string> whereArgs = { udid };
45
46 int32_t deleteResult = rdbStore->Delete(deletedRows, DEVICE_TABLE, DEVICE_DB_COND, whereArgs);
47 CHECK_AND_RETURN_RET_LOG(deleteResult == E_OK, E_DEVICE_OPER_ERR, "Delete failed");
48
49 return (deletedRows > 0) ? E_SUCCESS : E_FAIL;
50 }
51
UpdateDeviceInfo(const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)52 int32_t MediaLibraryDeviceDb::UpdateDeviceInfo(const ValuesBucket &values, const shared_ptr<RdbStore> &rdbStore)
53 {
54 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_DEVICE_OPER_ERR, "Invalid input");
55
56 ValueObject obj;
57 std::string udid;
58
59 auto contains = values.GetObject(DEVICE_DB_UDID, obj);
60 if (contains) {
61 obj.GetString(udid);
62 }
63
64 CHECK_AND_RETURN_RET_LOG(!udid.empty(), E_DEVICE_OPER_ERR, "Invalid dev id");
65
66 int32_t updatedRows(0);
67 vector<string> whereArgs = { udid };
68
69 int32_t updateResult = rdbStore->Update(updatedRows, DEVICE_TABLE, values, DEVICE_DB_COND, whereArgs);
70 CHECK_AND_RETURN_RET_LOG(updateResult == E_OK, E_DEVICE_OPER_ERR, "Update failed");
71
72 return (updatedRows > 0) ? E_SUCCESS : E_FAIL;
73 }
74 } // namespace Media
75 } // namespace OHOS
76