]> saetta.ns0.it Git - libgtkform/commitdiff
Use of GString in GtkForm::get_sql.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 15 Nov 2013 07:54:39 +0000 (08:54 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 15 Nov 2013 07:54:39 +0000 (08:54 +0100)
libgtkform/form.c

index b496164e936c2b77c64fdbfef4274bbfb597fca3..02bf80a5842af0e413a0ecbb17ad6fed998b5c9e 100644 (file)
@@ -2015,11 +2015,11 @@ gchar
        GSList *fields;
        GtkFormField *field;
        gchar *sql = "";
-       gchar *fields_names = "";
-       gchar *values = "";
+       GString *fields_names;
+       GString *values;
        const gchar *value;
        gchar *where = "";
-       const gchar *field_name;
+       gchar *field_name;
 
        g_return_val_if_fail (IS_GTK_FORM (form), sql);
 
@@ -2027,6 +2027,9 @@ gchar
 
        fields = priv->fields;
 
+       fields_names = g_string_new ("");
+       values = g_string_new ("");
+
        while (fields != NULL)
                {
                        field = (GtkFormField *)fields->data;
@@ -2036,14 +2039,14 @@ gchar
                        if (field_name != NULL && g_strcmp0 (field_name, "") != 0)
                                {
                                        field_name = g_strconcat (&priv->quote, field_name, &priv->quote, NULL);
-                                       if (g_strstr_len (fields_names, -1, field_name) == NULL)
+                                       if (g_strstr_len (fields_names->str, -1, field_name) == NULL)
                                                {
                                                        switch (type)
                                                                {
                                                                        case GTK_FORM_SQL_SELECT:
                                                                                if (gtk_form_field_is_to_load (field))
                                                                                        {
-                                                                                               fields_names = g_strconcat (fields_names, field_name, ", ", NULL);
+                                                                                               g_string_append_printf (fields_names, "%s, ", field_name);
                                                                                        }
                                                                                break;
 
@@ -2052,6 +2055,7 @@ gchar
                                                                                        {
                                                                                                if (gtk_form_field_is_auto_increment (field))
                                                                                                        {
+                                                                                                               gchar *sql;
                                                                                                                GdaDataModel *dm;
                                                                                                                gchar *field_name;
                                                                                                                guint new_val;
@@ -2060,16 +2064,20 @@ gchar
 
                                                                                                                g_object_get (field, "field", &field_name, NULL);
 
-                                                                                                               new_val = 0;
-                                                                                                               dm = gdaex_query (priv->gdaex,
-                                                                                                                                                 g_strdup_printf ("SELECT COALESCE (MAX (%s), 0) FROM %s",
-                                                                                                                                                 field_name,
-                                                                                                                                                 priv->table));
+                                                                                                               new_val = 1;
+                                                                                                               sql = g_strdup_printf ("SELECT COALESCE (MAX (%s), 0) + 1 FROM %s",
+                                                                                                                                      field_name,
+                                                                                                                                      priv->table);
+                                                                                                               dm = gdaex_query (priv->gdaex, sql);
+                                                                                                               g_free (sql);
                                                                                                                if (dm != NULL && gda_data_model_get_n_rows (dm) > 0)
                                                                                                                        {
                                                                                                                                new_val = gdaex_data_model_get_value_integer_at (dm, 0, 0);
                                                                                                                        }
-                                                                                                               new_val++;
+                                                                                                               if (dm != NULL)
+                                                                                                                       {
+                                                                                                                               g_object_unref (dm);
+                                                                                                                       }
 
                                                                                                                gtk_form_widget_set_value_stringify (gtk_form_field_get_form_widget (field), g_strdup_printf ("%d", new_val));
                                                                                                        }
@@ -2077,8 +2085,8 @@ gchar
                                                                                                value = gtk_form_field_get_value_sql (field);
                                                                                                if (value != NULL)
                                                                                                        {
-                                                                                                               fields_names = g_strconcat (fields_names, field_name, ", ", NULL);
-                                                                                                               values = g_strconcat (values, value, ", ", NULL);
+                                                                                                               g_string_append_printf (fields_names, "%s, ", field_name);
+                                                                                                               g_string_append_printf (values, "%s, ", value);
                                                                                                        }
                                                                                        }
                                                                                break;
@@ -2091,7 +2099,7 @@ gchar
                                                                                                value = gtk_form_field_get_value_sql (field);
                                                                                                if (value != NULL)
                                                                                                        {
-                                                                                                               fields_names = g_strconcat (fields_names, field_name, " = ", value, ", ", NULL);
+                                                                                                               g_string_append_printf (fields_names, "%s = %s, ", field_name, value);
                                                                                                        }
                                                                                        }
                                                                                break;
@@ -2099,30 +2107,32 @@ gchar
                                                }
                                }
 
+                       g_free (field_name);
+
                        fields = g_slist_next (fields);
                }
 
-       if (g_str_has_suffix (fields_names, ", "))
+       if (g_str_has_suffix (fields_names->str, ", "))
                {
-                       fields_names[strlen (fields_names) - 2] = '\0';
+                       fields_names->str[strlen (fields_names->str) - 2] = '\0';
                }
-       if (g_str_has_suffix (values, ", "))
+       if (g_str_has_suffix (values->str, ", "))
                {
-                       values[strlen (values) - 2] = '\0';
+                       values->str[strlen (values->str) - 2] = '\0';
                }
 
        switch (type)
                {
                        case GTK_FORM_SQL_SELECT:
-                               sql = g_strconcat ("SELECT ", fields_names, " FROM ", priv->table, NULL);
+                               sql = g_strconcat ("SELECT ", fields_names->str, " FROM ", priv->table, NULL);
                                break;
 
                        case GTK_FORM_SQL_INSERT:
-                               sql = g_strconcat ("INSERT INTO ", priv->table, " (", fields_names, ") VALUES (", values, ")", NULL);
+                               sql = g_strconcat ("INSERT INTO ", priv->table, " (", fields_names->str, ") VALUES (", values->str, ")", NULL);
                                break;
 
                        case GTK_FORM_SQL_UPDATE:
-                               sql = g_strconcat ("UPDATE ", priv->table, " SET ", fields_names, NULL);
+                               sql = g_strconcat ("UPDATE ", priv->table, " SET ", fields_names->str, NULL);
                                break;
 
                        case GTK_FORM_SQL_DELETE:
@@ -2140,6 +2150,9 @@ gchar
                                }
                }
 
+       g_string_free (fields_names, TRUE);
+       g_string_free (values, TRUE);
+
        return sql;
 }