[libgda] Improved data model export
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] Improved data model export
- Date: Wed, 27 Oct 2010 19:18:18 +0000 (UTC)
commit 7b356dfe191598bfe6a7640bf8a6e6689c4371b5
Author: Vivien Malerba <malerba gnome-db org>
Date: Wed Oct 27 18:50:55 2010 +0200
Improved data model export
uses a GdaDataModelIter iterator to export data
which makes it possible to export data models which
do not have a random access
libgda/gda-data-model.c | 67 +++++++++++++++++++++++++++++++++--------------
libgda/gda-util.c | 43 +++++++++++++++++++++--------
2 files changed, 78 insertions(+), 32 deletions(-)
---
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index b0bb221..14384c5 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -1013,6 +1013,11 @@ static gchar *export_to_text_separated (GdaDataModel *model, const gint *cols, g
* gda_data_model_export_to_file() documentation for more information about the @options argument (except for the
* "OVERWRITE" option).
*
+ * Warning: this function uses a #GdaDataModelIter iterator, and if @model does not offer a random access
+ * (check using gda_data_model_get_access_flags()), the iterator will be the same as normally used
+ * to access data in @model previously to calling this method, and this iterator will be moved (point to
+ * another row).
+ *
* Returns: a new string.
*/
gchar *
@@ -1229,7 +1234,12 @@ gda_data_model_export_to_string (GdaDataModel *model, GdaDataModelIOFormat forma
* <listitem><para>"INVALID_AS_NULL": a boolean value which, if set to %TRUE, considers any invalid data (for example for the date related values) as NULL</para></listitem>
* </itemizedlist>
*
- * Upon errors FALSE will be returned and @error will be assigned a
+ * Warning: this function uses a #GdaDataModelIter iterator, and if @model does not offer a random access
+ * (check using gda_data_model_get_access_flags()), the iterator will be the same as normally used
+ * to access data in @model previously to calling this method, and this iterator will be moved (point to
+ * another row).
+ *
+ * Upon errors %FALSE will be returned and @error will be assigned a
* #GError from the #GDA_DATA_MODEL_ERROR domain.
*
* Returns: TRUE if no error occurred
@@ -1237,8 +1247,8 @@ gda_data_model_export_to_string (GdaDataModel *model, GdaDataModelIOFormat forma
gboolean
gda_data_model_export_to_file (GdaDataModel *model, GdaDataModelIOFormat format,
const gchar *file,
- const gint *cols, G_GNUC_UNUSED gint nb_cols,
- const gint *rows, G_GNUC_UNUSED gint nb_rows,
+ const gint *cols, gint nb_cols,
+ const gint *rows, gint nb_rows,
GdaSet *options, GError **error)
{
gchar *body;
@@ -1248,7 +1258,7 @@ gda_data_model_export_to_file (GdaDataModel *model, GdaDataModelIOFormat format,
g_return_val_if_fail (!options || GDA_IS_SET (options), FALSE);
g_return_val_if_fail (file, FALSE);
- body = gda_data_model_export_to_string (model, format, cols, nb_cols, rows, nb_cols, options);
+ body = gda_data_model_export_to_string (model, format, cols, nb_cols, rows, nb_rows, options);
if (options) {
GdaHolder *holder;
@@ -1281,31 +1291,50 @@ gda_data_model_export_to_file (GdaDataModel *model, GdaDataModelIOFormat format,
}
static gchar *
-export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols, const gint *rows, gint nb_rows,
- gchar sep, gchar quote, gboolean field_quotes, gboolean null_as_empty, gboolean invalid_as_null)
+export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols,
+ const gint *rows, gint nb_rows,
+ gchar sep, gchar quote, gboolean field_quotes,
+ gboolean null_as_empty, gboolean invalid_as_null)
{
GString *str;
- gchar *retval;
- gint c, nbrows, r;
+ gint c;
+ GdaDataModelIter *iter;
+ gboolean addnl = FALSE;
g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
str = g_string_new ("");
- if (!rows)
- nbrows = gda_data_model_get_n_rows (model);
- else
- nbrows = nb_rows;
+ iter = gda_data_model_create_iter (model);
+ if (!iter)
+ return g_string_free (str, FALSE);
- for (r = 0; r < nbrows; r++) {
- if (r > 0)
+ if ((gda_data_model_iter_get_row (iter) == -1) && ! gda_data_model_iter_move_next (iter)) {
+ g_object_unref (iter);
+ return g_string_free (str, FALSE);
+ }
+
+ for (; gda_data_model_iter_is_valid (iter); gda_data_model_iter_move_next (iter)) {
+ if (rows) {
+ gint r;
+ for (r = 0; r < nb_rows; r++) {
+ if (gda_data_model_iter_get_row (iter) == rows[r])
+ break;
+ }
+ if (r == nb_rows)
+ continue;
+ }
+
+ if (addnl)
str = g_string_append_c (str, '\n');
+ else
+ addnl = TRUE;
for (c = 0; c < nb_cols; c++) {
GValue *value;
gchar *txt;
- value = (GValue *) gda_data_model_get_value_at (model, cols[c], rows ? rows[r] : r, NULL);
- if (invalid_as_null) {
+ value = (GValue*) gda_data_model_iter_get_value_at (iter, cols[c]);
+ if (value && invalid_as_null) {
if ((G_VALUE_TYPE (value) == G_TYPE_DATE)) {
GDate *date = (GDate*) g_value_get_boxed (value);
if (!g_date_valid (date))
@@ -1357,10 +1386,8 @@ export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols, c
}
}
- retval = str->str;
- g_string_free (str, FALSE);
-
- return retval;
+ g_object_unref (iter);
+ return g_string_free (str, FALSE);
}
static void
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index cfa3384..96024a8 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -34,6 +34,7 @@
#include <libgda/gda-util.h>
#include <libgda/gda-server-provider.h>
#include <libgda/gda-column.h>
+#include <libgda/gda-data-model-iter.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
@@ -331,6 +332,11 @@ gda_utility_check_data_model (GdaDataModel *model, gint nbcols, ...)
*
* Dump the data in a #GdaDataModel into a xmlNodePtr (as used in libxml).
*
+ * Warning: this function uses a #GdaDataModelIter iterator, and if @model does not offer a random access
+ * (check using gda_data_model_get_access_flags()), the iterator will be the same as normally used
+ * to access data in @model previously to calling this method, and this iterator will be moved (point to
+ * another row).
+ *
* Returns: TRUE if no error occurred
*/
gboolean
@@ -340,13 +346,14 @@ gda_utility_data_model_dump_data_to_xml (GdaDataModel *model, xmlNodePtr parent,
gboolean use_col_ids)
{
gboolean retval = TRUE;
- gint nrows, i;
gint *rcols, rnb_cols;
gchar **col_ids = NULL;
xmlNodePtr data = NULL;
+ GdaDataModelIter *iter;
/* compute columns if not provided */
if (!cols) {
+ gint i;
rnb_cols = gda_data_model_get_n_columns (model);
rcols = g_new (gint, rnb_cols);
for (i = 0; i < rnb_cols; i++)
@@ -377,23 +384,34 @@ gda_utility_data_model_dump_data_to_xml (GdaDataModel *model, xmlNodePtr parent,
}
/* add the model data to the XML output */
- if (!rows)
- nrows = gda_data_model_get_n_rows (model);
- else
- nrows = nb_rows;
- if (nrows > 0) {
+ iter = gda_data_model_create_iter (model);
+ if (iter && (gda_data_model_iter_get_row (iter) == -1) && ! gda_data_model_iter_move_next (iter)) {
+ g_object_unref (iter);
+ iter = NULL;
+ }
+ if (iter) {
xmlNodePtr row;
- gint r, c;
-
+
data = xmlNewChild (parent, NULL, (xmlChar*)"gda_array_data", NULL);
- for (r = 0; (r < nrows) && retval; r++) {
+ for (; retval && gda_data_model_iter_is_valid (iter); gda_data_model_iter_move_next (iter)) {
+ gint c;
+ if (rows) {
+ gint r;
+ for (r = 0; r < nb_rows; r++) {
+ if (gda_data_model_iter_get_row (iter) == rows[r])
+ break;
+ }
+ if (r == nb_rows)
+ continue;
+ }
+
row = xmlNewChild (data, NULL, (xmlChar*)"gda_array_row", NULL);
for (c = 0; c < rnb_cols; c++) {
GValue *value;
gchar *str = NULL;
xmlNodePtr field = NULL;
- value = (GValue *) gda_data_model_get_value_at (model, rcols [c], rows ? rows [r] : r, NULL);
+ value = (GValue*) gda_data_model_iter_get_value_at (iter, rcols[c]);
if (!value) {
retval = FALSE;
break;
@@ -436,10 +454,11 @@ gda_utility_data_model_dump_data_to_xml (GdaDataModel *model, xmlNodePtr parent,
g_free (str);
}
}
+ g_object_unref (iter);
}
- if(!cols)
- g_free(rcols);
+ if (!cols)
+ g_free (rcols);
if (use_col_ids) {
gint c;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]