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 "LocationOperation"
16 
17 #include "medialibrary_location_operations.h"
18 
19 #include "media_log.h"
20 #include "medialibrary_db_const.h"
21 #include "medialibrary_errno.h"
22 #include "medialibrary_unistore_manager.h"
23 
24 using namespace std;
25 using namespace OHOS::NativeRdb;
26 
27 namespace OHOS {
28 namespace Media {
InsertOperation(MediaLibraryCommand & cmd)29 int32_t MediaLibraryLocationOperations::InsertOperation(MediaLibraryCommand &cmd)
30 {
31     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
32     if (rdbStore == nullptr) {
33         MEDIA_ERR_LOG("location insert operation, rdbStore is null.");
34         return E_HAS_DB_ERROR;
35     }
36     int64_t outRowId = -1;
37     int32_t errCode = rdbStore->Insert(cmd, outRowId);
38     if (errCode != NativeRdb::E_OK || outRowId < 0) {
39         MEDIA_ERR_LOG("Location Insert into db failed, errCode = %{public}d", errCode);
40         return E_HAS_DB_ERROR;
41     }
42     return static_cast<int32_t>(outRowId);
43 }
44 
UpdateOperation(MediaLibraryCommand & cmd)45 int32_t MediaLibraryLocationOperations::UpdateOperation(MediaLibraryCommand &cmd)
46 {
47     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
48     if (rdbStore == nullptr) {
49         MEDIA_ERR_LOG("location update operation, rdbStore is null.");
50         return E_HAS_DB_ERROR;
51     }
52     int32_t updateRows = -1;
53     int32_t errCode = rdbStore->Update(cmd, updateRows);
54     if (errCode != NativeRdb::E_OK || updateRows < 0) {
55         MEDIA_ERR_LOG("Location Update db failed, errCode = %{public}d", errCode);
56         return E_HAS_DB_ERROR;
57     }
58     return static_cast<int32_t>(updateRows);
59 }
60 
DeleteOperation(MediaLibraryCommand & cmd)61 int32_t MediaLibraryLocationOperations::DeleteOperation(MediaLibraryCommand &cmd)
62 {
63     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
64     if (rdbStore == nullptr) {
65         MEDIA_ERR_LOG("location delete operation, rdbStore is null.");
66         return E_HAS_DB_ERROR;
67     }
68     int32_t deleteRows = -1;
69     int32_t errCode = rdbStore->Delete(cmd, deleteRows);
70     if (errCode != NativeRdb::E_OK || deleteRows < 0) {
71         MEDIA_ERR_LOG("Location Delete db failed, errCode = %{public}d", errCode);
72         return E_HAS_DB_ERROR;
73     }
74     return static_cast<int32_t>(deleteRows);
75 }
76 
QueryOperation(MediaLibraryCommand & cmd,const std::vector<std::string> & columns)77 shared_ptr<NativeRdb::ResultSet> MediaLibraryLocationOperations::QueryOperation(MediaLibraryCommand &cmd,
78     const std::vector<std::string> &columns)
79 {
80     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
81     if (rdbStore == nullptr) {
82         MEDIA_ERR_LOG("location query operation, rdbStore is null.");
83         return nullptr;
84     }
85     return rdbStore->Query(cmd, columns);
86 }
87 } // namespace Media
88 } // namespace OHOS
89