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 <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;
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     return NativeRdb::E_OK;
249 }
250 
OnUpgrade(RdbStore & store_,int oldVersion,int newVersion)251 int32_t OpenCallback::OnUpgrade(RdbStore& store_, int oldVersion, int newVersion)
252 {
253     HILOGI("rdbStore upgrade");
254     return NativeRdb::E_OK;
255 }
256 } // namespace DistributedDeviceProfile
257 } // namespace OHOS
258