• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             int32_t errCode = NativeRdb::E_OK;
47             EdmRdbOpenCallback edmRdbOpenCallback;
48             rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, EdmRdbFiledConst::EDM_RDB_VERSION,
49                 edmRdbOpenCallback, errCode);
50         }
51     }
52     return rdbStore_;
53 }
54 
CreateTable(const std::string & createTableSql)55 bool EdmRdbDataManager::CreateTable(const std::string &createTableSql)
56 {
57     EDMLOGD("EdmRdbDataManager CreateTable start.");
58     auto rdbStore = GetRdbStore();
59     if (rdbStore == nullptr) {
60         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
61         return false;
62     }
63     if (createTableSql.empty()) {
64         EDMLOGE("EdmRdbDataManager createTableSql is empty.");
65         return false;
66     }
67     int32_t ret = rdbStore->ExecuteSql(createTableSql);
68     if (ret != NativeRdb::E_OK) {
69         EDMLOGE("CreateTable failed, ret: %{public}d", ret);
70         return false;
71     }
72     return true;
73 }
74 
Insert(const std::string & tableName,const NativeRdb::ValuesBucket & valuesBucket)75 bool EdmRdbDataManager::Insert(const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket)
76 {
77     EDMLOGD("EdmRdbDataManager Insert data start.");
78     auto rdbStore = GetRdbStore();
79     if (rdbStore == nullptr) {
80         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
81         return false;
82     }
83     int64_t rowId = -1;
84     auto ret = rdbStore->InsertWithConflictResolution(rowId, tableName, valuesBucket,
85         NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
86     return ret == NativeRdb::E_OK;
87 }
88 
Delete(const NativeRdb::AbsRdbPredicates & predicates)89 bool EdmRdbDataManager::Delete(const NativeRdb::AbsRdbPredicates &predicates)
90 {
91     EDMLOGD("EdmRdbDataManager Delete data start.");
92     auto rdbStore = GetRdbStore();
93     if (rdbStore == nullptr) {
94         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
95         return false;
96     }
97     int32_t rowId = -1;
98     auto ret = rdbStore->Delete(rowId, predicates);
99     return ret == NativeRdb::E_OK;
100 }
101 
Update(const NativeRdb::ValuesBucket & valuesBucket,const NativeRdb::AbsRdbPredicates & predicates)102 bool EdmRdbDataManager::Update(const NativeRdb::ValuesBucket &valuesBucket,
103     const NativeRdb::AbsRdbPredicates &predicates)
104 {
105     EDMLOGD("EdmRdbDataManager Update data start.");
106     auto rdbStore = GetRdbStore();
107     if (rdbStore == nullptr) {
108         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
109         return false;
110     }
111     int32_t rowId = -1;
112     auto ret = rdbStore->Update(rowId, valuesBucket, predicates);
113     return ret == NativeRdb::E_OK;
114 }
115 
Query(const NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> & columns)116 std::shared_ptr<NativeRdb::ResultSet> EdmRdbDataManager::Query(const NativeRdb::AbsRdbPredicates &predicates,
117     const std::vector<std::string> &columns)
118 {
119     EDMLOGD("EdmRdbDataManager Query data start.");
120     auto rdbStore = GetRdbStore();
121     if (rdbStore == nullptr) {
122         EDMLOGE("EdmRdbDataManager GetRdbStore failed.");
123         return nullptr;
124     }
125     auto absSharedResultSet = rdbStore->Query(predicates, columns);
126     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
127         EDMLOGE("EdmRdbDataManager query date failed.");
128         return nullptr;
129     }
130     return absSharedResultSet;
131 }
132 } // namespace EDM
133 } // namespace OHOS