]> saetta.ns0.it Git - libgtkform/commitdiff
Added auto-increment fields (only for integer fields and only on INSERT).
authorAndrea Zagli <azagli@libero.it>
Sat, 17 Jul 2010 16:36:55 +0000 (18:36 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 17 Jul 2010 16:36:55 +0000 (18:36 +0200)
configure.ac
data/gtkform.dtd
src/field.c
src/field.h
src/form.c

index a5ee871f9f48d42febdf4c177e875d852f787732..ab52e49d12483647f7e0390c2c1279fa31eaeb7b 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.59)
-AC_INIT([libgtkform], [0.1.0], [azagli@libero.it])
+AC_INIT([libgtkform], [0.1.1], [azagli@libero.it])
 AC_CONFIG_SRCDIR([src/form.c])
 AC_CONFIG_HEADER([config.h])
 
index 7d94ff50f9c4c73d380c2b47ddcef8b874211f09..8ffdb5a69f0aaa90921c9b796066847520d6c57d 100644 (file)
 
 <!ELEMENT return-value (#PCDATA)>
 
-<!ELEMENT field (no-save?, obligatory?, default?, is-key?, type?, display-format?)>
+<!ELEMENT field (to-save?, obligatory?, default?, is-key?, auto-increment?, datetime-type?, display-format?)>
 
 <!ATTLIST field
        type  (boolean | datetime | float | integer | text)  #REQUIRED
        name  CDATA  #REQUIRED
 >
 
-<!ELEMENT to-save (y | n)>
+<!ELEMENT to-save (#PCDATA)>
 <!ELEMENT obligatory (#PCDATA)>
 <!ELEMENT default (#PCDATA)>
 <!ELEMENT is-key (#PCDATA)>
+<!ELEMENT auto-increment (#PCDATA)>
 <!ELEMENT datetime-type (date | time | datetime)>
 <!ELEMENT display-format (#PCDATA)>
index a3eac8025a20d8ae39022e6dc7d146d071376c8e..a7d7096bb457f6a8ea7a25a87bc308040300c00e 100644 (file)
@@ -25,6 +25,7 @@ enum
        PROP_FIELD,
        PROP_TO_SAVE,
        PROP_OBLIGATORY,
+       PROP_AUTO_INCREMENT,
        PROP_DATAMODEL,
        PROP_WIDGET
 };
@@ -50,6 +51,7 @@ struct _GtkFormFieldPrivate
                gboolean is_key;
                gboolean to_save;
                gboolean obligatory;
+               gboolean auto_increment;
 
                GdaDataModel *dm;
 
@@ -92,6 +94,13 @@ gtk_form_field_class_init (GtkFormFieldClass *klass)
                                                               FALSE,
                                                               G_PARAM_READWRITE));
 
+       g_object_class_install_property (object_class, PROP_AUTO_INCREMENT,
+                                        g_param_spec_boolean ("auto-increment",
+                                                              "Auto increment",
+                                                              "Whether the field auto increments (works only on  numeric fields)",
+                                                              FALSE,
+                                                              G_PARAM_READWRITE));
+
        g_object_class_install_property (object_class, PROP_WIDGET,
                                         g_param_spec_object ("form-widget",
                                                              "GtkFormWidget",
@@ -257,6 +266,23 @@ gtk_form_field_is_to_save (GtkFormField *field)
        return priv->to_save;
 }
 
+/**
+ * gtk_form_field_is_auto_increment:
+ * @field: a #GtkFormField object.
+ *
+ */
+gboolean
+gtk_form_field_is_auto_increment (GtkFormField *field)
+{
+       GtkFormFieldPrivate *priv;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE);
+
+       priv = GTK_FORM_FIELD_GET_PRIVATE (field);
+
+       return priv->auto_increment;
+}
+
 /**
  * gtk_form_field_is_empty:
  * @field: a #GtkFormField object.
@@ -361,6 +387,10 @@ gtk_form_field_set_property (GObject *object,
                                priv->obligatory = g_value_get_boolean (value);
                                break;
 
+                       case PROP_AUTO_INCREMENT:
+                               priv->auto_increment = g_value_get_boolean (value);
+                               break;
+
                        case PROP_DATAMODEL:
                                priv->dm = g_value_get_pointer (value);
                                break;
@@ -399,6 +429,10 @@ gtk_form_field_get_property (GObject *object,
                                g_value_set_boolean (value, priv->obligatory);
                                break;
 
+                       case PROP_AUTO_INCREMENT:
+                               g_value_set_boolean (value, priv->auto_increment);
+                               break;
+
                        case PROP_DATAMODEL:
                                g_value_set_pointer (value, priv->dm);
                                break;
index 919a4b3f6420bcd7f96c99d3d6041edd32f3f2dd..7d19f721ac9431619607bfc73f1dd7cd37dc03d1 100644 (file)
@@ -76,6 +76,8 @@ gboolean gtk_form_field_clear (GtkFormField *field);
 
 gboolean gtk_form_field_is_to_save (GtkFormField *field);
 
+gboolean gtk_form_field_is_auto_increment (GtkFormField *field);
+
 gboolean gtk_form_field_is_empty (GtkFormField *field);
 
 gboolean gtk_form_field_is_changed (GtkFormField *field);
index 0753e0594d77a3d90ba004a3ffc3672c1a7e7a69..da86517bd36249ce8c801d28eeb3fea2a30d4159 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2009 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -50,7 +50,8 @@ enum
        PROP_0,
        PROP_TABLE,
        PROP_KEY,
-       PROP_QUOTE
+       PROP_QUOTE,
+       PROP_GDAEX
 };
 
 static void gtk_form_class_init (GtkFormClass *class);
@@ -79,6 +80,8 @@ struct _GtkFormPrivate
                GSList *fields;
                GtkFormKey *key;
                gchar quote;
+
+               GdaEx *gdaex;
        };
 
 G_DEFINE_TYPE (GtkForm, gtk_form, G_TYPE_OBJECT)
@@ -112,6 +115,12 @@ gtk_form_class_init (GtkFormClass *class)
                                                            G_MININT8, G_MAXINT8, '"',
                                                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
+       g_object_class_install_property (object_class, PROP_GDAEX,
+                                        g_param_spec_pointer ("gdaex",
+                                                              "GdaEx object",
+                                                              "GdaEx object",
+                                                              G_PARAM_READWRITE));
+
        g_type_class_add_private (object_class, sizeof (GtkFormPrivate));
 }
 
@@ -122,6 +131,7 @@ gtk_form_init (GtkForm *form)
 
        priv->fields = NULL;
        priv->key = NULL;
+       priv->gdaex = NULL;
 }
 
 /**
@@ -418,6 +428,13 @@ GtkForm
                                                                                                                                                                                        gtk_form_key_add_field (priv->key, field);
                                                                                                                                                                                }
                                                                                                                                                                }
+                                                                                                                                                       else if (xmlStrcmp (node_field->name, (const xmlChar *)"auto-increment") == 0)
+                                                                                                                                                               {
+                                                                                                                                                                       if (gtk_form_field_boolean_str_to_boolean (prop))
+                                                                                                                                                                               {
+                                                                                                                                                                                       g_object_set (G_OBJECT (field), "auto-increment", TRUE, NULL);
+                                                                                                                                                                               }
+                                                                                                                                                               }
                                                                                                                                                        else if (xmlStrcmp (node_field->name, (const xmlChar *)"datetime-type") == 0)
                                                                                                                                                                {
                                                                                                                                                                        if (g_strcmp0 (type, "datetime") == 0)
@@ -1255,6 +1272,30 @@ gchar
                                                                        case GTK_FORM_SQL_INSERT:
                                                                                if (gtk_form_field_is_to_save (field))
                                                                                        {
+                                                                                               if (gtk_form_field_is_auto_increment (field))
+                                                                                                       {
+                                                                                                               GdaDataModel *dm;
+                                                                                                               gchar *field_name;
+                                                                                                               guint new_val;
+
+                                                                                                               g_return_val_if_fail (IS_GDAEX (priv->gdaex), NULL);
+
+                                                                                                               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));
+                                                                                                               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++;
+
+                                                                                                               gtk_form_widget_set_value_stringify (gtk_form_field_get_form_widget (field), g_strdup_printf ("%d", new_val));
+                                                                                                       }
+                                                                                       
                                                                                                value = gtk_form_field_get_value_sql (field);
                                                                                                if (value != NULL)
                                                                                                        {
@@ -1383,6 +1424,10 @@ gtk_form_set_property (GObject *object,
                                priv->quote = g_value_get_char (value);
                                break;
 
+                       case PROP_GDAEX:
+                               priv->gdaex = g_value_get_pointer (value);
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;
@@ -1413,6 +1458,10 @@ gtk_form_get_property (GObject *object,
                                g_value_set_char (value, priv->quote);
                                break;
 
+                       case PROP_GDAEX:
+                               g_value_set_pointer (value, priv->gdaex);
+                               break;
+
                        default:
                                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                                break;