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 "rdb_data_manager.h"
17
18 #include "scope_guard.h"
19 #include "hilog_wrapper.h"
20
21 namespace OHOS {
22 namespace AAFwk {
23
RdbDataManager(const RdbConfig & rdbConfig)24 RdbDataManager::RdbDataManager(const RdbConfig &rdbConfig) : rdbConfig_(rdbConfig) {}
25
~RdbDataManager()26 RdbDataManager::~RdbDataManager() {}
27
ClearCache()28 void RdbDataManager::ClearCache()
29 {
30 NativeRdb::RdbHelper::ClearCache();
31 }
32
GetRdbStore()33 std::shared_ptr<NativeRdb::RdbStore> RdbDataManager::GetRdbStore()
34 {
35 std::lock_guard<std::mutex> lock(rdbMutex_);
36 if (rdbStore_ != nullptr) {
37 return rdbStore_;
38 }
39 NativeRdb::RdbStoreConfig rdbStoreConfig(rdbConfig_.dbPath + rdbConfig_.dbName);
40 rdbStoreConfig.SetSecurityLevel(NativeRdb::SecurityLevel::S1);
41 int32_t errCode = NativeRdb::E_OK;
42 AbilityRdbOpenCallback abilityRdbOpenCallback(rdbConfig_);
43 rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, rdbConfig_.version, abilityRdbOpenCallback, errCode);
44 return rdbStore_;
45 }
46
InsertData(const NativeRdb::ValuesBucket & valuesBucket)47 bool RdbDataManager::InsertData(const NativeRdb::ValuesBucket &valuesBucket)
48 {
49 HILOG_DEBUG("InsertData start");
50 auto rdbStore = GetRdbStore();
51 if (rdbStore == nullptr) {
52 HILOG_ERROR("RdbStore is null");
53 return false;
54 }
55
56 int64_t rowId = -1;
57 auto ret = rdbStore->InsertWithConflictResolution(rowId, rdbConfig_.tableName, valuesBucket,
58 NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
59 return ret == NativeRdb::E_OK;
60 }
61
BatchInsert(int64_t & outInsertNum,const std::vector<NativeRdb::ValuesBucket> & valuesBuckets)62 bool RdbDataManager::BatchInsert(int64_t &outInsertNum, const std::vector<NativeRdb::ValuesBucket> &valuesBuckets)
63 {
64 auto rdbStore = GetRdbStore();
65 if (rdbStore == nullptr) {
66 HILOG_ERROR("RdbStore is null");
67 return false;
68 }
69 auto ret = rdbStore->BatchInsert(outInsertNum, rdbConfig_.tableName, valuesBuckets);
70 return ret == NativeRdb::E_OK;
71 }
72
UpdateData(const NativeRdb::ValuesBucket & valuesBucket,const NativeRdb::AbsRdbPredicates & absRdbPredicates)73 bool RdbDataManager::UpdateData(const NativeRdb::ValuesBucket &valuesBucket,
74 const NativeRdb::AbsRdbPredicates &absRdbPredicates)
75 {
76 auto rdbStore = GetRdbStore();
77 if (rdbStore == nullptr) {
78 HILOG_ERROR("RdbStore is null");
79 return false;
80 }
81 if (absRdbPredicates.GetTableName() != rdbConfig_.tableName) {
82 HILOG_ERROR("RdbStore table is invalid");
83 return false;
84 }
85 int32_t rowId = -1;
86 auto ret = rdbStore->Update(rowId, valuesBucket, absRdbPredicates);
87 return ret == NativeRdb::E_OK;
88 }
89
DeleteData(const NativeRdb::AbsRdbPredicates & absRdbPredicates)90 bool RdbDataManager::DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates)
91 {
92 auto rdbStore = GetRdbStore();
93 if (rdbStore == nullptr) {
94 HILOG_ERROR("RdbStore is null");
95 return false;
96 }
97 if (absRdbPredicates.GetTableName() != rdbConfig_.tableName) {
98 HILOG_ERROR("RdbStore table is invalid");
99 return false;
100 }
101 int32_t rowId = -1;
102 auto ret = rdbStore->Delete(rowId, absRdbPredicates);
103 return ret == NativeRdb::E_OK;
104 }
105
QueryData(const NativeRdb::AbsRdbPredicates & absRdbPredicates)106 std::shared_ptr<NativeRdb::AbsSharedResultSet> RdbDataManager::QueryData(
107 const NativeRdb::AbsRdbPredicates &absRdbPredicates)
108 {
109 auto rdbStore = GetRdbStore();
110 if (rdbStore == nullptr) {
111 HILOG_ERROR("RdbStore is null");
112 return nullptr;
113 }
114 if (absRdbPredicates.GetTableName() != rdbConfig_.tableName) {
115 HILOG_ERROR("RdbStore table is invalid");
116 return nullptr;
117 }
118 auto absSharedResultSet = rdbStore->Query(absRdbPredicates, std::vector<std::string>());
119 if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
120 HILOG_ERROR("absSharedResultSet failed");
121 return nullptr;
122 }
123 return absSharedResultSet;
124 }
125
CreateTable()126 bool RdbDataManager::CreateTable()
127 {
128 std::string createTableSql;
129 if (rdbConfig_.createTableSql.empty()) {
130 createTableSql = std::string("CREATE TABLE IF NOT EXISTS " + rdbConfig_.tableName +
131 "(KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);");
132 } else {
133 createTableSql = rdbConfig_.createTableSql;
134 }
135
136 auto rdbStore = GetRdbStore();
137 if (rdbStore == nullptr) {
138 HILOG_ERROR("RdbStore is null");
139 return false;
140 }
141 int ret = rdbStore->ExecuteSql(createTableSql);
142 if (ret != NativeRdb::E_OK) {
143 HILOG_ERROR("CreateTable failed, ret: %{public}d", ret);
144 return false;
145 }
146 for (const auto &sql : rdbConfig_.insertColumnSql) {
147 int32_t insertRet = rdbStore->ExecuteSql(sql);
148 if (insertRet != NativeRdb::E_OK) {
149 HILOG_WARN("ExecuteSql insertColumnSql failed, insertRet: %{public}d", insertRet);
150 }
151 }
152 return true;
153 }
154 } // namespace AAFwk
155 } // namespace OHOS
156