Remove unused upgrade script
[platform/core/telephony/tel-plugin-database.git] / src / database_main.c
index 471e4bf..6573d1c 100644 (file)
 #include <db-util.h>
 
 #include <tcore.h>
+#include <server.h>
 #include <plugin.h>
 #include <storage.h>
 
-static void* create_handle(Storage *strg, const char *path)
+#ifndef PLUGIN_VERSION
+#define PLUGIN_VERSION 1
+#endif
+
+static void *create_handle(Storage *strg, const char *path)
 {
        int rv = 0;
        sqlite3 *handle = NULL;
@@ -48,11 +53,17 @@ static void* create_handle(Storage *strg, const char *path)
 
 static gboolean remove_handle(Storage *strg, void *handle)
 {
+       int rv = 0;
+
        if (!handle)
                return FALSE;
 
-       db_util_close(handle);
-       //free(handle);
+       rv = db_util_close(handle);
+       if (rv != SQLITE_OK) {
+               err("fail to close database err(%d)", rv);
+               handle = NULL;
+               return FALSE;
+       }
 
        dbg("disconnected from database");
        return TRUE;
@@ -61,8 +72,8 @@ static gboolean remove_handle(Storage *strg, void *handle)
 static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
 {
        int rv = 0;
-       sqlite3_stmtstmt = NULL;
-       char szQuery[1000+1];   // +1 is for NULL Termination Character '\0'
+       sqlite3_stmt *stmt = NULL;
+       char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
 
        GHashTableIter iter;
        gpointer key, value;
@@ -78,43 +89,48 @@ static gboolean update_query_database(Storage *strg, void *handle, const char *q
                return FALSE;
        }
 
-       g_hash_table_iter_init(&iter, in_param);
-       while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-               dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
+       if (in_param) {
+               g_hash_table_iter_init(&iter, in_param);
+               while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+                       dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
 
-               if (!value || g_strcmp0((const char*) value, "") == 0) {
-                       dbg("bind null");
-                       rv = sqlite3_bind_null(stmt, atoi((const char*) key));
-               }
-               else {
-                       dbg("bind value");
-                       rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
-                                       SQLITE_STATIC);
-               }
+                       if (!value || g_strcmp0((const char *)value, "") == 0) {
+                               dbg("bind null");
+                               rv = sqlite3_bind_null(stmt, atoi((const char *)key));
+                       } else {
+                               dbg("bind value");
+                               rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
+                                               SQLITE_STATIC);
+                       }
 
-               if (rv != SQLITE_OK) {
-                       dbg("fail to bind data (%d)", rv);
-                       return FALSE;
+                       if (rv != SQLITE_OK) {
+                               dbg("fail to bind data (%d)", rv);
+                               break;
+                       }
                }
        }
 
+       if (rv != SQLITE_OK) {
+               sqlite3_finalize(stmt);
+               return FALSE;
+       }
+
        rv = sqlite3_step(stmt);
        dbg("update query executed (%d)", rv);
        sqlite3_finalize(stmt);
 
-       if (rv != SQLITE_DONE) {
+       if (rv != SQLITE_DONE)
                return FALSE;
-       }
 
        return TRUE;
 }
 
