Update preference api source from tizen_2.3
authorJiwoong Im <jiwoong.im@samsung.com>
Mon, 9 Feb 2015 08:22:10 +0000 (17:22 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Mon, 16 Feb 2015 11:04:43 +0000 (20:04 +0900)
JIRA ticket : TC-2312

- add busy handler
- get data path from app_get_data_path()
- exec query through prepare -> bind -> step.

Change-Id: Ie286e1617c78733e2e8cfdd938e00f5c5e56890f
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
src/preference.c

index ad85562aef1bf245b9f65c47a39915512766bb27..f9751c250c11dac37694e740291cb85eaddb1b1e 100755 (executable)
 #define LOG_TAG "CAPI_APPFW_APPLICATION_PREFERENCE"
 #define DBG_MODE (1)
 
-static sqlite3 *pref_db = NULL;
-static bool is_update_hook_registered = false;
-static pref_changed_cb_node_t *head = NULL;
+static sqlite3 *pref_db;
+static bool is_update_hook_registered;
+static pref_changed_cb_node_t *head;
 
 static void _finish(void *data)
 {
-       if (pref_db != NULL)
-       {
+       if (pref_db != NULL) {
                sqlite3_close(pref_db);
                pref_db = NULL;
        }
 }
 
+static int _busy_handler(void *pData, int count)
+{
+       if (count < 5) {
+               LOGD("Busy Handler Called! : PID(%d) / CNT(%d)\n",
+                               getpid(), count+1);
+               usleep((count+1)*100000);
+               return 1;
+       } else {
+               LOGD("Busy Handler will be returned SQLITE_BUSY error : PID(%d)\n",
+                               getpid());
+               return 0;
+       }
+}
+
 static int _initialize(void)
 {
-       char data_path[TIZEN_PATH_MAX] = {0, };
-       char db_path[TIZEN_PATH_MAX] = {0, };
+       char *data_path;
+       char db_path[TIZEN_PATH_MAX];
        int ret;
        char *errmsg;
 
-       if (app_get_data_directory(data_path, sizeof(data_path)) == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to get data directory", PREFERENCE_ERROR_IO_ERROR);
+       data_path = app_get_data_path();
+       if (data_path == NULL) {
+               LOGE("IO_ERROR(0x%08x) : fail to get data directory",
+                               PREFERENCE_ERROR_IO_ERROR);
                return PREFERENCE_ERROR_IO_ERROR;
        }
        snprintf(db_path, sizeof(db_path), "%s/%s", data_path, PREF_DB_NAME);
+       free(data_path);
 
        ret = sqlite3_open(db_path, &pref_db);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to open db(%s)", PREFERENCE_ERROR_IO_ERROR, sqlite3_errmsg(pref_db));
-               pref_db = NULL;
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to open db(%s)",
+                               PREFERENCE_ERROR_IO_ERROR, sqlite3_errmsg(pref_db));
+               _finish(NULL);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       ret = sqlite3_exec(pref_db, "CREATE TABLE IF NOT EXISTS pref ( pref_key TEXT PRIMARY KEY, pref_type TEXT, pref_data TEXT)",
-                      NULL, NULL, &errmsg);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create db table(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
+       ret = sqlite3_busy_handler(pref_db, _busy_handler, NULL);
+       if (ret != SQLITE_OK) {
+               LOGW("IO_ERROR(0x%08x) : fail to register busy handler(%s)\n",
+                               PREFERENCE_ERROR_IO_ERROR, sqlite3_errmsg(pref_db));
+       }
+
+       ret = sqlite3_exec(pref_db,
+                       "CREATE TABLE IF NOT EXISTS pref ( pref_key TEXT PRIMARY KEY, pref_type TEXT, pref_data TEXT)",
+                       NULL, NULL, &errmsg);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to create db table(%s)",
+                               PREFERENCE_ERROR_IO_ERROR, errmsg);
                sqlite3_free(errmsg);
-               sqlite3_close(pref_db);
-               pref_db = NULL;
+               _finish(NULL);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
@@ -86,58 +107,98 @@ static int _initialize(void)
        return PREFERENCE_ERROR_NONE;
 }
 
-//static int _write_data(const char *key, preference_type_e type, const char *data)
+static int _prepare_and_bind_stmt(char *buf, const char *type,
+               const char *data, const char *key, sqlite3_stmt **stmt)
+{
+       int ret;
+
+       ret = sqlite3_prepare(pref_db, buf, -1, stmt, NULL);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to prepare query (%d/%s)",
+                               PREFERENCE_ERROR_IO_ERROR,
+                               sqlite3_extended_errcode(pref_db),
+                               sqlite3_errmsg(pref_db));
+               return PREFERENCE_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_text(*stmt, 1, type, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to bind(1) query (%d/%s)",
+                               PREFERENCE_ERROR_IO_ERROR,
+                               sqlite3_extended_errcode(pref_db),
+                               sqlite3_errmsg(pref_db));
+               sqlite3_finalize(*stmt);
+               return PREFERENCE_ERROR_IO_ERROR;
+       }
+       ret = sqlite3_bind_text(*stmt, 2, data, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to bind(2) query (%d/%s)",
+                               PREFERENCE_ERROR_IO_ERROR,
+                               sqlite3_extended_errcode(pref_db),
+                               sqlite3_errmsg(pref_db));
+               sqlite3_finalize(*stmt);
+               return PREFERENCE_ERROR_IO_ERROR;
+       }
+       ret = sqlite3_bind_text(*stmt, 3, key, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to bind(3) query (%d/%s)",
+                               PREFERENCE_ERROR_IO_ERROR,
+                               sqlite3_extended_errcode(pref_db),
+                               sqlite3_errmsg(pref_db));
+               sqlite3_finalize(*stmt);
+               return PREFERENCE_ERROR_IO_ERROR;
+       }
+
+       return PREFERENCE_ERROR_NONE;
+}
+
 static int _write_data(const char *key, const char *type, const char *data)
 {
        int ret;
-       char *buf;
-       char *errmsg;
        bool exist = false;
+       sqlite3_stmt *stmt;
+       char buf[BUF_LEN];
 
-       if (key == NULL || key[0] == '\0'  || data == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (key == NULL || key[0] == '\0' || data == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
-
        /* insert data or update data if data already exist */
        ret = preference_is_existing(key, &exist);
        if (ret != PREFERENCE_ERROR_NONE)
-       {
                return ret;
-       }
 
-       // to use sqlite3_update_hook, we have to use INSERT/UPDATE operation instead of REPLACE operation
        if (exist)
-       {
-               buf = sqlite3_mprintf("UPDATE %s SET %s='%s', %s='%s' WHERE %s='%s';",
-                                                               PREF_TBL_NAME, PREF_F_TYPE_NAME, type, PREF_F_DATA_NAME, data, PREF_F_KEY_NAME, key);
-       }
+               snprintf(buf, sizeof(buf), "UPDATE %s SET %s=?1, %s=?2 WHERE %s=?3;",
+                               PREF_TBL_NAME, PREF_F_TYPE_NAME,
+                               PREF_F_DATA_NAME, PREF_F_KEY_NAME);
        else
-       {
-               buf = sqlite3_mprintf("INSERT INTO %s (%s, %s, %s) values ('%q', '%q', '%q');",
-                                                               PREF_TBL_NAME, PREF_F_KEY_NAME, PREF_F_TYPE_NAME, PREF_F_DATA_NAME, key, type, data);
-       }
+               snprintf(buf, sizeof(buf),
+                               "INSERT INTO %s (%s, %s, %s) values (?3, ?1, ?2);",
+                               PREF_TBL_NAME, PREF_F_KEY_NAME,
+                               PREF_F_TYPE_NAME, PREF_F_DATA_NAME);
 
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
-               return PREFERENCE_ERROR_IO_ERROR;
-       }
+       ret = _prepare_and_bind_stmt(buf, type, data, key, &stmt);
 
-       ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
-       sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x): fail to write data(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
-               sqlite3_free(errmsg);
+       if (ret != PREFERENCE_ERROR_NONE)
+               return ret;
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               LOGE("IO_ERROR(0x%08x): fail to write data(%d/%s)",
+                       PREFERENCE_ERROR_IO_ERROR,
+                       sqlite3_extended_errcode(pref_db),
+                       sqlite3_errmsg(pref_db));
+               sqlite3_finalize(stmt);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
+       sqlite3_finalize(stmt);
+
        return PREFERENCE_ERROR_NONE;
 }
 
-//static int _read_data(const char *key, preference_type_e *type, char *data)
 static int _read_data(const char *key, char *type, char *data)
 {
        int ret;
@@ -147,48 +208,48 @@ static int _read_data(const char *key, char *type, char *data)
        int columns;
        char *errmsg;
 
-       if (key == NULL || key[0] == '\0'  || data == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (key == NULL || key[0] == '\0'  || data == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
-       if (pref_db == NULL)
-       {
-               if (_initialize() != PREFERENCE_ERROR_NONE)
-               {
-                       LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
+       if (pref_db == NULL) {
+               if (_initialize() != PREFERENCE_ERROR_NONE) {
+                       LOGE("IO_ERROR(0x%08x) : fail to initialize db",
+                                       PREFERENCE_ERROR_IO_ERROR);
                        return PREFERENCE_ERROR_IO_ERROR;
                }
        }
 
-       buf = sqlite3_mprintf("SELECT %s, %s, %s FROM %s WHERE %s='%q';",
-                                                       PREF_F_KEY_NAME, PREF_F_TYPE_NAME, PREF_F_DATA_NAME, PREF_TBL_NAME, PREF_F_KEY_NAME, key);
+       buf = sqlite3_mprintf("SELECT %s, %s, %s FROM %s WHERE %s=%Q;",
+                       PREF_F_KEY_NAME, PREF_F_TYPE_NAME, PREF_F_DATA_NAME,
+                       PREF_TBL_NAME, PREF_F_KEY_NAME, key);
 
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
+       if (buf == NULL) {
+               LOGE("IO_ERROR(0x%08x) : fail to create query string",
+                               PREFERENCE_ERROR_IO_ERROR);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
        ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
        sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to read data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to read data (%s)",
+                               PREFERENCE_ERROR_IO_ERROR, errmsg);
                sqlite3_free(errmsg);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       if (rows == 0)
-       {
-               LOGE("NO_KEY(0x%08x) : fail to find given key(%s)", PREFERENCE_ERROR_NO_KEY, key);
+       if (rows == 0) {
+               LOGE("NO_KEY(0x%08x) : fail to find given key(%s)",
+                               PREFERENCE_ERROR_NO_KEY, key);
                sqlite3_free_table(result);
                return PREFERENCE_ERROR_NO_KEY;
        }
 
-       snprintf(type, 2, "%s", result[4]);                     // get type value
-       snprintf(data, BUF_LEN, "%s", result[5]);                       // get data value
+       snprintf(type, 2, "%s", result[4]);
+       snprintf(data, BUF_LEN, "%s", result[5]);
 
        sqlite3_free_table(result);
 
@@ -211,16 +272,19 @@ int preference_get_int(const char *key, int *value)
        char data[BUF_LEN];
        int ret;
 
+       if (value == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
+               return PREFERENCE_ERROR_INVALID_PARAMETER;
+       }
+
        ret = _read_data(key, type, data);
-       if (ret == PREFERENCE_ERROR_NONE)
-       {
+       if (ret == PREFERENCE_ERROR_NONE) {
                if (atoi(type) == PREFERENCE_TYPE_INT)
-               {
                        *value = atoi(data);
-               }
-               else
-               {
-                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
+               else {
+                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)",
+                                       PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
                        return PREFERENCE_ERROR_INVALID_PARAMETER;
                }
        }
@@ -244,16 +308,19 @@ int preference_get_double(const char *key, double *value)
 
        int ret;
 
+       if (value == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
+               return PREFERENCE_ERROR_INVALID_PARAMETER;
+       }
+
        ret = _read_data(key, type, data);
-       if (ret == PREFERENCE_ERROR_NONE)
-       {
+       if (ret == PREFERENCE_ERROR_NONE) {
                if (atoi(type) == PREFERENCE_TYPE_DOUBLE)
-               {
                        *value = atof(data);
-               }
-               else
-               {
-                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
+               else {
+                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)",
+                                       PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
                        return PREFERENCE_ERROR_INVALID_PARAMETER;
                }
        }
@@ -266,9 +333,9 @@ int preference_set_string(const char *key, const char *value)
        char type[2];
 
        snprintf(type, 2, "%d", PREFERENCE_TYPE_STRING);
-       if (strlen(value) > (BUF_LEN-1))
-       {
-               LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
+       if (strlen(value) > (BUF_LEN-1)) {
+               LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
        return _write_data(key, type, value);
@@ -281,27 +348,24 @@ int preference_get_string(const char *key, char **value)
 
        int ret;
 
-       if (value == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (value == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
        ret = _read_data(key, type, data);
-       if (ret == PREFERENCE_ERROR_NONE)
-       {
-               if (atoi(type) == PREFERENCE_TYPE_STRING)
-               {
+       if (ret == PREFERENCE_ERROR_NONE) {
+               if (atoi(type) == PREFERENCE_TYPE_STRING) {
                        *value = strdup(data);
-                       if (value == NULL)
-                       {
-                               LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
+                       if (value == NULL) {
+                               LOGE("OUT_OF_MEMORY(0x%08x)",
+                                               PREFERENCE_ERROR_OUT_OF_MEMORY);
                                return PREFERENCE_ERROR_OUT_OF_MEMORY;
                        }
-               }
-               else
-               {
-                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
+               } else {
+                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)",
+                                       PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
                        return PREFERENCE_ERROR_INVALID_PARAMETER;
                }
        }
@@ -325,16 +389,19 @@ int preference_get_boolean(const char *key, bool *value)
 
        int ret;
 
+       if (value == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
+               return PREFERENCE_ERROR_INVALID_PARAMETER;
+       }
+
        ret = _read_data(key, type, data);
-       if (ret == PREFERENCE_ERROR_NONE)
-       {
+       if (ret == PREFERENCE_ERROR_NONE) {
                if (atoi(type) == PREFERENCE_TYPE_BOOLEAN)
-               {
                        *value = (bool)atoi(data);
-               }
-               else
-               {
-                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
+               else {
+                       LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)",
+                                       PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
                        return PREFERENCE_ERROR_INVALID_PARAMETER;
                }
        }
@@ -343,7 +410,7 @@ int preference_get_boolean(const char *key, bool *value)
 }
 
 
-// TODO: below operation is too heavy, let's find the light way to check.
+/* TODO: below operation is too heavy, let's find the light way to check. */
 int preference_is_existing(const char *key, bool *exist)
 {
        int ret;
@@ -353,69 +420,60 @@ int preference_is_existing(const char *key, bool *exist)
        int columns;
        char *errmsg;
 
-       if (key == NULL  || key[0] == '\0'  || exist == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (key == NULL  || key[0] == '\0'  || exist == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
-       if (pref_db == NULL)
-       {
-               if (_initialize() != PREFERENCE_ERROR_NONE)
-               {
-                       LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
+       if (pref_db == NULL) {
+               if (_initialize() != PREFERENCE_ERROR_NONE) {
+                       LOGE("IO_ERROR(0x%08x) : fail to initialize db",
+                                       PREFERENCE_ERROR_IO_ERROR);
                        return PREFERENCE_ERROR_IO_ERROR;
                }
        }
 
        /* check data is exist */
-       buf = sqlite3_mprintf("SELECT %s FROM %s WHERE %s='%q';", PREF_F_KEY_NAME, PREF_TBL_NAME, PREF_F_KEY_NAME, key);
+       buf = sqlite3_mprintf("SELECT %s FROM %s WHERE %s=%Q;",
+                       PREF_F_KEY_NAME, PREF_TBL_NAME, PREF_F_KEY_NAME, key);
 
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
+       if (buf == NULL) {
+               LOGE("IO_ERROR(0x%08x) : fail to create query string",
+                               PREFERENCE_ERROR_IO_ERROR);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
        ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
        sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to read data(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to read data(%s)",
+                               PREFERENCE_ERROR_IO_ERROR, errmsg);
                sqlite3_free(errmsg);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
        if (rows > 0)
-       {
                *exist = true;
-       }
        else
-       {
                *exist = false;
-       }
 
        sqlite3_free_table(result);
        return PREFERENCE_ERROR_NONE;
 }
 
-static pref_changed_cb_node_t_find_node(const char *key)
+static pref_changed_cb_node_t *_find_node(const char *key)
 {
        pref_changed_cb_node_t *tmp_node;
 
-       if (key == NULL || key[0] == '\0' )
-       {
+       if (key == NULL || key[0] == '\0')
                return NULL;
-       }
 
        tmp_node = head;
 
-       while (tmp_node)
-       {
+       while (tmp_node) {
                if (strcmp(tmp_node->key, key) == 0)
-               {
                        break;
-               }
                tmp_node = tmp_node->next;
        }
 
@@ -427,35 +485,35 @@ static int _add_node(const char *key, preference_changed_cb cb, void *user_data)
 {
        pref_changed_cb_node_t *tmp_node;
 
-       if (key == NULL  || key[0] == '\0'  || cb == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (key == NULL || key[0] == '\0' || cb == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
        tmp_node = _find_node(key);
 
-       if (tmp_node != NULL)
-       {
+       if (tmp_node != NULL) {
                tmp_node->cb = cb;
                tmp_node->user_data = user_data;
-       }
-       else
-       {
-               tmp_node = (pref_changed_cb_node_t*)malloc(sizeof(pref_changed_cb_node_t));
-               if (tmp_node == NULL)
-               {
-                       LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
+       } else {
+               tmp_node =
+                       (pref_changed_cb_node_t *)malloc(sizeof(pref_changed_cb_node_t));
+               if (tmp_node == NULL) {
+                       LOGE("OUT_OF_MEMORY(0x%08x)",
+                                       PREFERENCE_ERROR_OUT_OF_MEMORY);
                        return PREFERENCE_ERROR_OUT_OF_MEMORY;
                }
 
                tmp_node->key = strdup(key);
-               if (tmp_node->key == NULL)
-               {
+               if (tmp_node->key == NULL) {
                        free(tmp_node);
                        LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
                        return PREFERENCE_ERROR_OUT_OF_MEMORY;
                }
+
+               if (head != NULL)
+                       head->prev = tmp_node;
                tmp_node->cb = cb;
                tmp_node->user_data = user_data;
                tmp_node->prev = NULL;
@@ -470,37 +528,27 @@ static int _remove_node(const char *key)
 {
        pref_changed_cb_node_t *tmp_node;
 
-       if (key == NULL || key[0] == '\0' )
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
+       if (key == NULL || key[0] == '\0') {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
                return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
        tmp_node = _find_node(key);
 
        if (tmp_node == NULL)
-       {
                return PREFERENCE_ERROR_NONE;
-       }
 
        if (tmp_node->prev != NULL)
-       {
                tmp_node->prev->next = tmp_node->next;
-       }
        else
-       {
                head = tmp_node->next;
-       }
 
        if (tmp_node->next != NULL)
-       {
                tmp_node->next->prev = tmp_node->prev;
-       }
 
        if (tmp_node->key)
-       {
                free(tmp_node->key);
-       }
 
        free(tmp_node);
 
@@ -512,22 +560,20 @@ static void _remove_all_node(void)
 {
        pref_changed_cb_node_t *tmp_node;
 
-       while (head)
-       {
+       while (head) {
                tmp_node = head;
                head = tmp_node->next;
 
                if (tmp_node->key)
-               {
                        free(tmp_node->key);
-               }
 
                free(tmp_node);
        }
 }
 
 
-static void _update_cb(void *data, int action, char const *db_name, char const *table_name, sqlite_int64 rowid)
+static void _update_cb(void *data, int action, char const *db_name,
+               char const *table_name, sqlite_int64 rowid)
 {
        int ret;
        char *buf;
@@ -537,34 +583,28 @@ static void _update_cb(void *data, int action, char const *db_name, char const *
        char *errmsg;
        pref_changed_cb_node_t *tmp_node;
 
-       // skip INSERT/DELETE event
        if (action != SQLITE_UPDATE)
-       {
                return;
-       }
 
-       if (strcmp(table_name, PREF_TBL_NAME) != 0)
-       {
-               LOGI("given table name (%s) is not same", table_name);
+       if (strcmp(table_name, PREF_TBL_NAME) != 0) {
+               SECURE_LOGE("given table name (%s) is not same", table_name);
                return;
        }
 
-       buf = sqlite3_mprintf("SELECT %s FROM %s WHERE rowid='%lld';", PREF_F_KEY_NAME, PREF_TBL_NAME, rowid);
+       buf = sqlite3_mprintf("SELECT %s FROM %s WHERE rowid='%lld';",
+                       PREF_F_KEY_NAME, PREF_TBL_NAME, rowid);
        if (buf == NULL)
-       {
                return;
-       }
+
        ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
        sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
+       if (ret != SQLITE_OK) {
                LOGI("fail to read data(%s)", errmsg);
                sqlite3_free(errmsg);
                return;
        }
 
-       if (rows == 0)
-       {
+       if (rows == 0) {
                sqlite3_free_table(result);
                return;
        }
@@ -572,9 +612,7 @@ static void _update_cb(void *data, int action, char const *db_name, char const *
        tmp_node = _find_node(result[1]);
 
        if (tmp_node != NULL && tmp_node->cb != NULL)
-       {
                tmp_node->cb(result[1], tmp_node->user_data);
-       }
 
        sqlite3_free_table(result);
 }
@@ -583,41 +621,51 @@ static void _update_cb(void *data, int action, char const *db_name, char const *
 int preference_remove(const char *key)
 {
        int ret;
-       char *buf;
-       char *errmsg;
+       char buf[BUF_LEN];
        bool exist;
+       sqlite3_stmt *stmt;
 
        ret = preference_is_existing(key, &exist);
        if (ret != PREFERENCE_ERROR_NONE)
-       {
                return ret;
-       }
 
        if (!exist)
-       {
-               return PREFERENCE_ERROR_NONE;
-       }
+               return PREFERENCE_ERROR_NO_KEY;
 
-       /* insert data or update data if data already exist */
-       buf = sqlite3_mprintf("DELETE FROM %s WHERE %s = '%s';",
-                                                       PREF_TBL_NAME, PREF_F_KEY_NAME, key);
+       snprintf(buf, sizeof(buf), "DELETE FROM %s WHERE %s = ?",
+                       PREF_TBL_NAME, PREF_F_KEY_NAME);
 
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
+       ret = sqlite3_prepare(pref_db, buf, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to prepare query (%d/%s)",
+                       PREFERENCE_ERROR_IO_ERROR,
+                       sqlite3_extended_errcode(pref_db),
+                       sqlite3_errmsg(pref_db));
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
-       sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to delete data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
-               sqlite3_free(errmsg);
+       ret = sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to bind(1) query (%d/%s)",
+                       PREFERENCE_ERROR_IO_ERROR,
+                       sqlite3_extended_errcode(pref_db),
+                       sqlite3_errmsg(pref_db));
+               sqlite3_finalize(stmt);
+               return PREFERENCE_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               LOGE("IO_ERROR(0x%08x): fail to delete data(%d/%s)",
+                       PREFERENCE_ERROR_IO_ERROR,
+                       sqlite3_extended_errcode(pref_db),
+                       sqlite3_errmsg(pref_db));
+               sqlite3_finalize(stmt);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       // if exist, remove changed cb
+       sqlite3_finalize(stmt);
+
         _remove_node(key);
 
        return PREFERENCE_ERROR_NONE;
@@ -630,58 +678,54 @@ int preference_remove_all(void)
        char *buf;
        char *errmsg;
 
-       if (pref_db == NULL)
-       {
-               if (_initialize() != PREFERENCE_ERROR_NONE)
-               {
-                       LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
+       if (pref_db == NULL) {
+               if (_initialize() != PREFERENCE_ERROR_NONE) {
+                       LOGE("IO_ERROR(0x%08x) : fail to initialize db",
+                                       PREFERENCE_ERROR_IO_ERROR);
                        return PREFERENCE_ERROR_IO_ERROR;
                }
        }
 
        /* insert data or update data if data already exist */
        buf = sqlite3_mprintf("DELETE FROM %s;", PREF_TBL_NAME);
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
+       if (buf == NULL) {
+               LOGE("IO_ERROR(0x%08x) : fail to create query string",
+                               PREFERENCE_ERROR_IO_ERROR);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
        ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
        sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to delete data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to delete data (%s)",
+                               PREFERENCE_ERROR_IO_ERROR, errmsg);
                sqlite3_free(errmsg);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       // if exist, remove changed cb
        _remove_all_node();
 
        return PREFERENCE_ERROR_NONE;
 }
 
 
-int preference_set_changed_cb(const char *key, preference_changed_cb callback, void *user_data)
+int preference_set_changed_cb(const char *key,
+               preference_changed_cb callback, void *user_data)
 {
        int ret;
        bool exist;
 
        ret = preference_is_existing(key, &exist);
        if (ret != PREFERENCE_ERROR_NONE)
-       {
                return ret;
-       }
 
-       if (!exist)
-       {
-               LOGE("NO_KEY(0x%08x) : fail to find given key(%s)", PREFERENCE_ERROR_NO_KEY, key);
+       if (!exist) {
+               LOGE("NO_KEY(0x%08x) : fail to find given key(%s)",
+                               PREFERENCE_ERROR_NO_KEY, key);
                return PREFERENCE_ERROR_NO_KEY;
        }
 
-       if (!is_update_hook_registered)
-       {
+       if (!is_update_hook_registered) {
                sqlite3_update_hook(pref_db, _update_cb, NULL);
                is_update_hook_registered = true;
        }
@@ -691,12 +735,17 @@ int preference_set_changed_cb(const char *key, preference_changed_cb callback, v
 
 int preference_unset_changed_cb(const char *key)
 {
-       if (pref_db == NULL)
-       {
-               if (_initialize() != PREFERENCE_ERROR_NONE)
-               {
-                       return PREFERENCE_ERROR_IO_ERROR;
-               }
+       int ret;
+       bool exist;
+
+       ret = preference_is_existing(key, &exist);
+       if (ret != PREFERENCE_ERROR_NONE)
+               return ret;
+
+       if (!exist) {
+               LOGE("NO_KEY(0x%08x) : fail to find given key(%s)",
+                               PREFERENCE_ERROR_NO_KEY, key);
+               return PREFERENCE_ERROR_NO_KEY;
        }
 
        return _remove_node(key);
@@ -712,47 +761,43 @@ int preference_foreach_item(preference_item_cb callback, void *user_data)
        char *errmsg;
        int i;
 
-       if (pref_db == NULL)
-       {
-               if (_initialize() != PREFERENCE_ERROR_NONE)
-               {
-                       LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
-                       return PREFERENCE_ERROR_IO_ERROR;
-               }
+       if (callback == NULL) {
+               LOGE("INVALID_PARAMETER(0x%08x)",
+                               PREFERENCE_ERROR_INVALID_PARAMETER);
+               return PREFERENCE_ERROR_INVALID_PARAMETER;
        }
 
-       if (callback == NULL)
-       {
-               LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
-               return PREFERENCE_ERROR_INVALID_PARAMETER;
+       if (pref_db == NULL) {
+               if (_initialize() != PREFERENCE_ERROR_NONE) {
+                       LOGE("IO_ERROR(0x%08x) : fail to initialize db",
+                                       PREFERENCE_ERROR_IO_ERROR);
+                       return PREFERENCE_ERROR_IO_ERROR;
+               }
        }
 
-       buf = sqlite3_mprintf("SELECT %s FROM %s;", PREF_F_KEY_NAME, PREF_TBL_NAME);
-       if (buf == NULL)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
+       buf = sqlite3_mprintf("SELECT %s FROM %s;",
+                       PREF_F_KEY_NAME, PREF_TBL_NAME);
+       if (buf == NULL) {
+               LOGE("IO_ERROR(0x%08x) : fail to create query string",
+                               PREFERENCE_ERROR_IO_ERROR);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
        ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
        sqlite3_free(buf);
-       if (ret != SQLITE_OK)
-       {
-               LOGE("IO_ERROR(0x%08x) : fail to read data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
+       if (ret != SQLITE_OK) {
+               LOGE("IO_ERROR(0x%08x) : fail to read data (%s)",
+                               PREFERENCE_ERROR_IO_ERROR, errmsg);
                sqlite3_free(errmsg);
                return PREFERENCE_ERROR_IO_ERROR;
        }
 
-       for (i = 1; i <= rows; i++)
-       {
+       for (i = 1; i <= rows; i++) {
                if (callback(result[i], user_data) != true)
-               {
                        break;
-               }
        }
 
        sqlite3_free_table(result);
 
        return PREFERENCE_ERROR_NONE;
 }
-