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 CHECK_VERSION_FAILED = -1;
29 constexpr int API12_5_0_RELEASE = 50;
30 constexpr int INVALID_VERSION = -50;
31 constexpr int64_t MILLISECOND_TO_NANO = 1000000;
32 constexpr int CLOCK_POWEROFF_ALARM = 12;
33 constexpr const char *HOLD_ON_REBOOT = "hold_on_reboot";
34 constexpr const char *DROP_ON_REBOOT = "drop_on_reboot";
35 
36 int GetInt(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line);
37 int64_t GetLong(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line);
38 std::string GetString(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, int line);
39 
40 class TimeDatabase {
41 public:
42     TimeDatabase();
43     static TimeDatabase &GetInstance();
44     bool Insert(const std::string &table, const OHOS::NativeRdb::ValuesBucket &insertValues);
45     bool Update(const OHOS::NativeRdb::ValuesBucket values, const OHOS::NativeRdb::AbsRdbPredicates &predicates);
46     std::shared_ptr<OHOS::NativeRdb::ResultSet> Query(
47         const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns);
48     bool Delete(const OHOS::NativeRdb::AbsRdbPredicates &predicates);
49     void ClearDropOnReboot();
50 
51 private:
52     bool RecoverDataBase();
53     std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
54 };
55 
56 class TimeDBOpenCallback : public OHOS::NativeRdb::RdbOpenCallback {
57 public:
58     int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
59     int OnOpen(OHOS::NativeRdb::RdbStore &rdbStore) override;
60     int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
61     int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
62 };
63 } // namespace MiscServices
64 } // namespace OHOS
65 #endif // TIMER_DATABASE_H