-static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
-               GHashTable *out_param, int out_param_cnt)
+static gboolean _read_query_database_internal(Storage *strg, void *handle, const char *query, GHashTable *in_param,
+               gpointer out_param, int out_param_cnt, gboolean in_order)
 {
-       int rv = 0, index = 0, outter_index = 0;
-       sqlite3_stmtstmt = NULL;
-       char szQuery[5000+1];   // +1 is for NULL Termination Character '\0'
+       int rv = 0, local_index = 0, outter_index = 0;
+       sqlite3_stmt *stmt = NULL;
+       char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
 
        GHashTableIter iter;
        gpointer key, value;
@@ -133,44 +149,53 @@ static gboolean read_query_database(Storage *strg, void *handle, const char *que
        if (in_param) {
                g_hash_table_iter_init(&iter, in_param);
                while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-                       dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
+                       dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
 
-                       if (!value || g_strcmp0((const char*) value, "") == 0) {
+                       if (!value || g_strcmp0((const char *)value, "") == 0) {
                                dbg("bind null");
-                               rv = sqlite3_bind_null(stmt, atoi((const char*) key));
-                       }
-                       else {
+                               rv = sqlite3_bind_null(stmt, atoi((const char *)key));
+                       } else {
                                dbg("bind value");
-                               rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
+                               rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
                                                SQLITE_STATIC);
                        }
 
                        if (rv != SQLITE_OK) {
                                dbg("fail to bind data (%d)", rv);
-                               return FALSE;
+                               break;
                        }
                }
        }
 
+       if (rv != SQLITE_OK) {
+               sqlite3_finalize(stmt);
+               return FALSE;
+       }
+
        rv = sqlite3_step(stmt);
-       dbg("read query executed (%d)", rv);
+       dbg("read query executed (%d), in_order (%d)", rv, in_order);
 
        while (rv == SQLITE_ROW) {
-
-               char tmp_key_outter[10];
                GHashTable *out_param_data;
 
-               out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
+               out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
 
-               for (index = 0; index < out_param_cnt; index++) {
-                       char *tmp = NULL, tmp_key[10];
-                       tmp = (char *) sqlite3_column_text(stmt, index);
-                       snprintf(tmp_key, sizeof(tmp_key), "%d", index);
-                       g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup(tmp));
+               for (local_index = 0; local_index < out_param_cnt; local_index++) {
+                       char tmp_key[10];
+                       const unsigned char *tmp;
+                       tmp = sqlite3_column_text(stmt, local_index);
+                       snprintf(tmp_key, sizeof(tmp_key), "%d", local_index);
+                       g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup((const char *)tmp));
                }
 
-               snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
-               g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
+               if (in_order) {
+                       GSList **temp = out_param;
+                       *temp = g_slist_append(*temp, out_param_data);
+               } else {
+                       char tmp_key_outter[10];
+                       snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
+                       g_hash_table_insert((GHashTable*)out_param, g_strdup(tmp_key_outter), out_param_data);
+               }
                outter_index++;
                rv = sqlite3_step(stmt);
        }
@@ -179,11 +204,43 @@ static gboolean read_query_database(Storage *strg, void *handle, const char *que
        return TRUE;
 }
 
+static gboolean read_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param,
+               GHashTable **out_param, int out_param_cnt)
+{
+       GHashTable *out_hash_table;
+
+       if (out_param == NULL)
+               return FALSE;
+
+       out_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal,
+                       g_free, (GDestroyNotify)g_hash_table_destroy);
+
+       if (_read_query_database_internal(strg,
+                       handle, query, in_param, out_hash_table, out_param_cnt, FALSE) == FALSE) {
+               g_hash_table_destroy(out_hash_table);
+               return FALSE;
+       }
+
+       *out_param = out_hash_table;
+
+       return TRUE;
+}
+
+static gboolean read_query_database_in_order(Storage *strg, void *handle, const char *query, GHashTable *in_param,
+               GSList **out_param, int out_param_cnt)
+{
+       if (_read_query_database_internal(strg,
+                       handle, query, in_param, out_param, out_param_cnt, TRUE) == FALSE)
+               return FALSE;
+
+       return TRUE;
+}
+
 static gboolean insert_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
 {
        int rv = 0;
-       sqlite3_stmtstmt = NULL;
-       char szQuery[5000+1];   // +1 is for NULL Termination Character '\0'
+       sqlite3_stmt *stmt = NULL;
+       char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
 
        GHashTableIter iter;
        gpointer key, value;
@@ -198,41 +255,47 @@ static gboolean insert_query_database(Storage *strg, void *handle, const char *q
                return FALSE;
        }
 
-       g_hash_table_iter_init(&iter, in_param);
-       while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-               dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
+       if (in_param) {
+               g_hash_table_iter_init(&iter, in_param);
+               while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+                       dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
 
-               if (!value || g_strcmp0((const char*) value, "") == 0) {
-                       dbg("bind null");
-                       rv = sqlite3_bind_null(stmt, atoi((const char*) key));
-               }
-               else {
-                       dbg("bind value");
-                       rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
-                                       SQLITE_STATIC);
-               }
+                       if (!value || g_strcmp0((const char *)value, "") == 0) {
+                               dbg("bind null");
+                               rv = sqlite3_bind_null(stmt, atoi((const char *)key));
+                       } else {
+                               dbg("bind value");
+                               rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
+                                               SQLITE_STATIC);
+                       }
 
