• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 <mutex>
17 #include <chrono>
18 #include <unistd.h>
19 #include "rdb_adapter.h"
20 #include "rdb_errno.h"
21 #include "distributed_device_profile_constants.h"
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 
25 namespace OHOS {
26 namespace DistributedDeviceProfile {
27 using namespace std::chrono_literals;
28 namespace {
29     const std::set<std::string> TABLES = {
30         "trust_device_table",
31         "accesser_table",
32         "accessee_table",
33         "access_control_table",
34         "subscribe_trust_info_table"
35     };
36     const std::string TAG = "rdbAdapter";
37 }
38 
RdbAdapter()39 RdbAdapter::RdbAdapter()
40 {
41     HILOGI("rdbAdapter constructor");
42 }
43 
~RdbAdapter()44 RdbAdapter::~RdbAdapter()
45 {
46     HILOGI("rdbAdapter destructor");
47 }
48 
Init()49 int32_t RdbAdapter::Init()
50 {
51     int32_t retryTimes = RDB_INIT_MAX_TIMES;
52     while (retryTimes > 0) {
53         if (GetRDBPtr() == DP_SUCCESS) {
54             HILOGI("rdbAdapter init success");
55             return DP_SUCCESS;
56         }
57         usleep(RDB_INIT_INTERVAL_TIME);
58         retryTimes--;
59     }
60     HILOGE("rdbAdapter init failed");
61     return DP_RDBADAPTER_INIT_FAIL;
62 }
63 
UnInit()64 int32_t RdbAdapter::UnInit()
65 {
66     HILOGI("rdbAdapter unInit");
67     {
68         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
69         store_ = nullptr;
70     }
71     return DP_SUCCESS;
72 }
73 
Put(int64_t & outRowId,const std::string & table,const ValuesBucket & values)74 int32_t RdbAdapter::Put(int64_t& outRowId, const std::string& table, const ValuesBucket& values)
75 {
76     if (TABLES.find(table) == TABLES.end()) {
77         HILOGE("table does not exist");
78         return DP_RDBADAPTER_TABLE_NOT_EXIST;
79     }
80     {
81         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
82         if (store_ == nullptr) {
83             HILOGE("RDBStore_ is null");
84             return DP_RDB_DB_PTR_NULL;
85         }
86         int32_t ret = store_->Insert(outRowId, table, values);
87         if (ret == E_SQLITE_CORRUPT) {
88             HILOGE("database corrupt ret:%{public}d", ret);
89             int32_t restoreRet = store_->Restore("");
90             if (restoreRet != E_OK) {
91                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
92                 return DP_RDB_DATABASE_RESTORE_FAIL;
93             }
94             ret = store_->Insert(outRowId, table, values);
95         }
96         if (ret != E_OK) {
97             HILOGE("rdbAdapter put failed ret:%{public}d", ret);
98             return DP_RDBADAPTER_PUT_FAIL;
99         }
100     }
101     return DP_SUCCESS;
102 }
103 
Delete(int32_t & deleteRows,const std::string & table,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)104 int32_t RdbAdapter::Delete(int32_t& deleteRows, const std::string& table, const std::string& whereClause,
105     const std::vector<ValueObject>& bindArgs)
106 {
107     if (TABLES.find(table) == TABLES.end()) {
108         HILOGE("table does not exist");
109         return DP_RDBADAPTER_TABLE_NOT_EXIST;
110     }
111     {
112         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
113         if (store_ == nullptr) {
114             HILOGE("RDBStore_ is null");
115             return DP_RDB_DB_PTR_NULL;
116         }
117         int32_t ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
118         if (ret == E_SQLITE_CORRUPT) {
119             HILOGE("database corrupt ret:%{public}d", ret);
120             int32_t restoreRet = store_->Restore("");
121             if (restoreRet != E_OK) {
122                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
123                 return DP_RDB_DATABASE_RESTORE_FAIL;
124             }
125             ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
126         }
127         if (ret != E_OK) {
128             HILOGE("rdbAdapter delete failed ret:%{public}d", ret);
129             return DP_RDBADAPTER_DELETE_FAIL;
130         }
131     }
132     return DP_SUCCESS;
133 }
134 
Update(int32_t & changedRows,const std::string & table,const ValuesBucket & values,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)135 int32_t RdbAdapter::Update(int32_t& changedRows, const std::string& table, const ValuesBucket& values,
136     const std::string& whereClause, const std::vector<ValueObject>& bindArgs)
137 {
138     if (TABLES.find(table) == TABLES.end()) {
139         HILOGE("table does not exist");
140         return DP_RDBADAPTER_TABLE_NOT_EXIST;
141     }
142     {
143         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
144         if (store_ == nullptr) {
145             HILOGE("RDBStore_ is null");
146             return DP_RDB_DB_PTR_NULL;
147         }
148         int32_t ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
149         if (ret == E_SQLITE_CORRUPT) {
150             HILOGE("database corrupt ret:%{public}d", ret);
151             int32_t restoreRet = store_->Restore("");
152             if (restoreRet != E_OK) {
153                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
154                 return DP_RDB_DATABASE_RESTORE_FAIL;
155             }
156             ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
157         }
158         if (ret != E_OK) {
159             HILOGE("rdbAdapter update failed ret:%{public}d", ret);
160             return DP_RDBADAPTER_UPDATE_FAIL;
161         }
162     }
163     return DP_SUCCESS;
164 }
165 
Get(const std::string & sql,const std::vector<ValueObject> & args)166 std::shared_ptr<ResultSet> RdbAdapter::Get(const std::string& sql, const std::vector<ValueObject>& args)
167 {
168     std::shared_ptr<ResultSet> resultSet = nullptr;
169     {
170         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
171         if (store_ == nullptr) {
172             HILOGE("RDBStore_ is null");
173             return nullptr;
174         }
175         resultSet = store_->QueryByStep(sql, args);
176         if (resultSet == nullptr) {
177             HILOGE("resultSet is null");
178             return nullptr;
179         }
180         int32_t rowCount = ROWCOUNT_INIT;
181         int32_t ret = resultSet->GetRowCount(rowCount);
182         if (ret == E_SQLITE_CORRUPT) {
183             HILOGE("database corrupt ret:%{public}d", ret);
184             resultSet->Close();
185             ret = store_->Restore("");
186             if (ret != E_OK) {
187                 HILOGE("Restore failed ret:%{public}d", ret);
188                 return nullptr;
189             }
190             resultSet = store_->QueryByStep(sql, args);
191         }
192     }
193     return resultSet;
194 }
195 
GetRDBPtr()196 int32_t RdbAdapter::GetRDBPtr()
197 {
198     int32_t version = RDB_VERSION_5_1;
199     OpenCallback helper;
200     RdbStoreConfig config(RDB_PATH + DATABASE_NAME);
201     config.SetHaMode(HAMode::MAIN_REPLICA);
202     config.SetAllowRebuild(true);
203     int32_t errCode = E_OK;
204     {
205         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
206         store_ = RdbHelper::GetRdbStore(config, version, helper, errCode);
207         if (store_ == nullptr) {
208             HILOGE("RDBStore_ is null");
209             return DP_RDB_DB_PTR_NULL;
210         }
211         NativeRdb::RebuiltType rebuiltType = NativeRdb::RebuiltType::NONE;
212         errCode = store_->GetRebuilt(rebuiltType);
213         if (errCode != E_OK) {
214             HILOGE("getRDBPtr failed errCode:%{public}d", errCode);
215             return DP_GET_RDBSTORE_FAIL;
216         }
217         if (rebuiltType == NativeRdb::RebuiltType::REBUILT) {
218             HILOGE("database corrupt");
219             int32_t restoreRet = store_->Restore("");
220             if (restoreRet != E_OK) {
221                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
222                 return DP_RDB_DATABASE_RESTORE_FAIL;
223             }
224         }
225     }
226     return DP_SUCCESS;
227 }
228 
CreateTable(const std::string & sql)229 int32_t RdbAdapter::CreateTable(const std::string& sql)
230 {
231     {
232         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
233         if (store_ == nullptr) {
234             HILOGE("RDBStore_ is null");
235             return DP_RDB_DB_PTR_NULL;
236         }
237         if (store_->ExecuteSql(sql) != E_OK) {
238             HILOGE("rdbAdapter create table failed");
239             return DP_RDBADAPTER_CREATE_TABLE_FAIL;
240         }
241     }
242     return DP_SUCCESS;
243 }
244 
OnCreate(RdbStore & store)245 int32_t OpenCallback::OnCreate(RdbStore& store)
246 {
247     HILOGI("rdbStore create");
248     if (CreateTable(store) != DP_SUCCESS) {
249         HILOGE("CreateTable failed");
250         return DP_CREATE_TABLE_FAIL;
251     }
252     if (CreateUniqueIndex(store) != DP_SUCCESS) {
253         HILOGE("CreateUniqueIndex failed");
254         return DP_CREATE_UNIQUE_INDEX_FAIL;
255     }
256     return DP_SUCCESS;
257 }
258 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)259 int32_t OpenCallback::OnUpgrade(RdbStore& store, int oldVersion, int newVersion)
260 {
261     HILOGI("rdbStore upgrade, oldVersion : %{public}d, newVersion : %{public}d", oldVersion, newVersion);
262     if (oldVersion == RDB_VERSION && newVersion == RDB_VERSION_5_1) {
263         int32_t ret = AddAcerColumn(store);
264         if (ret != DP_SUCCESS) {
265             HILOGE("AddAcerColumn failed");
266             return ret;
267         }
268         ret = AddAceeColumn(store);
269         if (ret != DP_SUCCESS) {
270             HILOGE("AddAceeColumn failed");
271             return ret;
272         }
273         ret = DropAndRebuildIndex(store);
274         if (ret != DP_SUCCESS) {
275             HILOGE("DropAndRebuildIndex failed");
276             return ret;
277         }
278     }
279     return DP_SUCCESS;
280 }
281 
CreateTable(RdbStore & store)282 int32_t OpenCallback::CreateTable(RdbStore& store)
283 {
284     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
285     if (store.ExecuteSql(CREATE_TURST_DEVICE_TABLE_SQL) != NativeRdb::E_OK) {
286         HILOGE("trust_device_table create failed");
287         return DP_CREATE_TABLE_FAIL;
288     }
289     if (store.ExecuteSql(CREATE_ACCESS_CONTROL_TABLE_SQL) != NativeRdb::E_OK) {
290         HILOGE("access_control_table create failed");
291         return DP_CREATE_TABLE_FAIL;
292     }
293     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_SQL) != NativeRdb::E_OK) {
294         HILOGE("accesser_table create failed");
295         return DP_CREATE_TABLE_FAIL;
296     }
297     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_SQL) != NativeRdb::E_OK) {
298         HILOGE("accessee_table create failed");
299         return DP_CREATE_TABLE_FAIL;
300     }
301     return DP_SUCCESS;
302 }
303 
CreateUniqueIndex(RdbStore & store)304 int32_t OpenCallback::CreateUniqueIndex(RdbStore& store)
305 {
306     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
307     if (store.ExecuteSql(CREATE_TURST_DEVICE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
308         HILOGE("trust_device_table unique index create failed");
309         return DP_CREATE_UNIQUE_INDEX_FAIL;
310     }
311     if (store.ExecuteSql(CREATE_ACCESS_CONTROL_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
312         HILOGE("access_control_table unique index create failed");
313         return DP_CREATE_UNIQUE_INDEX_FAIL;
314     }
315     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
316         HILOGE("accesser_table unique index create failed");
317         return DP_CREATE_UNIQUE_INDEX_FAIL;
318     }
319     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
320         HILOGE("accessee_table unique index create failed");
321         return DP_CREATE_UNIQUE_INDEX_FAIL;
322     }
323     return DP_SUCCESS;
324 }
325 
AddAcerColumn(RdbStore & store)326 int32_t OpenCallback::AddAcerColumn(RdbStore& store)
327 {
328     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
329     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_DEVICE_NAME) != NativeRdb::E_OK) {
330         HILOGE("add column to acer table failed");
331         return DP_CREATE_TABLE_FAIL;
332     }
333     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SERVICE_NAME) != NativeRdb::E_OK) {
334         HILOGE("add column to acer table failed");
335         return DP_CREATE_TABLE_FAIL;
336     }
337     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_CREDENTIAL_ID) != NativeRdb::E_OK) {
338         HILOGE("add column to acer table failed");
339         return DP_CREATE_TABLE_FAIL;
340     }
341     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_STATUS) != NativeRdb::E_OK) {
342         HILOGE("add column to acer table failed");
343         return DP_CREATE_TABLE_FAIL;
344     }
345     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SESSION_KEY_ID) != NativeRdb::E_OK) {
346         HILOGE("add column to acer table failed");
347         return DP_CREATE_TABLE_FAIL;
348     }
349     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SESSION_KEY_TIMESTAMP) != NativeRdb::E_OK) {
350         HILOGE("add column to acer table failed");
351         return DP_CREATE_TABLE_FAIL;
352     }
353     return DP_SUCCESS;
354 }
355 
AddAceeColumn(RdbStore & store)356 int32_t OpenCallback::AddAceeColumn(RdbStore &store)
357 {
358     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
359     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_DEVICE_NAME) != NativeRdb::E_OK) {
360         HILOGE("add column to acee table failed");
361         return DP_CREATE_TABLE_FAIL;
362     }
363     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SERVICE_NAME) != NativeRdb::E_OK) {
364         HILOGE("add column to acee table failed");
365         return DP_CREATE_TABLE_FAIL;
366     }
367     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_CREDENTIAL_ID) != NativeRdb::E_OK) {
368         HILOGE("add column to acee table failed");
369         return DP_CREATE_TABLE_FAIL;
370     }
371     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_STATUS) != NativeRdb::E_OK) {
372         HILOGE("add column to acee table failed");
373         return DP_CREATE_TABLE_FAIL;
374     }
375     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SESSION_KEY_ID) != NativeRdb::E_OK) {
376         HILOGE("add column to acee table failed");
377         return DP_CREATE_TABLE_FAIL;
378     }
379     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SESSION_KEY_TIMESTAMP) != NativeRdb::E_OK) {
380         HILOGE("add column to acee table failed");
381         return DP_CREATE_TABLE_FAIL;
382     }
383     return DP_SUCCESS;
384 }
385 
DropAndRebuildIndex(RdbStore & store)386 int32_t OpenCallback::DropAndRebuildIndex(RdbStore& store)
387 {
388     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
389     if (store.ExecuteSql(DROP_OLD_UNIQUE_INDEX_ON_ACER) != NativeRdb::E_OK) {
390         HILOGE("delete old unique index to acer table failed");
391         return DP_CREATE_TABLE_FAIL;
392     }
393     if (store.ExecuteSql(DROP_OLD_UNIQUE_INDEX_ON_ACEE) != NativeRdb::E_OK) {
394         HILOGE("delete old unique index to acee table failed");
395         return DP_CREATE_TABLE_FAIL;
396     }
397     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
398         HILOGE("create new unique index to acer table failed");
399         return DP_CREATE_TABLE_FAIL;
400     }
401     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
402         HILOGE("create new unique index to acee table failed");
403         return DP_CREATE_TABLE_FAIL;
404     }
405     return DP_SUCCESS;
406 }
407 } // namespace DistributedDeviceProfile
408 } // namespace OHOS
409