]> saetta.ns0.it Git - libgtkform/commitdiff
Added function GtkForm::get_sql_from_hashtable (closes #688).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 27 Jun 2013 07:00:36 +0000 (09:00 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 27 Jun 2013 07:00:36 +0000 (09:00 +0200)
Added function GtkForm::get_key_as_hashtable.
Added function GtkFormKey::get_values_as_hashtable.

.cproject
libgtkform/form.c
libgtkform/form.h
libgtkform/key.c
libgtkform/key.h
test/database.db
test/from_xml_with_db.c
test/test_db.gui

index b7d8ef04794853349719d7ef7d29f9b555125b34..751f3e8422d7b489b71c5495e7e1680365a5934f 100644 (file)
--- a/.cproject
+++ b/.cproject
@@ -27,7 +27,8 @@
                                                                <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1534144287" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
                                                        </tool>
                                                        <tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.base.1110736935" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.base">
-                                                               <option id="gnu.c.compiler.option.include.paths.467981111" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
+                                                               <option id="gnu.c.compiler.option.include.paths.467981111" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
+                                                                       <listOptionValue builtIn="false" value="&quot;C:\mingw\include&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;C:\mingw\include\glib-2.0&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;C:\mingw\include\gtk-2.0&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;C:\mingw\include\libxml2&quot;"/>
                        <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
                </cconfiguration>
        </storageModule>
-       <storageModule moduleId="scannerConfiguration">
-               <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
-       </storageModule>
        <storageModule moduleId="cdtBuildSystem" version="4.0.0">
                <project id="libgtkform.null.143602593" name="libgtkform"/>
        </storageModule>
        <storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
+       <storageModule moduleId="refreshScope"/>
+       <storageModule moduleId="scannerConfiguration">
+               <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+               <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.mingw.base.2062724017;cdt.managedbuild.toolchain.gnu.mingw.base.2062724017.62298528;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base.1801393322;cdt.managedbuild.tool.gnu.cpp.compiler.input.1534144287">
+                       <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileCPP"/>
+               </scannerConfigBuildInfo>
+               <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.mingw.base.2062724017;cdt.managedbuild.toolchain.gnu.mingw.base.2062724017.62298528;cdt.managedbuild.tool.gnu.c.compiler.mingw.base.1110736935;cdt.managedbuild.tool.gnu.c.compiler.input.120046929">
+                       <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>
+               </scannerConfigBuildInfo>
+       </storageModule>
 </cproject>
index 2177dbe1374438528a8096c9ee33772ba6775ca7..b496164e936c2b77c64fdbfef4274bbfb597fca3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2013 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
@@ -2143,6 +2143,87 @@ gchar
        return sql;
 }
 