-               if (rv != SQLITE_OK) {
-                       dbg("fail to bind data (%d)", rv);
-                       return FALSE;
+                       if (rv != SQLITE_OK) {
+                               dbg("fail to bind data (%d)", rv);
+                               break;
+                       }
                }
        }
 
+       if (rv != SQLITE_OK) {
+               sqlite3_finalize(stmt);
+               return FALSE;
+       }
+
        rv = sqlite3_step(stmt);
        dbg("insert query executed (%d)", rv);
        sqlite3_finalize(stmt);
 
-       if (rv != SQLITE_DONE) {
+       if (rv != SQLITE_DONE)
                return FALSE;
-       }
+
        return TRUE;
 }
 
 static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param)
 {
        int rv = 0;
-       sqlite3_stmtstmt = NULL;
-       char szQuery[1000+1];   // +1 is for NULL Termination Character '\0'
+       sqlite3_stmt *stmt = NULL;
+       char szQuery[1000+1];   /* +1 is for NULL Termination Character '\0' */
 
        GHashTableIter iter;
        gpointer key, value;
@@ -247,42 +310,48 @@ static gboolean remove_query_database(Storage *strg, void *handle, const char *q
                return FALSE;
        }
 
-       g_hash_table_iter_init(&iter, in_param);
-       while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-               dbg("key(%s), value(%s)", (const char*)key, (const char*)value);
+       if (in_param) {
+               g_hash_table_iter_init(&iter, in_param);
+               while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+                       dbg("key(%s), value(%s)", (const char *)key, (const char *)value);
+
+                       if (!value || g_strcmp0((const char *)value, "") == 0) {
+                               dbg("bind null");
+                               rv = sqlite3_bind_null(stmt, atoi((const char *)key));
+                       } else {
+                               dbg("bind value");
+                               rv = sqlite3_bind_text(stmt, atoi((const char *)key), (const char *)value, strlen((const char *)value),
+                                               SQLITE_STATIC);
+                       }
 
-               if (!value || g_strcmp0((const char*) value, "") == 0) {
-                       dbg("bind null");
-                       rv = sqlite3_bind_null(stmt, atoi((const char*) key));
-               }
-               else {
-                       dbg("bind value");
-                       rv = sqlite3_bind_text(stmt, atoi((const char*) key), (const char*) value, strlen((const char*) value),
-                                       SQLITE_STATIC);
+                       if (rv != SQLITE_OK) {
+                               dbg("fail to bind data (%d)", rv);
+                               break;
+                       }
                }
+       }
 
-               if (rv != SQLITE_OK) {
-                       dbg("fail to bind data (%d)", rv);
-                       return FALSE;
-               }
+       if (rv != SQLITE_OK) {
+               sqlite3_finalize(stmt);
+               return FALSE;
        }
 
        rv = sqlite3_step(stmt);
        dbg("remove query executed (%d)", rv);
        sqlite3_finalize(stmt);
 
-       if (rv != SQLITE_DONE) {
+       if (rv != SQLITE_DONE)
                return FALSE;
-       }
 
        return TRUE;
 }
 
-struct storage_operations ops = {
+static struct storage_operations ops = {
        .create_handle = create_handle,
        .remove_handle = remove_handle,
        .update_query_database = update_query_database,
        .read_query_database = read_query_database,
+       .read_query_database_in_order = read_query_database_in_order,
        .insert_query_database = insert_query_database,
        .remove_query_database = remove_query_database,
 };
@@ -306,15 +375,26 @@ static gboolean on_init(TcorePlugin *p)
 
 static void on_unload(TcorePlugin *p)
 {
+       Storage *strg;
+
+       if (!p)
+               return;
+
        dbg("i'm unload!");
+
+       strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "database");
+       if (!strg)
+               return;
+
+       tcore_storage_free(strg);
        return;
+
 }
 
-struct tcore_plugin_define_desc plugin_define_desc =
-{
+EXPORT_API struct tcore_plugin_define_desc plugin_define_desc = {
        .name = "DATABASE",
-       .priority = TCORE_PLUGIN_PRIORITY_HIGH -1,
-       .version = 1,
+       .priority = TCORE_PLUGIN_PRIORITY_HIGH - 1,
+       .version = PLUGIN_VERSION,
        .load = on_load,
        .init = on_init,
        .unload = on_unload