Modify tfeature init sequence
[platform/core/telephony/tel-plugin-database.git] / src / database_main.c
index 707358d..98dcde7 100644 (file)
@@ -25,7 +25,8 @@
 #include <strings.h>
 
 #include <glib.h>
-#include <db-util.h>
+#include <sqlite3.h>
+#include <unistd.h>
 
 #include <tcore.h>
 #include <server.h>
 #define PLUGIN_VERSION 1
 #endif
 
+#define BUSY_WAITING_USEC 50000 /* 0.05 sec */
+#define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
+
+
 static gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
 {
        int rv = 0;
@@ -83,7 +88,7 @@ static gboolean __update_query_database(Storage *strg, void *handle, const char
        }
 
        rv = sqlite3_step(stmt);
-       dbg("query executed (%d)", rv);
+       dbg("update query executed (%d)", rv);
        sqlite3_finalize(stmt);
 
        if (rv != SQLITE_DONE)
@@ -92,14 +97,36 @@ static gboolean __update_query_database(Storage *strg, void *handle, const char
        return TRUE;
 }
 
+static int _busy_handler(void *pData, int count)
+{
+        if (count < BUSY_WAITING_MAX) {
+               usleep(BUSY_WAITING_USEC);
+               return 1;
+       }
+
+       dbg("Busy Handler will be returned SQLITE_BUSY error\n");
+       return 0;
+}
+
 static void *create_handle(Storage *strg, const char *path)
 {
        int rv = 0;
        sqlite3 *handle = NULL;
 
-       rv = db_util_open(path, &handle, 0);
+       if (path == NULL) {
+               err("Invalid input param error");
+               return NULL;
+       }
+
+       rv = sqlite3_open(path, &handle);
+       if (rv != SQLITE_OK) {
+               err("fail to connect database err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
+               return NULL;
+       }
+
+       rv = sqlite3_busy_handler(handle, _busy_handler, NULL);
        if (rv != SQLITE_OK) {
-               err("fail to connect database err(%d)", rv);
+               err("fail to register busy handler err(%d), errmsg(%s)", rv, sqlite3_errmsg(handle));
                return NULL;
        }
 
@@ -114,7 +141,7 @@ static gboolean remove_handle(Storage *strg, void *handle)
        if (!handle)
                return FALSE;
 
-       rv = db_util_close(handle);
+       rv = sqlite3_close(handle);
        if (rv != SQLITE_OK) {
                err("fail to close database err(%d)", rv);
                handle = NULL;