[gnome-db] patch: config write support + get_n_rows + fixes



	Hi!

	Here is a patch containing changes to:

		* A couple of sgml files (?).
		
		* libgda/gda-config.c: write config support. Writes the
		configuration file every set_* call.
		
		* libgda/gda-recordset.[ch]: added get_n_rows_func to
		private recordset structure, changed
		gda_recordset_get_n_rows () to call the new one. Fixes
		in get_value_at.

		* libgda/gda_row.c: remove assert in
		gda_row_attributes_get_length(). There can be empty
		recordsets (it printed a CRITICAL for drop statements).

		* updated postgres and mysql recordsets to support
		get_n_rows.

	With this changes, gda-test runs again without problems.

	Comments?

-- 
Gonzalo Paniagua Javier <gonzalo gnome-db org>
http://www.gnome-db.org/~gonzalo/

Index: doc/C/tmpl/gda-recordset.sgml
===================================================================
RCS file: /cvs/gnome/libgda/doc/C/tmpl/gda-recordset.sgml,v
retrieving revision 1.8
diff -u -r1.8 gda-recordset.sgml
--- doc/C/tmpl/gda-recordset.sgml	4 May 2002 16:39:35 -0000	1.8
+++ doc/C/tmpl/gda-recordset.sgml	5 May 2002 18:45:46 -0000
@@ -49,6 +49,7 @@
 @cnc: 
 @fetch_func: 
 @desc_func: 
+ get_n_rows_func: 
 @user_data: 
 @Returns: 
 <!-- # Unused Parameters # -->
Index: doc/C/tmpl/gda-row.sgml
===================================================================
RCS file: /cvs/gnome/libgda/doc/C/tmpl/gda-row.sgml,v
retrieving revision 1.15
diff -u -r1.15 gda-row.sgml
--- doc/C/tmpl/gda-row.sgml	4 May 2002 16:39:35 -0000	1.15
+++ doc/C/tmpl/gda-row.sgml	5 May 2002 18:45:46 -0000
@@ -14,7 +14,7 @@
 
 </para>
 
-<!-- ##### TYPEDEF GdaRow ##### -->
+<!-- ##### STRUCT GdaRow ##### -->
 <para>
 
 </para>
Index: libgda/gda-config.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-config.c,v
retrieving revision 1.28
diff -u -r1.28 gda-config.c
--- libgda/gda-config.c	2 May 2002 17:25:01 -0000	1.28
+++ libgda/gda-config.c	5 May 2002 18:45:47 -0000
@@ -299,6 +299,71 @@
 	g_free (section);
 }
 
+static void
+add_xml_entry (xmlNodePtr parent, gda_config_entry *entry)
+{
+	xmlNodePtr new_node;
+
+	new_node = xmlNewTextChild (parent, NULL, "entry", NULL);
+	xmlSetProp (new_node, "name", entry->name ? entry->name : "");
+	xmlSetProp (new_node, "type", entry->type ? entry->type : "");
+	xmlSetProp (new_node, "value", entry->value ? entry->value : "");
+}
+
+static xmlNodePtr
+add_xml_section (xmlNodePtr parent, gda_config_section *section)
+{
+	xmlNodePtr new_node;
+
+	new_node = xmlNewTextChild (parent, NULL, "section", NULL);
+	xmlSetProp (new_node, "path", section->path ? section->path : "");
+	return new_node;
+}
+
+static void
+write_config_file ()
+{
+	gda_config_client *cfg_client;
+	xmlDocPtr doc;
+	xmlNodePtr root;
+	xmlNodePtr xml_section;
+	GList *ls; /* List of sections */
+	GList *le; /* List of entries */
+	gda_config_section *section;
+	gda_config_entry *entry;
+	gchar *user_config;
+
+	cfg_client = get_config_client ();
+	g_return_if_fail (cfg_client->user != NULL);
+
+	doc = xmlNewDoc ("1.0");
+	g_return_if_fail (doc != NULL);
+	root = xmlNewDocNode (doc, NULL, "libgda-config", NULL);
+	for (ls = cfg_client->user; ls; ls = ls->next){
+		section = ls->data;
+		if (section == NULL)
+			continue;
+
+		xml_section = add_xml_section (root, section);
+		for (le = section->entries; le; le = le->next){
+			entry = le->data;
+			if (entry == NULL)
+				continue;
+
+			add_xml_entry (xml_section, le->data);
+		}
+	}
+
+	user_config = g_strdup_printf ("%s%s", g_get_home_dir (),
+					LIBGDA_USER_CONFIG_FILE);
+	if (xmlSaveFormatFile (user_config, doc, TRUE) == -1){
+		g_warning ("Error saving config data to %s", user_config);
+	}
+
+	g_free (user_config);
+	xmlFreeDoc (doc);
+}
+
 /*
  * Public functions
  */
