]> saetta.ns0.it Git - libgdaex/commitdiff
Added functions GdaExGrid::fill_from_sql_with_missing_func and GdaExGrid::fill_from_d...
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Wed, 29 Jan 2014 11:37:32 +0000 (12:37 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Wed, 29 Jan 2014 11:37:32 +0000 (12:37 +0100)
src/gdaex.c
src/grid.c
src/grid.h
tests/grid.c

index 39e633a1b250e175a6d09c1d87ee22263103e9fb..57e86e9608c0532a363c2a3aef9670424ad19bb8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  gdaex.c
  *
- *  Copyright (C) 2005-2013 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2005-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *  
@@ -2833,7 +2833,9 @@ gdaex_fill_liststore_from_sql_with_missing_func (GdaEx *gdaex,
        g_return_if_fail (g_strcmp0 (_sql, "") != 0);
 
        dm = gdaex_query (gdaex, _sql);
+       g_free (_sql);
        gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, cols_format_func, missing_func, user_data);
+       g_object_unref (dm);
 }
 
 void
index ded508e549e2fd492d4c1f55a18a5fe5dda3bf0c..088b3d2288eff4d3f7643852c367772572f1e2e5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  grid.c
  *
- *  Copyright (C) 2010-2013 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *  
@@ -232,7 +232,11 @@ GtkWidget
 }
 
 gboolean
-gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error)
+gdaex_grid_fill_from_sql_with_missing_func (GdaExGrid *grid,
+                                            GdaEx *gdaex,
+                                            const gchar *sql,
+                                            GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data,
+                                            GError **error)
 {
        GdaDataModel *dm;
 
@@ -250,14 +254,17 @@ gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GErro
 
        dm = gdaex_query (gdaex, _sql);
        g_free (_sql);
-       ret = gdaex_grid_fill_from_datamodel (grid, dm, error);
+       ret = gdaex_grid_fill_from_datamodel_with_missing_func (grid, dm, missing_func, user_data, error);
        g_object_unref (dm);
 
        return ret;
 }
 
 gboolean
-gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error)
+gdaex_grid_fill_from_datamodel_with_missing_func (GdaExGrid *grid,
+                                                  GdaDataModel *dm,
+                                                  GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data,
+                                                  GError **error)
 {
        GdaExGridPrivate *priv;
 
@@ -284,6 +291,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
        GDateTime *gdatetime;
        gdouble dval;
 
+       gboolean call_missing_func;
+
        g_return_val_if_fail (GDAEX_IS_GRID (grid), FALSE);
        g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), FALSE);
 
@@ -304,6 +313,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
        columns = g_new0 (gint, cols);
        values = g_new0 (GValue, cols);
 
+       call_missing_func = FALSE;
+
        while (gda_data_model_iter_move_next (dm_iter))
                {
                        gtk_list_store_append (GTK_LIST_STORE (priv->model), &iter);
@@ -311,6 +322,8 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
                        cols_sorted = priv->columns->len;
                        for (col = 0; col < priv->columns->len; col++)
                                {
+                                       GValue gval = {0};
+
                                        columns[col] = col;
 
                                        col_gtype = gtk_tree_model_get_column_type (priv->model, col);
@@ -318,69 +331,77 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
                                        gdaex_col = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
                                        field_name = gdaex_grid_column_get_field_name (gdaex_col);
 
-                                       GValue gval = {0};
                                        g_value_init (&gval, col_gtype);
-                                       switch (col_gtype)
-                                               {
-                                                       case G_TYPE_STRING:
-                                                               gda_col = gda_data_model_describe_column (dm, col);
-                                                               gda_col_gtype = gda_column_get_g_type (gda_col);
-
-                                                               switch (gda_col_gtype)
-                                                                       {
-                                                                               case G_TYPE_STRING:
-                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name));
-                                                                                       break;
-
-                                                                               case G_TYPE_BOOLEAN:
-                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name) ? "X" : "");
-                                                                                       break;
-
-                                                                               case G_TYPE_INT:
-                                                                               case G_TYPE_FLOAT:
-                                                                               case G_TYPE_DOUBLE:
-                                                                                       dval = gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name);
-                                                                                       g_value_set_string (&gval, gdaex_format_money (dval, gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)), FALSE));
-                                                                                       break;
-
-                                                                               default:
-                                                                                       if (gda_col_gtype == G_TYPE_DATE
-                                                                                           || gda_col_gtype == G_TYPE_DATE_TIME
-                                                                                           || gda_col_gtype == GDA_TYPE_TIMESTAMP)
-                                                                                               {
-                                                                                                       gdatetime = gdaex_data_model_iter_get_field_value_gdatetime_at (dm_iter, field_name);
-                                                                                                       /* TODO find default format from locale */
-                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gda_col_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
-                                                                                                       g_date_time_unref (gdatetime);
-                                                                                               }
-                                                                                       else
-                                                                                               {
-                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name));
-                                                                                               }
-                                                                                       break;
-                                                                       }
-
-                                                               break;
-
-                                                       case G_TYPE_INT:
-                                                               g_value_set_int (&gval, gdaex_data_model_iter_get_field_value_integer_at (dm_iter, field_name));
-                                                               break;
-
-                                                       case G_TYPE_FLOAT:
-                                                               g_value_set_float (&gval, gdaex_data_model_iter_get_field_value_float_at (dm_iter, field_name));
-                                                               break;
 
