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 TIMER_DATABASE_H 17 #define TIMER_DATABASE_H 18 19 #include "rdb_helper.h" 20 #include "rdb_predicates.h" 21 #include "rdb_store.h" 22 23 namespace OHOS { 24 namespace MiscServices { 25 constexpr const char *DB_NAME = "/data/service/el1/public/database/time/time.db"; 26 constexpr int DATABASE_OPEN_VERSION = 1; 27 constexpr int DATABASE_OPEN_VERSION_2 = 2; 28 constexpr int DATABASE_OPEN_VERSION_3 = 3; 29 constexpr int CHECK_VERSION_FAILED = -1; 30 constexpr int API12_5_0_RELEASE = 50; 31 constexpr int INVALID_VERSION = -50; 32 constexpr int64_t MILLISECOND_TO_NANO = 1000000; 33 constexpr int CLOCK_POWEROFF_ALARM = 12; 34 constexpr const char *HOLD_ON_REBOOT = "hold_on_reboot"; 35 constexpr const char *DROP_ON_REBOOT = "drop_on_reboot"; 36 37 int GetInt(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line); 38 int64_t GetLong(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line); 39 std::string GetString(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line); 40 41 class TimeDatabase { 42 public: 43 TimeDatabase(); 44 static TimeDatabase &GetInstance(); 45 bool Insert(const std::string &table, const OHOS::NativeRdb::ValuesBucket &insertValues); 46 bool Update(const OHOS::NativeRdb::ValuesBucket values, const OHOS::NativeRdb::AbsRdbPredicates &predicates); 47 std::shared_ptr<OHOS::NativeRdb::ResultSet> Query( 48 const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns); 49 bool Delete(const OHOS::NativeRdb::AbsRdbPredicates &predicates); 50 void ClearDropOnReboot(); 51 52 private: 53 bool RecoverDataBase(); 54 std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 55 }; 56 57 class TimeDBOpenCallback : public OHOS::NativeRdb::RdbOpenCallback { 58 public: 59 int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; 60 int OnOpen(OHOS::NativeRdb::RdbStore &rdbStore) override; 61 int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override; 62 int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; 63 }; 64 } // namespace MiscServices 65 } // namespace OHOS 66 #endif // TIMER_DATABASE_H