@@ -446,6 +511,7 @@
 		entry->value = g_strdup (new_value);
 	}
 
+	write_config_file ();
 	do_notify (path);
 }
 
@@ -489,6 +555,7 @@
 		entry->value = g_strdup_printf ("%d", new_value);
 	}
 
+	write_config_file ();
 	do_notify (path);
 }
 
@@ -532,6 +599,7 @@
 		entry->value = g_strdup_printf ("%f", new_value);
 	}
 
+	write_config_file ();
 	do_notify (path);
 }
 
@@ -575,6 +643,7 @@
 		entry->value = g_strdup_printf ("%d", new_value);
 	}
 
+	write_config_file ();
 	do_notify (path);
 }
 
@@ -601,6 +670,7 @@
 
 	cfg_client->user = g_list_remove (cfg_client->user, section);
 	free_section (section, NULL);
+	write_config_file ();
 	do_notify (path);
 }
 
@@ -647,6 +717,7 @@
 	if (entry != NULL){
 		section->entries = g_list_remove (section->entries, entry);
 		free_entry (entry, NULL);
+		write_config_file ();
 		do_notify (path);
 	}
 }
Index: libgda/gda-recordset.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-recordset.c,v
retrieving revision 1.20
diff -u -r1.20 gda-recordset.c
--- libgda/gda-recordset.c	1 May 2002 19:10:45 -0000	1.20
+++ libgda/gda-recordset.c	5 May 2002 18:45:47 -0000
@@ -31,6 +31,7 @@
 	GdaConnection *cnc;
 	GdaRecordsetFetchFunc fetch_func;
 	GdaRecordsetDescribeFunc describe_func;
+        GdaGetNRows get_n_rows_func;
 	gpointer user_data;
 	GdaRowAttributes *attributes;
 
@@ -58,16 +59,11 @@
 static gint
 gda_recordset_get_n_rows (GdaDataModel *model)
 {
-	gint n;
 	GdaRecordset *recset = (GdaRecordset *) model;
 
 	g_return_val_if_fail (GDA_IS_RECORDSET (recset), -1);
 
-	n = 0;
-	while (gda_data_model_get_value_at (GDA_DATA_MODEL (recset), 0, n))
-		n++;
-
-	return n;
+	return recset->priv->get_n_rows_func (recset, recset->priv->user_data);
 }
 
 static gint
