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 "usb_right_database.h"
17
18 #include "hilog_wrapper.h"
19 #include "usb_errors.h"
20
21 namespace OHOS {
22 namespace USB {
23
24 std::shared_ptr<UsbRightDataBase> UsbRightDataBase::instance_ = nullptr;
25
UsbRightDataBase()26 UsbRightDataBase::UsbRightDataBase()
27 {
28 std::string rightDatabaseName = USB_RIGHT_DB_PATH + USB_RIGHT_DB_NAME;
29 int32_t errCode = OHOS::NativeRdb::E_OK;
30 OHOS::NativeRdb::RdbStoreConfig config(rightDatabaseName);
31 config.SetSecurityLevel(NativeRdb::SecurityLevel::S1);
32 UsbRightDataBaseCallBack sqliteOpenHelperCallback;
33 store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode);
34 if (errCode != OHOS::NativeRdb::E_OK) {
35 USB_HILOGE(MODULE_USB_SERVICE, "GetRdbStore errCode :%{public}d", errCode);
36 } else {
37 USB_HILOGI(MODULE_USB_SERVICE, "GetRdbStore success :%{public}d", errCode);
38 }
39 }
40
GetInstance()41 std::shared_ptr<UsbRightDataBase> UsbRightDataBase::GetInstance()
42 {
43 static std::mutex instanceMutex;
44 std::lock_guard<std::mutex> guard(instanceMutex);
45 if (instance_ == nullptr) {
46 USB_HILOGI(MODULE_USB_SERVICE, "reset to new instance");
47 instance_.reset(new UsbRightDataBase());
48 return instance_;
49 }
50 return instance_;
51 }
52
BeginTransaction()53 int32_t UsbRightDataBase::BeginTransaction()
54 {
55 if (store_ == nullptr) {
56 USB_HILOGE(MODULE_USB_SERVICE, "BeginTransaction store_ is nullptr");
57 return USB_RIGHT_RDB_NO_INIT;
58 }
59 int32_t ret = store_->BeginTransaction();
60 if (ret != OHOS::NativeRdb::E_OK) {
61 USB_HILOGE(MODULE_USB_SERVICE, "BeginTransaction fail :%{public}d", ret);
62 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
63 }
64 return USB_RIGHT_OK;
65 }
66
Commit()67 int32_t UsbRightDataBase::Commit()
68 {
69 if (store_ == nullptr) {
70 USB_HILOGE(MODULE_USB_SERVICE, "Commit store_ is nullptr");
71 return USB_RIGHT_RDB_NO_INIT;
72 }
73 int32_t ret = store_->Commit();
74 if (ret != OHOS::NativeRdb::E_OK) {
75 USB_HILOGE(MODULE_USB_SERVICE, "Commit fail :%{public}d", ret);
76 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
77 }
78 return USB_RIGHT_OK;
79 }
80
RollBack()81 int32_t UsbRightDataBase::RollBack()
82 {
83 if (store_ == nullptr) {
84 USB_HILOGE(MODULE_USB_SERVICE, "RollBack store_ is nullptr");
85 return USB_RIGHT_RDB_NO_INIT;
86 }
87 int32_t ret = store_->RollBack();
88 if (ret != OHOS::NativeRdb::E_OK) {
89 USB_HILOGE(MODULE_USB_SERVICE, "RollBack fail :%{public}d", ret);
90 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
91 }
92 return USB_RIGHT_OK;
93 }
94
Insert(const OHOS::NativeRdb::ValuesBucket & insertValues)95 int64_t UsbRightDataBase::Insert(const OHOS::NativeRdb::ValuesBucket &insertValues)
96 {
97 if (store_ == nullptr) {
98 USB_HILOGE(MODULE_USB_SERVICE, "Insert store_ is nullptr");
99 return USB_RIGHT_RDB_NO_INIT;
100 }
101 int64_t outRowId = 0;
102 int32_t ret = store_->Insert(outRowId, USB_RIGHT_TABLE_NAME, insertValues);
103 USB_HILOGI(MODULE_USB_SERVICE, "Insert id=%{public}" PRIu64 "", outRowId);
104 if (ret != OHOS::NativeRdb::E_OK) {
105 USB_HILOGE(MODULE_USB_SERVICE, "Insert ret :%{public}d", ret);
106 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
107 }
108 return outRowId;
109 }
110
Update(int32_t & changedRows,const OHOS::NativeRdb::ValuesBucket & values,const OHOS::NativeRdb::RdbPredicates & predicates)111 int32_t UsbRightDataBase::Update(
112 int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const OHOS::NativeRdb::RdbPredicates &predicates)
113 {
114 if (store_ == nullptr) {
115 USB_HILOGE(MODULE_USB_SERVICE, "Update(RdbPredicates) store_ is nullptr");
116 return USB_RIGHT_RDB_NO_INIT;
117 }
118 int32_t ret = store_->Update(changedRows, values, predicates);
119 if (ret != OHOS::NativeRdb::E_OK) {
120 USB_HILOGE(MODULE_USB_SERVICE, "Update(RdbPredicates) ret :%{public}d", ret);
121 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
122 }
123 return USB_RIGHT_OK;
124 }
125
Update(int32_t & changedRows,const OHOS::NativeRdb::ValuesBucket & values,const std::string & whereClause,const std::vector<std::string> & whereArgs)126 int32_t UsbRightDataBase::Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values,
127 const std::string &whereClause, const std::vector<std::string> &whereArgs)
128 {
129 if (store_ == nullptr) {
130 USB_HILOGE(MODULE_USB_SERVICE, "Update(whereClause) store_ is nullptr");
131 return USB_RIGHT_RDB_NO_INIT;
132 }
133 int32_t ret = store_->Update(changedRows, USB_RIGHT_TABLE_NAME, values, whereClause, whereArgs);
134 if (ret != OHOS::NativeRdb::E_OK) {
135 USB_HILOGE(MODULE_USB_SERVICE, "Update(whereClause) ret :%{public}d", ret);
136 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
137 }
138 return USB_RIGHT_OK;
139 }
140
Delete(const OHOS::NativeRdb::RdbPredicates & predicates)141 int32_t UsbRightDataBase::Delete(const OHOS::NativeRdb::RdbPredicates &predicates)
142 {
143 if (store_ == nullptr) {
144 USB_HILOGE(MODULE_USB_SERVICE, "Delete(RdbPredicates) store_ is nullptr");
145 return USB_RIGHT_RDB_NO_INIT;
146 }
147 int32_t deleteRow = 0;
148 int32_t ret = store_->Delete(deleteRow, predicates);
149 if (ret != OHOS::NativeRdb::E_OK) {
150 USB_HILOGE(MODULE_USB_SERVICE, "Delete(RdbPredicates) ret :%{public}d", ret);
151 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
152 }
153 return USB_RIGHT_OK;
154 }
155
Delete(int32_t & changedRows,const std::string & whereClause,const std::vector<std::string> & whereArgs)156 int32_t UsbRightDataBase::Delete(
157 int32_t &changedRows, const std::string &whereClause, const std::vector<std::string> &whereArgs)
158 {
159 if (store_ == nullptr) {
160 USB_HILOGE(MODULE_USB_SERVICE, "Delete store_ is nullptr");
161 return USB_RIGHT_RDB_NO_INIT;
162 }
163 int32_t ret = store_->Delete(changedRows, USB_RIGHT_TABLE_NAME, whereClause, whereArgs);
164 if (ret != OHOS::NativeRdb::E_OK) {
165 USB_HILOGE(MODULE_USB_SERVICE, "Delete(whereClause) ret :%{public}d", ret);
166 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
167 }
168 return USB_RIGHT_OK;
169 }
170
ExecuteSql(const std::string & sql,const std::vector<OHOS::NativeRdb::ValueObject> & bindArgs)171 int32_t UsbRightDataBase::ExecuteSql(const std::string &sql, const std::vector<OHOS::NativeRdb::ValueObject> &bindArgs)
172 {
173 if (store_ == nullptr) {
174 USB_HILOGE(MODULE_USB_SERVICE, "ExecuteSql store_ is nullptr");
175 return USB_RIGHT_RDB_NO_INIT;
176 }
177 int32_t ret = store_->ExecuteSql(sql, bindArgs);
178 if (ret != OHOS::NativeRdb::E_OK) {
179 USB_HILOGE(MODULE_USB_SERVICE, "ExecuteSql ret :%{public}d", ret);
180 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
181 }
182 return USB_RIGHT_OK;
183 }
184
QuerySql(const std::string & sql,const std::vector<std::string> & selectionArgs)185 std::shared_ptr<OHOS::NativeRdb::ResultSet> UsbRightDataBase::QuerySql(
186 const std::string &sql, const std::vector<std::string> &selectionArgs)
187 {
188 if (store_ == nullptr) {
189 USB_HILOGE(MODULE_USB_SERVICE, "QuerySql(sql) store_ is nullptr");
190 return nullptr;
191 }
192 return store_->QuerySql(sql);
193 }
194
Query(const OHOS::NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> & columns)195 std::shared_ptr<OHOS::NativeRdb::ResultSet> UsbRightDataBase::Query(
196 const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns)
197 {
198 if (store_ == nullptr) {
199 USB_HILOGE(MODULE_USB_SERVICE, "Query(AbsRdbPredicates) store_ is nullptr");
200 return nullptr;
201 }
202 return store_->Query(predicates, columns);
203 }
204
OnCreate(OHOS::NativeRdb::RdbStore & store)205 int32_t UsbRightDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store)
206 {
207 std::string sql = CREATE_USB_RIGHT_TABLE;
208 int32_t ret = store.ExecuteSql(sql);
209 if (ret != OHOS::NativeRdb::E_OK) {
210 USB_HILOGE(MODULE_USB_SERVICE, "OnCreate failed: %{public}d", ret);
211 return USB_RIGHT_RDB_EXECUTE_FAILTURE;
212 }
213 USB_HILOGI(MODULE_USB_SERVICE, "DB OnCreate Done: %{public}d", ret);
214 return USB_RIGHT_OK;
215 }
216
OnUpgrade(OHOS::NativeRdb::RdbStore & store,int32_t oldVersion,int32_t newVersion)217 int32_t UsbRightDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion)
218 {
219 USB_HILOGI(MODULE_USB_SERVICE, "DB OnUpgrade Enter %{public}d => %{public}d", oldVersion, newVersion);
220 if (oldVersion >= newVersion) {
221 return USB_RIGHT_OK;
222 }
223
224 std::string sql = SQL_ADD_TOKEN_ID;
225 int32_t ret = store.ExecuteSql(sql);
226 if (ret != OHOS::NativeRdb::E_OK) {
227 // ignore sql error when tokenId is already exists
228 USB_HILOGW(MODULE_USB_SERVICE, "DB OnUpgrade failed: %{public}d", ret);
229 }
230 return USB_RIGHT_OK;
231 }
232
OnDowngrade(OHOS::NativeRdb::RdbStore & store,int32_t oldVersion,int32_t newVersion)233 int32_t UsbRightDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion)
234 {
235 USB_HILOGI(MODULE_USB_SERVICE, "DB OnDowngrade Enter");
236 (void)store;
237 (void)oldVersion;
238 (void)newVersion;
239 return USB_RIGHT_OK;
240 }
241
242 } // namespace USB
243 } // namespace OHOS
244