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 
16 #include "edm_rdb_data_manager.h"
17 #include "edm_log.h"
18 #include "edm_rdb_filed_const.h"
19 #include "edm_rdb_open_callback.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 std::shared_ptr<NativeRdb::RdbStore> EdmRdbDataManager::rdbStore_ = nullptr;
24 std::shared_ptr<EdmRdbDataManager> EdmRdbDataManager::instance_ = nullptr;
25 std::mutex EdmRdbDataManager::mutexLock_;
26 
GetInstance()27 std::shared_ptr<EdmRdbDataManager> EdmRdbDataManager::GetInstance()
28 {
29     if (instance_ == nullptr) {
30         std::lock_guard<std::mutex> lock(mutexLock_);
31         if (instance_ == nullptr) {
32             instance_ = std::make_shared<EdmRdbDataManager>();
33         }
34     }
35     return instance_;
36 }
37 
GetRdbStore()38 std::shared_ptr<NativeRdb::RdbStore> EdmRdbDataManager::GetRdbStore()
39 {
40     EDMLOGD("EdmRdbDataManager GetRdbStore.");
41     if (rdbStore_ == nullptr) {
42         std::lock_guard<std::mutex> lock(mutexLock_);
43         if (rdbStore_ == nullptr) {
44             NativeRdb::RdbStoreConfig rdbStoreConfig(EdmRdbFiledConst::EDM_SERVICE_DATABASE_PATH +
45                 EdmRdbFiledConst::EDM_RDB_NAME);
46             rdbStoreConfig.SetSecurityLevel(NativeRdb::SecurityLevel::S1);
47             int32_t errCode = NativeRdb::E_OK;
48             EdmRdbOpenCallback edmRdbOpenCallback;
49             rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, EdmRdbFiledConst::EDM_RDB_VERSION,
50                 edmRdbOpenCallback, errCode);
51         }
52     }
53     return rdbStore_;
54 }
55 
CreateTable(const std::string & createTableSql)56 bool EdmRdbDataManager::CreateTable(const std::string &createTableSql)
57 {
58     EDMLOGD("EdmRdbDataManager CreateTable start.");
59     auto rdbStore = GetRdbStore();
60     if (rdbStore == nullptr) {
61         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
62         return false;
63     }
64     if (createTableSql.empty()) {
65         EDMLOGE("EdmRdbDataManager createTableSql is empty.");
66         return false;
67     }
68     int32_t ret = rdbStore->ExecuteSql(createTableSql);
69     if (ret != NativeRdb::E_OK) {
70         EDMLOGE("CreateTable failed, ret: %{public}d", ret);
71         return false;
72     }
73     return true;
74 }
75 
Insert(const std::string & tableName,const NativeRdb::ValuesBucket & valuesBucket)76 bool EdmRdbDataManager::Insert(const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket)
77 {
78     EDMLOGD("EdmRdbDataManager Insert data start.");
79     auto rdbStore = GetRdbStore();
80     if (rdbStore == nullptr) {
81         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
82         return false;
83     }
84     int64_t rowId = -1;
85     auto ret = rdbStore->InsertWithConflictResolution(rowId, tableName, valuesBucket,
86         NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
87     return ret == NativeRdb::E_OK;
88 }
89 
Delete(const NativeRdb::AbsRdbPredicates & predicates)90 bool EdmRdbDataManager::Delete(const NativeRdb::AbsRdbPredicates &predicates)
91 {
92     EDMLOGD("EdmRdbDataManager Delete data start.");
93     auto rdbStore = GetRdbStore();
94     if (rdbStore == nullptr) {
95         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
96         return false;
97     }
98     int32_t rowId = -1;
99     auto ret = rdbStore->Delete(rowId, predicates);
100     return ret == NativeRdb::E_OK;
101 }
102 
Update(const NativeRdb::ValuesBucket & valuesBucket,const NativeRdb::AbsRdbPredicates & predicates)103 bool EdmRdbDataManager::Update(const NativeRdb::ValuesBucket &valuesBucket,
104     const NativeRdb::AbsRdbPredicates &predicates)
105 {
106     EDMLOGD("EdmRdbDataManager Update data start.");
107     auto rdbStore = GetRdbStore();
108     if (rdbStore == nullptr) {
109         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
110         return false;
111     }
112     int32_t rowId = -1;
113     auto ret = rdbStore->Update(rowId, valuesBucket, predicates);
114     return ret == NativeRdb::E_OK;
115 }
116 
Query(const NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> & columns)117 std::shared_ptr<NativeRdb::ResultSet> EdmRdbDataManager::Query(const NativeRdb::AbsRdbPredicates &predicates,
118     const std::vector<std::string> &columns)
119 {
120     EDMLOGD("EdmRdbDataManager Query data start.");
121     auto rdbStore = GetRdbStore();
122     if (rdbStore == nullptr) {
123         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
124         return nullptr;
125     }
126     auto absSharedResultSet = rdbStore->Query(predicates, columns);
127     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
128         EDMLOGE("EdmRdbDataManager query date failed.");
129         return nullptr;
130     }
131     return absSharedResultSet;
132 }
133 } // namespace EDM
134 } // namespace OHOS