• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H
18 
19 #include <vector>
20 #include <mutex>
21 
22 #include "notification_bundle_option.h"
23 #include "reminder_request.h"
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_open_callback.h"
27 #include "rdb_store_config.h"
28 
29 namespace OHOS {
30 namespace Notification {
31 class ReminderStore {
32 public:
ReminderStore()33     ReminderStore() {};
~ReminderStore()34     virtual ~ReminderStore() {};
35 
36 public:
37     int32_t Init();
38     int32_t Delete(const int32_t reminderId);
39     int32_t Delete(const std::string& pkg, const int32_t userId, const int32_t uid);
40     int32_t DeleteUser(const int32_t userId);
41     int32_t UpdateOrInsert(const sptr<ReminderRequest>& reminder);
42     int32_t GetMaxId();
43     int32_t QueryActiveReminderCount();
44     bool IsReminderExist(const int32_t reminderId, const int32_t uid);
45     std::vector<sptr<ReminderRequest>> GetHalfHourReminders();
46     std::vector<sptr<ReminderRequest>> GetAllValidReminders();
47 
48 public:
49     static void GetUInt8Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
50         const std::string& name, uint8_t& value);
51     static void GetUInt16Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
52         const std::string& name, uint16_t& value);
53     static void GetInt32Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
54         const std::string& name, int32_t& value);
55     static void GetInt64Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
56         const std::string& name, int64_t& value);
57     static void GetUInt64Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
58         const std::string& name, uint64_t& value);
59     static void GetStringVal(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
60         const std::string& name, std::string& value);
61 
62     static const int32_t STATE_OK;
63     static const int32_t STATE_FAIL;
64 
65     static const std::string REMINDER_DB_DIR;
66     static const std::string REMINDER_DB_NAME;
67     static const std::string REMINDER_DB_TABLE;
68 
69 private:
70     /**
71      * @brief Inits the data in database when system boot on or proxy process reboot on.
72      *
73      * 1. Deletes all the reminders which IS_EXPIRED is true.
74      * 2. Sets all the value of STATE to ReminderRequest::REMINDER_STATUS_INACTIVE
75      *
76      * @return int32_t result code.
77      */
78     int32_t InitData();
79     int32_t DeleteBase(const std::string& deleteCondition);
80     int32_t Delete(const std::string& baseCondition, const std::string& assoConditon);
81     int32_t Insert(const sptr<ReminderRequest>& reminder);
82     int32_t Update(const sptr<ReminderRequest>& reminder);
83 
84     bool IsReminderExist(const sptr<ReminderRequest>& reminder);
85     std::vector<sptr<ReminderRequest>> GetReminders(const std::string& queryCondition);
86     sptr<ReminderRequest> BuildReminder(const std::shared_ptr<NativeRdb::ResultSet>& resultBase);
87 
88     std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& tableName, const std::string& colums,
89         const int32_t reminderId);
90     std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& queryCondition) const;
91 
92 private:
93     std::shared_ptr<NativeRdb::RdbStore> rdbStore_ = nullptr;
94 
95     std::mutex initMutex_;
96 
97 private:
98 class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback {
99 public:
100     int32_t OnCreate(NativeRdb::RdbStore& store) override;
101     int32_t OnUpgrade(NativeRdb::RdbStore& store, int32_t oldVersion, int32_t newVersion) override;
102     int32_t OnDowngrade(NativeRdb::RdbStore& store, int32_t currentVersion, int32_t targetVersion) override;
103 
104 private:
105     int32_t CreateTable(NativeRdb::RdbStore& store);
106     int32_t CopyData(NativeRdb::RdbStore& store);
107     std::vector<sptr<ReminderRequest>> GetOldReminders(NativeRdb::RdbStore& store);
108     void InsertNewReminders(NativeRdb::RdbStore& store, const std::vector<sptr<ReminderRequest>>& reminders);
109     void AddRdbColum(NativeRdb::RdbStore& store, const std::string& tableName,
110         const std::string& columnName, const std::string& columnType, const std::string& defValue);
111 };
112 };
113 }  // namespace Notification
114 }  // namespace OHOS
115 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H