]> saetta.ns0.it Git - libgdaex/commitdiff
Added functions GdaEx::fill_liststore_from_sql and
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 06:14:45 +0000 (08:14 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 06:14:45 +0000 (08:14 +0200)
GdaEx::fill_liststore_from_datamodel (to test).

configure.ac
src/gdaex.c
src/libgdaex.h

index 1446c03d525ba68704fc6c4a9a5d7cba68fa2c86..8854b6cc4d1f7f5b51608555edb3b9d82d56c61f 100644 (file)
@@ -30,7 +30,8 @@ GTK_DOC_CHECK(1.0)
 
 # Checks for libraries.
 PKG_CHECK_MODULES(GDAEX, [libgda-4.0 >= 4
-                          gio-2.0 >= 2.24])
+                          gio-2.0 >= 2.24
+                          gtk+-2.0 >= 2.20])
 
 AC_SUBST(GDAEX_CFLAGS)
 AC_SUBST(GDAEX_LIBS)
index 5fc131559543ad9107a9f52402ab0ed3d46ad282..b4455e62b82fca55710ea9f1eb90ef0673e92037 100644 (file)
@@ -2234,6 +2234,140 @@ gdaex_get_chr_quoting (GdaEx *gdaex)
        return chr;
 }
 
+void
+gdaex_fill_liststore_from_sql (GdaEx *gdaex,
+                               GtkListStore *lstore,
+                               const gchar *sql,
+                               guint *cols_formatted,
+                               gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+{
+       GdaDataModel *dm;
+
+       gchar *_sql;
+
+       g_return_if_fail (IS_GDAEX (gdaex));
+       g_return_if_fail (sql != NULL);
+
+       _sql = g_strstrip (g_strdup (sql));
+
+       g_return_if_fail (g_strcmp0 (_sql, "") != 0);
+
+       dm = gdaex_query (gdaex, _sql);
+       gdaex_fill_liststore_from_datamodel (gdaex, lstore, dm, cols_formatted, cols_format_func);
+}
+
+void
+gdaex_fill_liststore_from_datamodel (GdaEx *gdaex,
+                                     GtkListStore *lstore,
+                                     GdaDataModel *dm,
+                                     guint *cols_formatted,
+                                     gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+{
+       GtkTreeIter iter;
+
+       GdaDataModelIter *gda_iter;
+
+       guint cols;
+       guint col;
+
+       GType col_gtype;
+
+       GdaColumn *gcol;
+       GType gcol_gtype;
+
+       gint *columns;
+       GValue *values;
+
+       g_return_if_fail (IS_GDAEX (gdaex));
+       g_return_if_fail (GTK_IS_LIST_STORE (lstore));
+       g_return_if_fail (GDA_IS_DATA_MODEL (dm));
+
+       cols = gtk_tree_model_get_n_columns (GTK_TREE_MODEL (lstore));
+
+       gtk_list_store_clear (lstore);
+
+       gda_iter = gda_data_model_create_iter (dm);
+       if (gda_iter == NULL)
+               {
+                       return;
+               }
+
+       columns = g_malloc0 (cols * sizeof (gint));
+       values = g_malloc0 (cols * sizeof (GValue));
+
+       while (gda_data_model_iter_move_next (gda_iter))
+               {
+                       gtk_list_store_append (lstore, &iter);
+
+                       for (col = 0; col < cols; col++)
+                               {
+                                       columns[col] = col;
+
+                                       col_gtype = gtk_tree_model_get_column_type (GTK_TREE_MODEL (lstore), col);
+
+                                       GValue gval = {0};
+                                       g_value_init (&gval, col_gtype);
+                                       switch (col_gtype)
+                                               {
+                                                       case G_TYPE_STRING:
+                                                               gcol = gda_data_model_describe_column (dm, col);
+                                                               gcol_gtype = gda_column_get_g_type (gcol);
+
+                                                               switch (gcol_gtype)
+                                                                       {
+                                                                               case G_TYPE_STRING:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (gda_iter, col));
+                                                                                       break;
+
+                                                                               case G_TYPE_BOOLEAN:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col) ? "X" : "");
+                                                                                       break;
+
+                                                                               default:
+                                                                                       if (cols_format_func != NULL)
+                                                                                               {
+                                                                                                       g_value_set_string (&gval, (*cols_format_func) (gda_iter, col));
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (gda_iter, col)));
+                                                                                               }
+                                                                                       break;
+                                                                       }
+
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_INT:
+                                                               g_value_set_int (&gval, gdaex_data_model_iter_get_value_integer_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_FLOAT:
+                                                               g_value_set_float (&gval, gdaex_data_model_iter_get_value_float_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_DOUBLE:
+                                                               g_value_set_double (&gval, gdaex_data_model_iter_get_value_double_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       case G_TYPE_BOOLEAN:
+                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+
+                                                       default:
+                                                               values[col] = *gda_value_new_from_string (gdaex_data_model_iter_get_value_stringify_at (gda_iter, col), col_gtype);
+                                                               break;
+                                               }
+                               }
+
+                       gtk_list_store_set_valuesv (lstore, &iter, columns, values, cols);
+               }
+}
+
 /* PRIVATE */
 static void
 gdaex_create_connection_parser (GdaEx *gdaex)
index c391b573fea39809b466c2caaa892e4a20aeedf9..b35eb4e9bd98cfb68bb455bbaa2b5e3ba20fbe70 100644 (file)
  */
 
 #include <time.h>
+
 #include <glib.h>
 #include <glib-object.h>
+
+#include <gtk/gtk.h>
+
 #include <libgda/libgda.h>
 
 #ifndef __GDAEX_H__
@@ -174,6 +178,13 @@ gchar *gdaex_strescape (const gchar *source, const gchar *exceptions);
 
 gchar gdaex_get_chr_quoting (GdaEx *gdaex);
 
+void gdaex_fill_liststore_from_sql (GdaEx *gdaex, GtkListStore *lstore, const gchar *sql,
+                                     guint *cols_formatted,
+                                     gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+void gdaex_fill_liststore_from_datamodel (GdaEx *gdaex, GtkListStore *lstore, GdaDataModel *dm,
+                                     guint *cols_formatted,
+                                     gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+
 
 G_END_DECLS