Implement DatabaseManager in the server 04/118904/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 11:47:08 +0000 (20:47 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 13:08:23 +0000 (22:08 +0900)
Change-Id: Ib88f7fd66583ce4632d52a003b169c9479d664b4
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/DatabaseManager.cpp [new file with mode: 0644]
src/server/DatabaseManager.h [new file with mode: 0644]

diff --git a/src/server/DatabaseManager.cpp b/src/server/DatabaseManager.cpp
new file mode 100644 (file)
index 0000000..bb5a3bb
--- /dev/null
@@ -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 <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;
+}
diff --git a/src/server/DatabaseManager.h b/src/server/DatabaseManager.h
new file mode 100644 (file)
index 0000000..b274443
--- /dev/null
@@ -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 <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__ */