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 = 2; 38 39 constexpr const char *CREATE_USB_RIGHT_TABLE = "CREATE TABLE IF NOT EXISTS [usbRightInfoTable](" 40 "[id] INTEGER PRIMARY KEY AUTOINCREMENT, " 41 "[uid] INTEGER, " 42 "[installTime] INTEGER, " 43 "[updateTime] INTEGER, " 44 "[requestTime] INTEGER, " 45 "[validPeriod] INTEGER, " 46 "[deviceName] TEXT," 47 "[bundleName] TEXT," 48 "[tokenId] TEXT);"; 49 constexpr const char *SQL_ADD_TOKEN_ID = "ALTER TABLE usbRightInfoTable ADD COLUMN tokenId TEXT DEFAULT ''"; 50 51 class UsbRightDataBase { 52 public: 53 static std::shared_ptr<UsbRightDataBase> GetInstance(); 54 int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues); 55 int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, 56 const OHOS::NativeRdb::RdbPredicates &predicates); 57 int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause, 58 const std::vector<std::string> &whereArgs); 59 int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); 60 int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector<std::string> &whereArgs); 61 int32_t ExecuteSql(const std::string &sql, 62 const std::vector<OHOS::NativeRdb::ValueObject> &bindArgs = std::vector<OHOS::NativeRdb::ValueObject>()); 63 std::shared_ptr<OHOS::NativeRdb::ResultSet> QuerySql( 64 const std::string &sql, const std::vector<std::string> &selectionArgs); 65 std::shared_ptr<OHOS::NativeRdb::ResultSet> Query( 66 const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns); 67 int32_t BeginTransaction(); 68 int32_t Commit(); 69 int32_t RollBack(); 70 71 private: 72 UsbRightDataBase(); 73 DISALLOW_COPY_AND_MOVE(UsbRightDataBase); 74 75 static std::shared_ptr<UsbRightDataBase> instance_; 76 std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 77 }; 78 79 class UsbRightDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { 80 public: 81 int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; 82 int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 83 int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; 84 }; 85 86 } // namespace USB 87 } // namespace OHOS 88 89 #endif // USB_RIGHT_DATABASE_H 90