From 3475a960c23b295dd1321572f4bff3c7fe9ab23e Mon Sep 17 00:00:00 2001 From: Ricky Date: Tue, 11 Jun 2024 21:34:51 +0800 Subject: [PATCH] refactor GetUserTableName method, when createTable is fale need check from DB Signed-off-by: Ricky Change-Id: I6236022b377cef42e0c39440e5465a06685b0655 --- .../ans/src/notification_rdb_data_mgr.cpp | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/services/ans/src/notification_rdb_data_mgr.cpp b/services/ans/src/notification_rdb_data_mgr.cpp index e26739af9..b19e606fa 100644 --- a/services/ans/src/notification_rdb_data_mgr.cpp +++ b/services/ans/src/notification_rdb_data_mgr.cpp @@ -17,6 +17,7 @@ #include "ans_log_wrapper.h" #include "os_account_manager_helper.h" #include "rdb_errno.h" +#include #include #include @@ -524,10 +525,17 @@ std::string NotificationDataMgr::GetUserTableName(const int32_t &userId, bool cr } if (!createTable) { ANS_LOGD("Do not need create user table."); - return notificationRdbConfig_.tableName; - } - int ret = NativeRdb::E_OK; - { + std::lock_guard lock(rdbStorePtrMutex_); + std::string queryTableSql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'"; + auto absSharedResultSet = rdbStore_->QuerySql(queryTableSql); + int32_t rowCount = 0; + int32_t ret = absSharedResultSet->GetRowCount(rowCount); + if (ret == NativeRdb::E_OK && rowCount == 1) { + userTableInit_.insert(userId); + return tableName; + } + } else { + int ret = NativeRdb::E_OK; std::lock_guard lock(rdbStorePtrMutex_); if (rdbStore_ == nullptr) { return notificationRdbConfig_.tableName; @@ -535,13 +543,13 @@ std::string NotificationDataMgr::GetUserTableName(const int32_t &userId, bool cr std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);"; ret = rdbStore_->ExecuteSql(createTableSql); + if (ret == NativeRdb::E_OK) { + userTableInit_.insert(userId); + ANS_LOGD("createTable %{public}s succeed", tableName.c_str()); + return tableName; + } + ANS_LOGW("createTable %{public}s failed, code: %{code}d", tableName.c_str(), ret); } - if (ret == NativeRdb::E_OK) { - userTableInit_.insert(userId); - ANS_LOGD("createTable %{public}s succeed", tableName.c_str()); - return tableName; - } - ANS_LOGW("createTable %{public}s failed, code: %{code}d", tableName.c_str(), ret); return notificationRdbConfig_.tableName; } return tableName; -- Gitee