-                                                       case G_TYPE_DOUBLE:
-                                                               g_value_set_double (&gval, gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name));
-                                                               break;
-
-                                                       case G_TYPE_BOOLEAN:
-                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name));
-                                                               break;
+                                       /* checking if field exists */
+                                       if (gda_data_model_get_column_index (dm, field_name) == -1)
+                                               {
+                                                       call_missing_func = TRUE;
+                                               }
+                                       else
+                                               {
+                                                       switch (col_gtype)
+                                                               {
+                                                                       case G_TYPE_STRING:
+                                                                               gda_col = gda_data_model_describe_column (dm, col);
+                                                                               gda_col_gtype = gda_column_get_g_type (gda_col);
 
-                                                       default:
-                                                               gval = *gda_value_new_from_string (gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name), col_gtype);
-                                                               break;
+                                                                               switch (gda_col_gtype)
+                                                                                       {
+                                                                                               case G_TYPE_STRING:
+                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name));
+                                                                                                       break;
+
+                                                                                               case G_TYPE_BOOLEAN:
+                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name) ? "X" : "");
+                                                                                                       break;
+
+                                                                                               case G_TYPE_INT:
+                                                                                               case G_TYPE_FLOAT:
+                                                                                               case G_TYPE_DOUBLE:
+                                                                                                       dval = gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name);
+                                                                                                       g_value_set_string (&gval, gdaex_format_money (dval, gdaex_grid_column_get_decimals ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col)), FALSE));
+                                                                                                       break;
+
+                                                                                               default:
+                                                                                                       if (gda_col_gtype == G_TYPE_DATE
+                                                                                                               || gda_col_gtype == G_TYPE_DATE_TIME
+                                                                                                               || gda_col_gtype == GDA_TYPE_TIMESTAMP)
+                                                                                                               {
+                                                                                                                       gdatetime = gdaex_data_model_iter_get_field_value_gdatetime_at (dm_iter, field_name);
+                                                                                                                       /* TODO find default format from locale */
+                                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gda_col_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
+                                                                                                                       g_date_time_unref (gdatetime);
+                                                                                                               }
+                                                                                                       else
+                                                                                                               {
+                                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name));
+                                                                                                               }
+                                                                                                       break;
+                                                                                       }
+
+                                                                               break;
+
+                                                                       case G_TYPE_INT:
+                                                                               g_value_set_int (&gval, gdaex_data_model_iter_get_field_value_integer_at (dm_iter, field_name));
+                                                                               break;
+
+                                                                       case G_TYPE_FLOAT:
+                                                                               g_value_set_float (&gval, gdaex_data_model_iter_get_field_value_float_at (dm_iter, field_name));
+                                                                               break;
+
+                                                                       case G_TYPE_DOUBLE:
+                                                                               g_value_set_double (&gval, gdaex_data_model_iter_get_field_value_double_at (dm_iter, field_name));
+                                                                               break;
+
+                                                                       case G_TYPE_BOOLEAN:
+                                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_field_value_boolean_at (dm_iter, field_name));
+                                                                               break;
+
+                                                                       default:
+                                                                               gval = *gda_value_new_from_string (gdaex_data_model_iter_get_field_value_stringify_at (dm_iter, field_name), col_gtype);
+                                                                               break;
+                                                               }
                                                }
                                        values[col] = gval;
 
