Remove dependency on db-util 78/125778/3
authordahyeong.kim <dahyeong.kim@samsung.com>
Thu, 13 Apr 2017 12:30:33 +0000 (21:30 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Thu, 20 Apr 2017 01:25:14 +0000 (10:25 +0900)
Change-Id: I0f3e4f993cf75f076c5ebfc1b9d6b7cb15908318
Signed-off-by: dahyeong.kim <dahyeong.kim@samsung.com>
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
CMakeLists.txt
alarm-manager-registry.c
alarm-manager.c
packaging/alarm-manager.spec

index c4a57e6..055df9c 100644 (file)
@@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(
        include
 )
 
-SET(DEPS_PKGS "glib-2.0 dlog aul bundle db-util appsvc pkgmgr-info pkgmgr vconf gio-2.0 gio-unix-2.0 capi-system-device libtzplatform-config libsystemd-login eventsystem notification")
+SET(DEPS_PKGS "glib-2.0 dlog aul bundle appsvc pkgmgr-info pkgmgr vconf gio-2.0 gio-unix-2.0 capi-system-device libtzplatform-config libsystemd-login eventsystem notification sqlite3")
 
 IF(_APPFW_FEATURE_ALARM_MANAGER_MODULE_LOG)
 ADD_DEFINITIONS("-D_APPFW_FEATURE_ALARM_MANAGER_MODULE_LOG")
index cdd4e56..1e8d7d3 100644 (file)
  *
  */
 
-#include<stdio.h>
-#include<stdlib.h>
-#include<time.h>
-#include<signal.h>
-#include<string.h>
-#include<sys/types.h>
-
-#include<glib.h>
-#include <db-util.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <signal.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <sqlite3.h>
+#include <glib.h>
 #if !GLIB_CHECK_VERSION(2, 31, 0)
 #include <glib/gmacros.h>
 #endif
-#include"alarm.h"
-#include"alarm-internal.h"
 
-#define SIG_TIMER 0x32
+#include "alarm.h"
+#include "alarm-internal.h"
 
 #define MAX_GCONF_PATH_LEN 256
-#define MAX_QUERY_LEN 4096
 
 extern GSList *g_disabled_alarm_list;
 
@@ -188,7 +186,7 @@ bool _load_alarms_from_registry()
 {
        int i = 0;
        int col_idx;
-       char query[MAX_QUERY_LEN] = {0,};
+       const char *query = "select * from alarmmgr";
        sqlite3_stmt *stmt = NULL;
        const char *tail = NULL;
        alarm_info_t *alarm_info = NULL;
@@ -207,8 +205,6 @@ bool _load_alarms_from_registry()
        int noti_len;
        char *noti;
 
-       snprintf(query, MAX_QUERY_LEN, "select * from alarmmgr");
-
        if (SQLITE_OK != sqlite3_prepare(alarmmgr_db, query, strlen(query), &stmt, &tail)) {
                ALARM_MGR_EXCEPTION_PRINT("sqlite3_prepare() is failed.");
                return false;
index e2ed729..dbe69c4 100644 (file)
@@ -34,7 +34,6 @@
 #include <aul.h>
 #include <aul_svc.h>
 #include <bundle.h>
-#include <db-util.h>
 #include <vconf.h>
 #include <vconf-keys.h>
 #include <dlfcn.h>
@@ -46,6 +45,7 @@
 #include <notification.h>
 #include <notification_ipc.h>
 #include <notification_internal.h>
+#include <sqlite3.h>
 
 #include <glib.h>
 #if !GLIB_CHECK_VERSION(2, 31, 0)
@@ -159,6 +159,7 @@ static void __initialize();
 void on_bus_name_owner_changed(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path,
                const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data);
 bool __get_caller_unique_name(int pid, char *unique_name, bool *is_app, uid_t uid);
+static int __db_busyhandler(void *pData, int count);
 
 #ifdef _APPFW_FEATURE_ALARM_MANAGER_MODULE_LOG
 static void __initialize_module_log(void);
@@ -3451,12 +3452,16 @@ gboolean alarm_manager_alarm_get_all_info(AlarmManager *pObject, GDBusMethodInvo
                current_tm.tm_year + 1900, current_tm.tm_mon + 1, current_tm.tm_mday, current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
        db_path = strdup(db_path_tmp);
 
-       if (db_util_open(db_path, &alarmmgr_tool_db, DB_UTIL_REGISTER_HOOK_METHOD) != SQLITE_OK) {
-               ALARM_MGR_EXCEPTION_PRINT("Opening [%s] failed", db_path);
+       if (sqlite3_open(db_path, &alarmmgr_tool_db) != SQLITE_OK) {
+               ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]", db_path);
                return_code = ERR_ALARM_SYSTEM_FAIL;
-               g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", db_path, return_code));
-               free(db_path);
-               return true;
+               goto done;
+       }
+       /* Register busy handler */
+       if (sqlite3_busy_handler(alarmmgr_tool_db, __db_busyhandler, NULL) != SQLITE_OK) {
+               ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
+               return_code = ERR_ALARM_SYSTEM_FAIL;
+               goto done;
        }
 
        /* Drop a table */
@@ -3466,11 +3471,8 @@ gboolean alarm_manager_alarm_get_all_info(AlarmManager *pObject, GDBusMethodInvo
        /* Create a table if it does not exist */
        if (sqlite3_exec(alarmmgr_tool_db, query_for_creating_table, NULL, NULL, &error_message) != SQLITE_OK) {
                ALARM_MGR_EXCEPTION_PRINT("Creating the table is failed. error message = %s", error_message);
-               sqlite3_close(alarmmgr_tool_db);
                return_code = ERR_ALARM_SYSTEM_FAIL;
-               g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", db_path, return_code));
-               free(db_path);
-               return true;
+               goto done;
        }
 
        /* Get information of all alarms and save those into the DB. */
@@ -3510,12 +3512,14 @@ gboolean alarm_manager_alarm_get_all_info(AlarmManager *pObject, GDBusMethodInvo
 
                sqlite3_free(query);
        }
-
-       sqlite3_close(alarmmgr_tool_db);
-
        return_code = ALARMMGR_RESULT_SUCCESS;
+
+done:
        g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", db_path, return_code));
-       free(db_path);
+       if (alarmmgr_tool_db)
+               sqlite3_close(alarmmgr_tool_db);
+       if (db_path)
+               free(db_path);
        return true;
 }
 
@@ -3859,7 +3863,7 @@ static bool __initialize_dbus()
 }
 
 #define ALARMMGR_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".alarmmgr.db")
-#define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
+#define QUERY_CREATE_TABLE_ALARMMGR "create table if not exists alarmmgr \
                                (alarm_id integer primary key,\
                                                start integer,\
                                                end integer,\
@@ -3890,31 +3894,41 @@ static bool __initialize_dbus()
                                                dst_service_name_mod text \
                                                )"
 
+static int __db_busyhandler(void *pData, int count) {
+       if (5 - count > 0) {
+               struct timespec time = {
+                       .tv_sec = 0,
+                       .tv_nsec = (count + 1) * 100 * 1000 * 1000
+               };
+               nanosleep(&time, NULL);
+               ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler called. count: %d", count + 1);
+               return 1;
+       } else {
+               ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler will return SQLITE_BUSY error");
+               return 0;
+       }
+}
+
 static bool __initialize_db()
 {
        char *error_message = NULL;
-       int ret;
-
-       if (access(ALARMMGR_DB_FILE, F_OK) == 0) {
-               ret = db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db, DB_UTIL_REGISTER_HOOK_METHOD);
 
-               if (ret != SQLITE_OK) {
-                       ALARM_MGR_EXCEPTION_PRINT("====>>>> connect menu_db [%s] failed", ALARMMGR_DB_FILE);
-                       return false;
-               }
-
-               return true;
+       /* Create or Open the DB file */
+       if (sqlite3_open(ALARMMGR_DB_FILE, &alarmmgr_db) != SQLITE_OK) {
+               ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]. error: %s", ALARMMGR_DB_FILE, sqlite3_errmsg(alarmmgr_db));
+               return false;
        }
-
-       ret = db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db, DB_UTIL_REGISTER_HOOK_METHOD);
-
-       if (ret != SQLITE_OK) {
-               ALARM_MGR_EXCEPTION_PRINT("====>>>> connect menu_db [%s] failed", ALARMMGR_DB_FILE);
+       /* Register busy handler */
+       if (sqlite3_busy_handler(alarmmgr_db, __db_busyhandler, NULL) != SQLITE_OK) {
+               ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
+               sqlite3_close(alarmmgr_db);
                return false;
        }
-
-       if (SQLITE_OK != sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message)) {
+       /* Create alarmmgr table */
+       if (sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message) != SQLITE_OK) {
                ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, error message = %s", QUERY_CREATE_TABLE_ALARMMGR, error_message);
+               sqlite3_close(alarmmgr_db);
+               sqlite3_free(error_message);
                return false;
        }
 
index 51d19b6..309c5b8 100644 (file)
@@ -19,7 +19,6 @@ BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(aul)
 BuildRequires: pkgconfig(bundle)
 BuildRequires: pkgconfig(sqlite3)
-BuildRequires: pkgconfig(db-util)
 BuildRequires: pkgconfig(vconf)
 BuildRequires: pkgconfig(appsvc)
 BuildRequires: pkgconfig(pkgmgr-info)