1 /*
2 * Copyright (c) 2024 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 "profile_data_rdb_adapter.h"
17
18 #include <mutex>
19 #include <chrono>
20 #include <unistd.h>
21
22 #include "distributed_device_profile_constants.h"
23 #include "dp_services_constants.h"
24 #include "distributed_device_profile_errors.h"
25 #include "distributed_device_profile_log.h"
26 #include "rdb_errno.h"
27
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 IMPLEMENT_SINGLE_INSTANCE(ProfileDataRdbAdapter);
31 using namespace std::chrono_literals;
32 namespace {
33 const std::set<std::string> TABLES = {
34 "device_profile",
35 "service_profile",
36 "characteristic_profile",
37 "device_icon_info",
38 "product_info"
39 };
40 const std::string TAG = "ProfileDatardbAdapter";
41 }
42
Init()43 int32_t ProfileDataRdbAdapter::Init()
44 {
45 int32_t retryTimes = RDB_INIT_MAX_TIMES;
46 while (retryTimes > 0) {
47 if (GetRDBPtr() == DP_SUCCESS) {
48 HILOGI("ProfileDatardbAdapter init success");
49 return DP_SUCCESS;
50 }
51 usleep(RDB_INIT_INTERVAL_TIME);
52 retryTimes--;
53 }
54 HILOGE("ProfileDatardbAdapter init failed");
55 return DP_RDBADAPTER_INIT_FAIL;
56 }
57
UnInit()58 int32_t ProfileDataRdbAdapter::UnInit()
59 {
60 HILOGI("ProfileDatardbAdapter unInit");
61 {
62 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
63 store_ = nullptr;
64 }
65 return DP_SUCCESS;
66 }
67
Put(int64_t & outRowId,const std::string & table,const ValuesBucket & values)68 int32_t ProfileDataRdbAdapter::Put(int64_t& outRowId, const std::string& table, const ValuesBucket& values)
69 {
70 if (TABLES.find(table) == TABLES.end()) {
71 HILOGE("table does not exist");
72 return DP_RDBADAPTER_TABLE_NOT_EXIST;
73 }
74 {
75 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
76 if (store_ == nullptr) {
77 HILOGE("RDBStore_ is null");
78 return DP_RDB_DB_PTR_NULL;
79 }
80 int32_t ret = store_->Insert(outRowId, table, values);
81 if (ret == E_SQLITE_CORRUPT) {
82 HILOGE("database corrupt ret:%{public}d", ret);
83 int32_t restoreRet = store_->Restore("");
84 if (restoreRet != E_OK) {
85 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
86 return DP_RDB_DATABASE_RESTORE_FAIL;
87 }
88 ret = store_->Insert(outRowId, table, values);
89 }
90 if (ret != E_OK) {
91 HILOGE("ProfileDatardbAdapter put failed ret:%{public}d", ret);
92 return DP_RDBADAPTER_PUT_FAIL;
93 }
94 }
95 return DP_SUCCESS;
96 }
97
Delete(int32_t & deleteRows,const std::string & table,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)98 int32_t ProfileDataRdbAdapter::Delete(int32_t& deleteRows, const std::string& table, const std::string& whereClause,
99 const std::vector<ValueObject>& bindArgs)
100 {
101 if (TABLES.find(table) == TABLES.end()) {
102 HILOGE("table does not exist");
103 return DP_RDBADAPTER_TABLE_NOT_EXIST;
104 }
105 {
106 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
107 if (store_ == nullptr) {
108 HILOGE("RDBStore_ is null");
109 return DP_RDB_DB_PTR_NULL;
110 }
111 int32_t ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
112 if (ret == E_SQLITE_CORRUPT) {
113 HILOGE("database corrupt ret:%{public}d", ret);
114 int32_t restoreRet = store_->Restore("");
115 if (restoreRet != E_OK) {
116 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
117 return DP_RDB_DATABASE_RESTORE_FAIL;
118 }
119 ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
120 }
121 if (ret != E_OK) {
122 HILOGE("ProfileDatardbAdapter delete failed ret:%{public}d", ret);
123 return DP_RDBADAPTER_DELETE_FAIL;
124 }
125 }
126 return DP_SUCCESS;
127 }
128
Update(int32_t & changedRows,const std::string & table,const ValuesBucket & values,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)129 int32_t ProfileDataRdbAdapter::Update(int32_t& changedRows, const std::string& table, const ValuesBucket& values,
130 const std::string& whereClause, const std::vector<ValueObject>& bindArgs)
131 {
132 if (TABLES.find(table) == TABLES.end()) {
133 HILOGE("table does not exist");
134 return DP_RDBADAPTER_TABLE_NOT_EXIST;
135 }
136 {
137 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
138 if (store_ == nullptr) {
139 HILOGE("RDBStore_ is null");
140 return DP_RDB_DB_PTR_NULL;
141 }
142 int32_t ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
143 if (ret == E_SQLITE_CORRUPT) {
144 HILOGE("database corrupt ret:%{public}d", ret);
145 int32_t restoreRet = store_->Restore("");
146 if (restoreRet != E_OK) {
147 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
148 return DP_RDB_DATABASE_RESTORE_FAIL;
149 }
150 ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
151 }
152 if (ret != E_OK) {
153 HILOGE("ProfileDatardbAdapter update failed ret:%{public}d", ret);
154 return DP_RDBADAPTER_UPDATE_FAIL;
155 }
156 }
157 return DP_SUCCESS;
158 }
159
Get(const std::string & sql,const std::vector<ValueObject> & args)160 std::shared_ptr<ResultSet> ProfileDataRdbAdapter::Get(const std::string& sql, const std::vector<ValueObject>& args)
161 {
162 std::shared_ptr<ResultSet> resultSet = nullptr;
163 {
164 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
165 if (store_ == nullptr) {
166 HILOGE("RDBStore_ is null");
167 return nullptr;
168 }
169 resultSet = store_->QueryByStep(sql, args);
170 if (resultSet == nullptr) {
171 HILOGE("resultSet is null");
172 return nullptr;
173 }
174 int32_t rowCount = ROWCOUNT_INIT;
175 int32_t ret = resultSet->GetRowCount(rowCount);
176 if (ret == E_SQLITE_CORRUPT) {
177 HILOGE("database corrupt ret:%{public}d", ret);
178 resultSet->Close();
179 ret = store_->Restore("");
180 if (ret != E_OK) {
181 HILOGE("Restore failed ret:%{public}d", ret);
182 return nullptr;
183 }
184 resultSet = store_->QueryByStep(sql, args);
185 }
186 }
187 return resultSet;
188 }
189
GetRDBPtr()190 int32_t ProfileDataRdbAdapter::GetRDBPtr()
191 {
192 int32_t version = RDB_VERSION_5_1;
193 ProfileDataOpenCallback helper;
194 RdbStoreConfig config(PROFILE_DATA_RDB_PATH + PROFILE_DATA_DATABASE_NAME);
195 config.SetSecurityLevel(SecurityLevel::S2);
196 config.SetHaMode(HAMode::MAIN_REPLICA);
197 config.SetAllowRebuild(true);
198 int32_t errCode = E_OK;
199 {
200 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
201 store_ = RdbHelper::GetRdbStore(config, version, helper, errCode);
202 if (store_ == nullptr) {
203 HILOGE("RDBStore_ is null");
204 return DP_RDB_DB_PTR_NULL;
205 }
206 NativeRdb::RebuiltType rebuiltType = NativeRdb::RebuiltType::NONE;
207 errCode = store_->GetRebuilt(rebuiltType);
208 if (errCode != E_OK) {
209 HILOGE("getRDBPtr failed errCode:%{public}d", errCode);
210 return DP_GET_RDBSTORE_FAIL;
211 }
212 if (rebuiltType == NativeRdb::RebuiltType::REBUILT) {
213 HILOGE("database corrupt");
214 int32_t restoreRet = store_->Restore("");
215 if (restoreRet != E_OK) {
216 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
217 return DP_RDB_DATABASE_RESTORE_FAIL;
218 }
219 }
220 }
221 return DP_SUCCESS;
222 }
223
IsInit()224 bool ProfileDataRdbAdapter::IsInit()
225 {
226 {
227 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
228 if (store_ == nullptr) {
229 return false;
230 }
231 }
232 return true;
233 }
234
CreateTable(const std::string & sql)235 int32_t ProfileDataRdbAdapter::CreateTable(const std::string& sql)
236 {
237 {
238 std::lock_guard<std::mutex> lock(ProfileDataRdbAdapterMtx_);
239 if (store_ == nullptr) {
240 HILOGE("RDBStore_ is null");
241 return DP_RDB_DB_PTR_NULL;
242 }
243 if (store_->ExecuteSql(sql) != E_OK) {
244 HILOGE("ProfileDatardbAdapter create table failed");
245 return DP_RDBADAPTER_TABLE_NOT_EXIST;
246 }
247 }
248 return DP_SUCCESS;
249 }
250
OnCreate(RdbStore & store)251 int32_t ProfileDataOpenCallback::OnCreate(RdbStore& store)
252 {
253 HILOGI("rdbStore create");
254 return NativeRdb::E_OK;
255 }
256
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)257 int32_t ProfileDataOpenCallback::OnUpgrade(RdbStore& store, int oldVersion, int newVersion)
258 {
259 HILOGI("rdbStore upgrade : %{public}d -> %{public}d", oldVersion, newVersion);
260 if (oldVersion == RDB_VERSION && newVersion == RDB_VERSION_5_1) {
261 return UpdateFromVer1To2(store);
262 }
263 return NativeRdb::E_OK;
264 }
265
UpdateFromVer1To2(RdbStore & store)266 int32_t ProfileDataOpenCallback::UpdateFromVer1To2(RdbStore& store)
267 {
268 int32_t ret = store.ExecuteSql(ALTER_TABLE_DP_ADD_COLUMN_PRODUCT_NAME_SQL);
269 if (ret != NativeRdb::E_OK) {
270 HILOGE("add column to device_profile table failed, ret:%{public}d", ret);
271 return ret;
272 }
273 ret = store.ExecuteSql(ALTER_TABLE_DP_RENAME_COLUMN_INTERNAL_MODEL_SQL);
274 if (ret != NativeRdb::E_OK) {
275 HILOGE("add column to device_icon_info table failed, ret:%{public}d", ret);
276 return ret;
277 }
278 ret = store.ExecuteSql(ALTER_TABLE_DEVICE_ICON_INFO_ADD_COLUMN_INTENAL_MODEL_SQL);
279 if (ret != NativeRdb::E_OK) {
280 HILOGE("add column to device_icon_info table failed, ret:%{public}d", ret);
281 return ret;
282 }
283 return NativeRdb::E_OK;
284 }
285 } // namespace DistributedDeviceProfile
286 } // namespace OHOS
287