Separate the database implementation into its own shared object 99/115599/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 20 Feb 2017 13:18:19 +0000 (22:18 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 21 Feb 2017 02:20:02 +0000 (11:20 +0900)
With this separation, API-sides are capable to use the database.

Change-Id: Ia2ad68184f305cee292fd69ff3d3bfd0dca40ad7
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
14 files changed:
CMakeLists.txt
include/ContextErrorUtil.h [new file with mode: 0644]
include/ContextTypes.h
include/Database.h
include/PlatformDatabase.h [new file with mode: 0644]
src/client/CMakeLists.txt
src/client/ServiceProxy.cpp
src/database/CMakeLists.txt [new file with mode: 0644]
src/database/Database.cpp [moved from src/server/Database.cpp with 74% similarity]
src/server/CMakeLists.txt
src/server/MethodCall.cpp
src/server/PlatformDatabase.cpp [new file with mode: 0644]
src/shared/CMakeLists.txt [new file with mode: 0644]
src/shared/ContextErrorUtil.cpp [moved from src/shared/ContextTypes.cpp with 96% similarity]

index e581025..06fccde 100644 (file)
@@ -26,5 +26,11 @@ INSTALL(
        FILES_MATCHING PATTERN "*.h"
 )
 
+SET(target_prefix "context")
+SET(target_shared "${target_prefix}-shared")
+SET(target_database "${target_prefix}-database")
+
+ADD_SUBDIRECTORY(src/shared)
+ADD_SUBDIRECTORY(src/database)
 ADD_SUBDIRECTORY(src/client)
 ADD_SUBDIRECTORY(src/server)
diff --git a/include/ContextErrorUtil.h b/include/ContextErrorUtil.h
new file mode 100644 (file)
index 0000000..9d65226
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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_ERROR_UTIL_H__
+#define __CONTEXT_ERROR_UTIL_H__
+
+#include <ContextTypes.h>
+
+#define CTX_ERROR_DOMAIN       (ctx::dbusErrorQuark())
+#define CTX_ERROR_STR(X)       (ctx::getErrorString(X))
+
+namespace ctx {
+       const char* getErrorString(int error);
+       GQuark dbusErrorQuark(void);
+}
+
+#endif
index 3dd1aa8..95f2bbb 100644 (file)
@@ -49,9 +49,6 @@
 #define E_DATA_EXIST   (TIZEN_ERROR_CONTEXT | 0X09)    /* Data exists */
 #define E_INV_DATA             (TIZEN_ERROR_CONTEXT | 0X0A)    /* Invalid data */
 
-#define CTX_ERROR_DOMAIN       (ctx::dbusErrorQuark())
-#define CTX_ERROR_STR(X)       (ctx::getErrorString(X))
-
 /* Logging and Error Handling */
 #define _I SLOGI
 #define _D SLOGD
@@ -94,9 +91,4 @@
 #define HANDLE_GERROR(Err) \
        do { if ((Err)) { _E("GError: %s", Err->message); g_error_free(Err); Err = NULL; } } while (0)
 
-namespace ctx {
-       const char* getErrorString(int error);
-       GQuark dbusErrorQuark(void);
-}
-
 #endif /* __CONTEXT_TYPES_H__ */
index cd53a12..d784fec 100644 (file)
@@ -29,17 +29,13 @@ struct sqlite3;
 
 namespace ctx {
 
-       class Credential;
        class Tuple;
 
        class EXPORT_API Database {
        public:
-               Database(const std::string& dbName, uid_t uid);
-               Database(const std::string& dbName, const Credential& credential);
-               Database(const std::string& dbName, const Credential* credential);
-               Database(const std::string& dbName);
+               Database(const std::string& dbPath);
 
-               ~Database();
+               virtual ~Database();
 
                bool open();
                void close();
diff --git a/include/PlatformDatabase.h b/include/PlatformDatabase.h
new file mode 100644 (file)
index 0000000..3300b75
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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_PLATFORM_DATABASE_H__
+#define __CONTEXT_PLATFORM_DATABASE_H__
+
+#include <ContextTypes.h>
+#include <Database.h>
+
+namespace ctx {
+
+       class Credential;
+
+       class EXPORT_API PlatformDatabase : public Database {
+       public:
+               PlatformDatabase(const std::string& dbName, uid_t uid);
+               PlatformDatabase(const std::string& dbName, const Credential& credential);
+               PlatformDatabase(const std::string& dbName, const Credential* credential);
+               PlatformDatabase(const std::string& dbName);
+       };
+
+}
+
+#endif /* __CONTEXT_PLATFORM_DATABASE_H__ */
index a954396..917e80b 100644 (file)
@@ -1,6 +1,8 @@
-SET(target "${PROJECT_NAME}-client")
+SET(target "${target_prefix}-client")
 
-FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
+SET(DEPS "${DEPS} sqlite3")
+
+FILE(GLOB_RECURSE SRCS *.cpp)
 MESSAGE("Sources: ${SRCS}")
 
 INCLUDE(FindPkgConfig)
@@ -11,15 +13,15 @@ FOREACH(flag ${PKG_CLIENT_CFLAGS})
 ENDFOREACH(flag)
 
 ADD_LIBRARY(${target} SHARED ${SRCS})
-TARGET_LINK_LIBRARIES(${target} ${PKG_CLIENT_LDFLAGS})
+TARGET_LINK_LIBRARIES(${target} ${PKG_CLIENT_LDFLAGS} ${target_shared})
 SET_TARGET_PROPERTIES(${target} PROPERTIES SOVERSION ${MAJORVER})
 SET_TARGET_PROPERTIES(${target} PROPERTIES VERSION ${FULLVER})
 
 
-SET(PC_NAME ${target})
+SET(PC_NAME "${PROJECT_NAME}-client")
 SET(PC_DESCRIPTION "Tizen Context-Service Internal Shared Library for Clients")
 SET(PC_REQUIRED ${DEPS})
-SET(PC_LDFLAGS -l${target})
+SET(PC_LDFLAGS "-l${target} -l${target_shared} -l${target_database}")
 
 CONFIGURE_FILE(
        ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in
index ca90c15..2c88675 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <ContextErrorUtil.h>
 #include <IServiceResultListener.h>
 #include <IServiceSignalListener.h>
 #include <ServiceProxy.h>
diff --git a/src/database/CMakeLists.txt b/src/database/CMakeLists.txt
new file mode 100644 (file)
index 0000000..2b7a508
--- /dev/null
@@ -0,0 +1,20 @@
+SET(target ${target_database})
+
+SET(DEPS "${DEPS} sqlite3")
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(PKG_DATABASE REQUIRED ${DEPS})
+
+FOREACH(flag ${PKG_DATABASE_CFLAGS})
+   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_LIBRARY(${target} SHARED ${SRCS})
+TARGET_LINK_LIBRARIES(${target} ${PKG_DATABASE_LDFLAGS} ${target_shared})
+SET_TARGET_PROPERTIES(${target} PROPERTIES SOVERSION ${MAJORVER})
+SET_TARGET_PROPERTIES(${target} PROPERTIES VERSION ${FULLVER})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_LIBDIR})
similarity index 74%
rename from src/server/Database.cpp
rename to src/database/Database.cpp
index 5e13348..e8e63ca 100644 (file)
  */
 
 #include <sqlite3.h>
-#include <tzplatform_config.h>
-#include <ScopeMutex.h>
-#include <Credential.h>
 #include <Tuple.h>
 #include <Database.h>
 
-#define ROOT_UID       ((uid_t)0)
-
 using namespace ctx;
 
-static GMutex __pathMutex;
-
-static std::string __getSystemPath(const std::string& dbName)
-{
-       std::string path = "." + dbName + ".db";
-
-       {
-               ScopeMutex sm(&__pathMutex);
-               path = tzplatform_mkpath(TZ_SYS_DB, path.c_str());
-       }
-
-       return path;
-}
-
-static std::string __getUserPath(const std::string& dbName, uid_t uid)
-{
-       std::string path = "." + dbName + ".db";
-
-       tzplatform_context* context = NULL;
-       tzplatform_context_create(&context);
-       IF_FAIL_RETURN_TAG(context, "", _E, "tzplatform_context_create() failed");
-
-       if (tzplatform_context_set_user(context, uid) != E_NONE) {
-               _E("tzplatform_context_set_user() failed");
-               tzplatform_context_destroy(context);
-               return "";
-       }
-
-       {
-               ScopeMutex sm(&__pathMutex);
-               path = tzplatform_context_mkpath(context, TZ_USER_DB, path.c_str());
-       }
-
-       tzplatform_context_destroy(context);
-
-       return path;
-}
-
-Database::Database(const std::string& dbName, uid_t uid) :
+Database::Database(const std::string& dbPath) :
        __dbHandle(NULL),
+       __dbPath(dbPath),
        __transactionOn(false)
 {
-       if (Credential::isSystemUid(uid)) {
-               __dbPath = __getSystemPath(dbName);
-       } else {
-               __dbPath = __getUserPath(dbName, uid);
-       }
-
-       _I("Path: %s", __dbPath.c_str());
-}
-
-Database::Database(const std::string& dbName, const Credential& credential) :
-       Database(dbName, credential.getUid())
-{
-}
-
-Database::Database(const std::string& dbName, const Credential* credential) :
-       Database(dbName, credential->getUid())
-{
-}
-
-Database::Database(const std::string& dbName) :
-       Database(dbName, ROOT_UID)
-{
+       _D("%s", __dbPath.c_str());
 }
 
 Database::~Database()
index 7e8c6ab..ea9bd3c 100644 (file)
@@ -1,8 +1,8 @@
-SET(target "${PROJECT_NAME}-server")
+SET(target "${target_prefix}-server")
 
 SET(DEPS "${DEPS} sqlite3 libsystemd-login libtzplatform-config alarm-service cynara-creds-gdbus cynara-client cynara-session")
 
-FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
+FILE(GLOB_RECURSE SRCS *.cpp)
 MESSAGE("Sources: ${SRCS}")
 
 INCLUDE(FindPkgConfig)
@@ -13,15 +13,15 @@ FOREACH(flag ${PKG_SERVER_CFLAGS})
 ENDFOREACH(flag)
 
 ADD_LIBRARY(${target} SHARED ${SRCS})
-TARGET_LINK_LIBRARIES(${target} ${LIB_PKG_LDFLAGS})
+TARGET_LINK_LIBRARIES(${target} ${LIB_PKG_LDFLAGS} ${target_shared} ${target_database})
 SET_TARGET_PROPERTIES(${target} PROPERTIES SOVERSION ${MAJORVER})
 SET_TARGET_PROPERTIES(${target} PROPERTIES VERSION ${FULLVER})
 
 
-SET(PC_NAME ${target})
+SET(PC_NAME "${PROJECT_NAME}-server")
 SET(PC_DESCRIPTION "Tizen Context-Service Internal Shared Library for Servers")
 SET(PC_REQUIRED ${DEPS})
-SET(PC_LDFLAGS -l${target})
+SET(PC_LDFLAGS "-l${target} -l${target_shared} -l${target_database}")
 
 CONFIGURE_FILE(
        ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in
index 9e7e8ee..c8e53cd 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <ContextErrorUtil.h>
 #include <ClientBase.h>
 #include <MethodCall.h>
 
diff --git a/src/server/PlatformDatabase.cpp b/src/server/PlatformDatabase.cpp
new file mode 100644 (file)
index 0000000..4ca1dc7
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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 <sqlite3.h>
+#include <tzplatform_config.h>
+#include <ScopeMutex.h>
+#include <Credential.h>
+#include <Tuple.h>
+#include <PlatformDatabase.h>
+
+#define ROOT_UID       ((uid_t)0)
+
+using namespace ctx;
+
+static GMutex __pathMutex;
+
+static std::string __getSystemPath(const std::string& dbName)
+{
+       std::string path = "." + dbName + ".db";
+
+       {
+               ScopeMutex sm(&__pathMutex);
+               path = tzplatform_mkpath(TZ_SYS_DB, path.c_str());
+       }
+
+       return path;
+}
+
+static std::string __getUserPath(const std::string& dbName, uid_t uid)
+{
+       std::string path = "." + dbName + ".db";
+
+       tzplatform_context* context = NULL;
+       tzplatform_context_create(&context);
+       IF_FAIL_RETURN_TAG(context, "", _E, "tzplatform_context_create() failed");
+
+       if (tzplatform_context_set_user(context, uid) != E_NONE) {
+               _E("tzplatform_context_set_user() failed");
+               tzplatform_context_destroy(context);
+               return "";
+       }
+
+       {
+               ScopeMutex sm(&__pathMutex);
+               path = tzplatform_context_mkpath(context, TZ_USER_DB, path.c_str());
+       }
+
+       tzplatform_context_destroy(context);
+
+       return path;
+}
+
+static std::string __getPath(const std::string& dbName, uid_t uid)
+{
+       if (Credential::isSystemUid(uid)) {
+               return __getSystemPath(dbName);
+       } else {
+               return __getUserPath(dbName, uid);
+       }
+}
+
+PlatformDatabase::PlatformDatabase(const std::string& dbName, uid_t uid) :
+       Database(__getPath(dbName, uid))
+{
+}
+
+PlatformDatabase::PlatformDatabase(const std::string& dbName, const Credential& credential) :
+       PlatformDatabase(dbName, credential.getUid())
+{
+}
+
+PlatformDatabase::PlatformDatabase(const std::string& dbName, const Credential* credential) :
+       PlatformDatabase(dbName, credential->getUid())
+{
+}
+
+PlatformDatabase::PlatformDatabase(const std::string& dbName) :
+       PlatformDatabase(dbName, ROOT_UID)
+{
+}
diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt
new file mode 100644 (file)
index 0000000..1710951
--- /dev/null
@@ -0,0 +1,18 @@
+SET(target "${target_prefix}-shared")
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(PKG_SHARED REQUIRED ${DEPS})
+
+FOREACH(flag ${PKG_SHARED_CFLAGS})
+   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_LIBRARY(${target} SHARED ${SRCS})
+TARGET_LINK_LIBRARIES(${target} ${PKG_SHARED_LDFLAGS})
+SET_TARGET_PROPERTIES(${target} PROPERTIES SOVERSION ${MAJORVER})
+SET_TARGET_PROPERTIES(${target} PROPERTIES VERSION ${FULLVER})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_LIBDIR})
similarity index 96%
rename from src/shared/ContextTypes.cpp
rename to src/shared/ContextErrorUtil.cpp
index 0a8daaa..8b1def9 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <ContextTypes.h>
+#include <ContextErrorUtil.h>
 
 #define ERROR_PREFIX   CTX_DBUS_IFACE "Error."
 
@@ -51,7 +51,7 @@ EXPORT_API const char* ctx::getErrorString(int error)
        return "Unknown";
 }
 
-GQuark ctx::dbusErrorQuark(void)
+EXPORT_API GQuark ctx::dbusErrorQuark(void)
 {
        static volatile gsize quarkVolatile = 0;
        g_dbus_error_register_error_domain("tizen-ctx-error-quark",