From: Andrea Zagli Date: Sat, 16 Nov 2013 16:13:46 +0000 (+0100) Subject: Multifields key (closes #797). X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=e93918ddf405712dcaae17cc5007fdb310efbbd8;p=libgtkform Multifields key (closes #797). --- diff --git a/.anjuta/default.profile b/.anjuta/default.profile new file mode 100644 index 0000000..054294f --- /dev/null +++ b/.anjuta/default.profile @@ -0,0 +1,8 @@ + + + + + + diff --git a/.anjuta_sym_db.db b/.anjuta_sym_db.db new file mode 100644 index 0000000..99d8107 Binary files /dev/null and b/.anjuta_sym_db.db differ diff --git a/libgtkform.anjuta b/libgtkform.anjuta new file mode 100644 index 0000000..793b25e --- /dev/null +++ b/libgtkform.anjuta @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libgtkform/form.c b/libgtkform/form.c index e35669d..9ed93f0 100644 --- a/libgtkform/form.c +++ b/libgtkform/form.c @@ -2056,17 +2056,52 @@ gchar if (gtk_form_field_is_auto_increment (field)) { gchar *field_name; + GString *key_where; guint new_val; g_return_val_if_fail (IS_GDAEX (priv->gdaex), NULL); - g_object_get (field, "field", &field_name, NULL); + where = NULL; + if (IS_GTK_FORM_KEY (priv->key) + && gtk_form_key_field_is_key (priv->key, field)) + { + /* it depends on other key fields value */ + GSList *key_fields; + GtkFormField *kffield; + + key_fields = gtk_form_key_get_fields (priv->key); + if (key_fields != NULL) + { + key_where = g_string_new (""); + } + while (key_fields) + { + kffield = (GtkFormField *)key_fields->data; + if (g_strcmp0 (gtk_form_field_get_field_name (field), + gtk_form_field_get_field_name (kffield)) != 0) + { + g_string_append (key_where, + gtk_form_key_get_field_sql (priv->key, kffield)); + } + + key_fields = g_slist_next (key_fields); + } + } + + if (g_strcmp0 (key_where->str, "") != 0) + { + g_string_erase (key_where, 0, 5); + } + + g_object_get (field, "field", &field_name, key_where->str); new_val = gdaex_get_new_id (priv->gdaex, priv->table, field_name, NULL); gtk_form_widget_set_value_stringify (gtk_form_field_get_form_widget (field), g_strdup_printf ("%d", new_val)); + + g_string_free (key_where, TRUE); } - + value = gtk_form_field_get_value_sql (field); if (value != NULL) { @@ -2078,8 +2113,8 @@ gchar case GTK_FORM_SQL_UPDATE: if (gtk_form_field_is_to_save (field) - && (!IS_GTK_FORM_KEY (priv->key) || - !gtk_form_key_field_is_key (priv->key, field))) + && (!IS_GTK_FORM_KEY (priv->key) + || !gtk_form_key_field_is_key (priv->key, field))) { value = gtk_form_field_get_value_sql (field); if (value != NULL) @@ -2133,6 +2168,10 @@ gchar { sql = g_strconcat (sql, where, NULL); } + if (where != NULL) + { + g_free (where); + } } g_string_free (fields_names, TRUE); diff --git a/libgtkform/key.c b/libgtkform/key.c index bdd8332..a10eafb 100644 --- a/libgtkform/key.c +++ b/libgtkform/key.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2013 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -72,17 +72,14 @@ GtkFormKey gboolean gtk_form_key_add_field (GtkFormKey *gtk_form_key, GtkFormField *field) { - gboolean ret = FALSE; - - GtkFormKeyPrivate *priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); + GtkFormKeyPrivate *priv; g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE); + priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); priv->fields = g_slist_append (priv->fields, field); - ret = TRUE; - - return ret; + return TRUE; } /** @@ -94,17 +91,14 @@ gtk_form_key_add_field (GtkFormKey *gtk_form_key, GtkFormField *field) gboolean gtk_form_key_remove_field (GtkFormKey *gtk_form_key, GtkFormField *field) { - gboolean ret = FALSE; - - GtkFormKeyPrivate *priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); + GtkFormKeyPrivate *priv; g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE); + priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); priv->fields = g_slist_remove (priv->fields, field); - ret = TRUE; - - return ret; + return TRUE; } /** @@ -117,6 +111,8 @@ gtk_form_key_add_fields (GtkFormKey *gtk_form_key, ...) { GtkFormField *field; + g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), FALSE); + va_list vargs; va_start (vargs, gtk_form_key); @@ -127,6 +123,25 @@ gtk_form_key_add_fields (GtkFormKey *gtk_form_key, ...) } va_end (vargs); + + return TRUE; +} + +/** + * gtk_form_key_get_fields: + * @gtk_form_key: a #GtkFormKey object. + * + */ +GSList +*gtk_form_key_get_fields (GtkFormKey *gtk_form_key) +{ + GtkFormKeyPrivate *priv; + + g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), NULL); + + priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); + + return priv->fields; } /** @@ -177,6 +192,33 @@ gtk_form_key_check (GtkFormKey *gtk_form_key, GtkFormWidget **form_widget) return ret; } +/** + * gtk_form_key_get_field_sql: + * @gtk_form_key: + * @gtk_form_field: + * + */ +gchar +*gtk_form_key_get_field_sql (GtkFormKey *gtk_form_key, GtkFormField *gtk_form_field) +{ + gchar *ret; + + gchar *field_name; + + g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), ""); + g_return_val_if_fail (IS_GTK_FORM_FIELD (gtk_form_field), ""); + + g_object_get (G_OBJECT (gtk_form_field), + "field", &field_name, + NULL); + + ret = g_strdup_printf (" AND %s = %s", + field_name, + gtk_form_field_get_value_sql (gtk_form_field)); + + return ret; +} + /** * gtk_form_key_get_sql: * @gtk_form_key: a #GtkFormKey object. @@ -186,40 +228,34 @@ gchar *gtk_form_key_get_sql (GtkFormKey *gtk_form_key) { GtkFormField *field; - gchar *sql = ""; - gchar *field_name; + GString *sql; GtkFormKeyPrivate *priv; GSList *fields; - g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), sql); + g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), ""); priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key); fields = priv->fields; + sql = g_string_new (""); while (fields != NULL) { field = (GtkFormField *)fields->data; - g_object_get (G_OBJECT (field), - "field", &field_name, - NULL); - - sql = g_strconcat (sql, " AND ", field_name, " = ", - gtk_form_field_get_value_sql (field), NULL); + g_string_append (sql, gtk_form_key_get_field_sql (gtk_form_key, field)); fields = g_slist_next (fields); } - if (strcmp (sql, "") != 0) + if (g_strcmp0 (sql->str, "") != 0) { - sql = g_strconcat (" WHERE ", g_strdup (sql + 5), NULL); + g_string_erase (sql, 0, 5); + g_string_printf (sql, " WHERE %s", sql->str); } - g_free (field_name); - - return sql; + return sql->str; } /** diff --git a/libgtkform/key.h b/libgtkform/key.h index 636b5e2..d6fd775 100644 --- a/libgtkform/key.h +++ b/libgtkform/key.h @@ -59,8 +59,11 @@ gboolean gtk_form_key_remove_field (GtkFormKey *gtk_form_key, GtkFormField *fiel gboolean gtk_form_key_add_fields (GtkFormKey *gtk_form_key, ...); +GSList *gtk_form_key_get_fields (GtkFormKey *gtk_form_key); + gboolean gtk_form_key_check (GtkFormKey *gtk_form_key, GtkFormWidget **form_widget); +gchar *gtk_form_key_get_field_sql (GtkFormKey *gtk_form_key, GtkFormField *gtk_form_field); gchar *gtk_form_key_get_sql (GtkFormKey *gtk_form_key); GHashTable *gtk_form_key_get_values_as_hashtable (GtkFormKey *gtk_form_key);