• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (instance_ == nullptr) {
44         USB_HILOGI(MODULE_USB_SERVICE, "reset to new instance");
45         instance_.reset(new UsbRightDataBase());
46         return instance_;
47     }
48     return instance_;
49 }
50 
BeginTransaction()51 int32_t UsbRightDataBase::BeginTransaction()
52 {
53     if (store_ == nullptr) {
54         USB_HILOGE(MODULE_USB_SERVICE, "BeginTransaction store_ is nullptr");
55         return USB_RIGHT_RDB_NO_INIT;
56     }
57     int32_t ret = store_->BeginTransaction();
58     if (ret != OHOS::NativeRdb::E_OK) {
59         USB_HILOGE(MODULE_USB_SERVICE, "BeginTransaction fail :%{public}d", ret);
60         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
61     }
62     return USB_RIGHT_OK;
63 }
64 
Commit()65 int32_t UsbRightDataBase::Commit()
66 {
67     if (store_ == nullptr) {
68         USB_HILOGE(MODULE_USB_SERVICE, "Commit store_ is nullptr");
69         return USB_RIGHT_RDB_NO_INIT;
70     }
71     int32_t ret = store_->Commit();
72     if (ret != OHOS::NativeRdb::E_OK) {
73         USB_HILOGE(MODULE_USB_SERVICE, "Commit fail :%{public}d", ret);
74         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
75     }
76     return USB_RIGHT_OK;
77 }
78 
RollBack()79 int32_t UsbRightDataBase::RollBack()
80 {
81     if (store_ == nullptr) {
82         USB_HILOGE(MODULE_USB_SERVICE, "RollBack store_ is nullptr");
83         return USB_RIGHT_RDB_NO_INIT;
84     }
85     int32_t ret = store_->RollBack();
86     if (ret != OHOS::NativeRdb::E_OK) {
87         USB_HILOGE(MODULE_USB_SERVICE, "RollBack fail :%{public}d", ret);
88         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
89     }
90     return USB_RIGHT_OK;
91 }
92 
Insert(const OHOS::NativeRdb::ValuesBucket & insertValues)93 int64_t UsbRightDataBase::Insert(const OHOS::NativeRdb::ValuesBucket &insertValues)
94 {
95     if (store_ == nullptr) {
96         USB_HILOGE(MODULE_USB_SERVICE, "Insert store_ is  nullptr");
97         return USB_RIGHT_RDB_NO_INIT;
98     }
99     int64_t outRowId = 0;
100     int32_t ret = store_->Insert(outRowId, USB_RIGHT_TABLE_NAME, insertValues);
101     USB_HILOGI(MODULE_USB_SERVICE, "Insert id=%{public}" PRIu64 "", outRowId);
102     if (ret != OHOS::NativeRdb::E_OK) {
103         USB_HILOGE(MODULE_USB_SERVICE, "Insert ret :%{public}d", ret);
104         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
105     }
106     return outRowId;
107 }
108 
Update(int32_t & changedRows,const OHOS::NativeRdb::ValuesBucket & values,const OHOS::NativeRdb::RdbPredicates & predicates)109 int32_t UsbRightDataBase::Update(
110     int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const OHOS::NativeRdb::RdbPredicates &predicates)
111 {
112     if (store_ == nullptr) {
113         USB_HILOGE(MODULE_USB_SERVICE, "Update(RdbPredicates) store_ is nullptr");
114         return USB_RIGHT_RDB_NO_INIT;
115     }
116     int32_t ret = store_->Update(changedRows, values, predicates);
117     if (ret != OHOS::NativeRdb::E_OK) {
118         USB_HILOGE(MODULE_USB_SERVICE, "Update(RdbPredicates) ret :%{public}d", ret);
119         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
120     }
121     return USB_RIGHT_OK;
122 }
123 
Update(int32_t & changedRows,const OHOS::NativeRdb::ValuesBucket & values,const std::string & whereClause,const std::vector<std::string> & whereArgs)124 int32_t UsbRightDataBase::Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values,
125     const std::string &whereClause, const std::vector<std::string> &whereArgs)
126 {
127     if (store_ == nullptr) {
128         USB_HILOGE(MODULE_USB_SERVICE, "Update(whereClause) store_ is nullptr");
129         return USB_RIGHT_RDB_NO_INIT;
130     }
131     int32_t ret = store_->Update(changedRows, USB_RIGHT_TABLE_NAME, values, whereClause, whereArgs);
132     if (ret != OHOS::NativeRdb::E_OK) {
133         USB_HILOGE(MODULE_USB_SERVICE, "Update(whereClause) ret :%{public}d", ret);
134         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
135     }
136     return USB_RIGHT_OK;
137 }
138 
Delete(const OHOS::NativeRdb::RdbPredicates & predicates)139 int32_t UsbRightDataBase::Delete(const OHOS::NativeRdb::RdbPredicates &predicates)
140 {
141     if (store_ == nullptr) {
142         USB_HILOGE(MODULE_USB_SERVICE, "Delete(RdbPredicates) store_ is  nullptr");
143         return USB_RIGHT_RDB_NO_INIT;
144     }
145     int32_t deleteRow = 0;
146     int32_t ret = store_->Delete(deleteRow, predicates);
147     if (ret != OHOS::NativeRdb::E_OK) {
148         USB_HILOGE(MODULE_USB_SERVICE, "Delete(RdbPredicates) ret :%{public}d", ret);
149         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
150     }
151     return USB_RIGHT_OK;
152 }
153 
Delete(int32_t & changedRows,const std::string & whereClause,const std::vector<std::string> & whereArgs)154 int32_t UsbRightDataBase::Delete(
155     int32_t &changedRows, const std::string &whereClause, const std::vector<std::string> &whereArgs)
156 {
157     if (store_ == nullptr) {
158         USB_HILOGE(MODULE_USB_SERVICE, "Delete store_ is nullptr");
159         return USB_RIGHT_RDB_NO_INIT;
160     }
161     int32_t ret = store_->Delete(changedRows, USB_RIGHT_TABLE_NAME, whereClause, whereArgs);
162     if (ret != OHOS::NativeRdb::E_OK) {
163         USB_HILOGE(MODULE_USB_SERVICE, "Delete(whereClause) ret :%{public}d", ret);
164         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
165     }
166     return USB_RIGHT_OK;
167 }
168 
ExecuteSql(const std::string & sql,const std::vector<OHOS::NativeRdb::ValueObject> & bindArgs)169 int32_t UsbRightDataBase::ExecuteSql(const std::string &sql, const std::vector<OHOS::NativeRdb::ValueObject> &bindArgs)
170 {
171     if (store_ == nullptr) {
172         USB_HILOGE(MODULE_USB_SERVICE, "ExecuteSql store_ is nullptr");
173         return USB_RIGHT_RDB_NO_INIT;
174     }
175     int32_t ret = store_->ExecuteSql(sql, bindArgs);
176     if (ret != OHOS::NativeRdb::E_OK) {
177         USB_HILOGE(MODULE_USB_SERVICE, "ExecuteSql ret :%{public}d", ret);
178         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
179     }
180     return USB_RIGHT_OK;
181 }
182 
QuerySql(const std::string & sql,const std::vector<std::string> & selectionArgs)183 std::shared_ptr<OHOS::NativeRdb::ResultSet> UsbRightDataBase::QuerySql(
184     const std::string &sql, const std::vector<std::string> &selectionArgs)
185 {
186     if (store_ == nullptr) {
187         USB_HILOGE(MODULE_USB_SERVICE, "QuerySql(sql) store_ is nullptr");
188         return nullptr;
189     }
190     return store_->QuerySql(sql);
191 }
192 
Query(const OHOS::NativeRdb::AbsRdbPredicates & predicates,const std::vector<std::string> & columns)193 std::shared_ptr<OHOS::NativeRdb::ResultSet> UsbRightDataBase::Query(
194     const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns)
195 {
196     if (store_ == nullptr) {
197         USB_HILOGE(MODULE_USB_SERVICE, "Query(AbsRdbPredicates) store_ is nullptr");
198         return nullptr;
199     }
200     return store_->Query(predicates, columns);
201 }
202 
OnCreate(OHOS::NativeRdb::RdbStore & store)203 int32_t UsbRightDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store)
204 {
205     std::string sql = CREATE_USB_RIGHT_TABLE;
206     int32_t ret = store.ExecuteSql(sql);
207     if (ret != OHOS::NativeRdb::E_OK) {
208         USB_HILOGE(MODULE_USB_SERVICE, "OnCreate failed: %{public}d", ret);
209         return USB_RIGHT_RDB_EXECUTE_FAILTURE;
210     }
211     USB_HILOGI(MODULE_USB_SERVICE, "DB OnCreate Done: %{public}d", ret);
212     return USB_RIGHT_OK;
213 }
214 
OnUpgrade(OHOS::NativeRdb::RdbStore & store,int32_t oldVersion,int32_t newVersion)215 int32_t UsbRightDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion)
216 {
217     USB_HILOGI(MODULE_USB_SERVICE, "DB OnUpgrade Enter");
218     (void)store;
219     (void)oldVersion;
220     (void)newVersion;
221     return USB_RIGHT_OK;
222 }
223 
OnDowngrade(OHOS::NativeRdb::RdbStore & store,int32_t oldVersion,int32_t newVersion)224 int32_t UsbRightDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion)
225 {
226     USB_HILOGI(MODULE_USB_SERVICE, "DB OnDowngrade Enter");
227     (void)store;
228     (void)oldVersion;
229     (void)newVersion;
230     return USB_RIGHT_OK;
231 }
232 
233 } // namespace USB
234 } // namespace OHOS
235