@@ -428,11 +449,28 @@ gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **erro
                                }
 
                        gtk_list_store_set_valuesv (GTK_LIST_STORE (priv->model), &iter, columns, values, cols);
+                       if (call_missing_func
+                           && missing_func != NULL)
+                               {
+                                       missing_func (GTK_LIST_STORE (priv->model), &iter, user_data);
+                               }
                }
 
        return TRUE;
 }
 
+gboolean
+gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error)
+{
+       return gdaex_grid_fill_from_sql_with_missing_func (grid, gdaex, sql, NULL, NULL, error);
+}
+
+gboolean
+gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error)
+{
+       return gdaex_grid_fill_from_datamodel_with_missing_func (grid, dm, NULL, NULL, error);
+}
+
 #ifdef SOLIPA_FOUND
 void
 gdaex_grid_set_solipa (GdaExGrid *grid, Solipa *solipa)
index a7f7873197b54a801e7a9df59082bf4de00d4944..0f3ff3166eb6df6918a127ec7b405a4966837369 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  grid.h
  *
- *  Copyright (C) 2010-2013 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *  
@@ -73,6 +73,18 @@ void gdaex_grid_clear (GdaExGrid *grid);
 
 GtkWidget *gdaex_grid_get_widget (GdaExGrid *grid);
 
+typedef void (*GdaExGridFillListStoreMissingFunc) (GtkListStore *lstore, GtkTreeIter *iter, gpointer user_data);
+
+gboolean gdaex_grid_fill_from_sql_with_missing_func (GdaExGrid *grid,
+                                                     GdaEx *gdaex,
+                                                     const gchar *sql,
+                                                     GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data,
+                                                     GError **error);
+gboolean gdaex_grid_fill_from_datamodel_with_missing_func (GdaExGrid *grid,
+                                                           GdaDataModel *dm,
+                                                           GdaExGridFillListStoreMissingFunc missing_func, gpointer user_data,
+                                                           GError **error);
+
 gboolean gdaex_grid_fill_from_sql (GdaExGrid *grid, GdaEx *gdaex, const gchar *sql, GError **error);
 gboolean gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GError **error);
 
index 294fe1647b7bc256ce70dc3dfe2a571f65f434c4..3fa350966144c7b72dbff2446c613f2c625412ab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2013 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2011-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 GdaExGridColumn *gcol_birthday;
 
+static void
+missing_func (GtkListStore *lstore, GtkTreeIter *iter, gpointer user_data)
+{
+       gtk_list_store_set (lstore, iter,
+                           7, "missing",
+                           -1);
+}
+
 static void
 on_btn_birthday_clicked (GtkButton *button,
                          gpointer user_data)
@@ -91,10 +99,13 @@ main (int argc, char **argv)
        gcol = gdaex_grid_column_new ("Married", "married", G_TYPE_BOOLEAN, TRUE, TRUE, TRUE, TRUE, -1);
        gdaex_grid_add_column (grid, gcol);
 
+       gcol = gdaex_grid_column_new_defaults ("Missing", "missing", G_TYPE_STRING);
+       gdaex_grid_add_column (grid, gcol);
+
        wgrid = gdaex_grid_get_widget (grid);
        gtk_container_add (GTK_CONTAINER (scrolledw), wgrid);
 
-       gdaex_grid_fill_from_sql (grid, gdaex, "SELECT * FROM clients", NULL);
+       gdaex_grid_fill_from_sql_with_missing_func (grid, gdaex, "SELECT * FROM clients", missing_func, NULL, NULL);
 
        hbtnbox = gtk_hbutton_box_new ();
        gtk_box_pack_start (GTK_BOX (vbox), hbtnbox, FALSE, FALSE, 0);