[gnome-db] export to csv

Andrea Zagli <[email protected]>
Newsgroups gmane.comp.gnome.db
Message-ID <[email protected]>
- i think that could be useful an additional parameter (or an option)  
to gda_data_model_export_to_file that allow to make the first line of   
the file the list of fields name; so i prepared the patch attached  
named fields_name_header.patch

- i don't know if so for every database, but when the field is of type  
date (or time or timestamp) and it is null postgresql wants in csv  
file a zero-lenght string; so i prepared another patch (in attach  
named null_date.patch) (it makes use of the two functions that i send  
2 days ago)


PS: i don't know if it is the right way to send you/make a patch...

_______________________________________________
gnome-db-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gnome-db-list
fields_name_header.patch (text/x-patch, 2.7 KB)
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index 70ffd79..2c9ac5e 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -1050,10 +1050,13 @@ gda_data_model_export_to_string (GdaDataModel *model, GdaDataModelIOFormat forma
 	}
 
 	case GDA_DATA_MODEL_IO_TEXT_SEPARATED: {
+		gchar *retval;
 		gchar sep = ',';
 		gchar quote = '"';
 		gboolean field_quote = TRUE;
 
+		g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
+
 		if (options) {
 			GdaHolder *holder;
 
@@ -1094,25 +1097,61 @@ gda_data_model_export_to_string (GdaDataModel *model, GdaDataModelIOFormat forma
 				else 
 					g_warning (_("The '%s' parameter must hold a boolean value, ignored."), "FIELD_QUOTE");
 			}
+
+			holder = gda_set_get_holder (options, "FIELDS_NAME");
+			if (holder) {
+				const GValue *value;
+				value = gda_holder_get_value (holder);
+				if (value && (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN))
+					{
+						gint col;
+						gint *rcols;
+						gint rnb_cols;
+
+						if (cols)
+							{
+								rcols = (gint *)cols;
+								rnb_cols = nb_cols;
+							}
+						else
+							{
+								gint i;
+
+								rnb_cols = gda_data_model_get_n_columns (model);
+								rcols = g_new (gint, rnb_cols);
+								for (i = 0; i < rnb_cols; i++)
+									rcols[i] = i;
+							}
+
+						retval = g_strdup ("");
+						for (col = 0; col < rnb_cols; col++)
+							{
+								retval = g_strconcat (retval, g_strdup_printf ("%c", quote), gda_data_model_get_column_name (model, rcols[col]), g_strdup_printf ("%c", quote), g_strdup_printf ("%c", sep), NULL);
+							}
+						if (g_strcmp0 (retval, "") != 0)
+							{
+								retval[strlen (retval) - 1] = '\n';
+							}
+					}
+				else
+					g_warning (_("The '%s' parameter must hold a boolean value, ignored."), "FIELDS_NAME");
+			}
 		}
 
+		if (!retval) retval = g_strdup ("");
 		if (cols)
-			return export_to_text_separated (model, cols, nb_cols, rows, nb_rows, sep, quote, field_quote);
+			retval = g_strconcat (retval, export_to_text_separated (model, cols, nb_cols, rows, nb_rows, sep, quote, field_quote), NULL);
 		else {
-			gchar *retval;
 			gint *rcols, rnb_cols, i;
 			
-			g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
-			
 			rnb_cols = gda_data_model_get_n_columns (model);
 			rcols = g_new (gint, rnb_cols);
 			for (i = 0; i < rnb_cols; i++)
 				rcols[i] = i;
-			retval = export_to_text_separated (model, rcols, rnb_cols, rows, nb_rows, sep, quote, field_quote);
-			g_free (rcols);
-			
-			return retval;
+			retval = g_strconcat (retval, export_to_text_separated (model, rcols, rnb_cols, rows, nb_rows, sep, quote, field_quote), NULL);
+			g_free (rcols);			
 		}
+		return retval;
 	}
 
 	default:
null_date.patch (text/x-patch, 2 KB)
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index 70ffd79..ad4a746 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -1229,18 +1268,52 @@ export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols, c
 			else {
 				gchar *tmp;
 				gsize len, size;
-				
-				tmp = gda_value_stringify (value);
-				len = strlen (tmp);
-				size = 2 * len + 3;
-				txt = g_new (gchar, size);
-
-				len = csv_write2 (txt, size, tmp, len, quote);
-				txt [len] = 0;
-				if (!field_quotes) {
-					txt [len - 1] = 0;
-					memmove (txt, txt+1, len);
+				GdaColumn *gdacol;
+				GType colgtype;
+
+				tmp = NULL;
+
+				gdacol = gda_data_model_describe_column (model, cols[c]);
+				colgtype = gda_column_get_g_type (gdacol);
+				if (colgtype == GDA_TYPE_TIMESTAMP
+			        || colgtype == G_TYPE_DATE
+			        || colgtype == GDA_TYPE_TIME) {
+			    	if (!gda_value_is_null (value)) {
+			    		if (gda_value_isa (value, GDA_TYPE_TIMESTAMP)) {
+			    			const GdaTimestamp *tt;
+			    			tt = (const GdaTimestamp *)gda_value_get_timestamp (value);
+			    			if (gda_timestamp_valid (tt)) {
+								tmp = gda_value_stringify (value);
+			    			}
+			    		} else if (gda_value_isa (value, G_TYPE_DATE)) {
+			    			tmp = gda_value_stringify (value);
+						} else if (gda_value_isa (value, GDA_TYPE_TIME)) {
+			    			const GdaTime *tt;
+			    			tt = (const GdaTime *)gda_value_get_time (value);
+			    			if (gda_time_valid (tt)) {
+								tmp = gda_value_stringify (value);
+			    			}
+			    		}
+			    	}
+				}
+			    else {
+			    	tmp = gda_value_stringify (value);
+			    }
+
+				if (tmp != NULL) {
+					len = strlen (tmp);
+					size = 2 * len + 3;
+					txt = g_new (gchar, size);
+
+					len = csv_write2 (txt, size, tmp, len, quote);
+					txt [len] = 0;
+					if (!field_quotes) {
+						txt [len - 1] = 0;
+						memmove (txt, txt+1, len);
+					}
 				}
+				else
+					txt = g_strdup ("");
 			}
 			if (c > 0)
 				str = g_string_append_c (str, sep);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.