From a4402485c82fbe50366d4d096b2619b192a313d3 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 14 Mar 2017 20:47:08 +0900 Subject: [PATCH] Implement DatabaseManager in the server Change-Id: Ib88f7fd66583ce4632d52a003b169c9479d664b4 Signed-off-by: Mu-Woong Lee --- src/server/DatabaseManager.cpp | 111 +++++++++++++++++++++++++++++++++++++++++ src/server/DatabaseManager.h | 48 ++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 src/server/DatabaseManager.cpp create mode 100644 src/server/DatabaseManager.h diff --git a/src/server/DatabaseManager.cpp b/src/server/DatabaseManager.cpp new file mode 100644 index 0000000..bb5a3bb --- /dev/null +++ b/src/server/DatabaseManager.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 +#include "DatabaseManager.h" + +using namespace ctx; + +Database* DatabaseManager::__systemDatabase = NULL; +Database* DatabaseManager::__userDatabase = NULL; + +DatabaseManager::DatabaseManager() +{ +} + +bool DatabaseManager::openSystem() +{ + IF_FAIL_RETURN(!isSystemReady(), true); + + __systemDatabase = new(std::nothrow) PlatformDatabase(BASE_PATH); + IF_FAIL_RETURN_TAG(__systemDatabase, false, _E, "%s", CTX_ERROR_STR(E_NO_MEM)); + + if (!__systemDatabase->open()) { + _E("DB open failed"); + delete __systemDatabase; + __systemDatabase = NULL; + return false; + } + + _I("System DB opened"); + return true; +} + +bool DatabaseManager::openUser(uid_t uid) +{ + if (isUserReady()) { + _W("Closing the current DB"); + closeUser(); + } + + __userDatabase = new(std::nothrow) PlatformDatabase(BASE_PATH, uid); + IF_FAIL_RETURN_TAG(__userDatabase, false, _E, "%s", CTX_ERROR_STR(E_NO_MEM)); + + if (!__userDatabase->open()) { + _E("DB open failed"); + delete __userDatabase; + __userDatabase = NULL; + return false; + } + + _I("User DB opened"); + return true; +} + +void DatabaseManager::closeSystem() +{ + if (!__systemDatabase) + return; + + __systemDatabase->close(); + _I("System DB closed"); + + delete __systemDatabase; + __systemDatabase = NULL; + +} + +void DatabaseManager::closeUser() +{ + if (!__userDatabase) + return; + + __userDatabase->close(); + _I("User DB closed"); + + delete __userDatabase; + __userDatabase = NULL; +} + +bool DatabaseManager::isSystemReady() +{ + return (__systemDatabase != NULL); +} + +bool DatabaseManager::isUserReady() +{ + return (__userDatabase != NULL); +} + +Database& DatabaseManager::getSystem() +{ + return *__systemDatabase; +} + +Database& DatabaseManager::getUser() +{ + return *__userDatabase; +} diff --git a/src/server/DatabaseManager.h b/src/server/DatabaseManager.h new file mode 100644 index 0000000..b274443 --- /dev/null +++ b/src/server/DatabaseManager.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 __CONTEXT_STORE_DATABASE_MANAGER_H__ +#define __CONTEXT_STORE_DATABASE_MANAGER_H__ + +#include +#include + +namespace ctx { + + class DatabaseManager { + public: + static bool openSystem(); + static bool openUser(uid_t uid); + + static void closeSystem(); + static void closeUser(); + + static bool isSystemReady(); + static bool isUserReady(); + + static Database& getSystem(); + static Database& getUser(); + + private: + DatabaseManager(); + + static Database* __systemDatabase; + static Database* __userDatabase; + }; + +} + +#endif /* __CONTEXT_STORE_DATABASE_MANAGER_H__ */ -- 2.7.4