Improve table creation function 20/222520/2 accepted/tizen/unified/20200117.120757 submit/tizen/20200117.014021
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 16 Jan 2020 00:16:59 +0000 (09:16 +0900)
committerhj kim <backto.kim@samsung.com>
Fri, 17 Jan 2020 01:44:16 +0000 (01:44 +0000)
Use Gstring instead

Change-Id: I8e13f40b91e428fb58fa0daedc92e599da9ffbbf
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-db-utils.c

index 865609b..37e87b9 100755 (executable)
@@ -245,125 +245,113 @@ int _media_svc_make_table_query(const char *table_name, media_svc_table_slist_e
        table_info_s *tb = NULL;
        column_info_s *col_ptr = NULL;
        char *sql = NULL;
-       char table_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
-       char index_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
-       char trigger_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
-       char table_query_sub[1024] = {0, };
-       char temp[1024] = {0 ,};
-       bool flag = false;
-       bool sub_flag = false;
+       GString *table_query = g_string_new(NULL);
+       GString *index_query = g_string_new(NULL);
+       GString *trigger_query = g_string_new(NULL);
+       GString *unique_query = g_string_new(NULL);
        int len = 0;
        int i = 0;
 
+       if (!table_query || !index_query || !trigger_query || !unique_query) {
+               media_svc_error("g_string_new failed");
+               ret = MS_MEDIA_ERR_INTERNAL;
+               goto ERROR;
+       }
+
        tb = g_hash_table_lookup(table, table_name);
        if (tb == NULL) {
                media_svc_debug("lookup fail.. table name [%s] ", table_name);
-               tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_MEDIA);
+               ret = MS_MEDIA_ERR_INTERNAL;
+               goto ERROR;
        }
 
        len = g_slist_length(column_list[list]);
-
        if (len == 0) {
                media_svc_error("Invalid column");
-               return MS_MEDIA_ERR_INTERNAL;
+               ret = MS_MEDIA_ERR_INTERNAL;
+               goto ERROR;
        }
 
        for (i = 1; i < len; i++) {
                col_ptr = g_slist_nth_data(column_list[list], i);
                /*create table */
                if (col_ptr->has_option) {
-                       if (flag == true) {
-                               snprintf(temp, sizeof(temp), ", %s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
-                       } else {
-                               snprintf(temp, sizeof(temp), "%s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
-                               flag = true;
-                       }
-                       SAFE_STRLCAT(table_query, temp, sizeof(table_query));
+                       if (table_query->len != 0)
+                               g_string_append_printf(table_query, ", %s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
+                       else
+                               g_string_append_printf(table_query, "%s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
                } else {
-                       if (flag == true) {
-                               snprintf(temp, sizeof(temp), ", %s %s", col_ptr->name, col_ptr->type);
-                       } else {
-                               snprintf(temp, sizeof(temp), "%s %s", col_ptr->name, col_ptr->type);
-                               flag = true;
-                       }
-                       SAFE_STRLCAT(table_query, temp, sizeof(table_query));
+                       if (table_query->len != 0)
+                               g_string_append_printf(table_query, ", %s %s", col_ptr->name, col_ptr->type);
+                       else
+                               g_string_append_printf(table_query, "%s %s", col_ptr->name, col_ptr->type);
                }
-               memset(temp, 0, sizeof(temp));
 
                /*unique */
                if (col_ptr->is_unique) {
-                       if (sub_flag == true) {
-                               snprintf(temp, sizeof(temp), ", %s", col_ptr->name);
-                       } else {
-                               snprintf(temp, sizeof(temp), "%s", col_ptr->name);
-                               sub_flag = true;
-                       }
-                       SAFE_STRLCAT(table_query_sub, temp, sizeof(table_query_sub));
+                       if (unique_query->len != 0)
+                               g_string_append_printf(unique_query, ", %s", col_ptr->name);
+                       else
+                               g_string_append_printf(unique_query, "%s", col_ptr->name);
                }
-               memset(temp, 0, sizeof(temp));
 
                /*create index */
-               if (col_ptr->is_index) {
-                       snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
-                       SAFE_STRLCAT(index_query, temp, sizeof(index_query));
-               }
-               memset(temp, 0, sizeof(temp));
+               if (col_ptr->is_index)
+                       g_string_append_printf(index_query, MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
 
                /*create trigger */
                if (col_ptr->is_trigger) {
                        if (STRING_VALID(tb->trigger_name)) {
                                if (strncmp(table_name, MEDIA_SVC_DB_TABLE_ALBUM, strlen(MEDIA_SVC_DB_TABLE_ALBUM)) == 0)
-                                       snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_TRIGGER_WITH_COUNT, tb->trigger_name, tb->event_table, tb->action_table, tb->event_table, col_ptr->name, col_ptr->name, col_ptr->name, col_ptr->name);
+                                       g_string_append_printf(trigger_query, MEDIA_SVC_DB_QUERY_TRIGGER_WITH_COUNT, tb->trigger_name, tb->event_table, tb->action_table, tb->event_table, col_ptr->name, col_ptr->name, col_ptr->name, col_ptr->name);
                                else
-                                       snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_TRIGGER, tb->trigger_name, tb->event_table, tb->action_table, col_ptr->name, col_ptr->name);
-
-                               SAFE_STRLCAT(trigger_query, temp, sizeof(trigger_query));
+                                       g_string_append_printf(trigger_query, MEDIA_SVC_DB_QUERY_TRIGGER, tb->trigger_name, tb->event_table, tb->action_table, col_ptr->name, col_ptr->name);
                        } else {
                                media_svc_error("invalid trigger name");
                        }
                }
-               memset(temp, 0, sizeof(temp));
        }
 
        /*send queries */
-       if (sub_flag == true) {
-               sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE_WITH_UNIQUE, table_name, table_query, table_query_sub);
-               ret = _media_svc_sql_query(sql, uid);
-               SQLITE3_SAFE_FREE(sql);
-               memset(table_query, 0, sizeof(table_query));
-               memset(table_query_sub, 0, sizeof(table_query_sub));
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-       } else {
-               sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE, table_name, table_query);
-               ret = _media_svc_sql_query(sql, uid);
-               SQLITE3_SAFE_FREE(sql);
-               memset(table_query, 0, sizeof(table_query));
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-       }
+       if (unique_query->len > 0)
+               sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE_WITH_UNIQUE, table_name, table_query->str, unique_query->str);
+       else
+               sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE, table_name, table_query->str);
 