+gchar
+*gtk_form_get_sql_from_hashtable (GtkForm *form,
+                                  GtkFormSqlType type,
+                                  GHashTable *ht_keys,
+                                  GHashTable *ht_fields)
+{
+       GtkFormPrivate *priv;
+
+       gchar *sql;
+
+       GHashTable *_ht_fields;
+
+       GHashTableIter ht_iter_fields;
+
+       gpointer key_fields;
+       gpointer value_fields;
+
+       GtkFormField *ffield;
+
+       GdaExSqlType gdaexsqltype;
+
+       g_return_val_if_fail (IS_GTK_FORM (form), "");
+
+       priv = GTK_FORM_GET_PRIVATE (form);
+
+       g_return_val_if_fail (IS_GDAEX (priv->gdaex), "");
+
+       sql = "";
+
+       _ht_fields = g_hash_table_new (g_str_hash, g_str_equal);
+
+       g_hash_table_iter_init (&ht_iter_fields, ht_fields);
+       while (g_hash_table_iter_next (&ht_iter_fields, &key_fields, &value_fields))
+               {
+                       ffield = gtk_form_get_form_field_from_name (form, (const gchar *)key_fields);
+                       if (ffield != NULL)
+                               {
+                                       switch (type)
+                                               {
+                                                       case GTK_FORM_SQL_SELECT:
+                                                               gdaexsqltype = GDAEX_SQL_SELECT;
+                                                               if (gtk_form_field_is_to_load (ffield))
+                                                                       {
+                                                                               g_hash_table_insert (_ht_fields, key_fields, value_fields);
+                                                                       }
+                                                               break;
+
+                                                       case GTK_FORM_SQL_INSERT:
+                                                               gdaexsqltype = GDAEX_SQL_INSERT;
+                                                               if (gtk_form_field_is_to_save (ffield))
+                                                                       {
+                                                                               g_hash_table_insert (_ht_fields, key_fields, value_fields);
+                                                                       }
+                                                               break;
+
+                                                       case GTK_FORM_SQL_UPDATE:
+                                                               gdaexsqltype = GDAEX_SQL_UPDATE;
+                                                               if (gtk_form_field_is_to_save (ffield)
+                                                                   && (!IS_GTK_FORM_KEY (priv->key) ||
+                                                                       !gtk_form_key_field_is_key (priv->key, ffield)))
+                                                                       {
+                                                                               g_hash_table_insert (_ht_fields, key_fields, value_fields);
+                                                                       }
+                                                               break;
+
+                                                       case GTK_FORM_SQL_DELETE:
+                                                               gdaexsqltype = GDAEX_SQL_DELETE;
+                                                               break;
+                                               }
+                               }
+               }
+
+       sql = g_strdup (gdaex_get_sql_from_hashtable (priv->gdaex,
+                                           gdaexsqltype,
+                                           priv->table,
+                                           ht_keys,
+                                           _ht_fields));
+
+       return sql;
+}
+
 /**
  * gtk_form_fill_from_datamodel:
  * @form: a #GtkForm object.
@@ -2373,6 +2454,21 @@ gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable)
        return ret;
 }
 
+GHashTable
+*gtk_form_get_key_as_hashtable (GtkForm *form)
+{
+       GtkFormPrivate *priv;
+       GHashTable *ht;
+
+       g_return_val_if_fail (IS_GTK_FORM (form), NULL);
+
+       priv = GTK_FORM_GET_PRIVATE (form);
+
+       ht = gtk_form_key_get_values_as_hashtable (priv->key);
+
+       return ht;
+}
+
 /**
  * gtk_form_get_values_as_xml:
  * @form:
index ea23150e151604e08bac094a51b34c76bc02df05..54d4c76c7ab970c8dc7f4b7746ebfd5e7a015004 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2013 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
@@ -123,6 +123,8 @@ typedef enum
 
 gchar *gtk_form_get_sql (GtkForm *form, GtkFormSqlType type);
 
+gchar *gtk_form_get_sql_from_hashtable (GtkForm *form, GtkFormSqlType type, GHashTable *ht_keys, GHashTable *ht_fields);
+
 gboolean gtk_form_fill_from_datamodel (GtkForm *form, GdaDataModel *dm, gint row);
 gboolean gtk_form_fill_from_table (GtkForm *form);
 
@@ -132,6 +134,8 @@ GHashTable *gtk_form_get_values_as_hashtable (GtkForm *form);
 GHashTable *gtk_form_get_original_values_as_hashtable (GtkForm *form);
 gboolean gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable);
 
+GHashTable *gtk_form_get_key_as_hashtable (GtkForm *form);
+
 xmlNode *gtk_form_get_values_as_xml (GtkForm *form);
 gboolean gtk_form_fill_from_xml (GtkForm *form, xmlNode *xnode);
 
index 4d24a44c260d14000e1820e903c7a1d90f6e9596..6abc43b11d1e7239ed205f3f056442daf8888847 100644 (file)
@@ -222,6 +222,57 @@ gchar
        return sql;
 }
 
+/**
+ * gtk_form_key_get_valus_as_hashtable:
+ * @gtk_form_key: a #GtkFormKey object.
+ *
+ */
+GHashTable
+*gtk_form_key_get_values_as_hashtable (GtkFormKey *gtk_form_key)
+{
+       GHashTable *ht_ret;
+
+       GtkFormField *field;
+       gchar *field_name;
+       GValue *gval;
+
+       GtkFormKeyPrivate *priv;
+       GSList *fields;
+
+       g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), NULL);
+
+       ht_ret = NULL;
+
+       priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
+
+       fields = priv->fields;
+
+       if (fields != NULL)
+               {
+                       ht_ret = g_hash_table_new (g_str_hash, g_str_equal);
+               }
+
+       while (fields != NULL)
+               {
+                       field = (GtkFormField *)fields->data;
+
+                       field_name = gtk_form_field_get_field_name (field);
+
+                       gval = gtk_form_field_get_value (field);
+                       if (gval != NULL)
+                               {
+                                       /* TODO check if the field already exists */
+                                       g_hash_table_insert (ht_ret, field_name, gval);
+                               }
+
+                       fields = g_slist_next (fields);
+               }
+
+       g_free (field_name);
+
+       return ht_ret;
+}
+
 /**
  * gtk_form_key_field_is_key:
  * @gtk_form_key: a #GtkFormKey object.
index 144c2ecb548560bb95f3b22f27ef799258f88486..636b5e261961f4108f4feb0bea54eac68abe6416 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2009 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2013 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
@@ -63,6 +63,8 @@ gboolean gtk_form_key_check (GtkFormKey *gtk_form_key, GtkFormWidget **form_widg
 
 gchar *gtk_form_key_get_sql (GtkFormKey *gtk_form_key);
 
+GHashTable *gtk_form_key_get_values_as_hashtable (GtkFormKey *gtk_form_key);
+
 gboolean gtk_form_key_field_is_key (GtkFormKey *gtk_form_key, GtkFormField *field);
 
 
index c0668b95f803381fe8bdb9c89eb016e01171e97e..602456713569ef10155bfc06dab307e3a5a1ddf2 100644 (file)
Binary files a/test/database.db and b/test/database.db differ
index 6715599f613c75dd4c4454bf66fc78b16508265c..f30e729d7192e9aae885c27d2bbef52bbde435d8 100644 (file)
@@ -45,12 +45,16 @@ GtkWidget *w;
 GtkWidget *lbl_id;
 GtkWidget *txtv_sql;
 GtkWidget *btn_new;
-GtkWidget *btn_edit;
+GtkWidget *btn_save;
 GtkWidget *btn_delete;
 GtkWidget *cb_customers;
 GtkWidget *btn_open;
 GtkWidget *btn_cancel;
 
+GtkWidget *btn_save_ht;
+GtkWidget *btn_delete_ht;
+GtkWidget *btn_open_ht;
+
 GtkListStore *lstore_customers;
 
 GtkTextBuffer *buf;
@@ -182,6 +186,112 @@ on_btn_cancel_clicked (GtkButton *button,
        gtk_form_reset (form);
 }
 
+G_MODULE_EXPORT void
+on_btn_save_ht_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       GHashTable *ht_keys;
+       GHashTable *ht_fields;
+
+       gchar *sql;
+
+       if (!gtk_form_check (form, FALSE, NULL, TRUE, w, FALSE))
+               {
+                       return;
+               }
+
+       ht_keys = gtk_form_get_key_as_hashtable (form);
+       ht_fields = gtk_form_get_values_as_hashtable (form);
+
+       if (current_id == 0)
+               {
+                       /* must find new id */
+                       GdaDataModel *dm;
+                       gint new_id = 0;
+
+                       sql = "SELECT MAX(id) FROM customers";
+                       dm = gdaex_query (gdaex, sql);
+
+                       if (dm != NULL)
+                               {
+                                       new_id = gdaex_data_model_get_value_integer_at (dm, 0, 0);
+                               }
+                       new_id++;
+
+                       gtk_label_set_text (GTK_LABEL (lbl_id), g_strdup_printf ("%d", new_id));
+
+                       sql = gtk_form_get_sql_from_hashtable (form, GTK_FORM_SQL_INSERT, ht_keys, ht_fields);
+               }
+       else
+               {
+                       sql = gtk_form_get_sql_from_hashtable (form, GTK_FORM_SQL_UPDATE, ht_keys, ht_fields);
+               }
+
+       gtk_text_buffer_set_text (buf, sql, strlen (sql));
+
+       gdaex_execute (gdaex, sql);
+
+       fill_cb_customers ();
+       on_btn_new_clicked (NULL, NULL);}
+
+G_MODULE_EXPORT void
+on_btn_delete_ht_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       GHashTable *ht_keys;
+
+       ht_keys = gtk_form_get_key_as_hashtable (form);
+       gchar *sql = gtk_form_get_sql_from_hashtable (form, GTK_FORM_SQL_DELETE, ht_keys, NULL);
+
+       gtk_text_buffer_set_text (buf, sql, strlen (sql));
+
+       gdaex_execute (gdaex, sql);
+
+       fill_cb_customers ();
+       on_btn_new_clicked (NULL, NULL);
+}
+
+G_MODULE_EXPORT void
+on_btn_open_ht_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       GtkTreeIter iter;
+
+       gint id;
+
+       GHashTable *ht_keys;
+       GHashTable *ht_fields;
+
+       GdaDataModel *dm;
+
+       if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (cb_customers), &iter))
+               {
+                       return;
+               }
+
+       gtk_tree_model_get (GTK_TREE_MODEL (lstore_customers), &iter,
+                           0, &id,
+                           -1);
+
+       gtk_label_set_text (GTK_LABEL (lbl_id), g_strdup_printf ("%d", id));
+
+       ht_keys = gtk_form_get_key_as_hashtable (form);
+       ht_fields = gtk_form_get_values_as_hashtable (form);
+       gchar *sql = gtk_form_get_sql_from_hashtable (form, GTK_FORM_SQL_SELECT, ht_keys, ht_fields);
+
+       gtk_text_buffer_set_text (buf, sql, strlen (sql));
+
+       dm = gdaex_query (gdaex, sql);
+       if (dm == NULL)
+               {
+                       on_btn_new_clicked (NULL, NULL);
+                       return;
+               }
+
+       gtk_form_fill_from_datamodel (form, dm, 0);
+       current_id = id;
+}
+
 void
 fill_cb_customers (void)
 {
@@ -228,6 +338,8 @@ main (int argc, char **argv)
        form = gtk_form_new_from_file (GUIDIR "/test_db.xml", NULL);
        if (form == NULL) return 0;
 
+       g_object_set (G_OBJECT (form), "gdaex", gdaex, NULL);
+
        gtkbuilder = gtk_form_get_gtkbuilder (form);
 
        error = NULL;
@@ -244,12 +356,16 @@ main (int argc, char **argv)
 
        lbl_id = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "lbl_id"));
        btn_new = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_new"));