@@ -108,17 +104,17 @@
 	for (i = fetched_count; i <= row; i++) {
 		GdaRow *row_data;
 		GList *value_list = NULL;
-		GList *l;
+		GdaField *field;
 
 		row_data = recset->priv->fetch_func (recset, i, recset->priv->user_data);
 		if (!row_data)
 			break;
 
-		for (l = row_data, datacol = 0; l != NULL; datacol++, l = l->next) {
+		for (datacol = 0; 
+		     (field = gda_row_get_field (row_data, datacol)) != NULL;
+		     datacol++) {
 			GdaValue *value;
-			GdaField *field;
 
-			field = gda_row_get_field (row_data, datacol);
 			value = gda_field_get_value (field);
 			value_list = g_list_append (value_list, value);
 		}
@@ -233,6 +229,7 @@
 gda_recordset_new (GdaConnection *cnc,
 		   GdaRecordsetFetchFunc fetch_func,
 		   GdaRecordsetDescribeFunc desc_func,
+		   GdaGetNRows get_n_rows_func,
 		   gpointer user_data)
 {
 	GdaRecordset *recset;
@@ -248,6 +245,7 @@
 	recset->priv->cnc = cnc;
 	recset->priv->fetch_func = fetch_func;
 	recset->priv->describe_func = desc_func;
+	recset->priv->get_n_rows_func = get_n_rows_func;
 	recset->priv->user_data = user_data;
 
 	/* get recordset description */
Index: libgda/gda-recordset.h
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-recordset.h,v
retrieving revision 1.9
diff -u -r1.9 gda-recordset.h
--- libgda/gda-recordset.h	27 Apr 2002 16:13:49 -0000	1.9
+++ libgda/gda-recordset.h	5 May 2002 18:45:47 -0000
@@ -55,10 +55,14 @@
 typedef GdaRowAttributes * (* GdaRecordsetDescribeFunc) (GdaRecordset *recset,
 							 gpointer user_data);
 
+typedef gint (* GdaGetNRows) (GdaRecordset *recset,
+				 gpointer user_data);
+
 GType          gda_recordset_get_type (void);
 GdaRecordset  *gda_recordset_new (GdaConnection *cnc,
 				  GdaRecordsetFetchFunc fetch_func,
 				  GdaRecordsetDescribeFunc desc_func,
+				  GdaGetNRows get_n_rows_func,
 				  gpointer user_data);
 GdaConnection *gda_recordset_get_connection (GdaRecordset *recset);
 const gchar   *gda_recordset_get_command_text (GdaRecordset *recset);
Index: libgda/gda-row.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-row.c,v
retrieving revision 1.27
diff -u -r1.27 gda-row.c
--- libgda/gda-row.c	5 May 2002 01:47:54 -0000	1.27
+++ libgda/gda-row.c	5 May 2002 18:45:47 -0000
@@ -178,7 +178,6 @@
 gint
 gda_row_attributes_get_length (GdaRowAttributes *attrs)
 {
-	g_return_val_if_fail (attrs != NULL, 0);
 	return g_list_length (attrs);
 }
 
Index: providers/mysql/gda-mysql-recordset.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/mysql/gda-mysql-recordset.c,v
retrieving revision 1.10
diff -u -r1.10 gda-mysql-recordset.c
--- providers/mysql/gda-mysql-recordset.c	27 Apr 2002 16:13:50 -0000	1.10
+++ providers/mysql/gda-mysql-recordset.c	5 May 2002 18:45:47 -0000
@@ -188,6 +188,17 @@
 	return attrs;
 }
 
+static gint
+get_n_rows_func (GdaRecordset *recset, gpointer user_data)
+{
+	MYSQL_RES *mysql_res = user_data;
+
+	g_return_val_if_fail (GDA_IS_RECORDSET (recset), 0);
+	g_return_val_if_fail (user_data != NULL, 0);
+
+	return mysql_num_rows (mysql_res);
+}
+
 /*
  * Public functions
  */
@@ -200,7 +211,8 @@
 	g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
 	g_return_val_if_fail (mysql_res != NULL, NULL);
 
-	recset = gda_recordset_new (cnc, fetch_func, describe_func, mysql_res);
+	recset = gda_recordset_new (cnc, fetch_func, describe_func,
+				    get_n_rows_func, mysql_res);
 	g_object_set_data_full (G_OBJECT (recset), OBJECT_DATA_RECSET_HANDLE,
 				mysql_res, (GDestroyNotify) free_mysql_res);
 
Index: providers/postgres/gda-postgres-recordset.c
===================================================================
RCS file: /cvs/gnome/libgda/providers/postgres/gda-postgres-recordset.c,v
retrieving revision 1.14
diff -u -r1.14 gda-postgres-recordset.c
--- providers/postgres/gda-postgres-recordset.c	27 Apr 2002 16:13:51 -0000	1.14
+++ providers/postgres/gda-postgres-recordset.c	5 May 2002 18:45:47 -0000
@@ -162,6 +162,22 @@
 	return attrs;
 }
 
+static gint
+get_n_rows_func (GdaRecordset *recset, gpointer user_data)
+{
+	GdaPostgresRecordsetPrivate *priv_data = user_data;
+	PGresult *pg_res;
+
+	g_return_val_if_fail (GDA_IS_RECORDSET (recset), 0);
+	g_return_val_if_fail (user_data != NULL, 0);
+
+	pg_res = priv_data->pg_res;
+	if (!pg_res)
+		return 0;
+
+	return PQntuples (pg_res);
+}
+
 /*
  * Public functions
  */
@@ -184,7 +200,8 @@
 	priv_data->type_data = cnc_priv_data->type_data;
 	priv_data->h_table = cnc_priv_data->h_table;
 
-	recset = gda_recordset_new (cnc, fetch_func, describe_func, priv_data);
+	recset = gda_recordset_new (cnc, fetch_func, describe_func,
+				    get_n_rows_func, priv_data);
 
 	g_object_set_data_full (G_OBJECT (recset), OBJECT_DATA_RECSET_HANDLE,
 				priv_data, (GDestroyNotify) free_postgres_res);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]