-       if (STRING_VALID(index_query)) {
-               ret = _media_svc_sql_query(index_query, uid);
-               memset(index_query, 0, sizeof(index_query));
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
+       ret = _media_svc_sql_query(sql, uid);
+       SQLITE3_SAFE_FREE(sql);
+       if (ret != MS_MEDIA_ERR_NONE)
+               goto ERROR;
+
+       if (index_query->len > 0) {
+               ret = _media_svc_sql_query(index_query->str, uid);
+               if (ret != MS_MEDIA_ERR_NONE)
+                       goto ERROR;
        }
 
-       if (STRING_VALID(trigger_query)) {
-               ret = _media_svc_sql_query(trigger_query, uid);
-               memset(trigger_query, 0, sizeof(trigger_query));
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
+       if (trigger_query->len > 0) {
+               ret = _media_svc_sql_query(trigger_query->str, uid);
+               if (ret != MS_MEDIA_ERR_NONE)
+                       goto ERROR;
        }
 
        /*create view */
-       if (strncmp(table_name, MEDIA_SVC_DB_TABLE_PLAYLIST, strlen(MEDIA_SVC_DB_TABLE_PLAYLIST)) == 0) {
+       if (strncmp(table_name, MEDIA_SVC_DB_TABLE_PLAYLIST, strlen(MEDIA_SVC_DB_TABLE_PLAYLIST)) == 0)
                ret = __create_playlist_view(uid);
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-
-       } else if (strncmp(table_name, MEDIA_SVC_DB_TABLE_TAG, strlen(MEDIA_SVC_DB_TABLE_TAG)) == 0) {
+       else if (strncmp(table_name, MEDIA_SVC_DB_TABLE_TAG, strlen(MEDIA_SVC_DB_TABLE_TAG)) == 0)
                ret = __create_tag_view(uid);
-               media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-       }
 
-       return MS_MEDIA_ERR_NONE;
+ERROR:
+       if (index_query)
+               g_string_free(index_query, TRUE);
+       if (trigger_query)
+               g_string_free(trigger_query, TRUE);
+       if (unique_query)
+               g_string_free(unique_query, TRUE);
+       if (table_query)
+               g_string_free(table_query, TRUE);
+
+       return ret;
 }
 
 static int __media_svc_upgrade_table_query(sqlite3 *db_handle, const char *table_name, media_svc_table_slist_e list, uid_t uid)