]> saetta.ns0.it Git - libgdaexgrid/commitdiff
Added functions GdaExGridHtml::fill_from_[sql|datamodel|sqlbuilder]_with_missing_func.
authorAndrea Zagli <azagli@libero.it>
Sun, 28 Mar 2021 16:05:36 +0000 (18:05 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 28 Mar 2021 16:05:36 +0000 (18:05 +0200)
src/grid.c
src/grid.h
tests/grid_html_from_xml.c

index 486400ae33eed9882d1790429d1d9b241b789e08..ee5124cb3d3e4ac0b3ffd2844af7cccc6dbb08d3 100644 (file)
@@ -597,9 +597,16 @@ static GValue
                            || 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);
+                                       if (gdatetime != NULL)
+                                               {
+                                                       /* 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, "");
+                                               }
                                }
                        else
                                {
@@ -1117,6 +1124,186 @@ gchar
        return ret;
 }
 
+/**
+ * gdaex_grid_html_fill_from_sql_with_missing_func:
+ * @grid: a #GdaExGrid object.
+ * @gdaex: a #GdaEx object.
+ * @sql: the sql statement to use as datasource.
+ * @missing_func:
+ * @user_data:
+ * @pagination_start:
+ * @pagination_page:
+ * @error: where return errors.
+ *
+ * Fills the grid.
+ *
+ * Returns: TRUE if success.
+ */
+gchar
+*gdaex_grid_html_fill_from_sql_with_missing_func (GdaExGrid *grid,
+                                                  GdaEx *gdaex,
+                                                  const gchar *sql,
+                                                  GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                  guint pagination_start,
+                                                  guint pagination_page,
+                                                  GError **error)
+{
+       GdaDataModel *dm;
+
+       gchar *_sql;
+
+       gchar *ret;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), "");
+       g_return_val_if_fail (IS_GDAEX (gdaex), "");
+       g_return_val_if_fail (sql != NULL, "");
+
+       _sql = g_strstrip (g_strdup (sql));
+
+       g_return_val_if_fail (g_strcmp0 (_sql, "") != 0, "");
+
+       dm = gdaex_query (gdaex, _sql);
+       g_free (_sql);
+       ret = gdaex_grid_html_fill_from_datamodel_with_missing_func (grid, dm, missing_func, user_data, pagination_start, pagination_page, error);
+       g_object_unref (dm);
+
+       return ret;
+}
+
+/**
+ * gdaex_grid_html_fill_from_datamodel_with_missing_func:
+ * @grid: a #GdaExGrid object.
+ * @dm: a #GdaDataModel object to use as datasource.
+ * @missing_func:
+ * @user_data:
+ * @pagination_start:
+ * @pagination_page:
+ * @error: where return errors.
+ *
+ * Fills the grid.
+ *
+ * Returns: TRUE if success.
+ */
+gchar
+*gdaex_grid_html_fill_from_datamodel_with_missing_func (GdaExGrid *grid,
+                                                        GdaDataModel *dm,
+                                                        GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                        guint pagination_start,
+                                                        guint pagination_page,
+                                                        GError **error)
+{
+       gchar *ret;
+
+       GdaExGridPrivate *priv;
+
+       GString *str;
+
+       GdaDataModelIter *dm_iter;
+
+       guint row;
+       guint rows;
+
+       guint col;
+
+       GdaExGridColumn *gdaex_col;
+
+       const gchar *field_name;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), "");
+       g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), "");
+
+       priv = gdaex_grid_get_instance_private (grid);
+
+       dm_iter = gda_data_model_create_iter (dm);
+       g_return_val_if_fail (GDA_IS_DATA_MODEL_ITER (dm_iter), "");
+
+       str = g_string_new ("");
+
+       rows = gda_data_model_get_n_rows (dm);
+
+       if (priv->app_textdomain != NULL)
+               {
+                       textdomain (priv->app_textdomain);
+               }
+
+       while (gda_data_model_iter_move_next (dm_iter))
+               {
+                       g_string_append (str, "<tr>\n");
+                       for (col = 0; col < priv->columns->len; col++)
+                               {
+                                       GValue gval = {0};
+
+                                       g_value_init (&gval, G_TYPE_STRING);
+
+                                       gdaex_col = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
+                                       field_name = gdaex_grid_column_get_field_name (gdaex_col);
+
+                                       /* checking if field exists */
+                                       if (gda_data_model_get_column_index (dm, field_name) == -1)
+                                               {
+                                                       g_value_set_string (&gval, missing_func (dm_iter, gdaex_col, user_data));
+                                               }
+                                       else
+                                               {
+                                                       _set_gvalue_stringify_from_datamodel((GdaExGridColumn *)g_ptr_array_index (priv->columns, col), &gval, dm, dm_iter, field_name);
+                                               }
+
+                                       g_string_append_printf (str, "\t<td>%s</td>\n", g_value_get_string (&gval));
+                               }
+
+                       g_string_append (str, "</tr>\n");
+               }
+
+       ret = g_strdup (str->str);
+       g_string_free (str, TRUE);
+
+       if (priv->app_textdomain != NULL)
+               {
+                       textdomain (GETTEXT_PACKAGE);
+               }
+
+       return ret;
+}
+
+/**
+ * gdaex_grid_html_fill_from_sqlbuilder_with_missing_func:
+ * @grid: a #GdaExGrid object.
+ * @gdaex: a #GdaEx object.
+ * @builder: a #GdaExSqlBuilder object.
+ * @missing_func:
+ * @user_data:
+ * @pagination_start:
+ * @pagination_page:
+ * @error: where return errors.
+ *
+ * Fills the grid.
+ *
+ * Returns: TRUE if success.
+ */
+gchar
+*gdaex_grid_html_fill_from_sqlbuilder_with_missing_func (GdaExGrid *grid,
+                                                         GdaEx *gdaex,
+                                                         GdaExSqlBuilder *builder,
+                                                         GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                         guint pagination_start,
+                                                         guint pagination_page,
+                                                         GError **error)
+{
+       GdaDataModel *dm;
+
+       gchar *ret;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), "");
+       g_return_val_if_fail (IS_GDAEX (gdaex), "");
+       g_return_val_if_fail (GDAEX_IS_SQLBUILDER (builder), "");
+
+       dm = gdaex_sql_builder_query (builder, gdaex, NULL);
+       ret = gdaex_grid_html_fill_from_datamodel_with_missing_func (grid, dm, missing_func, user_data, pagination_start, pagination_page, error);
+       g_object_unref (dm);
+
+       return ret;
+}
+
 /* PRIVATE */
 static void
 gdaex_grid_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
