From 1cb49b4c965fef704499cd720e7e65312681ab07 Mon Sep 17 00:00:00 2001 From: sinikang Date: Wed, 5 Sep 2018 11:42:55 +0900 Subject: [PATCH] Modify a duplicate code to use a function Change-Id: I4b35c70e148164850c496a0e3c13f31cedfac88c --- packaging/tel-plugin-database.spec | 2 +- src/database_main.c | 131 ++++++++++++++----------------------- 2 files changed, 50 insertions(+), 83 deletions(-) diff --git a/packaging/tel-plugin-database.spec b/packaging/tel-plugin-database.spec index 3efc8a3..5dd2721 100644 --- a/packaging/tel-plugin-database.spec +++ b/packaging/tel-plugin-database.spec @@ -1,6 +1,6 @@ %define major 0 %define minor 1 -%define patchlevel 43 +%define patchlevel 44 Name: tel-plugin-database Version: %{major}.%{minor}.%{patchlevel} diff --git a/src/database_main.c b/src/database_main.c index 6573d1c..707358d 100644 --- a/src/database_main.c +++ b/src/database_main.c @@ -36,40 +36,7 @@ #define PLUGIN_VERSION 1 #endif -static void *create_handle(Storage *strg, const char *path) -{ - int rv = 0; - sqlite3 *handle = NULL; - - rv = db_util_open(path, &handle, 0); - if (rv != SQLITE_OK) { - err("fail to connect database err(%d)", rv); - return NULL; - } - - dbg("connected to %s", path); - return handle; -} - -static gboolean remove_handle(Storage *strg, void *handle) -{ - int rv = 0; - - if (!handle) - return FALSE; - - 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; -} - -static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param) +static gboolean __update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param) { int rv = 0; sqlite3_stmt *stmt = NULL; @@ -116,7 +83,7 @@ static gboolean update_query_database(Storage *strg, void *handle, const char *q } rv = sqlite3_step(stmt); - dbg("update query executed (%d)", rv); + dbg("query executed (%d)", rv); sqlite3_finalize(stmt); if (rv != SQLITE_DONE) @@ -125,6 +92,50 @@ static gboolean update_query_database(Storage *strg, void *handle, const char *q return TRUE; } +static void *create_handle(Storage *strg, const char *path) +{ + int rv = 0; + sqlite3 *handle = NULL; + + rv = db_util_open(path, &handle, 0); + if (rv != SQLITE_OK) { + err("fail to connect database err(%d)", rv); + return NULL; + } + + dbg("connected to %s", path); + return handle; +} + +static gboolean remove_handle(Storage *strg, void *handle) +{ + int rv = 0; + + if (!handle) + return FALSE; + + 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; +} + +static gboolean update_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param) +{ + gboolean ret = TRUE; + + dbg("update query"); + + ret = __update_query_database(strg, handle, query, in_param); + + return ret; +} + 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) { @@ -293,57 +304,13 @@ static gboolean insert_query_database(Storage *strg, void *handle, const char *q static gboolean remove_query_database(Storage *strg, void *handle, const char *query, GHashTable *in_param) { - int rv = 0; - sqlite3_stmt *stmt = NULL; - char szQuery[1000+1]; /* +1 is for NULL Termination Character '\0' */ + gboolean ret = TRUE; - GHashTableIter iter; - gpointer key, value; dbg("remove query"); - memset(szQuery, '\0', 1001); - strncpy(szQuery, query, 1000); - - rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL); - if (rv != SQLITE_OK) { - err("fail to connect to table (%d)", rv); - return FALSE; - } - - 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 (rv != SQLITE_OK) { - dbg("fail to bind data (%d)", rv); - break; - } - } - } - - if (rv != SQLITE_OK) { - sqlite3_finalize(stmt); - return FALSE; - } + ret = __update_query_database(strg, handle, query, in_param); - rv = sqlite3_step(stmt); - dbg("remove query executed (%d)", rv); - sqlite3_finalize(stmt); - - if (rv != SQLITE_DONE) - return FALSE; - - return TRUE; + return ret; } static struct storage_operations ops = { -- 2.7.4