-       btn_edit = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_edit"));
+       btn_save = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_save"));
        btn_delete = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_delete"));
        cb_customers = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "cb_customers"));
        btn_open = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_open"));
        btn_cancel = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_cancel"));
 
+       btn_save_ht = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_save_ht"));
+       btn_delete_ht = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_delete_ht"));
+       btn_open_ht = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_open_ht"));
+
        lstore_customers = GTK_LIST_STORE (gtk_builder_get_object (gtkbuilder, "lstore_customers"));
 
        fill_cb_customers ();
index b50ae34e26940142b906b9704f2cc8024d53a723..40a020d1185e03202f95ca8451e90bdb53fdedcf 100644 (file)
             <property name="position">2</property>
           </packing>
         </child>
+        <child>
+          <object class="GtkHButtonBox" id="hbuttonbox3">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="layout_style">center</property>
+            <child>
+              <object class="GtkButton" id="btn_save_ht">
+                <property name="label" translatable="yes">Salva HashTable</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <signal name="clicked" handler="on_btn_save_ht_clicked" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="btn_delete_ht">
+                <property name="label" translatable="yes">Elimina HashTable</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <signal name="clicked" handler="on_btn_delete_ht_clicked" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="btn_open_ht">
+                <property name="label" translatable="yes">Apri HashTable</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <signal name="clicked" handler="on_btn_open_ht_clicked" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">2</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">3</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkTable" id="table2">
             <property name="visible">True</property>
           <packing>
             <property name="expand">True</property>
             <property name="fill">True</property>
-            <property name="position">3</property>
+            <property name="position">4</property>
           </packing>
         </child>
       </object>