index 2b8257e261422202de7d13eb1c2d40a426a66587..09bdcc88aacf4c2a881edb69555dd547b049269b 100644 (file)
@@ -115,8 +115,31 @@ gboolean gdaex_grid_fill_from_datamodel (GdaExGrid *grid, GdaDataModel *dm, GErr
 gboolean gdaex_grid_fill_from_sqlbuilder (GdaExGrid *grid, GdaEx *gdaex, GdaExSqlBuilder *builder, GError **error);
 
 
+typedef gchar *(*GdaExGridHtmlFillMissingFunc) (GdaDataModelIter *iter, GdaExGridColumn *column, gpointer user_data);
+
 gchar *gdaex_grid_html_get_header (GdaExGrid *grid);
 
+gchar *gdaex_grid_html_fill_from_sql_with_missing_func (GdaExGrid *grid,
+                                                        GdaEx *gdaex,
+                                                        const gchar *sql,
+                                                        GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                        guint pagination_start,
+                                                        guint pagination_page,
+                                                        GError **error);
+gchar *gdaex_grid_html_fill_from_datamodel_with_missing_func (GdaExGrid *grid,
+                                                              GdaDataModel *dm,
+                                                              GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                              guint pagination_start,
+                                                              guint pagination_page,
+                                                              GError **error);
+gchar *gdaex_grid_html_fill_from_sqlbuilder_with_missing_func (GdaExGrid *grid,
+                                                               GdaEx *gdaex,
+                                                               GdaExSqlBuilder *sqlbuilder,
+                                                               GdaExGridHtmlFillMissingFunc missing_func, gpointer user_data,
+                                                               guint pagination_start,
+                                                               guint pagination_page,
+                                                               GError **error);
+
 
 G_END_DECLS
 
index 6387584cc23c48a5468b17b4974fe48702bde6c1..6ba50003eaf70d5e3a30fe9c850de16c6b1ceeef 100644 (file)
 #include "grid.h"
 #include "gridcolumn.h"
 
+static gchar
+*missing_func (GdaDataModelIter *iter, GdaExGridColumn *column, gpointer user_data)
+{
+       return "";
+}
+
 int
 main (int argc, char **argv)
 {
@@ -42,5 +48,7 @@ main (int argc, char **argv)
 
        g_printf ("%s\n\n", gdaex_grid_html_get_header (grid));
 
+       g_printf ("%s\n\n", gdaex_grid_html_fill_from_sql_with_missing_func (grid, gdaex, "SELECT name, surname, id, birthday, married, incoming, age FROM clients", missing_func, NULL, 0, 0, NULL));
+
        return 0;
 }