--- /dev/null
+/*
+ * 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 <PlatformDatabase.h>
+#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;
+}
--- /dev/null
+/*
+ * 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 <Database.h>
+#include <ContextStoreTypesPrivate.h>
+
+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__ */