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 #ifndef USB_RIGHT_DATABASE_H 17 #define USB_RIGHT_DATABASE_H 18 19 #include <pthread.h> 20 21 #include "data_ability_predicates.h" 22 #include "rdb_errno.h" 23 #include "rdb_helper.h" 24 #include "rdb_open_callback.h" 25 #include "rdb_predicates.h" 26 #include "rdb_store.h" 27 #include "result_set.h" 28 #include "value_object.h" 29 30 namespace OHOS { 31 namespace USB { 32 33 static std::string USB_RIGHT_DB_PATH = "/data/service/el1/public/usb_service/"; 34 35 constexpr const char *USB_RIGHT_DB_NAME = "usbRight.db"; 36 constexpr const char *USB_RIGHT_TABLE_NAME = "usbRightInfoTable"; 37 constexpr int32_t DATABASE_OPEN_VERSION = 1; 38 constexpr int32_t DATABASE_NEW_VERSION = 2; 39 40 constexpr const char *CREATE_USB_RIGHT_TABLE = "CREATE TABLE IF NOT EXISTS [usbRightInfoTable](" 41 "[id] INTEGER PRIMARY KEY AUTOINCREMENT, " 42 "[uid] INTEGER, " 43 "[installTime] INTEGER, " 44 "[updateTime] INTEGER, " 45 "[requestTime] INTEGER, " 46 "[validPeriod] INTEGER, " 47 "[deviceName] TEXT," 48 "[bundleName] TEXT );"; 49 50 class UsbRightDataBase { 51 public: 52 static std::shared_ptr<UsbRightDataBase> GetInstance(); 53 int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues); 54 int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, 55 const OHOS::NativeRdb::RdbPredicates &predicates); 56 int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause, 57 const std::vector<std::string> &whereArgs); 58 int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); 59 int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector<std::string> &whereArgs); 60 int32_t ExecuteSql(const std::string &sql, 61 const std::vector<OHOS::NativeRdb::ValueObject> &bindArgs = std::vector<OHOS::NativeRdb::ValueObject>()); 62 std::shared_ptr<OHOS::NativeRdb::ResultSet> QuerySql( 63 const std::string &sql, const std::vector<std::string> &selectionArgs); 64 std::shared_ptr<OHOS::NativeRdb::ResultSet> Query( 65 const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns); 66 int32_t BeginTransaction(); 67 int32_t Commit(); 68 int32_t RollBack(); 69 70 private: 71 UsbRightDataBase(); 72 DISALLOW_COPY_AND_MOVE(UsbRightDataBase); 73 74 static std::shared_ptr<UsbRightDataBase> instance_; 75 std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 76 }; 77 78 class UsbRightDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { 79 public: 80 int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; 81 int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 82 int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; 83 }; 84 85 } // namespace USB 86 } // namespace OHOS 87 88 #endif // USB_RIGHT_DATABASE_H 89