From 8f4ea82aee7967616661325278d2fa8126e03241 Mon Sep 17 00:00:00 2001 From: let_it_rot Date: Thu, 4 Sep 2025 14:52:06 +0800 Subject: [PATCH] refactor reminder db Signed-off-by: let_it_rot --- services/reminder/include/reminder_store.h | 99 +-- .../include/reminder_store_strategy.h | 208 +++--- services/reminder/include/reminder_table.h | 323 ++++---- .../reminder/include/reminder_table_old.h | 239 ------ .../reminder/src/reminder_data_manager.cpp | 7 +- services/reminder/src/reminder_store.cpp | 559 +++++--------- .../reminder/src/reminder_store_strategy.cpp | 704 ++++++------------ services/reminder/src/reminder_table.cpp | 155 ++-- services/reminder/src/reminder_table_old.cpp | 157 ---- 9 files changed, 805 insertions(+), 1646 deletions(-) delete mode 100644 services/reminder/include/reminder_table_old.h delete mode 100644 services/reminder/src/reminder_table_old.cpp diff --git a/services/reminder/include/reminder_store.h b/services/reminder/include/reminder_store.h index ed9ba8b84..84c745730 100644 --- a/services/reminder/include/reminder_store.h +++ b/services/reminder/include/reminder_store.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,60 +13,71 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H #include -#include +#include + +#include "reminder_store_strategy.h" -#include "notification_bundle_option.h" -#include "reminder_request.h" #include "rdb_errno.h" #include "rdb_helper.h" -#include "rdb_open_callback.h" #include "rdb_store_config.h" +#include "rdb_open_callback.h" -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class ReminderStore { public: - ReminderStore() {}; - virtual ~ReminderStore() {}; + ReminderStore() = default; + ~ReminderStore() = default; public: int32_t Init(); - int32_t Delete(const int32_t reminderId); - int32_t Delete(const std::string& pkg, const int32_t userId, const int32_t uid); - int32_t DeleteUser(const int32_t userId); + + /** + * @brief Delete reminder by reminder id, reminder type. + */ + int32_t Delete(const int32_t reminderId, const int32_t type); + /** + * @brief Delete reminder by bundle name, user id, uid. + */ + int32_t Delete(const std::string& bundleName, const int32_t userId, const int32_t uid); + /** + * @brief Delete reminder by user id. + */ + int32_t Delete(const int32_t userId); + /** + * @brief Update if the reminder exists, otherwise insert. + */ int32_t UpdateOrInsert(const sptr& reminder); - int32_t GetMaxId(); - int32_t QueryActiveReminderCount(); + /** + * @brief Check reminder_id is in db. + */ bool IsReminderExist(const int32_t reminderId, const int32_t uid); - std::vector> GetHalfHourReminders(); + /** + * @brief Query reminder count. + */ + int32_t QueryActiveReminderCount(); + /** + * @brief Select all reminders. + */ std::vector> GetAllValidReminders(); + /** + * @brief Select reminders for the next X hours. + */ + std::vector> GetXHourReminders(); + + int32_t GetMaxId(); public: - static void GetUInt8Val(const std::shared_ptr& resultSet, - const std::string& name, uint8_t& value); - static void GetUInt16Val(const std::shared_ptr& resultSet, - const std::string& name, uint16_t& value); + static constexpr int32_t STATE_OK = 0; + static constexpr int32_t STATE_FAIL = -1; + +private: static void GetInt32Val(const std::shared_ptr& resultSet, const std::string& name, int32_t& value); - static void GetInt64Val(const std::shared_ptr& resultSet, - const std::string& name, int64_t& value); - static void GetUInt64Val(const std::shared_ptr& resultSet, - const std::string& name, uint64_t& value); - static void GetStringVal(const std::shared_ptr& resultSet, - const std::string& name, std::string& value); - - static const int32_t STATE_OK; - static const int32_t STATE_FAIL; - static const std::string REMINDER_DB_DIR; - static const std::string REMINDER_DB_NAME; - static const std::string REMINDER_DB_TABLE; - -private: /** * @brief Inits the data in database when system boot on or proxy process reboot on. * @@ -76,23 +87,21 @@ private: * @return int32_t result code. */ int32_t InitData(); - int32_t DeleteBase(const std::string& deleteCondition); int32_t Delete(const std::string& baseCondition, const std::string& assoConditon); int32_t Insert(const sptr& reminder); int32_t Update(const sptr& reminder); bool IsReminderExist(const sptr& reminder); std::vector> GetReminders(const std::string& queryCondition); - sptr BuildReminder(const std::shared_ptr& resultBase); - + sptr BuildReminder(const std::shared_ptr& result); + std::shared_ptr Query(const std::string& tableName, const std::string& colums, const int32_t reminderId); std::shared_ptr Query(const std::string& queryCondition) const; private: - std::shared_ptr rdbStore_ = nullptr; - - std::mutex initMutex_; + std::shared_ptr rdbStore_; + std::unordered_map> strategy_; private: class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback { @@ -103,13 +112,9 @@ public: private: int32_t CreateTable(NativeRdb::RdbStore& store); - int32_t CopyData(NativeRdb::RdbStore& store); - std::vector> GetOldReminders(NativeRdb::RdbStore& store); - void InsertNewReminders(NativeRdb::RdbStore& store, const std::vector>& reminders); void AddRdbColum(NativeRdb::RdbStore& store, const std::string& tableName, const std::string& columnName, const std::string& columnType, const std::string& defValue); }; }; -} // namespace Notification -} // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_H \ No newline at end of file +} // namespace OHOS::Notification +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H \ No newline at end of file diff --git a/services/reminder/include/reminder_store_strategy.h b/services/reminder/include/reminder_store_strategy.h index daadfdd92..ef6809550 100644 --- a/services/reminder/include/reminder_store_strategy.h +++ b/services/reminder/include/reminder_store_strategy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,182 +12,156 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_STRATEGY_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_STRATEGY_H - -#include -#include +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H #include "reminder_request.h" -#include "rdb_store.h" -namespace OHOS { -namespace Notification { -class ReminderStrategy { -public: - /** - * @brief Gets the value from rdb result. - * - * @param resultSet the rdb result. - * @param name the column name in rdb. - * @param value the column value in rdb. - */ - template - static void GetRdbValue(const std::shared_ptr& resultSet, - const std::string& name, T& value); +#include "result_set.h" +#include "values_bucket.h" +namespace OHOS::Notification { +class ReminderBaseStrategy { public: + ReminderBaseStrategy() = default; + virtual ~ReminderBaseStrategy() = default; + /** - * @brief Persist the reminder to the database. + * @brief Store the reminder to the database. */ - static void AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket &values, const bool oldVersion = false); + virtual void StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) = 0; /** - * @brief Restore the reminder from the database(old version rdb). + * @brief Restore the reminder from the database. */ - static void RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + virtual sptr RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) = 0; /** - * @brief Restore the reminder from the database. + * @brief Get table name. */ - static void RecoverFromDb(sptr& reminder, const std::shared_ptr& resultSet); + virtual std::string GetTableName() = 0; -private: /** - * @brief Recovery time related fields from the database(old version rdb). + * @brief Get table columns. */ - static void RecoverTimeFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + virtual std::string GetTableColumns() = 0; + +protected: /** - * @brief Recovery id related fields from the database(old version rdb). + * @brief Store the reminder to the database(base). */ - static void RecoverIdFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + void StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& values); + /** - * @brief Recovery context related from the database(old version rdb). + * @brief Restore the reminder from the database(base). */ - static void RecoverContextFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + void RestoreFromRdb(sptr& reminder, const std::shared_ptr& results); +private: /** - * @brief Recovery time related fields from the database. + * @brief Restore the time related fields from the database. */ - static void RecoverTimeFromDb(sptr& reminder, - const std::shared_ptr& resultSet); + void RestoreTime(sptr& reminder, const std::shared_ptr& results); /** - * @brief Recovery id related fields from the database. + * @brief Restore the id related fields from the database. */ - static void RecoverIdFromDb(sptr& reminder, - const std::shared_ptr& resultSet); + void RestoreId(sptr& reminder, const std::shared_ptr& results); /** - * @brief Recovery context related from the database. + * @brief Restore the context related from the database. */ - static void RecoverContextFromDb(sptr& reminder, - const std::shared_ptr& resultSet); + void RestoreContext(sptr& reminder, const std::shared_ptr& results); }; -class ReminderTimerStrategy { +class ReminderTimerStrategy : public ReminderBaseStrategy { public: + ReminderTimerStrategy() = default; + ~ReminderTimerStrategy() = default; + /** - * @brief Persist the reminder to the database. + * @brief Store the reminder to the database. */ - static void AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket& values); + void StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) override; /** - * @brief Restore the reminder from the database(old version rdb). + * @brief Restore the reminder from the database. */ - static void RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + sptr RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) override; /** - * @brief Restore the reminder from the database. + * @brief Get table name. + */ + std::string GetTableName() override; + + /** + * @brief Get table columns. */ - static void RecoverFromDb(sptr& reminder, const std::shared_ptr& baseResult, - const std::shared_ptr& resultSet); + std::string GetTableColumns() override; }; -class ReminderAlarmStrategy { +class ReminderAlarmStrategy : public ReminderBaseStrategy { public: + ReminderAlarmStrategy() = default; + ~ReminderAlarmStrategy() = default; + /** - * @brief Persist the reminder to the database. + * @brief Store the reminder to the database. */ - static void AppendValuesBucket(const sptr &reminder, NativeRdb::ValuesBucket &values); + void StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) override; /** - * @brief Restore the reminder from the database(old version rdb). + * @brief Restore the reminder from the database. */ - static void RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + sptr RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) override; /** - * @brief Restore the reminder from the database. + * @brief Get table name. */ - static void RecoverFromDb(sptr& reminder, const std::shared_ptr& baseResult, - const std::shared_ptr& resultSet); + std::string GetTableName() override; + + /** + * @brief Get table columns. + */ + std::string GetTableColumns() override; }; -class ReminderCalendarStrategy { +class ReminderCalendarStrategy : public ReminderBaseStrategy { public: + ReminderCalendarStrategy() = default; + ~ReminderCalendarStrategy() = default; + /** - * @brief Persist the reminder to the database. + * @brief Store the reminder to the database. */ - static void AppendValuesBucket(const sptr &reminder, NativeRdb::ValuesBucket &values); + void StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) override; /** - * @brief Restore the reminder from the database(old version rdb). + * @brief Restore the reminder from the database. */ - static void RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet); + sptr RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) override; /** - * @brief Restore the reminder from the database. + * @brief Get table name. + */ + std::string GetTableName() override; + + /** + * @brief Get table columns. */ - static void RecoverFromDb(sptr& reminder, const std::shared_ptr& baseResult, - const std::shared_ptr& resultSet); + std::string GetTableColumns() override; private: - static void RecoverTime(sptr& reminder, const std::shared_ptr& resultSet); + /** + * @brief Restore the time related fields from the database. + */ + void RestoreTime(sptr& reminder, const std::shared_ptr& results); }; - -template -void ReminderStrategy::GetRdbValue(const std::shared_ptr& resultSet, - const std::string& name, T& value) -{ - value = T(); - int32_t columnIndex = -1; - resultSet->GetColumnIndex(name, columnIndex); - if (columnIndex == -1) { - ANSR_LOGE("the column %{public}s does not exsit.", name.c_str()); - return; - } - - if constexpr (std::is_same_v) { - resultSet->GetString(columnIndex, value); - } else if constexpr (std::is_same_v) { - resultSet->GetLong(columnIndex, value); - } else if constexpr (std::is_same_v) { - int64_t t = 0; - resultSet->GetLong(columnIndex, t); - value = static_cast(t); - } else if constexpr (std::is_same_v) { - resultSet->GetInt(columnIndex, value); - } else if constexpr (std::is_same_v) { - int32_t t = 0; - resultSet->GetInt(columnIndex, t); - value = static_cast(t); - } else if constexpr (std::is_same_v) { - int32_t t = 0; - resultSet->GetInt(columnIndex, t); - value = static_cast(t); - } else if constexpr (std::is_same_v) { - int32_t t = 0; - resultSet->GetInt(columnIndex, t); - value = static_cast(t); - } -} -} // namespace Notification -} // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_STORE_STRATEGY_H \ No newline at end of file +} // namespace OHOS::Notification +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H \ No newline at end of file diff --git a/services/reminder/include/reminder_table.h b/services/reminder/include/reminder_table.h index eb10d63c1..1978acd1e 100644 --- a/services/reminder/include/reminder_table.h +++ b/services/reminder/include/reminder_table.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,175 +19,121 @@ #include #include -namespace OHOS { -namespace Notification { -class ReminderBaseTable { +namespace OHOS::Notification { +class ReminderOldTable { public: - /* - * reminder base table name - */ - static const std::string TABLE_NAME; - - /* - * reminder id - */ - static const std::string REMINDER_ID; - - /* - * package name - */ - static const std::string PACKAGE_NAME; - - /* - * user id - */ - static const std::string USER_ID; - - /* - * uid - */ - static const std::string UID; - - /* - * systerm app flag - */ - static const std::string SYSTEM_APP; - - /* - * reminder type: Timer/Calendar/Alarm - */ - static const std::string REMINDER_TYPE; - - /* - * reminder time - */ - static const std::string REMINDER_TIME; - - /* - * trigger time - */ - static const std::string TRIGGER_TIME; - - /* - * time interval - */ - static const std::string TIME_INTERVAL; - - /* - * snooze times - */ - static const std::string SNOOZE_TIMES; - - /* - * dynamic snooze times - */ - static const std::string DYNAMIC_SNOOZE_TIMES; - - /* - * ring duration - */ - static const std::string RING_DURATION; - - /* - * expired flag - */ - static const std::string IS_EXPIRED; - - /* - * reminder state - */ - static const std::string STATE; - - /* - * action button information - */ - static const std::string ACTION_BUTTON_INFO; - - /* - * custom button uri - */ - static const std::string CUSTOM_BUTTON_URI; - - /* - * slot type - */ - static const std::string SLOT_ID; - - /* - * snoozeslot type - */ - static const std::string SNOOZE_SLOT_ID; - - /* - * notification id - */ - static const std::string NOTIFICATION_ID; - - /* - * notification title - */ - static const std::string TITLE; - static const std::string TITLE_RESOURCE_ID; - - /* - * notification content - */ - static const std::string CONTENT; - static const std::string CONTENT_RESOURCE_ID; - - /* - * notification snooze content - */ - static const std::string SNOOZE_CONTENT; - static const std::string SNOOZE_CONTENT_RESOURCE_ID; - - /* - * notification expired content - */ - static const std::string EXPIRED_CONTENT; - static const std::string EXPIRED_CONTENT_RESOURCE_ID; - - /* - * want agent information - */ - static const std::string WANT_AGENT; - - /* - * max screen want agent information - */ - static const std::string MAX_SCREEN_WANT_AGENT; - - /* - * tap dismissed flag - */ - static const std::string TAP_DISMISSED; - - /* - * auto deleted time - */ - static const std::string AUTO_DELETED_TIME; - - /* - * group id - */ - static const std::string GROUP_ID; + // Reminder Table Basic Columns. + static constexpr const char* TABLE_NAME = "reminder"; + static constexpr const char* REMINDER_ID = "reminder_id"; + static constexpr const char* PKG_NAME = "package_name"; + static constexpr const char* USER_ID = "user_id"; + static constexpr const char* UID = "uid"; + static constexpr const char* SYS_APP = "system_app"; + static constexpr const char* APP_LABEL = "app_label"; + static constexpr const char* REMINDER_TYPE = "reminder_type"; + static constexpr const char* REMINDER_TIME = "reminder_time"; + static constexpr const char* TRIGGER_TIME = "trigger_time"; + static constexpr const char* RTC_TRIGGER_TIME = "rtc_trigger_time"; + static constexpr const char* TIME_INTERVAL = "time_interval"; + static constexpr const char* SNOOZE_TIMES = "snooze_times"; + static constexpr const char* DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; + static constexpr const char* RING_DURATION = "ring_duration"; + static constexpr const char* IS_EXPIRED = "is_expired"; + static constexpr const char* IS_ACTIVE = "is_active"; + static constexpr const char* STATE = "state"; + static constexpr const char* ZONE_ID = "zone_id"; + static constexpr const char* HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; + static constexpr const char* ACTION_BUTTON_INFO = "button_info"; + static constexpr const char* CUSTOM_BUTTON_URI = "custom_button_uri"; + static constexpr const char* SLOT_ID = "slot_id"; + static constexpr const char* SNOOZE_SLOT_ID = "snooze_slot_id"; + static constexpr const char* NOTIFICATION_ID = "notification_id"; + static constexpr const char* TITLE = "title"; + static constexpr const char* CONTENT = "content"; + static constexpr const char* SNOOZE_CONTENT = "snooze_content"; + static constexpr const char* EXPIRED_CONTENT = "expired_content"; + static constexpr const char* AGENT = "agent"; + static constexpr const char* MAX_SCREEN_AGENT = "maxScreen_agent"; + static constexpr const char* TAP_DISMISSED = "tapDismissed"; + static constexpr const char* AUTO_DELETED_TIME = "autoDeletedTime"; + static constexpr const char* REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; + static constexpr const char* GROUP_ID = "groupId"; + static constexpr const char* CUSTOM_RING_URI = "custom_ring_uri"; + static constexpr const char* CREATOR_BUNDLE_NAME = "creator_bundle_name"; + + // Reminder Table Calendar Columns. + static constexpr const char* REPEAT_DAYS = "repeat_days"; + static constexpr const char* REPEAT_MONTHS = "repeat_months"; + static constexpr const char* FIRST_DESIGNATE_YEAR = "first_designate_year"; + static constexpr const char* FIRST_DESIGNATE_MONTH = "first_designate_month"; + static constexpr const char* FIRST_DESIGNATE_DAY = "first_designate_day"; + static constexpr const char* CALENDAR_YEAR = "calendar_year"; + static constexpr const char* CALENDAR_MONTH = "calendar_month"; + static constexpr const char* CALENDAR_DAY = "calendar_day"; + static constexpr const char* CALENDAR_HOUR = "calendar_hour"; + static constexpr const char* CALENDAR_MINUTE = "calendar_minute"; + + // Reminder Table Alarm Columns. + static constexpr const char* ALARM_HOUR = "alarm_hour"; + static constexpr const char* ALARM_MINUTE = "alarm_minute"; - /* - * custom ring uri - */ - static const std::string CUSTOM_RING_URI; + static void InitDbColumns(); + static std::string ADD_COLUMNS; + static std::string SELECT_COLUMNS; - /* - * creator bundle name - */ - static const std::string CREATOR_BUNDLE_NAME; +private: + static void InitBasicColumns(); + static void InitCalendarColumns(); + static void InitAlarmColumns(); + static void AddColumn(const std::string& name, const std::string& type, bool isEnd = false); +}; +class ReminderBaseTable { +public: /* - * creator uid + * reminder base table name */ - static const std::string CREATOR_UID; - - static const std::string RING_CHANNEL; + static constexpr const char* TABLE_NAME = "reminder_base"; + + /* + * reminder base table field + */ + static constexpr const char* REMINDER_ID = "reminder_id"; + static constexpr const char* PACKAGE_NAME = "package_name"; + static constexpr const char* USER_ID = "user_id"; + static constexpr const char* UID = "uid"; + static constexpr const char* SYSTEM_APP = "system_app"; + static constexpr const char* REMINDER_TYPE = "reminder_type"; + static constexpr const char* REMINDER_TIME = "reminder_time"; + static constexpr const char* TRIGGER_TIME = "trigger_time"; + static constexpr const char* TIME_INTERVAL = "time_interval"; + static constexpr const char* SNOOZE_TIMES = "snooze_times"; + static constexpr const char* DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; + static constexpr const char* RING_DURATION = "ring_duration"; + static constexpr const char* IS_EXPIRED = "is_expired"; + static constexpr const char* STATE = "state"; + static constexpr const char* ACTION_BUTTON_INFO = "button_info"; + static constexpr const char* CUSTOM_BUTTON_URI = "custom_button_uri"; + static constexpr const char* SLOT_ID = "slot_id"; + static constexpr const char* SNOOZE_SLOT_ID = "snooze_slot_id"; + static constexpr const char* NOTIFICATION_ID = "notification_id"; + static constexpr const char* TITLE = "title"; + static constexpr const char* TITLE_RESOURCE_ID = "title_resource_id"; + static constexpr const char* CONTENT = "content"; + static constexpr const char* CONTENT_RESOURCE_ID = "content_resource_id"; + static constexpr const char* SNOOZE_CONTENT = "snooze_content"; + static constexpr const char* SNOOZE_CONTENT_RESOURCE_ID = "snooze_content_resource_id"; + static constexpr const char* EXPIRED_CONTENT = "expired_content"; + static constexpr const char* EXPIRED_CONTENT_RESOURCE_ID = "expired_content_resource_id"; + static constexpr const char* WANT_AGENT = "want_agent"; + static constexpr const char* MAX_SCREEN_WANT_AGENT = "max_screen_want_agent"; + static constexpr const char* TAP_DISMISSED = "tap_dismissed"; + static constexpr const char* AUTO_DELETED_TIME = "auto_deleted_time"; + static constexpr const char* GROUP_ID = "group_id"; + static constexpr const char* CUSTOM_RING_URI = "custom_ring_uri"; + static constexpr const char* CREATOR_BUNDLE_NAME = "creator_bundle_name"; + static constexpr const char* CREATOR_UID = "creator_uid"; + static constexpr const char* RING_CHANNEL = "ring_channel"; public: static void InitDbColumns(); @@ -202,15 +148,15 @@ public: /* * reminder alarm table name */ - static const std::string TABLE_NAME; + static constexpr const char* TABLE_NAME = "reminder_alarm"; /* * reminder timer table field */ - static const std::string REMINDER_ID; - static const std::string ALARM_HOUR; - static const std::string ALARM_MINUTE; - static const std::string REPEAT_DAYS_OF_WEEK; + static constexpr const char* REMINDER_ID = "reminder_id"; + static constexpr const char* ALARM_HOUR = "alarm_hour"; + static constexpr const char* ALARM_MINUTE = "alarm_minute"; + static constexpr const char* REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; public: static void InitDbColumns(); @@ -225,23 +171,23 @@ public: /* * reminder calendar table name */ - static const std::string TABLE_NAME; + static constexpr const char* TABLE_NAME = "reminder_calendar"; /* * reminder calendar table field */ - static const std::string REMINDER_ID; - static const std::string FIRST_DESIGNATE_YEAR; - static const std::string FIRST_DESIGNATE_MONTH; - static const std::string FIRST_DESIGNATE_DAY; - static const std::string CALENDAR_DATE_TIME; - static const std::string CALENDAR_END_DATE_TIME; - static const std::string REPEAT_DAYS; - static const std::string REPEAT_MONTHS; - static const std::string REPEAT_DAYS_OF_WEEK; - static const std::string RRULE_WANT_AGENT; - static const std::string EXCLUDE_DATES; - static const std::string CALENDAR_LAST_DATE_TIME; + static constexpr const char* REMINDER_ID = "reminder_id"; + static constexpr const char* FIRST_DESIGNATE_YEAR = "first_designate_year"; + static constexpr const char* FIRST_DESIGNATE_MONTH = "first_designate_month"; + static constexpr const char* FIRST_DESIGNATE_DAY = "first_designate_day"; + static constexpr const char* CALENDAR_DATE_TIME = "calendar_date_time"; + static constexpr const char* CALENDAR_END_DATE_TIME = "calendar_end_date_time"; + static constexpr const char* REPEAT_DAYS = "repeat_days"; + static constexpr const char* REPEAT_MONTHS = "repeat_months"; + static constexpr const char* REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; + static constexpr const char* RRULE_WANT_AGENT = "rrule_want_agent"; + static constexpr const char* EXCLUDE_DATES = "exclude_dates"; + static constexpr const char* CALENDAR_LAST_DATE_TIME = "calendar_last_date_time"; public: static void InitDbColumns(); @@ -256,15 +202,15 @@ public: /* * reminder timer table name */ - static const std::string TABLE_NAME; + static constexpr const char* TABLE_NAME = "reminder_timer"; /* * reminder timer table field */ - static const std::string REMINDER_ID; - static const std::string TRIGGER_SECOND; - static const std::string START_DATE_TIME; - static const std::string END_DATE_TIME; + static constexpr const char* REMINDER_ID = "reminder_id"; + static constexpr const char* TRIGGER_SECOND = "trigger_second"; + static constexpr const char* START_DATE_TIME = "start_date_time"; + static constexpr const char* END_DATE_TIME = "end_date_time"; public: static void InitDbColumns(); @@ -273,6 +219,5 @@ public: static std::string ADD_COLUMNS; static std::string SELECT_COLUMNS; }; -} // namespace Notification -} // namespace OHOS +} // namespace OHOS::Notification #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_TABLE_H diff --git a/services/reminder/include/reminder_table_old.h b/services/reminder/include/reminder_table_old.h deleted file mode 100644 index 77c228fd1..000000000 --- a/services/reminder/include/reminder_table_old.h +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_TABLE_OLD_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_TABLE_OLD_H - -#include -#include - -namespace OHOS { -namespace Notification { -class ReminderTable { -public: - static void InitDbColumns(); - - static const std::string TABLE_NAME; - - // Reminder Table Basic Columns. - - /* - * reminder id - */ - static const std::string REMINDER_ID; - - /* - * package name - */ - static const std::string PKG_NAME; - - /* - * user id - */ - static const std::string USER_ID; - - /* - * uid - */ - static const std::string UID; - - /* - * systerm app flag - */ - static const std::string SYS_APP; - - /* - * app label - */ - static const std::string APP_LABEL; - - /* - * reminder type: Timer/Calendar/Alarm - */ - static const std::string REMINDER_TYPE; - - /* - * reminder time - */ - static const std::string REMINDER_TIME; - - /* - * trigger time - */ - static const std::string TRIGGER_TIME; - - /* - * RTC trigger time - */ - static const std::string RTC_TRIGGER_TIME; - - /* - * time interval - */ - static const std::string TIME_INTERVAL; - - /* - * snooze times - */ - static const std::string SNOOZE_TIMES; - - /* - * dynamic snooze times - */ - static const std::string DYNAMIC_SNOOZE_TIMES; - - /* - * ring duration - */ - static const std::string RING_DURATION; - - /* - * expired flag - */ - static const std::string IS_EXPIRED; - - /* - * active flag - */ - static const std::string IS_ACTIVE; - - /* - * reminder state - */ - static const std::string STATE; - - /* - * zone id - */ - static const std::string ZONE_ID; - - /* - * scheduled timeout flag - */ - static const std::string HAS_SCHEDULED_TIMEOUT; - - /* - * action button information - */ - static const std::string ACTION_BUTTON_INFO; - - /* - * custom button uri - */ - static const std::string CUSTOM_BUTTON_URI; - - /* - * slot type - */ - static const std::string SLOT_ID; - - /* - * snoozeslot type - */ - static const std::string SNOOZE_SLOT_ID; - - /* - * notification id - */ - static const std::string NOTIFICATION_ID; - - /* - * notification title - */ - static const std::string TITLE; - - /* - * notification content - */ - static const std::string CONTENT; - - /* - * notification snooze content - */ - static const std::string SNOOZE_CONTENT; - - /* - * notification expired content - */ - static const std::string EXPIRED_CONTENT; - - /* - * agent information - */ - static const std::string AGENT; - - /* - * max screen agent information - */ - static const std::string MAX_SCREEN_AGENT; - - /* - * tap dismissed flag - */ - static const std::string TAP_DISMISSED; - - /* - * auto deleted time - */ - static const std::string AUTO_DELETED_TIME; - - /* - * repeat days of week - */ - static const std::string REPEAT_DAYS_OF_WEEK; - - /* - * reminder group id - */ - static const std::string GROUP_ID; - - /* - * reminder ring uri - */ - static const std::string CUSTOM_RING_URI; - - /* - * reminder creator bundle name - */ - static const std::string CREATOR_BUNDLE_NAME; - - // Reminder Table Calendar Columns. - static const std::string REPEAT_DAYS; - static const std::string REPEAT_MONTHS; - static const std::string FIRST_DESIGNATE_YEAR; - static const std::string FIRST_DESIGNATE_MONTH; - static const std::string FIRST_DESIGNATE_DAY; - static const std::string CALENDAR_YEAR; - static const std::string CALENDAR_MONTH; - static const std::string CALENDAR_DAY; - static const std::string CALENDAR_HOUR; - static const std::string CALENDAR_MINUTE; - - // Reminder Table Alarm Columns. - static const std::string ALARM_HOUR; - static const std::string ALARM_MINUTE; - - static std::string ADD_COLUMNS; - static std::string SELECT_COLUMNS; - -private: - static void InitBasicColumns(); - static void InitCalendarColumns(); - static void InitAlarmColumns(); - static void AddColumn(const std::string& name, const std::string& type, bool isEnd = false); -}; -} // namespace Notification -} // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_TABLE_OLD_H diff --git a/services/reminder/src/reminder_data_manager.cpp b/services/reminder/src/reminder_data_manager.cpp index fe38e5eac..96b5c555f 100644 --- a/services/reminder/src/reminder_data_manager.cpp +++ b/services/reminder/src/reminder_data_manager.cpp @@ -287,7 +287,7 @@ void ReminderDataManager::CancelRemindersImplLocked(const std::string& packageNa return; } if (isCancelAllPackage) { - store_->DeleteUser(userId); + store_->Delete(userId); } else { store_->Delete(packageName, userId, uid); } @@ -1253,10 +1253,9 @@ sptr ReminderDataManager::GetRecentReminder() it++; continue; } - int32_t reminderId = (*it)->GetReminderId(); if (!(*it)->IsShare()) { totalCount_--; - store_->Delete(reminderId); + store_->Delete((*it)->GetReminderId(), static_cast((*it)->GetReminderType())); } it = reminderVector_.erase(it); } @@ -1714,7 +1713,7 @@ void ReminderDataManager::RemoveReminderLocked(const int32_t reminderId, bool is it = reminderVector_.erase(it); if (!isShare) { totalCount_--; - store_->Delete(reminderId); + store_->Delete(reminderId, static_cast((*it)->GetReminderType())); } break; } else { diff --git a/services/reminder/src/reminder_store.cpp b/services/reminder/src/reminder_store.cpp index 7849661ed..1dbf77075 100644 --- a/services/reminder/src/reminder_store.cpp +++ b/services/reminder/src/reminder_store.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,40 +15,31 @@ #include "reminder_store.h" -#include #include -#include #include +#include +#include -#include "fa_ability_context.h" #include "ans_log_wrapper.h" #include "reminder_table.h" -#include "reminder_table_old.h" #include "reminder_request_alarm.h" #include "reminder_request_timer.h" #include "reminder_request_calendar.h" -#include "reminder_store_strategy.h" -#include "reminder_utils.h" - -namespace OHOS { -namespace Notification { -namespace { -constexpr int32_t REMINDER_RDB_VERSION_V1 = 1; -constexpr int32_t REMINDER_RDB_VERSION_V2 = 2; -constexpr int32_t REMINDER_RDB_VERSION_V3 = 3; -constexpr int32_t REMINDER_RDB_VERSION_V4 = 4; -constexpr int32_t REMINDER_RDB_VERSION_V5 = 5; -constexpr int32_t REMINDER_RDB_VERSION_V6 = 6; -constexpr int32_t REMINDER_RDB_VERSION_V7 = 7; -constexpr int32_t REMINDER_RDB_VERSION_V8 = 8; -constexpr int32_t REMINDER_RDB_VERSION = 9; -constexpr int64_t DURATION_PRELOAD_TIME = 10 * 60 * 60 * 1000; // 10h, millisecond -} -const int32_t ReminderStore::STATE_OK = 0; -const int32_t ReminderStore::STATE_FAIL = -1; -const std::string ReminderStore::REMINDER_DB_DIR = "/data/service/el1/public/notification/"; -const std::string ReminderStore::REMINDER_DB_NAME = "notification.db"; +namespace OHOS::Notification { +static constexpr int32_t REMINDER_RDB_VERSION_V1 = 1; +static constexpr int32_t REMINDER_RDB_VERSION_V2 = 2; +static constexpr int32_t REMINDER_RDB_VERSION_V3 = 3; +static constexpr int32_t REMINDER_RDB_VERSION_V4 = 4; +static constexpr int32_t REMINDER_RDB_VERSION_V5 = 5; +static constexpr int32_t REMINDER_RDB_VERSION_V6 = 6; +static constexpr int32_t REMINDER_RDB_VERSION_V7 = 7; +static constexpr int32_t REMINDER_RDB_VERSION_V8 = 8; +static constexpr int32_t REMINDER_RDB_VERSION = 9; +static constexpr int64_t DURATION_PRELOAD_TIME = 10 * 60 * 60 * 1000; // 10h, millisecond + +static constexpr const char* REMINDER_DB_DIR = "/data/service/el1/public/notification/"; +static constexpr const char* REMINDER_DB_NAME = "notification.db"; int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore& store) { @@ -83,18 +74,6 @@ int32_t ReminderStore::ReminderStoreDataCallBack::OnUpgrade( AddRdbColum(store, ReminderCalendarTable::TABLE_NAME, ReminderCalendarTable::CALENDAR_LAST_DATE_TIME, "BIGINT", "0"); [[fallthrough]]; - case REMINDER_RDB_VERSION_V7: - AddRdbColum(store, ReminderBaseTable::TABLE_NAME, ReminderBaseTable::TITLE_RESOURCE_ID, "INT", "0"); - AddRdbColum(store, ReminderBaseTable::TABLE_NAME, - ReminderBaseTable::CONTENT_RESOURCE_ID, "INT", "0"); - AddRdbColum(store, ReminderBaseTable::TABLE_NAME, - ReminderBaseTable::SNOOZE_CONTENT_RESOURCE_ID, "INT", "0"); - AddRdbColum(store, ReminderBaseTable::TABLE_NAME, - ReminderBaseTable::EXPIRED_CONTENT_RESOURCE_ID, "INT", "0"); - [[fallthrough]]; - case REMINDER_RDB_VERSION_V8: - AddRdbColum(store, ReminderBaseTable::TABLE_NAME, ReminderBaseTable::RING_CHANNEL, "INT", "0"); - [[fallthrough]]; default: break; } @@ -181,7 +160,10 @@ std::vector> ReminderStore::ReminderStoreDataCallBack::Get return reminders; } - while (queryResult->GoToNextRow() == NativeRdb::E_OK) { + bool isLastRow = false; + queryResult->IsAtLastRow(isLastRow); + while (!isLastRow) { + queryResult->GoToNextRow(); int32_t reminderId; int32_t reminderType; GetInt32Val(queryResult, ReminderTable::REMINDER_ID, reminderId); @@ -211,6 +193,7 @@ std::vector> ReminderStore::ReminderStoreDataCallBack::Get if (reminderReq != nullptr) { reminders.push_back(reminderReq); } + queryResult->IsAtLastRow(isLastRow); } return reminders; } @@ -277,24 +260,25 @@ void ReminderStore::ReminderStoreDataCallBack::AddRdbColum(NativeRdb::RdbStore& } } -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Init() +int32_t ReminderStore::Init() { ANSR_LOGD("Reminder store init."); - std::lock_guard lock(initMutex_); - if (access(REMINDER_DB_DIR.c_str(), F_OK) != 0) { - int createDir = mkdir(REMINDER_DB_DIR.c_str(), S_IRWXU); + if (access(REMINDER_DB_DIR, F_OK) != 0) { + int createDir = mkdir(REMINDER_DB_DIR, S_IRWXU); if (createDir != 0) { - ANSR_LOGE("Failed to create directory %{private}s", REMINDER_DB_DIR.c_str()); + ANSR_LOGE("Failed to create directory %{private}s.", REMINDER_DB_DIR); return STATE_FAIL; } } + ReminderTable::InitDbColumns(); ReminderBaseTable::InitDbColumns(); ReminderTimerTable::InitDbColumns(); ReminderAlarmTable::InitDbColumns(); ReminderCalendarTable::InitDbColumns(); - std::string dbConfig = REMINDER_DB_DIR + REMINDER_DB_NAME; + std::string dbConfig(REMINDER_DB_DIR); + dbConfig.append(REMINDER_DB_NAME); NativeRdb::RdbStoreConfig config(dbConfig); config.SetSecurityLevel(NativeRdb::SecurityLevel::S1); @@ -305,49 +289,43 @@ __attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Init() ANSR_LOGE("ReminderStore init fail, errCode %{public}d.", errCode); return STATE_FAIL; } - int32_t result = InitData(); - return result; + strategy_.emplace(static_cast(ReminderRequest::ReminderType::TIMER), + std::make_shared()); + strategy_.emplace(static_cast(ReminderRequest::ReminderType::CALENDAR), + std::make_shared()); + strategy_.emplace(static_cast(ReminderRequest::ReminderType::ALARM), + std::make_shared()); + return InitData(); } -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Delete(const int32_t reminderId) +int32_t ReminderStore::Delete(const int32_t reminderId, const int32_t type) { if (rdbStore_ == nullptr) { ANSR_LOGE("Rdb store is not initialized."); return STATE_FAIL; } - std::string condition = ReminderBaseTable::REMINDER_ID + " = " + std::to_string(reminderId); - rdbStore_->BeginTransaction(); - int32_t delRows = STATE_FAIL; - std::vector whereArgs; - int32_t ret = rdbStore_->Delete(delRows, ReminderBaseTable::TABLE_NAME, condition, whereArgs); - if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Delete from %{public}s failed, reminderId = %{public}d", - ReminderBaseTable::TABLE_NAME.c_str(), reminderId); - rdbStore_->RollBack(); + auto iter = strategy_.find(type); + if (iter == strategy_.end() || iter->second == nullptr) { + ANSR_LOGW("Not find the reminder type[%{public}d] strategy.", type); return STATE_FAIL; } - delRows = STATE_FAIL; - ret = rdbStore_->Delete(delRows, ReminderAlarmTable::TABLE_NAME, condition, whereArgs); - if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Delete from %{public}s failed, reminderId = %{public}d", - ReminderAlarmTable::TABLE_NAME.c_str(), reminderId); - rdbStore_->RollBack(); - return STATE_FAIL; - } - delRows = STATE_FAIL; - ret = rdbStore_->Delete(delRows, ReminderCalendarTable::TABLE_NAME, condition, whereArgs); + + std::string condition; + // where reminder_id=id + condition.append(ReminderBaseTable::REMINDER_ID).append("=").append(std::to_string(reminderId)); + rdbStore_->BeginTransaction(); + int32_t delRows = STATE_FAIL; + int32_t ret = rdbStore_->Delete(delRows, ReminderBaseTable::TABLE_NAME, condition); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Delete from %{public}s failed, reminderId = %{public}d", - ReminderCalendarTable::TABLE_NAME.c_str(), reminderId); + ANSR_LOGE("Delete from reminder_base failed, reminderId:%{public}d.", reminderId); rdbStore_->RollBack(); return STATE_FAIL; } delRows = STATE_FAIL; - ret = rdbStore_->Delete(delRows, ReminderTimerTable::TABLE_NAME, condition, whereArgs); + ret = rdbStore_->Delete(delRows, iter->second->GetTableName(), condition); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Delete from %{public}s failed, reminderId = %{public}d", - ReminderTimerTable::TABLE_NAME.c_str(), reminderId); + ANSR_LOGE("Delete from reminder_* failed, reminderId:%{public}d.", reminderId); rdbStore_->RollBack(); return STATE_FAIL; } @@ -355,43 +333,55 @@ __attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Delete(const int32_t return STATE_OK; } -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Delete(const std::string& pkg, const int32_t userId, - const int32_t uid) +int32_t ReminderStore::Delete(const std::string& bundleName, const int32_t userId, const int32_t uid) { - std::string assoConditon = "(SELECT " + ReminderBaseTable::REMINDER_ID + " FROM " + ReminderBaseTable::TABLE_NAME - + " WHERE " + ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::PACKAGE_NAME + " = '" + pkg - + "' AND " + ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::USER_ID + " = " + std::to_string(userId); + std::string condition1; + // package_name='bundleName' + condition1.append(ReminderBaseTable::PACKAGE_NAME).append("='").append(bundleName).append("'"); + std::string condition2; + // user_id=userId + condition1.append(ReminderBaseTable::USER_ID).append("=").append(std::to_string(userId)); - std::string baseCondtion = ReminderBaseTable::PACKAGE_NAME + " = '" + pkg + "' AND " - + ReminderBaseTable::USER_ID + " = " + std::to_string(userId); + std::string assoConditon; // Conditions for the joint query + assoConditon.append("(SELECT ").append(ReminderBaseTable::REMINDER_ID).append(" FROM "); + assoConditon.append(ReminderBaseTable::TABLE_NAME).append(" WHERE "); + assoConditon.append(condition1).append(" AND ").append(condition2); + + std::string baseCondtion; + baseCondtion.append(condition1).append(" AND ").append(condition2); if (uid != -1) { - assoConditon += " AND " + ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::UID - + " = " + std::to_string(uid); - baseCondtion += " AND " + ReminderBaseTable::UID + " = " + std::to_string(uid); + std::string condition3; + // uid=uid + condition3.append(ReminderBaseTable::UID).append("=").append(std::to_string(uid)); + assoConditon.append(" AND ").append(condition3); + baseCondtion.append(" AND ").append(condition3); } - assoConditon += ")"; + assoConditon.append(")"); return Delete(baseCondtion, assoConditon); } -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::DeleteUser(const int32_t userId) +int32_t ReminderStore::Delete(const int32_t userId) { - std::string assoConditon = "(SELECT " + ReminderBaseTable::REMINDER_ID + " FROM " + ReminderBaseTable::TABLE_NAME - + " WHERE " + ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::USER_ID + " = " - + std::to_string(userId) + ")"; + std::string condition; + // user_id=userId + condition.append(ReminderBaseTable::USER_ID).append("=").append(std::to_string(userId)); - std::string baseCondtion = ReminderBaseTable::USER_ID + " = " + std::to_string(userId); - return Delete(baseCondtion, assoConditon); + std::string assoConditon; // Conditions for the joint query + assoConditon.append("(SELECT ").append(ReminderBaseTable::REMINDER_ID).append(" FROM "); + assoConditon.append(ReminderBaseTable::TABLE_NAME).append(" WHERE "); + assoConditon.append(condition).append(")"); + return Delete(condition, assoConditon); } -int32_t ReminderStore::UpdateOrInsert( - const sptr& reminder) +int32_t ReminderStore::UpdateOrInsert(const sptr& reminder) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); + if (rdbStore_ == nullptr || reminder == nullptr) { + ANSR_LOGE("Rdb store is not initialized or reminder is nullptr."); return STATE_FAIL; } - if (reminder != nullptr && reminder->IsShare()) { + if (reminder->IsShare()) { + // calendar datashare reminder return STATE_OK; } if (IsReminderExist(reminder)) { @@ -401,6 +391,67 @@ int32_t ReminderStore::UpdateOrInsert( } } +bool ReminderStore::IsReminderExist(const int32_t reminderId, const int32_t uid) +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return false; + } + NativeRdb::AbsRdbPredicates absRdbPredicates(ReminderBaseTable::TABLE_NAME); + absRdbPredicates.EqualTo(ReminderBaseTable::REMINDER_ID, std::to_string(reminderId)); + absRdbPredicates.EqualTo(ReminderBaseTable::CREATOR_UID, std::to_string(uid)); + auto result = rdbStore_->Query(absRdbPredicates, std::vector()); + if (result == nullptr) { + ANSR_LOGE("Query db result is null."); + return false; + } + int32_t count; + result->GetRowCount(count); + if (count != 1) { + return false; + } + return resultNum != 0; +} + +int32_t ReminderStore::QueryActiveReminderCount() +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return 0; + } + std::string queryCondition = "SELECT * FROM "; + queryCondition.append(ReminderBaseTable::TABLE_NAME).append(" WHERE ").append(ReminderBaseTable::IS_EXPIRED) + .append(" = 'false' AND ").append(ReminderTable::REMINDER_TYPE).append(" != 1"); + std::vector whereArgs; + auto resultSet = rdbStore_->QuerySql(queryCondition, whereArgs); + int32_t baseTableNum = 0; + resultSet->GetRowCount(baseTableNum); + + queryCondition = "SELECT * FROM "; + int64_t nowTime = GetCurrentTime(); + queryCondition.append(ReminderCalendarTable::TABLE_NAME).append(" WHERE (") + .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append(" < ").append(std::to_string(nowTime)).append(" AND ") + .append(ReminderCalendarTable::CALENDAR_END_DATE_TIME) + .append(" > ").append(std::to_string(nowTime)).append(" ) OR ") + .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append("> ").append(std::to_string(nowTime)).append(" OR (") + .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append("< ").append(std::to_string(nowTime)).append(" AND ") + .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append(" = ") + .append(ReminderCalendarTable::CALENDAR_END_DATE_TIME).append(")"); + resultSet = rdbStore_->QuerySql(queryCondition, whereArgs); + int32_t calenderTableNum = 0; + resultSet->GetRowCount(calenderTableNum); + return baseTableNum + calenderTableNum; +} + +std::vector> ReminderStore::GetAllValidReminders() +{ + std::string sql = "SELECT " + ReminderBaseTable::SELECT_COLUMNS + " FROM " + + ReminderBaseTable::TABLE_NAME + " WHERE " + + ReminderBaseTable::IS_EXPIRED + " = 'false' ORDER BY " + + ReminderBaseTable::TRIGGER_TIME + " ASC"; + return GetReminders(sql); +} + int32_t ReminderStore::GetMaxId() { if (rdbStore_ == nullptr) { @@ -431,58 +482,9 @@ int32_t ReminderStore::GetMaxId() return maxId; } -__attribute__((no_sanitize("cfi"))) std::vector> ReminderStore::GetAllValidReminders() -{ - std::string sql = "SELECT " + ReminderBaseTable::SELECT_COLUMNS + " FROM " - + ReminderBaseTable::TABLE_NAME + " WHERE " - + ReminderBaseTable::IS_EXPIRED + " = 'false' ORDER BY " - + ReminderBaseTable::TRIGGER_TIME + " ASC"; - ANSR_LOGD("GetAllValidReminders sql =%{public}s", sql.c_str()); - return GetReminders(sql); -} - -__attribute__((no_sanitize("cfi"))) std::vector> ReminderStore::GetHalfHourReminders() -{ - int64_t nowTime = GetCurrentTime(); - std::string sql = "SELECT * FROM " + - ReminderBaseTable::TABLE_NAME + " WHERE (" + - ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::IS_EXPIRED + " = 'false' AND " + - ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::REMINDER_TYPE + " != 1 AND " + - ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::TRIGGER_TIME + " < " + - std::to_string(nowTime + DURATION_PRELOAD_TIME) + ") OR " + - ReminderBaseTable::TABLE_NAME + "." + ReminderBaseTable::REMINDER_ID + " IN " + - "(SELECT " + ReminderBaseTable::REMINDER_ID + " FROM " + ReminderCalendarTable::TABLE_NAME + " WHERE (" + - ReminderCalendarTable::TABLE_NAME + "." + ReminderCalendarTable::CALENDAR_DATE_TIME + " <= " + - std::to_string(nowTime + DURATION_PRELOAD_TIME) + " AND " + - ReminderCalendarTable::TABLE_NAME + "." + ReminderCalendarTable::CALENDAR_END_DATE_TIME + " >= " + - std::to_string(nowTime) + ") OR (" + (ReminderCalendarTable::CALENDAR_DATE_TIME) + " < " + - std::to_string(nowTime + DURATION_PRELOAD_TIME) + " AND " + ReminderCalendarTable::CALENDAR_END_DATE_TIME + - " = " + ReminderCalendarTable::CALENDAR_DATE_TIME + ")) ORDER BY " + - ReminderBaseTable::TRIGGER_TIME + " ASC"; - ANSR_LOGD("GetHalfHourReminders sql =%{public}s", sql.c_str()); - return GetReminders(sql); -} - -void ReminderStore::GetUInt8Val(const std::shared_ptr& resultSet, - const std::string& name, uint8_t& value) -{ - int32_t val; - GetInt32Val(resultSet, name, val); - value = static_cast(val); -} - -void ReminderStore::GetUInt16Val(const std::shared_ptr& resultSet, - const std::string& name, uint16_t& value) -{ - int32_t val; - GetInt32Val(resultSet, name, val); - value = static_cast(val); -} - void ReminderStore::GetInt32Val(const std::shared_ptr& resultSet, const std::string& name, int32_t& value) { - value = 0; int32_t columnIndex = -1; resultSet->GetColumnIndex(name, columnIndex); if (columnIndex == -1) { @@ -492,83 +494,18 @@ void ReminderStore::GetInt32Val(const std::shared_ptr& res resultSet->GetInt(columnIndex, value); } -void ReminderStore::GetInt64Val(const std::shared_ptr& resultSet, - const std::string& name, int64_t& value) +int32_t ReminderStore::InitData() { - value = 0; - int32_t columnIndex = -1; - resultSet->GetColumnIndex(name, columnIndex); - if (columnIndex == -1) { - ANSR_LOGE("the column %{public}s does not exsit.", name.c_str()); - return; - } - resultSet->GetLong(columnIndex, value); -} - -void ReminderStore::GetUInt64Val(const std::shared_ptr& resultSet, - const std::string& name, uint64_t& value) -{ - int64_t val; - GetInt64Val(resultSet, name, val); - value = static_cast(val); -} - -void ReminderStore::GetStringVal(const std::shared_ptr& resultSet, - const std::string& name, std::string& value) -{ - int32_t columnIndex = -1; - resultSet->GetColumnIndex(name, columnIndex); - if (columnIndex == -1) { - ANSR_LOGE("the column %{public}s does not exsit.", name.c_str()); - return; - } - resultSet->GetString(columnIndex, value); -} - -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::InitData() -{ - ANSR_LOGD("Reminder data init."); - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return STATE_FAIL; - } - // delete all the reminders which IS_EXPIRED is true. - std::string deleteCondition = ReminderBaseTable::IS_EXPIRED + " is true"; - DeleteBase(deleteCondition); - // set all the value of STATE to ReminderRequest::REMINDER_STATUS_INACTIVE NativeRdb::ValuesBucket statusValues; statusValues.PutInt(ReminderBaseTable::STATE, ReminderRequest::REMINDER_STATUS_INACTIVE); int32_t statusChangedRows = STATE_FAIL; - int32_t ret = rdbStore_->Update(statusChangedRows, ReminderBaseTable::TABLE_NAME, statusValues); - ANSR_LOGD("Change status to inactive, changed rows: %{public}d.", statusChangedRows); - if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Init data failed."); - return STATE_FAIL; - } + // already check rdbStore_ is not nullptr before the call + rdbStore_->Update(statusChangedRows, ReminderBaseTable::TABLE_NAME, statusValues); return STATE_OK; } -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::DeleteBase(const std::string& deleteCondition) -{ - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return STATE_FAIL; - } - int32_t deletedRows = STATE_FAIL; - std::vector whereArgs; - int32_t ret = rdbStore_->Delete(deletedRows, ReminderBaseTable::TABLE_NAME, deleteCondition, whereArgs); - if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Delete operation failed, deleteConditon: %{public}s," \ - "result: %{public}d.", deleteCondition.c_str(), ret); - } - ANSR_LOGD("Delete operation done, deleteConditon: %{public}s," \ - "deleted rows: %{public}d.", deleteCondition.c_str(), deletedRows); - return deletedRows; -} - -__attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Delete(const std::string& baseCondition, - const std::string& assoConditon) +int32_t ReminderStore::Delete(const std::string& baseCondition, const std::string& assoConditon) { if (rdbStore_ == nullptr) { ANSR_LOGE("Rdb store is not initialized."); @@ -622,114 +559,77 @@ __attribute__((no_sanitize("cfi"))) int32_t ReminderStore::Delete(const std::str int32_t ReminderStore::Insert(const sptr& reminder) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); + int32_t type = static_cast(reminder->GetReminderType()); + auto iter = strategy_.find(type); + if (iter == strategy_.end() || iter->second == nullptr) { + ANSR_LOGW("Not find the reminder type[%{public}d] strategy.", type); return STATE_FAIL; } - int64_t rowId = STATE_FAIL; NativeRdb::ValuesBucket baseValues; - ReminderStrategy::AppendValuesBucket(reminder, baseValues); - + NativeRdb::ValuesBucket values; + iter->second->StoreToRdb(reminder, baseValues, values); + rdbStore_->BeginTransaction(); - // insert reminder_base + int64_t rowId = STATE_FAIL; + int32_t reminderId = reminder->GetReminderId(); + // insert to reminder_base int32_t ret = rdbStore_->Insert(rowId, ReminderBaseTable::TABLE_NAME, baseValues); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Insert reminder_base operation failed, result: %{public}d, reminderId=%{public}d.", - ret, reminder->GetReminderId()); + ANSR_LOGE("Insert reminder_base failed, result:%{public}d, reminderId:%{public}d.", ret, reminderId); rdbStore_->RollBack(); return STATE_FAIL; } - - // insert reminder_alarm or reminder_calendar - NativeRdb::ValuesBucket values; + + // insert reminder_* rowId = STATE_FAIL; - switch (reminder->GetReminderType()) { - case ReminderRequest::ReminderType::CALENDAR: { - ReminderCalendarStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Insert(rowId, ReminderCalendarTable::TABLE_NAME, values); - break; - } - case ReminderRequest::ReminderType::ALARM: { - ReminderAlarmStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Insert(rowId, ReminderAlarmTable::TABLE_NAME, values); - break; - } - case ReminderRequest::ReminderType::TIMER: { - ReminderTimerStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Insert(rowId, ReminderTimerTable::TABLE_NAME, values); - break; - } - default: { - ANSR_LOGE("Insert reminder_base operation failed, unkown type."); - ret = STATE_FAIL; - break; - } - } + ret = rdbStore_->Insert(rowId, iter->second->GetTableName(), values); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Insert operation failed, result: %{public}d, reminderId=%{public}d.", - ret, reminder->GetReminderId()); + ANSR_LOGE("Insert reminder_* failed, result:%{public}d, reminderId:%{public}d.", ret, reminderId); rdbStore_->RollBack(); return STATE_FAIL; } rdbStore_->Commit(); - ANSR_LOGD("Insert successfully, reminderId=%{public}d.", reminder->GetReminderId()); + ANSR_LOGD("Insert successfully, reminderId:%{public}d.", reminderId); return STATE_OK; } -int32_t ReminderStore::Update( - const sptr& reminder) +int32_t ReminderStore::Update(const sptr& reminder) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); + int32_t type = static_cast(reminder->GetReminderType()); + auto iter = strategy_.find(type); + if (iter == strategy_.end() || iter->second == nullptr) { + ANSR_LOGW("Not find the reminder type[%{public}d] strategy.", type); return STATE_FAIL; } - int32_t rowId = STATE_FAIL; NativeRdb::ValuesBucket baseValues; - ReminderStrategy::AppendValuesBucket(reminder, baseValues); + NativeRdb::ValuesBucket values; + iter->second->StoreToRdb(reminder, baseValues, values); std::string updateCondition = ReminderBaseTable::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); rdbStore_->BeginTransaction(); + int32_t rowId = STATE_FAIL; + int32_t reminderId = reminder->GetReminderId(); // update reminder_base std::vector whereArgs; int32_t ret = rdbStore_->Update(rowId, ReminderBaseTable::TABLE_NAME, baseValues, updateCondition, whereArgs); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Update reminder_base operation failed, result: %{public}d, reminderId=%{public}d.", - ret, reminder->GetReminderId()); + ANSR_LOGE("Update reminder_base failed, result:%{public}d, reminderId:%{public}d.", ret, reminderId); rdbStore_->RollBack(); return STATE_FAIL; } - // update reminder_alarm or reminder_calendar - NativeRdb::ValuesBucket values; + // update reminder_* rowId = STATE_FAIL; - switch (reminder->GetReminderType()) { - case ReminderRequest::ReminderType::CALENDAR: - ReminderCalendarStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Update(rowId, ReminderCalendarTable::TABLE_NAME, values, updateCondition, whereArgs); - break; - case ReminderRequest::ReminderType::ALARM: - ReminderAlarmStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Update(rowId, ReminderAlarmTable::TABLE_NAME, values, updateCondition, whereArgs); - break; - case ReminderRequest::ReminderType::TIMER: - ReminderTimerStrategy::AppendValuesBucket(reminder, values); - ret = rdbStore_->Update(rowId, ReminderTimerTable::TABLE_NAME, values, updateCondition, whereArgs); - break; - default: - ANSR_LOGE("Insert reminder_base operation failed, unkown type."); - ret = STATE_FAIL; - break; - } + ret = rdbStore_->Update(rowId, iter->second->GetTableName(), values, updateCondition, whereArgs); if (ret != NativeRdb::E_OK) { - ANSR_LOGE("Update operation failed, result: %{public}d, reminderId=%{public}d.", - ret, reminder->GetReminderId()); + ANSR_LOGE("Update reminder_* failed, result:%{public}d, reminderId:%{public}d.", ret, reminderId); rdbStore_->RollBack(); return STATE_FAIL; } rdbStore_->Commit(); - ANSR_LOGD("Update successfully, reminderId=%{public}d.", reminder->GetReminderId()); + ANSR_LOGD("Update successfully, reminderId:%{public}d.", reminderId); return STATE_OK; } @@ -747,21 +647,6 @@ bool ReminderStore::IsReminderExist(const sptr& reminder) return resultNum != 0; } -bool ReminderStore::IsReminderExist(const int32_t reminderId, const int32_t uid) -{ - NativeRdb::AbsRdbPredicates absRdbPredicates(ReminderBaseTable::TABLE_NAME); - absRdbPredicates.EqualTo(ReminderBaseTable::REMINDER_ID, std::to_string(reminderId)); - absRdbPredicates.EqualTo(ReminderBaseTable::CREATOR_UID, std::to_string(uid)); - auto queryResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); - if (queryResultSet == nullptr) { - ANSR_LOGE("QueryResultSet is null."); - return false; - } - int32_t resultNum; - queryResultSet->GetRowCount(resultNum); - return resultNum != 0; -} - std::vector> ReminderStore::GetReminders(const std::string& queryCondition) { std::vector> reminders; @@ -773,51 +658,37 @@ std::vector> ReminderStore::GetReminders(const std::string if (queryResultSet == nullptr) { return reminders; } - while (queryResultSet->GoToNextRow() == NativeRdb::E_OK) { + bool isAtLastRow = false; + queryResultSet->IsAtLastRow(isAtLastRow); + while (!isAtLastRow) { + queryResultSet->GoToNextRow(); sptr reminder = BuildReminder(queryResultSet); if (reminder != nullptr) { reminders.push_back(reminder); } + queryResultSet->IsAtLastRow(isAtLastRow); } ANSR_LOGD("Size=%{public}zu", reminders.size()); return reminders; } -sptr ReminderStore::BuildReminder(const std::shared_ptr& resultBase) +sptr ReminderStore::BuildReminder(const std::shared_ptr& baseResults) { - int32_t reminderId; - int32_t reminderType; - GetInt32Val(resultBase, ReminderBaseTable::REMINDER_ID, reminderId); - GetInt32Val(resultBase, ReminderBaseTable::REMINDER_TYPE, reminderType); - - sptr reminder = nullptr; - std::shared_ptr resultSet; - switch (reminderType) { - case (static_cast(ReminderRequest::ReminderType::TIMER)): { - reminder = new (std::nothrow) ReminderRequestTimer(reminderId); - resultSet = Query(ReminderTimerTable::TABLE_NAME, ReminderTimerTable::SELECT_COLUMNS, reminderId); - ReminderTimerStrategy::RecoverFromDb(reminder, resultBase, resultSet); - break; - } - case (static_cast(ReminderRequest::ReminderType::CALENDAR)): { - reminder = new (std::nothrow) ReminderRequestCalendar(reminderId); - resultSet = Query(ReminderCalendarTable::TABLE_NAME, ReminderCalendarTable::SELECT_COLUMNS, reminderId); - ReminderCalendarStrategy::RecoverFromDb(reminder, resultBase, resultSet); - break; - } - case (static_cast(ReminderRequest::ReminderType::ALARM)): { - reminder = new (std::nothrow) ReminderRequestAlarm(reminderId); - resultSet = Query(ReminderAlarmTable::TABLE_NAME, ReminderAlarmTable::SELECT_COLUMNS, reminderId); - ReminderAlarmStrategy::RecoverFromDb(reminder, resultBase, resultSet); - break; - } - default: { - ANSR_LOGE("ReminderType from database is error, reminderType %{public}d.", reminderType); - break; - } + int32_t type = static_cast(ReminderRequest::ReminderType::INVALID); + GetInt32Val(baseResults, ReminderBaseTable::REMINDER_TYPE, type); + auto iter = strategy_.find(type); + if (iter == strategy_.end() || iter->second == nullptr) { + ANSR_LOGW("Not find the reminder type[%{public}d] strategy.", type); + return nullptr; } + + int32_t reminderId = 0; + GetInt32Val(baseResults, ReminderBaseTable::REMINDER_ID, reminderId); + std::shared_ptr results = Query(iter->second->GetTableName(), + iter->second->GetTableColumns(), reminderId); + sptr reminder = iter->second->RestoreFromRdb(baseResults, results); if (reminder == nullptr) { - ANSR_LOGW("BuildReminder fail."); + ANSR_LOGW("Restore reminder[%{public}d] failed.", reminderId); } return reminder; } @@ -853,36 +724,4 @@ std::shared_ptr ReminderStore::Query(const std::string& qu std::vector whereArgs; return rdbStore_->QuerySql(queryCondition, whereArgs); } - -int32_t ReminderStore::QueryActiveReminderCount() -{ - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return 0; - } - std::string queryCondition = "SELECT * FROM "; - queryCondition.append(ReminderBaseTable::TABLE_NAME).append(" WHERE ").append(ReminderBaseTable::IS_EXPIRED) - .append(" = 'false' AND ").append(ReminderTable::REMINDER_TYPE).append(" != 1"); - std::vector whereArgs; - auto resultSet = rdbStore_->QuerySql(queryCondition, whereArgs); - int32_t baseTableNum = 0; - resultSet->GetRowCount(baseTableNum); - - queryCondition = "SELECT * FROM "; - int64_t nowTime = GetCurrentTime(); - queryCondition.append(ReminderCalendarTable::TABLE_NAME).append(" WHERE (") - .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append(" < ").append(std::to_string(nowTime)).append(" AND ") - .append(ReminderCalendarTable::CALENDAR_END_DATE_TIME) - .append(" > ").append(std::to_string(nowTime)).append(" ) OR ") - .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append("> ").append(std::to_string(nowTime)).append(" OR (") - .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append("< ").append(std::to_string(nowTime)).append(" AND ") - .append(ReminderCalendarTable::CALENDAR_DATE_TIME).append(" = ") - .append(ReminderCalendarTable::CALENDAR_END_DATE_TIME).append(")"); - resultSet = rdbStore_->QuerySql(queryCondition, whereArgs); - int32_t calenderTableNum = 0; - resultSet->GetRowCount(calenderTableNum); - return baseTableNum + calenderTableNum; -} - -} // namespace Notification -} // namespace OHOS +} // namespace OHOS::Notification diff --git a/services/reminder/src/reminder_store_strategy.cpp b/services/reminder/src/reminder_store_strategy.cpp index 6b5f24d21..4c367ebfd 100644 --- a/services/reminder/src/reminder_store_strategy.cpp +++ b/services/reminder/src/reminder_store_strategy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,18 +16,41 @@ #include "reminder_store_strategy.h" #include "ans_log_wrapper.h" -#include "reminder_store.h" #include "reminder_table.h" -#include "reminder_table_old.h" #include "reminder_request_alarm.h" #include "reminder_request_timer.h" #include "reminder_request_calendar.h" -namespace OHOS { -namespace Notification { -void ReminderStrategy::AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket &values, const bool oldVersion) +namespace OHOS::Notification { +template +static void GetRdbValue(const std::shared_ptr& resultSet, + const std::string& name, T& value) { + int32_t columnIndex = -1; + resultSet->GetColumnIndex(name, columnIndex); + if (columnIndex == -1) { + ANSR_LOGW("The column %{public}s does not exsit.", name.c_str()); + return; + } + + if constexpr (std::is_same_v) { + resultSet->GetString(columnIndex, value); + } else if constexpr (std::is_same_v) { + resultSet->GetLong(columnIndex, value); + } else if constexpr (std::is_same_v) { + int64_t t = 0; + resultSet->GetLong(columnIndex, t); + value = static_cast(t); + } else if constexpr (std::is_integral_v) { + int32_t t = 0; + resultSet->GetInt(columnIndex, t); + value = static_cast(t); + } +} + +void ReminderBaseStrategy::StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& values) +{ + // already check the reminder not nullptr before calling values.PutInt(ReminderBaseTable::REMINDER_ID, reminder->GetReminderId()); values.PutString(ReminderBaseTable::PACKAGE_NAME, reminder->GetBundleName()); values.PutInt(ReminderBaseTable::USER_ID, reminder->GetUserId()); @@ -56,16 +79,11 @@ void ReminderStrategy::AppendValuesBucket(const sptr& reminder, values.PutString(ReminderBaseTable::EXPIRED_CONTENT, reminder->GetExpiredContent()); values.PutInt(ReminderBaseTable::EXPIRED_CONTENT_RESOURCE_ID, reminder->GetExpiredContentResourceId()); - if (oldVersion) { - values.PutString(ReminderBaseTable::WANT_AGENT, reminder->GetWantAgentStr()); - values.PutString(ReminderBaseTable::MAX_SCREEN_WANT_AGENT, reminder->GetMaxWantAgentStr()); - } else { - std::string wantInfoStr; - std::string maxWantInfoStr; - reminder->SerializeWantAgent(wantInfoStr, maxWantInfoStr); - values.PutString(ReminderBaseTable::WANT_AGENT, wantInfoStr); - values.PutString(ReminderBaseTable::MAX_SCREEN_WANT_AGENT, maxWantInfoStr); - } + std::string wantInfoStr; + std::string maxWantInfoStr; + reminder->SerializeWantAgent(wantInfoStr, maxWantInfoStr); + values.PutString(ReminderBaseTable::WANT_AGENT, wantInfoStr); + values.PutString(ReminderBaseTable::MAX_SCREEN_WANT_AGENT, maxWantInfoStr); values.PutString(ReminderBaseTable::TAP_DISMISSED, reminder->IsTapDismissed() ? "true" : "false"); values.PutLong(ReminderBaseTable::AUTO_DELETED_TIME, reminder->GetAutoDeletedTime()); @@ -76,600 +94,362 @@ void ReminderStrategy::AppendValuesBucket(const sptr& reminder, values.PutInt(ReminderBaseTable::CREATOR_UID, reminder->GetCreatorUid()); } -void ReminderStrategy::RecoverTimeFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) -{ - uint64_t reminderTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REMINDER_TIME, reminderTime); - reminder->SetReminderTimeInMilli(reminderTime); - - uint64_t triggerTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::TRIGGER_TIME, triggerTime); - reminder->SetTriggerTimeInMilli(triggerTime); - - uint64_t timeInterval = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::TIME_INTERVAL, timeInterval); - reminder->SetTimeInterval(timeInterval); - - uint8_t snoozeTimes = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::SNOOZE_TIMES, snoozeTimes); - reminder->SetSnoozeTimes(snoozeTimes); - - uint8_t dynamicSnoozeTimes = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::DYNAMIC_SNOOZE_TIMES, dynamicSnoozeTimes); - reminder->SetSnoozeTimesDynamic(dynamicSnoozeTimes); - - uint64_t ringDuration = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::RING_DURATION, ringDuration); - reminder->SetRingDuration(ringDuration); - - int64_t autoDeletedTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::AUTO_DELETED_TIME, autoDeletedTime); - reminder->SetAutoDeletedTime(autoDeletedTime); -} - -void ReminderStrategy::RecoverIdFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) -{ - int32_t reminderId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REMINDER_ID, reminderId); - reminder->SetReminderId(reminderId); - - int32_t userId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::USER_ID, userId); - reminder->InitUserId(userId); - - int32_t uid = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::UID, uid); - reminder->InitUid(uid); - - int32_t reminderType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REMINDER_TYPE, reminderType); - reminder->SetReminderType(ReminderRequest::ReminderType(reminderType)); - - int32_t slotType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::SLOT_ID, slotType); - reminder->SetSlotType(NotificationConstant::SlotType(slotType)); - - int32_t snoozeSlotType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::SNOOZE_SLOT_ID, snoozeSlotType); - reminder->SetSnoozeSlotType(NotificationConstant::SlotType(snoozeSlotType)); - - int32_t notificationId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::NOTIFICATION_ID, notificationId); - reminder->SetNotificationId(notificationId); - - std::string groupId; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::GROUP_ID, groupId); - reminder->SetGroupId(groupId); -} - -void ReminderStrategy::RecoverContextFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) -{ - std::string bundleName; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::PKG_NAME, bundleName); - reminder->InitBundleName(bundleName); - - std::string title; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::TITLE, title); - reminder->SetTitle(title); - - std::string content; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CONTENT, content); - reminder->SetContent(content); - - std::string snoozeContent; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::SNOOZE_CONTENT, snoozeContent); - reminder->SetSnoozeContent(snoozeContent); - - std::string expiredContent; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::EXPIRED_CONTENT, expiredContent); - reminder->SetExpiredContent(expiredContent); - - std::string customButtonUri; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CUSTOM_BUTTON_URI, customButtonUri); - reminder->SetCustomButtonUri(customButtonUri); - - std::string customRingUri; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CUSTOM_RING_URI, customRingUri); - reminder->SetCustomRingUri(customRingUri); - - std::string creatorBundleName; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CREATOR_BUNDLE_NAME, creatorBundleName); - reminder->InitCreatorBundleName(creatorBundleName); -} - -void ReminderStrategy::RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) +void ReminderBaseStrategy::RestoreFromRdb(sptr& reminder, + const std::shared_ptr& results) { - if (reminder == nullptr || resultSet == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); - return; - } + // already check the reminder and the results not nullptr before calling + RestoreTime(reminder, results); + RestoreId(reminder, results); + RestoreContext(reminder, results); - ReminderStrategy::RecoverTimeFromOldVersion(reminder, resultSet); - ReminderStrategy::RecoverIdFromOldVersion(reminder, resultSet); - ReminderStrategy::RecoverContextFromOldVersion(reminder, resultSet); + uint8_t state = 0; + GetRdbValue(results, ReminderBaseTable::STATE, state); + reminder->SetState(state); std::string isSystemApp; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::SYS_APP, isSystemApp); + GetRdbValue(results, ReminderBaseTable::SYSTEM_APP, isSystemApp); reminder->SetSystemApp(isSystemApp == "true" ? true : false); std::string isExpired; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::IS_EXPIRED, isExpired); + GetRdbValue(results, ReminderBaseTable::IS_EXPIRED, isExpired); reminder->SetExpired(isExpired == "true" ? true : false); std::string actionButtons; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::ACTION_BUTTON_INFO, actionButtons); + GetRdbValue(results, ReminderBaseTable::ACTION_BUTTON_INFO, actionButtons); reminder->DeserializeButtonInfo(actionButtons); - uint8_t state = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::STATE, state); - reminder->SetState(state); - - uint8_t repeatDaysOfWeek = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); - reminder->SetRepeatDaysOfWeek(repeatDaysOfWeek); - std::string wantAgent; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::AGENT, wantAgent); - reminder->SetWantAgentStr(wantAgent); + GetRdbValue(results, ReminderBaseTable::WANT_AGENT, wantAgent); + reminder->DeserializeWantAgent(wantAgent, 0); - std::string maxScreenWantAgent; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::MAX_SCREEN_AGENT, maxScreenWantAgent); - reminder->SetMaxWantAgentStr(maxScreenWantAgent); + std::string maxWantAgent; + GetRdbValue(results, ReminderBaseTable::MAX_SCREEN_WANT_AGENT, maxWantAgent); + reminder->DeserializeWantAgent(maxWantAgent, 1); std::string tapDismissed; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::TAP_DISMISSED, tapDismissed); + GetRdbValue(results, ReminderBaseTable::TAP_DISMISSED, tapDismissed); reminder->SetTapDismissed(tapDismissed == "true" ? true : false); + + int32_t ringChannel = 0; + GetRdbValue(results, ReminderBaseTable::RING_CHANNEL, ringChannel); + reminder->SetRingChannel(static_cast(ringChannel)); } -void ReminderStrategy::RecoverTimeFromDb(sptr& reminder, - const std::shared_ptr& resultSet) +void ReminderBaseStrategy::RestoreTime(sptr& reminder, + const std::shared_ptr& results) { uint64_t reminderTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::REMINDER_TIME, reminderTime); + GetRdbValue(results, ReminderBaseTable::REMINDER_TIME, reminderTime); reminder->SetReminderTimeInMilli(reminderTime); uint64_t triggerTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TRIGGER_TIME, triggerTime); + GetRdbValue(results, ReminderBaseTable::TRIGGER_TIME, triggerTime); reminder->SetTriggerTimeInMilli(triggerTime); uint64_t timeInterval = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TIME_INTERVAL, timeInterval); + GetRdbValue(results, ReminderBaseTable::TIME_INTERVAL, timeInterval); reminder->SetTimeInterval(timeInterval); uint8_t snoozeTimes = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SNOOZE_TIMES, snoozeTimes); + GetRdbValue(results, ReminderBaseTable::SNOOZE_TIMES, snoozeTimes); reminder->SetSnoozeTimes(snoozeTimes); uint8_t dynamicSnoozeTimes = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::DYNAMIC_SNOOZE_TIMES, dynamicSnoozeTimes); + GetRdbValue(results, ReminderBaseTable::DYNAMIC_SNOOZE_TIMES, dynamicSnoozeTimes); reminder->SetSnoozeTimesDynamic(dynamicSnoozeTimes); uint64_t ringDuration = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::RING_DURATION, ringDuration); + GetRdbValue(results, ReminderBaseTable::RING_DURATION, ringDuration); reminder->SetRingDuration(ringDuration); int64_t autoDeletedTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::AUTO_DELETED_TIME, autoDeletedTime); + GetRdbValue(results, ReminderBaseTable::AUTO_DELETED_TIME, autoDeletedTime); reminder->SetAutoDeletedTime(autoDeletedTime); } -void ReminderStrategy::RecoverIdFromDb(sptr& reminder, - const std::shared_ptr& resultSet) +void ReminderBaseStrategy::RestoreId(sptr& reminder, + const std::shared_ptr& results) { int32_t reminderId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::REMINDER_ID, reminderId); + GetRdbValue(results, ReminderBaseTable::REMINDER_ID, reminderId); reminder->SetReminderId(reminderId); int32_t userId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::USER_ID, userId); - reminder->InitUserId(userId); + GetRdbValue(results, ReminderBaseTable::USER_ID, userId); + reminder->SetUserId(userId); int32_t uid = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::UID, uid); - reminder->InitUid(uid); - - int32_t reminderType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::REMINDER_TYPE, reminderType); - reminder->SetReminderType(ReminderRequest::ReminderType(reminderType)); + GetRdbValue(results, ReminderBaseTable::UID, uid); + reminder->SetUid(uid); int32_t slotType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SLOT_ID, slotType); + GetRdbValue(results, ReminderBaseTable::SLOT_ID, slotType); reminder->SetSlotType(NotificationConstant::SlotType(slotType)); int32_t snoozeSlotType = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SNOOZE_SLOT_ID, snoozeSlotType); + GetRdbValue(results, ReminderBaseTable::SNOOZE_SLOT_ID, snoozeSlotType); reminder->SetSnoozeSlotType(NotificationConstant::SlotType(snoozeSlotType)); int32_t notificationId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::NOTIFICATION_ID, notificationId); + GetRdbValue(results, ReminderBaseTable::NOTIFICATION_ID, notificationId); reminder->SetNotificationId(notificationId); std::string groupId; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::GROUP_ID, groupId); + GetRdbValue(results, ReminderBaseTable::GROUP_ID, groupId); reminder->SetGroupId(groupId); - int32_t creatorUid; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CREATOR_UID, creatorUid); - reminder->InitCreatorUid(creatorUid); + int32_t creatorUid = 0; + GetRdbValue(results, ReminderBaseTable::CREATOR_UID, creatorUid); + reminder->SetCreatorUid(creatorUid); int32_t titleResourceId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TITLE_RESOURCE_ID, titleResourceId); + GetRdbValue(results, ReminderBaseTable::TITLE_RESOURCE_ID, titleResourceId); reminder->SetTitleResourceId(titleResourceId); int32_t contentResourceId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CONTENT_RESOURCE_ID, contentResourceId); + GetRdbValue(results, ReminderBaseTable::CONTENT_RESOURCE_ID, contentResourceId); reminder->SetContentResourceId(contentResourceId); int32_t snoozeContentResourceId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SNOOZE_CONTENT_RESOURCE_ID, - snoozeContentResourceId); + GetRdbValue(results, ReminderBaseTable::SNOOZE_CONTENT_RESOURCE_ID, snoozeContentResourceId); reminder->SetSnoozeContentResourceId(snoozeContentResourceId); int32_t expiredContentResourceId = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::EXPIRED_CONTENT_RESOURCE_ID, - expiredContentResourceId); + GetRdbValue(results, ReminderBaseTable::EXPIRED_CONTENT_RESOURCE_ID, expiredContentResourceId); reminder->SetExpiredContentResourceId(expiredContentResourceId); } -void ReminderStrategy::RecoverContextFromDb(sptr& reminder, - const std::shared_ptr& resultSet) +void ReminderBaseStrategy::RestoreContext(sptr& reminder, + const std::shared_ptr& results) { std::string bundleName; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::PACKAGE_NAME, bundleName); - reminder->InitBundleName(bundleName); + GetRdbValue(results, ReminderBaseTable::PACKAGE_NAME, bundleName); + reminder->SetBundleName(bundleName); std::string customButtonUri; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CUSTOM_BUTTON_URI, customButtonUri); + GetRdbValue(results, ReminderBaseTable::CUSTOM_BUTTON_URI, customButtonUri); reminder->SetCustomButtonUri(customButtonUri); std::string title; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TITLE, title); + GetRdbValue(results, ReminderBaseTable::TITLE, title); reminder->SetTitle(title); std::string content; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CONTENT, content); + GetRdbValue(results, ReminderBaseTable::CONTENT, content); reminder->SetContent(content); std::string snoozeContent; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SNOOZE_CONTENT, snoozeContent); + GetRdbValue(results, ReminderBaseTable::SNOOZE_CONTENT, snoozeContent); reminder->SetSnoozeContent(snoozeContent); std::string expiredContent; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::EXPIRED_CONTENT, expiredContent); + GetRdbValue(results, ReminderBaseTable::EXPIRED_CONTENT, expiredContent); reminder->SetExpiredContent(expiredContent); std::string customRingUri; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CUSTOM_RING_URI, customRingUri); + GetRdbValue(results, ReminderBaseTable::CUSTOM_RING_URI, customRingUri); reminder->SetCustomRingUri(customRingUri); std::string creatorBundleName; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::CREATOR_BUNDLE_NAME, creatorBundleName); - reminder->InitCreatorBundleName(creatorBundleName); + GetRdbValue(results, ReminderBaseTable::CREATOR_BUNDLE_NAME, creatorBundleName); + reminder->SetCreatorBundleName(creatorBundleName); } -void ReminderStrategy::RecoverFromDb(sptr& reminder, - const std::shared_ptr& resultSet) +void ReminderTimerStrategy::StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) { - if (reminder == nullptr || resultSet == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); + if (reminder == nullptr || reminder->GetReminderType() != ReminderRequest::ReminderType::TIMER) { return; } - ReminderStrategy::RecoverTimeFromDb(reminder, resultSet); - ReminderStrategy::RecoverIdFromDb(reminder, resultSet); - ReminderStrategy::RecoverContextFromDb(reminder, resultSet); - - uint8_t state = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::STATE, state); - reminder->SetState(state); - - std::string isSystemApp; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::SYSTEM_APP, isSystemApp); - reminder->SetSystemApp(isSystemApp == "true" ? true : false); - - std::string isExpired; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::IS_EXPIRED, isExpired); - reminder->SetExpired(isExpired == "true" ? true : false); - - std::string actionButtons; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::ACTION_BUTTON_INFO, actionButtons); - reminder->DeserializeButtonInfo(actionButtons); - - std::string wantAgent; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::WANT_AGENT, wantAgent); - reminder->DeserializeWantAgent(wantAgent, 0); - - std::string maxWantAgent; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::MAX_SCREEN_WANT_AGENT, maxWantAgent); - reminder->DeserializeWantAgent(maxWantAgent, 1); + // already check the reminder not nullptr and type is timer before calling + ReminderBaseStrategy::StoreToRdb(reminder, baseValues); + ReminderRequestTimer* timer = static_cast(reminder.GetRefPtr()); + values.PutInt(ReminderTimerTable::REMINDER_ID, reminder->GetReminderId()); + values.PutLong(ReminderTimerTable::TRIGGER_SECOND, timer->GetInitInfo()); + values.PutLong(ReminderTimerTable::START_DATE_TIME, 0); + values.PutLong(ReminderTimerTable::END_DATE_TIME, 0); +} - std::string tapDismissed; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::TAP_DISMISSED, tapDismissed); - reminder->SetTapDismissed(tapDismissed == "true" ? true : false); +sptr ReminderTimerStrategy::RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) +{ + if (baseResults == nullptr || results == nullptr) { + ANSR_LOGW("ResultSet is nullptr."); + return nullptr; + } + sptr reminder = sptr::MakeSptr(ReminderRequest::ReminderType::TIMER); + ReminderBaseStrategy::RestoreFromRdb(reminder, baseResults); + ReminderRequestTimer* timer = static_cast(reminder.GetRefPtr()); - int32_t ringChannel; - ReminderStrategy::GetRdbValue(resultSet, ReminderBaseTable::RING_CHANNEL, ringChannel); - reminder->SetRingChannel(static_cast(ringChannel)); + uint64_t seconds = 0; + GetRdbValue(results, ReminderTimerTable::TRIGGER_SECOND, seconds); + timer->SetInitInfo(seconds); + return reminder; } -void ReminderTimerStrategy::AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket& values) +std::string ReminderTimerStrategy::GetTableName() { - uint64_t seconds = 0; - if (reminder->GetReminderType() == ReminderRequest::ReminderType::TIMER) { - ReminderRequestTimer* timer = static_cast(reminder.GetRefPtr()); - seconds = timer->GetInitInfo(); - } - values.PutInt(ReminderTimerTable::REMINDER_ID, reminder->GetReminderId()); - values.PutLong(ReminderTimerTable::TRIGGER_SECOND, seconds); - values.PutLong(ReminderTimerTable::START_DATE_TIME, 0); - values.PutLong(ReminderTimerTable::END_DATE_TIME, 0); + return ReminderTimerTable::TABLE_NAME; } -void ReminderTimerStrategy::RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) +std::string ReminderTimerStrategy::GetTableColumns() { - if (resultSet == nullptr || reminder == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); - return; - } - ReminderStrategy::RecoverFromOldVersion(reminder, resultSet); + return ReminderTimerTable::SELECT_COLUMNS; } -void ReminderTimerStrategy::RecoverFromDb(sptr& reminder, - const std::shared_ptr& baseResult, const std::shared_ptr& resultSet) +void ReminderAlarmStrategy::StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) { - if (resultSet == nullptr || reminder == nullptr || baseResult == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); + if (reminder == nullptr || reminder->GetReminderType() != ReminderRequest::ReminderType::ALARM) { return; } - ReminderStrategy::RecoverFromDb(reminder, baseResult); - if (reminder->GetReminderType() == ReminderRequest::ReminderType::TIMER) { - ReminderRequestTimer* timer = static_cast(reminder.GetRefPtr()); - uint64_t seconds; - ReminderStrategy::GetRdbValue(resultSet, ReminderTimerTable::TRIGGER_SECOND, seconds); - timer->SetInitInfo(seconds); - } + // already check the reminder not nullptr and type is alarm before calling + ReminderBaseStrategy::StoreToRdb(reminder, baseValues); + ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); + values.PutInt(ReminderAlarmTable::REMINDER_ID, reminder->GetReminderId()); + values.PutInt(ReminderAlarmTable::ALARM_HOUR, alarm->GetHour()); + values.PutInt(ReminderAlarmTable::ALARM_MINUTE, alarm->GetMinute()); + values.PutInt(ReminderAlarmTable::REPEAT_DAYS_OF_WEEK, alarm->GetRepeatDaysOfWeek()); } -void ReminderAlarmStrategy::AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket& values) +sptr ReminderAlarmStrategy::RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) { + if (baseResults == nullptr || results == nullptr) { + ANSR_LOGW("ResultSet is nullptr."); + return nullptr; + } + sptr reminder = sptr::MakeSptr(ReminderRequest::ReminderType::ALARM); + ReminderBaseStrategy::RestoreFromRdb(reminder, baseResults); + ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); + uint8_t hour = 0; + GetRdbValue(results, ReminderAlarmTable::ALARM_HOUR, hour); + alarm->SetHour(hour); + uint8_t minute = 0; + GetRdbValue(results, ReminderAlarmTable::ALARM_MINUTE, minute); + alarm->SetMinute(minute); + uint8_t repeatDaysOfWeek = 0; - if (reminder->GetReminderType() == ReminderRequest::ReminderType::ALARM) { - ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); - hour = alarm->GetHour(); - minute = alarm->GetMinute(); - repeatDaysOfWeek = alarm->GetRepeatDaysOfWeek(); - } - values.PutInt(ReminderAlarmTable::REMINDER_ID, reminder->GetReminderId()); - values.PutInt(ReminderAlarmTable::ALARM_HOUR, hour); - values.PutInt(ReminderAlarmTable::ALARM_MINUTE, minute); - values.PutInt(ReminderAlarmTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); + GetRdbValue(results, ReminderAlarmTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); + alarm->SetRepeatDaysOfWeek(repeatDaysOfWeek); + return reminder; } -void ReminderAlarmStrategy::RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) +std::string ReminderAlarmStrategy::GetTableName() { - if (resultSet == nullptr || reminder == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); - return; - } - ReminderStrategy::RecoverFromOldVersion(reminder, resultSet); - - if (reminder->GetReminderType() == ReminderRequest::ReminderType::ALARM) { - ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); - uint8_t hour = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::ALARM_HOUR, hour); - alarm->SetHour(hour); + return ReminderAlarmTable::TABLE_NAME; +} - uint8_t minute = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::ALARM_MINUTE, minute); - alarm->SetMinute(minute); - } +std::string ReminderAlarmStrategy::GetTableColumns() +{ + return ReminderAlarmTable::SELECT_COLUMNS; } -void ReminderAlarmStrategy::RecoverFromDb(sptr& reminder, - const std::shared_ptr& baseResult, const std::shared_ptr& resultSet) +void ReminderCalendarStrategy::StoreToRdb(const sptr& reminder, NativeRdb::ValuesBucket& baseValues, + NativeRdb::ValuesBucket& values) { - if (resultSet == nullptr || reminder == nullptr || baseResult == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); + if (reminder == nullptr || reminder->GetReminderType() != ReminderRequest::ReminderType::CALENDAR) { return; } - ReminderStrategy::RecoverFromDb(reminder, baseResult); - if (reminder->GetReminderType() == ReminderRequest::ReminderType::ALARM) { - ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); - uint8_t hour = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderAlarmTable::ALARM_HOUR, hour); - alarm->SetHour(hour); - - uint8_t minute = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderAlarmTable::ALARM_MINUTE, minute); - alarm->SetMinute(minute); - - uint8_t repeatDaysOfWeek = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderAlarmTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); - alarm->SetRepeatDaysOfWeek(repeatDaysOfWeek); - } + // already check the reminder not nullptr and type is calendar before calling + ReminderBaseStrategy::StoreToRdb(reminder, baseValues); + ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); + values.PutInt(ReminderCalendarTable::REMINDER_ID, reminder->GetReminderId()); + values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_YEAR, calendar->GetFirstDesignateYear()); + values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_MONTH, calendar->GetFirstDesignageMonth()); + values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_DAY, calendar->GetFirstDesignateDay()); + values.PutLong(ReminderCalendarTable::CALENDAR_DATE_TIME, calendar->GetDateTime()); + values.PutLong(ReminderCalendarTable::CALENDAR_END_DATE_TIME, calendar->GetEndDateTime()); + values.PutLong(ReminderCalendarTable::CALENDAR_LAST_DATE_TIME, calendar->GetLastStartDateTime()); + values.PutInt(ReminderCalendarTable::REPEAT_DAYS, calendar->GetRepeatDay()); + values.PutInt(ReminderCalendarTable::REPEAT_MONTHS, calendar->GetRepeatMonth()); + values.PutInt(ReminderCalendarTable::REPEAT_DAYS_OF_WEEK, calendar->GetRepeatDaysOfWeek()); + values.PutString(ReminderCalendarTable::RRULE_WANT_AGENT, calendar->SerializationRRule()); + values.PutString(ReminderCalendarTable::EXCLUDE_DATES, calendar->SerializationExcludeDates()); } -void ReminderCalendarStrategy::AppendValuesBucket(const sptr& reminder, - NativeRdb::ValuesBucket& values) +sptr ReminderCalendarStrategy::RestoreFromRdb(const std::shared_ptr& baseResults, + const std::shared_ptr& results) { - uint16_t firstDesignateYear = 0; - uint8_t firstDesignateMonth = 0; - uint8_t firstDesignateDay = 0; - uint64_t dateTime = 0; + if (baseResults == nullptr || results == nullptr) { + ANSR_LOGW("ResultSet is nullptr."); + return nullptr; + } + sptr reminder = sptr::MakeSptr(ReminderRequest::ReminderType::CALENDAR); + ReminderBaseStrategy::RestoreFromRdb(reminder, baseResults); + RestoreTime(reminder, results); + ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); + uint32_t repeatDay = 0; + GetRdbValue(results, ReminderCalendarTable::REPEAT_DAYS, repeatDay); + calendar->SetRepeatDay(repeatDay); + uint16_t repeatMonth = 0; + GetRdbValue(results, ReminderCalendarTable::REPEAT_MONTHS, repeatMonth); + calendar->SetRepeatMonth(repeatMonth); + uint8_t repeatDaysOfWeek = 0; - uint64_t endDateTime = 0; - uint64_t lastStartDateTime = 0; + GetRdbValue(results, ReminderCalendarTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); + calendar->SetRepeatDaysOfWeek(repeatDaysOfWeek); + std::string rruleWantAgent; + GetRdbValue(results, ReminderCalendarTable::RRULE_WANT_AGENT, rruleWantAgent); + calendar->DeserializationRRule(rruleWantAgent); + std::string excludeDates; - if (reminder != nullptr && reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) { - ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); - if (calendar != nullptr) { - repeatDay = calendar->GetRepeatDay(); - repeatMonth = calendar->GetRepeatMonth(); - firstDesignateYear = calendar->GetFirstDesignateYear(); - firstDesignateMonth = calendar->GetFirstDesignageMonth(); - firstDesignateDay = calendar->GetFirstDesignateDay(); - dateTime = calendar->GetDateTime(); - repeatDaysOfWeek = calendar->GetRepeatDaysOfWeek(); - endDateTime = calendar->GetEndDateTime(); - lastStartDateTime = calendar->GetLastStartDateTime(); - rruleWantAgent = calendar->SerializationRRule(); - excludeDates = calendar->SerializationExcludeDates(); - } - } - values.PutInt(ReminderCalendarTable::REMINDER_ID, reminder->GetReminderId()); - values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_YEAR, firstDesignateYear); - values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_MONTH, firstDesignateMonth); - values.PutInt(ReminderCalendarTable::FIRST_DESIGNATE_DAY, firstDesignateDay); - values.PutLong(ReminderCalendarTable::CALENDAR_DATE_TIME, dateTime); - values.PutLong(ReminderCalendarTable::CALENDAR_END_DATE_TIME, endDateTime); - values.PutLong(ReminderCalendarTable::CALENDAR_LAST_DATE_TIME, lastStartDateTime); - values.PutInt(ReminderCalendarTable::REPEAT_DAYS, repeatDay); - values.PutInt(ReminderCalendarTable::REPEAT_MONTHS, repeatMonth); - values.PutInt(ReminderCalendarTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); - values.PutString(ReminderCalendarTable::RRULE_WANT_AGENT, rruleWantAgent); - values.PutString(ReminderCalendarTable::EXCLUDE_DATES, excludeDates); + GetRdbValue(results, ReminderCalendarTable::EXCLUDE_DATES, excludeDates); + calendar->DeserializationExcludeDates(excludeDates); + return reminder; } -void ReminderCalendarStrategy::RecoverFromOldVersion(sptr& reminder, - const std::shared_ptr& resultSet) +std::string ReminderCalendarStrategy::GetTableName() { - if (resultSet == nullptr || reminder == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); - return; - } - ReminderStrategy::RecoverFromOldVersion(reminder, resultSet); - if (reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) { - ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); - uint32_t repeatDay = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REPEAT_DAYS, repeatDay); - calendar->SetRepeatDay(repeatDay); - - uint16_t repeatMonth = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::REPEAT_MONTHS, repeatMonth); - calendar->SetRepeatMonth(repeatMonth); - - uint16_t firstDesignateYear = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::FIRST_DESIGNATE_YEAR, firstDesignateYear); - calendar->SetFirstDesignateYear(firstDesignateYear); - - uint8_t firstDesignateMonth = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::FIRST_DESIGNATE_MONTH, firstDesignateMonth); - calendar->SetFirstDesignageMonth(firstDesignateMonth); - - uint8_t firstDesignateDay = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::FIRST_DESIGNATE_DAY, firstDesignateDay); - calendar->SetFirstDesignateDay(firstDesignateDay); - - uint16_t year = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CALENDAR_YEAR, year); - calendar->SetYear(year); - - uint8_t month = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CALENDAR_MONTH, month); - calendar->SetMonth(month); - - uint8_t day = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CALENDAR_DAY, day); - calendar->SetDay(day); - - uint8_t hour = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CALENDAR_HOUR, hour); - calendar->SetHour(hour); - - uint8_t minute = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderTable::CALENDAR_MINUTE, minute); - calendar->SetMinute(minute); - } + return ReminderCalendarTable::TABLE_NAME; } -void ReminderCalendarStrategy::RecoverTime(sptr& reminder, - const std::shared_ptr& resultSet) +std::string ReminderCalendarStrategy::GetTableColumns() { - if (reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) { - ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); - - uint16_t firstDesignateYear = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::FIRST_DESIGNATE_YEAR, - firstDesignateYear); - calendar->SetFirstDesignateYear(firstDesignateYear); - - uint8_t firstDesignateMonth = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::FIRST_DESIGNATE_MONTH, - firstDesignateMonth); - calendar->SetFirstDesignageMonth(firstDesignateMonth); - - uint8_t firstDesignateDay = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::FIRST_DESIGNATE_DAY, - firstDesignateDay); - calendar->SetFirstDesignateDay(firstDesignateDay); - - uint64_t dateTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::CALENDAR_DATE_TIME, dateTime); - calendar->SetDateTime(dateTime); - - uint64_t endDateTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::CALENDAR_END_DATE_TIME, endDateTime); - if (endDateTime != 0 && endDateTime >= dateTime) { - calendar->SetEndDateTime(endDateTime); - } else { - calendar->SetEndDateTime(dateTime); - } - - uint64_t lastStartDateTime = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::CALENDAR_LAST_DATE_TIME, - lastStartDateTime); - if (lastStartDateTime == 0) { - calendar->SetLastStartDateTime(dateTime); - } else { - calendar->SetLastStartDateTime(lastStartDateTime); - } - } + return ReminderCalendarTable::SELECT_COLUMNS; } -void ReminderCalendarStrategy::RecoverFromDb(sptr& reminder, - const std::shared_ptr& baseResult, const std::shared_ptr& resultSet) +void ReminderCalendarStrategy::RestoreTime(sptr& reminder, + const std::shared_ptr& results) { - if (resultSet == nullptr || reminder == nullptr || baseResult == nullptr) { - ANSR_LOGE("ResultSet is null or reminder is null"); - return; + // already check the reminder and the results not nullptr before calling + ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); + + uint16_t firstDesignateYear = 0; + GetRdbValue(results, ReminderCalendarTable::FIRST_DESIGNATE_YEAR, firstDesignateYear); + calendar->SetFirstDesignateYear(firstDesignateYear); + + uint8_t firstDesignateMonth = 0; + GetRdbValue(results, ReminderCalendarTable::FIRST_DESIGNATE_MONTH, firstDesignateMonth); + calendar->SetFirstDesignageMonth(firstDesignateMonth); + + uint8_t firstDesignateDay = 0; + GetRdbValue(results, ReminderCalendarTable::FIRST_DESIGNATE_DAY, firstDesignateDay); + calendar->SetFirstDesignateDay(firstDesignateDay); + + uint64_t dateTime = 0; + GetRdbValue(results, ReminderCalendarTable::CALENDAR_DATE_TIME, dateTime); + calendar->SetDateTime(dateTime); + + uint64_t endDateTime = 0; + GetRdbValue(results, ReminderCalendarTable::CALENDAR_END_DATE_TIME, endDateTime); + if (endDateTime != 0 && endDateTime >= dateTime) { + calendar->SetEndDateTime(endDateTime); + } else { + calendar->SetEndDateTime(dateTime); } - ReminderStrategy::RecoverFromDb(reminder, baseResult); - ReminderCalendarStrategy::RecoverTime(reminder, resultSet); - if (reminder != nullptr && reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) { - ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); - - uint32_t repeatDay = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::REPEAT_DAYS, repeatDay); - calendar->SetRepeatDay(repeatDay); - - uint16_t repeatMonth = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::REPEAT_MONTHS, repeatMonth); - calendar->SetRepeatMonth(repeatMonth); - - uint8_t repeatDaysOfWeek = 0; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::REPEAT_DAYS_OF_WEEK, repeatDaysOfWeek); - calendar->SetRepeatDaysOfWeek(repeatDaysOfWeek); - - std::string rruleWantAgent; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::RRULE_WANT_AGENT, rruleWantAgent); - calendar->DeserializationRRule(rruleWantAgent); - - std::string excludeDates; - ReminderStrategy::GetRdbValue(resultSet, ReminderCalendarTable::EXCLUDE_DATES, excludeDates); - calendar->DeserializationExcludeDates(excludeDates); + + uint64_t lastStartDateTime = 0; + GetRdbValue(results, ReminderCalendarTable::CALENDAR_LAST_DATE_TIME, lastStartDateTime); + if (lastStartDateTime == 0) { + calendar->SetLastStartDateTime(dateTime); + } else { + calendar->SetLastStartDateTime(lastStartDateTime); } } -} } \ No newline at end of file diff --git a/services/reminder/src/reminder_table.cpp b/services/reminder/src/reminder_table.cpp index 21ce0af9f..5d28d692b 100644 --- a/services/reminder/src/reminder_table.cpp +++ b/services/reminder/src/reminder_table.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,75 +15,7 @@ #include "reminder_table.h" -namespace OHOS { -namespace Notification { -// reminder base table -const std::string ReminderBaseTable::TABLE_NAME = "reminder_base"; -const std::string ReminderBaseTable::REMINDER_ID = "reminder_id"; -const std::string ReminderBaseTable::PACKAGE_NAME = "package_name"; -const std::string ReminderBaseTable::USER_ID = "user_id"; -const std::string ReminderBaseTable::UID = "uid"; -const std::string ReminderBaseTable::SYSTEM_APP = "system_app"; -const std::string ReminderBaseTable::REMINDER_TYPE = "reminder_type"; -const std::string ReminderBaseTable::REMINDER_TIME = "reminder_time"; -const std::string ReminderBaseTable::TRIGGER_TIME = "trigger_time"; -const std::string ReminderBaseTable::TIME_INTERVAL = "time_interval"; -const std::string ReminderBaseTable::SNOOZE_TIMES = "snooze_times"; -const std::string ReminderBaseTable::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; -const std::string ReminderBaseTable::RING_DURATION = "ring_duration"; -const std::string ReminderBaseTable::IS_EXPIRED = "is_expired"; -const std::string ReminderBaseTable::STATE = "state"; -const std::string ReminderBaseTable::ACTION_BUTTON_INFO = "button_info"; -const std::string ReminderBaseTable::CUSTOM_BUTTON_URI = "custom_button_uri"; -const std::string ReminderBaseTable::SLOT_ID = "slot_id"; -const std::string ReminderBaseTable::SNOOZE_SLOT_ID = "snooze_slot_id"; -const std::string ReminderBaseTable::NOTIFICATION_ID = "notification_id"; -const std::string ReminderBaseTable::TITLE = "title"; -const std::string ReminderBaseTable::TITLE_RESOURCE_ID = "title_resource_id"; -const std::string ReminderBaseTable::CONTENT = "content"; -const std::string ReminderBaseTable::CONTENT_RESOURCE_ID = "content_resource_id"; -const std::string ReminderBaseTable::SNOOZE_CONTENT = "snooze_content"; -const std::string ReminderBaseTable::SNOOZE_CONTENT_RESOURCE_ID = "snooze_content_resource_id"; -const std::string ReminderBaseTable::EXPIRED_CONTENT = "expired_content"; -const std::string ReminderBaseTable::EXPIRED_CONTENT_RESOURCE_ID = "expired_content_resource_id"; -const std::string ReminderBaseTable::WANT_AGENT = "want_agent"; -const std::string ReminderBaseTable::MAX_SCREEN_WANT_AGENT = "max_screen_want_agent"; -const std::string ReminderBaseTable::TAP_DISMISSED = "tap_dismissed"; -const std::string ReminderBaseTable::AUTO_DELETED_TIME = "auto_deleted_time"; -const std::string ReminderBaseTable::GROUP_ID = "group_id"; -const std::string ReminderBaseTable::CUSTOM_RING_URI = "custom_ring_uri"; -const std::string ReminderBaseTable::CREATOR_BUNDLE_NAME = "creator_bundle_name"; -const std::string ReminderBaseTable::CREATOR_UID = "creator_uid"; -const std::string ReminderBaseTable::RING_CHANNEL = "ring_channel"; - -// reminder alarm table -const std::string ReminderAlarmTable::TABLE_NAME = "reminder_alarm"; -const std::string ReminderAlarmTable::REMINDER_ID = "reminder_id"; -const std::string ReminderAlarmTable::ALARM_HOUR = "alarm_hour"; -const std::string ReminderAlarmTable::ALARM_MINUTE = "alarm_minute"; -const std::string ReminderAlarmTable::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; - -// reminder calendar table -const std::string ReminderCalendarTable::TABLE_NAME = "reminder_calendar"; -const std::string ReminderCalendarTable::REMINDER_ID = "reminder_id"; -const std::string ReminderCalendarTable::FIRST_DESIGNATE_YEAR = "first_designate_year"; -const std::string ReminderCalendarTable::FIRST_DESIGNATE_MONTH = "first_designate_month"; -const std::string ReminderCalendarTable::FIRST_DESIGNATE_DAY = "first_designate_day"; -const std::string ReminderCalendarTable::CALENDAR_DATE_TIME = "calendar_date_time"; -const std::string ReminderCalendarTable::CALENDAR_END_DATE_TIME = "calendar_end_date_time"; -const std::string ReminderCalendarTable::REPEAT_DAYS = "repeat_days"; -const std::string ReminderCalendarTable::REPEAT_MONTHS = "repeat_months"; -const std::string ReminderCalendarTable::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; -const std::string ReminderCalendarTable::RRULE_WANT_AGENT = "rrule_want_agent"; -const std::string ReminderCalendarTable::EXCLUDE_DATES = "exclude_dates"; -const std::string ReminderCalendarTable::CALENDAR_LAST_DATE_TIME = "calendar_last_date_time"; - -// reminder timer table -const std::string ReminderTimerTable::TABLE_NAME = "reminder_timer"; -const std::string ReminderTimerTable::REMINDER_ID = "reminder_id"; -const std::string ReminderTimerTable::TRIGGER_SECOND = "trigger_second"; -const std::string ReminderTimerTable::START_DATE_TIME = "start_date_time"; -const std::string ReminderTimerTable::END_DATE_TIME = "end_date_time"; +namespace OHOS::Notification { std::string ReminderBaseTable::ADD_COLUMNS = ""; std::string ReminderBaseTable::SELECT_COLUMNS = ""; @@ -97,6 +29,88 @@ std::string ReminderCalendarTable::SELECT_COLUMNS = ""; std::string ReminderTimerTable::ADD_COLUMNS = ""; std::string ReminderTimerTable::SELECT_COLUMNS = ""; +std::string ReminderOldTable::ADD_COLUMNS = ""; +std::string ReminderOldTable::SELECT_COLUMNS = ""; + +void ReminderOldTable::InitDbColumns() +{ + InitBasicColumns(); + InitCalendarColumns(); + InitAlarmColumns(); +} + +void ReminderOldTable::InitBasicColumns() +{ + AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY"); + AddColumn(PKG_NAME, "TEXT NOT NULL"); + AddColumn(USER_ID, "INT NOT NULL"); + AddColumn(UID, "INT NOT NULL"); + AddColumn(SYS_APP, "TEXT NOT NULL"); + AddColumn(APP_LABEL, "TEXT"); + AddColumn(REMINDER_TYPE, "INT NOT NULL"); + AddColumn(REMINDER_TIME, "BIGINT NOT NULL"); + AddColumn(TRIGGER_TIME, "BIGINT NOT NULL"); + AddColumn(RTC_TRIGGER_TIME, "BIGINT NOT NULL"); + AddColumn(TIME_INTERVAL, "BIGINT NOT NULL"); + AddColumn(SNOOZE_TIMES, "INT NOT NULL"); + AddColumn(DYNAMIC_SNOOZE_TIMES, "INT NOT NULL"); + AddColumn(RING_DURATION, "BIGINT NOT NULL"); + AddColumn(IS_EXPIRED, "TEXT NOT NULL"); + AddColumn(IS_ACTIVE, "TEXT NOT NULL"); + AddColumn(STATE, "INT NOT NULL"); + AddColumn(ZONE_ID, "TEXT"); + AddColumn(HAS_SCHEDULED_TIMEOUT, "TEXT"); + AddColumn(ACTION_BUTTON_INFO, "TEXT"); + AddColumn(CUSTOM_BUTTON_URI, "TEXT"); + AddColumn(SLOT_ID, "INT"); + AddColumn(SNOOZE_SLOT_ID, "INT"); + AddColumn(NOTIFICATION_ID, "INT NOT NULL"); + AddColumn(TITLE, "TEXT"); + AddColumn(CONTENT, "TEXT"); + AddColumn(SNOOZE_CONTENT, "TEXT"); + AddColumn(EXPIRED_CONTENT, "TEXT"); + AddColumn(AGENT, "TEXT"); + AddColumn(MAX_SCREEN_AGENT, "TEXT"); + AddColumn(TAP_DISMISSED, "TEXT"); + AddColumn(AUTO_DELETED_TIME, "BIGINT"); + AddColumn(REPEAT_DAYS_OF_WEEK, "INT"); + AddColumn(GROUP_ID, "TEXT"); + AddColumn(CUSTOM_RING_URI, "TEXT"); + AddColumn(CREATOR_BUNDLE_NAME, "TEXT", false); +} + +void ReminderOldTable::InitCalendarColumns() +{ + AddColumn(REPEAT_DAYS, "INT"); + AddColumn(REPEAT_MONTHS, "INT"); + AddColumn(FIRST_DESIGNATE_YEAR, "INT"); + AddColumn(FIRST_DESIGNATE_MONTH, "INT"); + AddColumn(FIRST_DESIGNATE_DAY, "INT"); + AddColumn(CALENDAR_YEAR, "INT"); + AddColumn(CALENDAR_MONTH, "INT"); + AddColumn(CALENDAR_DAY, "INT"); + AddColumn(CALENDAR_HOUR, "INT"); + AddColumn(CALENDAR_MINUTE, "INT"); +} + +void ReminderOldTable::InitAlarmColumns() +{ + AddColumn(ALARM_HOUR, "INT"); + AddColumn(ALARM_MINUTE, "INT", true); +} + +void ReminderOldTable ::AddColumn( + const std::string& name, const std::string& type, bool isEnd) +{ + if (!isEnd) { + SELECT_COLUMNS.append(name).append(","); + ADD_COLUMNS += name + " " + type + ", "; + } else { + SELECT_COLUMNS.append(name); + ADD_COLUMNS += name + " " + type; + } +} + static inline void AddColumn(const std::string& name, const std::string& type, std::string& sqlOfColumns, std::string& columns) { @@ -185,4 +199,3 @@ void ReminderTimerTable::InitDbColumns() AddColumnEnd(END_DATE_TIME, "BIGINT NOT NULL", ADD_COLUMNS, SELECT_COLUMNS); } } -} diff --git a/services/reminder/src/reminder_table_old.cpp b/services/reminder/src/reminder_table_old.cpp deleted file mode 100644 index 1def1b8bc..000000000 --- a/services/reminder/src/reminder_table_old.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "reminder_table_old.h" - -namespace OHOS { -namespace Notification { -// Reminder Table Basic Columns. -const std::string ReminderTable::TABLE_NAME = "reminder"; -const std::string ReminderTable::REMINDER_ID = "reminder_id"; -const std::string ReminderTable::PKG_NAME = "package_name"; -const std::string ReminderTable::USER_ID = "user_id"; -const std::string ReminderTable::UID = "uid"; -const std::string ReminderTable::SYS_APP = "system_app"; -const std::string ReminderTable::APP_LABEL = "app_label"; -const std::string ReminderTable::REMINDER_TYPE = "reminder_type"; -const std::string ReminderTable::REMINDER_TIME = "reminder_time"; -const std::string ReminderTable::TRIGGER_TIME = "trigger_time"; -const std::string ReminderTable::RTC_TRIGGER_TIME = "rtc_trigger_time"; -const std::string ReminderTable::TIME_INTERVAL = "time_interval"; -const std::string ReminderTable::SNOOZE_TIMES = "snooze_times"; -const std::string ReminderTable::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; -const std::string ReminderTable::RING_DURATION = "ring_duration"; -const std::string ReminderTable::IS_EXPIRED = "is_expired"; -const std::string ReminderTable::IS_ACTIVE = "is_active"; -const std::string ReminderTable::STATE = "state"; -const std::string ReminderTable::ZONE_ID = "zone_id"; -const std::string ReminderTable::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; -const std::string ReminderTable::ACTION_BUTTON_INFO = "button_info"; -const std::string ReminderTable::CUSTOM_BUTTON_URI = "custom_button_uri"; -const std::string ReminderTable::SLOT_ID = "slot_id"; -const std::string ReminderTable::SNOOZE_SLOT_ID = "snooze_slot_id"; -const std::string ReminderTable::NOTIFICATION_ID = "notification_id"; -const std::string ReminderTable::TITLE = "title"; -const std::string ReminderTable::CONTENT = "content"; -const std::string ReminderTable::SNOOZE_CONTENT = "snooze_content"; -const std::string ReminderTable::EXPIRED_CONTENT = "expired_content"; -const std::string ReminderTable::AGENT = "agent"; -const std::string ReminderTable::MAX_SCREEN_AGENT = "maxScreen_agent"; -const std::string ReminderTable::TAP_DISMISSED = "tapDismissed"; -const std::string ReminderTable::AUTO_DELETED_TIME = "autoDeletedTime"; -const std::string ReminderTable::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; -const std::string ReminderTable::GROUP_ID = "groupId"; -const std::string ReminderTable::CUSTOM_RING_URI = "custom_ring_uri"; -const std::string ReminderTable::CREATOR_BUNDLE_NAME = "creator_bundle_name"; - -// Reminder Table Calendar Columns. -const std::string ReminderTable::REPEAT_DAYS = "repeat_days"; -const std::string ReminderTable::REPEAT_MONTHS = "repeat_months"; -const std::string ReminderTable::FIRST_DESIGNATE_YEAR = "first_designate_year"; -const std::string ReminderTable::FIRST_DESIGNATE_MONTH = "first_designate_month"; -const std::string ReminderTable::FIRST_DESIGNATE_DAY = "first_designate_day"; -const std::string ReminderTable::CALENDAR_YEAR = "calendar_year"; -const std::string ReminderTable::CALENDAR_MONTH = "calendar_month"; -const std::string ReminderTable::CALENDAR_DAY = "calendar_day"; -const std::string ReminderTable::CALENDAR_HOUR = "calendar_hour"; -const std::string ReminderTable::CALENDAR_MINUTE = "calendar_minute"; - -// Reminder Table Alarm Columns. -const std::string ReminderTable::ALARM_HOUR = "alarm_hour"; -const std::string ReminderTable::ALARM_MINUTE = "alarm_minute"; - -std::string ReminderTable::ADD_COLUMNS = ""; -std::string ReminderTable::SELECT_COLUMNS = ""; - -void ReminderTable::InitDbColumns() -{ - InitBasicColumns(); - InitCalendarColumns(); - InitAlarmColumns(); -} - -void ReminderTable::InitBasicColumns() -{ - AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY"); - AddColumn(PKG_NAME, "TEXT NOT NULL"); - AddColumn(USER_ID, "INT NOT NULL"); - AddColumn(UID, "INT NOT NULL"); - AddColumn(SYS_APP, "TEXT NOT NULL"); - AddColumn(APP_LABEL, "TEXT"); - AddColumn(REMINDER_TYPE, "INT NOT NULL"); - AddColumn(REMINDER_TIME, "BIGINT NOT NULL"); - AddColumn(TRIGGER_TIME, "BIGINT NOT NULL"); - AddColumn(RTC_TRIGGER_TIME, "BIGINT NOT NULL"); - AddColumn(TIME_INTERVAL, "BIGINT NOT NULL"); - AddColumn(SNOOZE_TIMES, "INT NOT NULL"); - AddColumn(DYNAMIC_SNOOZE_TIMES, "INT NOT NULL"); - AddColumn(RING_DURATION, "BIGINT NOT NULL"); - AddColumn(IS_EXPIRED, "TEXT NOT NULL"); - AddColumn(IS_ACTIVE, "TEXT NOT NULL"); - AddColumn(STATE, "INT NOT NULL"); - AddColumn(ZONE_ID, "TEXT"); - AddColumn(HAS_SCHEDULED_TIMEOUT, "TEXT"); - AddColumn(ACTION_BUTTON_INFO, "TEXT"); - AddColumn(CUSTOM_BUTTON_URI, "TEXT"); - AddColumn(SLOT_ID, "INT"); - AddColumn(SNOOZE_SLOT_ID, "INT"); - AddColumn(NOTIFICATION_ID, "INT NOT NULL"); - AddColumn(TITLE, "TEXT"); - AddColumn(CONTENT, "TEXT"); - AddColumn(SNOOZE_CONTENT, "TEXT"); - AddColumn(EXPIRED_CONTENT, "TEXT"); - AddColumn(AGENT, "TEXT"); - AddColumn(MAX_SCREEN_AGENT, "TEXT"); - AddColumn(TAP_DISMISSED, "TEXT"); - AddColumn(AUTO_DELETED_TIME, "BIGINT"); - AddColumn(REPEAT_DAYS_OF_WEEK, "INT"); - AddColumn(GROUP_ID, "TEXT"); - AddColumn(CUSTOM_RING_URI, "TEXT"); - AddColumn(CREATOR_BUNDLE_NAME, "TEXT", false); -} - -void ReminderTable::InitCalendarColumns() -{ - AddColumn(REPEAT_DAYS, "INT"); - AddColumn(REPEAT_MONTHS, "INT"); - AddColumn(FIRST_DESIGNATE_YEAR, "INT"); - AddColumn(FIRST_DESIGNATE_MONTH, "INT"); - AddColumn(FIRST_DESIGNATE_DAY, "INT"); - AddColumn(CALENDAR_YEAR, "INT"); - AddColumn(CALENDAR_MONTH, "INT"); - AddColumn(CALENDAR_DAY, "INT"); - AddColumn(CALENDAR_HOUR, "INT"); - AddColumn(CALENDAR_MINUTE, "INT"); -} - -void ReminderTable::InitAlarmColumns() -{ - AddColumn(ALARM_HOUR, "INT"); - AddColumn(ALARM_MINUTE, "INT", true); -} - -void ReminderTable ::AddColumn( - const std::string& name, const std::string& type, bool isEnd) -{ - if (!isEnd) { - SELECT_COLUMNS.append(name).append(","); - ADD_COLUMNS += name + " " + type + ", "; - } else { - SELECT_COLUMNS.append(name); - ADD_COLUMNS += name + " " + type; - } -} -} -} -- Gitee