[libgda] Added the GDA_CONNECTION_OPTIONS_AUTO_META_DATA connection flag



commit a5eaa3a0ac13d4656131cd39074e8c20b2c06f4f
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Jul 7 17:03:53 2010 +0200

    Added the GDA_CONNECTION_OPTIONS_AUTO_META_DATA connection flag
    
    which, if specified, maintains up to date the meta store associated
    to the connection, if any.

 doc/C/tmpl/gda-connection.sgml                 |    1 +
 libgda/gda-connection.c                        |  263 +++++++++++++++++++++++-
 libgda/gda-connection.h                        |   16 ++-
 tools/browser/auth-dialog.c                    |    6 +-
 tools/browser/browser-connection-priv.h        |    9 +-
 tools/browser/browser-connection.c             |  215 ++++++++++++++------
 tools/browser/doc/tmpl/browser-connection.sgml |    6 +-
 tools/browser/login-dialog.c                   |    6 +-
 tools/gda-sql.c                                |    8 +-
 9 files changed, 459 insertions(+), 71 deletions(-)
---
diff --git a/doc/C/tmpl/gda-connection.sgml b/doc/C/tmpl/gda-connection.sgml
index 8e411cd..24ef318 100644
--- a/doc/C/tmpl/gda-connection.sgml
+++ b/doc/C/tmpl/gda-connection.sgml
@@ -173,6 +173,7 @@ A connection to a database
 @GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE: 
 @GDA_CONNECTION_OPTIONS_THREAD_SAFE: 
 @GDA_CONNECTION_OPTIONS_THREAD_ISOLATED: 
+ GDA_CONNECTION_OPTIONS_AUTO_META_DATA: 
 
 <!-- ##### ENUM GdaConnectionError ##### -->
 <para>
diff --git a/libgda/gda-connection.c b/libgda/gda-connection.c
index 25570e7..d0bbf09 100644
--- a/libgda/gda-connection.c
+++ b/libgda/gda-connection.c
@@ -85,6 +85,9 @@ struct _GdaConnectionPrivate {
 	guint                 next_task_id; /* starts at 1 as 0 is an error */
 	GArray               *waiting_tasks; /* array of CncTask pointers to tasks to be executed */
 	GArray               *completed_tasks; /* array of CncTask pointers to tasks already executed */
+
+	/* auto meta data update */
+	GArray               *trans_meta_context; /* Array of GdaMetaContext pointers */
 };
 
 /* represents an asynchronous execution task */
@@ -129,6 +132,7 @@ static void                 gda_connection_lock      (GdaLockable *lockable);
 static gboolean             gda_connection_trylock   (GdaLockable *lockable);
 static void                 gda_connection_unlock    (GdaLockable *lockable);
 
+static void update_meta_store_after_statement_exec (GdaConnection *cnc, GdaStatement *stmt, GdaSet *params);
 
 enum {
 	ERROR,
@@ -405,8 +409,11 @@ gda_connection_init (GdaConnection *cnc, GdaConnectionClass *klass)
 	cnc->priv->next_task_id = 1;
 	cnc->priv->waiting_tasks = g_array_new (FALSE, FALSE, sizeof (gpointer));
 	cnc->priv->completed_tasks = g_array_new (FALSE, FALSE, sizeof (gpointer));
+
+	cnc->priv->trans_meta_context = NULL;
 }
 
+static void auto_update_meta_context_free (GdaMetaContext *context);
 static void prepared_stms_foreach_func (GdaStatement *gda_stmt, GdaPStmt *prepared_stmt, GdaConnection *cnc);
 static void
 gda_connection_dispose (GObject *object)
@@ -464,6 +471,17 @@ gda_connection_dispose (GObject *object)
 		cnc->priv->completed_tasks = NULL;
 	}
 
+	if (cnc->priv->trans_meta_context) {
+		gint i;
+		for (i = 0; i < cnc->priv->trans_meta_context->len; i++) {
+			GdaMetaContext *context;
+			context = g_array_index (cnc->priv->trans_meta_context, GdaMetaContext*, i);
+			auto_update_meta_context_free (context);
+		}
+		g_array_free (cnc->priv->trans_meta_context, TRUE);
+		cnc->priv->trans_meta_context = NULL;
+	}
+
 	/* chain to parent class */
 	parent_class->dispose (object);
 }
@@ -1441,6 +1459,21 @@ gda_connection_close (GdaConnection *cnc)
 	gda_connection_unlock ((GdaLockable*) cnc);
 }
 
+static void
+add_connection_event_from_error (GdaConnection *cnc, GError **error)
+{
+	GdaConnectionEvent *event;
+	gchar *str;
+	event = gda_connection_event_new (GDA_CONNECTION_EVENT_WARNING);
+	str = g_strdup_printf (_("Error while maintaining the meta data up to date: %s"),
+			       error && *error && (*error)->message ? (*error)->message : _("No detail"));
+	gda_connection_event_set_description (event, str);
+	g_free (str);
+	if (error)
+		g_clear_error (error);
+	gda_connection_add_event (cnc, event);
+}
+
 /**
  * gda_connection_close_no_warning:
  * @cnc: a #GdaConnection object.
@@ -1466,6 +1499,26 @@ gda_connection_close_no_warning (GdaConnection *cnc)
 		return;
 	}
 
+	if (cnc->priv->meta_store &&
+	    cnc->priv->trans_meta_context &&
+	    gda_connection_get_transaction_status (cnc)) {
+		GdaConnection *mscnc;
+		mscnc = gda_meta_store_get_internal_connection (cnc->priv->meta_store);
+		if (cnc != mscnc) {
+			gint i;
+			for (i = 0; i < cnc->priv->trans_meta_context->len; i++) {
+				GdaMetaContext *context;
+				GError *lerror = NULL;
+				context = g_array_index (cnc->priv->trans_meta_context, GdaMetaContext*, i);
+				if (! gda_connection_update_meta_store (cnc, context, &lerror))
+					add_connection_event_from_error (cnc, &lerror);
+				auto_update_meta_context_free (context);
+			}
+			g_array_free (cnc->priv->trans_meta_context, TRUE);
+			cnc->priv->trans_meta_context = NULL;
+		}
+	}
+
 	/* get rid of prepared statements to avoid problems */
 	if (cnc->priv->prepared_stmts) {
 		g_hash_table_foreach (cnc->priv->prepared_stmts, 
@@ -2090,6 +2143,8 @@ async_stmt_exec_cb (GdaServerProvider *provider, GdaConnection *cnc, guint task_
 				g_array_remove_index (cnc->priv->waiting_tasks, 0);
 				g_array_append_val (cnc->priv->completed_tasks, task);
 			}
+			else
+				update_meta_store_after_statement_exec (cnc, task->stmt, task->params);
 			cnc_task_unlock (task);
 		}
 	}
@@ -2195,6 +2250,8 @@ gda_connection_async_statement_execute (GdaConnection *cnc, GdaStatement *stmt,
 			g_array_remove_index (cnc->priv->waiting_tasks, i);
 			g_array_append_val (cnc->priv->completed_tasks, task);
 		}
+		else
+			update_meta_store_after_statement_exec (cnc, task->stmt, task->params);
 		cnc_task_unlock (task);
 	}
 
@@ -2384,6 +2441,8 @@ gda_connection_statement_execute_v (GdaConnection *cnc, GdaStatement *stmt, GdaS
 								       model_usage, types, last_inserted_row, 
 								       NULL, NULL, NULL, error);
 	g_free (types);
+	if (obj)
+		update_meta_store_after_statement_exec (cnc, stmt, params);
 	gda_connection_unlock ((GdaLockable*) cnc);
 	g_object_unref ((GObject*) cnc);
 
@@ -2661,6 +2720,7 @@ gda_connection_statement_execute_select_fullv (GdaConnection *cnc, GdaStatement
 			      "%s", _("Statement is not a selection statement"));
 		g_object_unref (model);
 		model = NULL;
+		update_meta_store_after_statement_exec (cnc, stmt, params);
 	}
 	return model;
 }
@@ -2732,6 +2792,7 @@ gda_connection_statement_execute_select_full (GdaConnection *cnc, GdaStatement *
 			      "%s", _("Statement is not a selection statement"));
 		g_object_unref (model);
 		model = NULL;
+		update_meta_store_after_statement_exec (cnc, stmt, params);
 	}
 	return model;
 }
@@ -2809,8 +2870,10 @@ gda_connection_repetitive_statement_execute (GdaConnection *cnc, GdaRepetitiveSt
 				g_propagate_error (error, lerror);
 			}
 		}
-		else
-			retlist = g_slist_prepend (retlist, obj);	
+		else {
+			update_meta_store_after_statement_exec (cnc, stmt, (GdaSet*) list->data);
+			retlist = g_slist_prepend (retlist, obj);
+		}
 	}
 	g_slist_free (sets_list);
 
@@ -5282,3 +5345,199 @@ gda_connection_unlock  (GdaLockable *lockable)
 	
 	gda_mutex_unlock (cnc->priv->mutex);
 }
+
+static gchar *
+get_next_word (gchar *str, gboolean for_ident, gchar **out_next)
+{
+	if (!str) {
+		*out_next = NULL;
+		return NULL;
+	}
+
+	gchar *start;
+	gchar *ptr;
+	gboolean inquotes = FALSE;
+	for (ptr = str; *ptr; ptr++) {
+		if ((*ptr == ' ') || (*ptr == '\n') || (*ptr == '\t') || (*ptr == '\r'))
+			continue;
+		break;
+	}
+	start = ptr;
+	/*g_print ("%s ([%s]) => [%s]", __FUNCTION__, str, start);*/
+	for (; *ptr; ptr++) {
+		if ((*ptr >= 'a') && (*ptr <= 'z')) {
+			if (! for_ident)
+				*ptr += 'A' - 'a';
+			continue;
+		}
+		else if ((*ptr >= 'A') && (*ptr <= 'Z'))
+			continue;
+		else if ((*ptr >= '0') && (*ptr <= '9'))
+			continue;
+		else if (*ptr == '_')
+			continue;
+		else if (for_ident) {
+			if (*ptr == '\'') {
+				if (inquotes) {
+					*ptr = '"';
+					inquotes = FALSE;
+					ptr++;
+					break;
+				}
+				else {
+					*ptr = '"';
+					inquotes = TRUE;
+				}
+				continue;
+			}
+		}
+		break;
+	}
+	if (ptr != start) {
+		if (*ptr) {
+			*ptr = 0;
+			*out_next = ptr + 1;
+		}
+		else
+			*out_next = ptr;
+	}
+	else
+		*out_next = NULL;
+	/*g_print (" -- [%s]\n", *out_next);*/
+	return start;
+}
+
+
+/*
+ * the returned context has:
+ *  - the @table_name attribute as a static string
+ *  - the @column_names[x] as a static string, not the @column_names array itself which has to be freed
+ */
+static GdaMetaContext *
+meta_data_context_from_statement (GdaConnection *cnc, GdaStatement *stmt, GdaSet *params)
+{
+	GdaMetaContext *context = NULL;
+	gchar *sql, *current, *next;
+	sql = gda_statement_to_sql (stmt, params, NULL);
+	if (!sql)
+		return NULL;
+
+	current = get_next_word (sql, FALSE, &next);
+	if (current && (!strcmp (current, "CREATE") || !strcmp (current, "DROP") ||
+			!strcmp (current, "ALTER"))) {
+		current = get_next_word (next, FALSE, &next);
+		if (current && (!strcmp (current, "TABLE") || !strcmp (current, "VIEW"))) {
+			gchar *tmp;
+			current = get_next_word (next, TRUE, &next);
+			/*g_print ("CONTEXT: update for table [%s]\n", current);*/
+			context = g_new0 (GdaMetaContext, 1);
+			context->table_name = "_tables";
+			context->size = 1;
+			context->column_names = g_new0 (gchar *, 1);
+			context->column_names[0] = "table_name";
+			context->column_values = g_new0 (GValue *, 1);
+			tmp = gda_sql_identifier_quote (current, cnc, cnc->priv->provider_obj,
+							TRUE,
+							cnc->priv->options & GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE);
+			g_value_take_string ((context->column_values[0] = gda_value_new (G_TYPE_STRING)),
+					     tmp);
+		}
+	}
+
+	g_free (sql);
+	return context;
+}
+
+/*
+ * update_meta_store_after_statement_exec
+ *
+ * Updates the meta store associated to @cnc if it exists and if @cnc has the
+ * GDA_CONNECTION_OPTIONS_AUTO_META_DATA flag.
+ */
+static void
+update_meta_store_after_statement_exec (GdaConnection *cnc, GdaStatement *stmt, GdaSet *params)
+{
+	if (! cnc->priv->meta_store ||
+	    ! (cnc->priv->options & GDA_CONNECTION_OPTIONS_AUTO_META_DATA))
+		return;
+
+	GdaSqlStatementType type;
+	type = gda_statement_get_statement_type (stmt);
+	if (type == GDA_SQL_STATEMENT_BEGIN) {
+		/* initialize cnc->priv->trans_meta_context if meta store's connection is not @cnc */
+		GdaConnection *mscnc;
+		mscnc = gda_meta_store_get_internal_connection (cnc->priv->meta_store);
+		if (cnc != mscnc) {
+			g_assert (! cnc->priv->trans_meta_context);
+			cnc->priv->trans_meta_context = g_array_new (FALSE, FALSE, sizeof (GdaMetaContext*));
+		}
+		return;
+	}
+	else if (type == GDA_SQL_STATEMENT_ROLLBACK) {
+		/* re-run all the meta store updates started since the BEGIN */
+		GdaConnection *mscnc;
+		mscnc = gda_meta_store_get_internal_connection (cnc->priv->meta_store);
+		if (cnc != mscnc) {
+			gint i;
+			g_assert (cnc->priv->trans_meta_context);
+			for (i = 0; i < cnc->priv->trans_meta_context->len; i++) {
+				GdaMetaContext *context;
+				GError *lerror = NULL;
+				context = g_array_index (cnc->priv->trans_meta_context, GdaMetaContext*, i);
+				if (! gda_connection_update_meta_store (cnc, context, &lerror))
+					add_connection_event_from_error (cnc, &lerror);
+				auto_update_meta_context_free (context);
+			}
+			g_array_free (cnc->priv->trans_meta_context, TRUE);
+			cnc->priv->trans_meta_context = NULL;
+		}
+		return;
+	}
+	else if (type == GDA_SQL_STATEMENT_COMMIT) {
+		/* get rid of the meta store updates */
+		GdaConnection *mscnc;
+		mscnc = gda_meta_store_get_internal_connection (cnc->priv->meta_store);
+		if (cnc != mscnc) {
+			gint i;
+			g_assert (cnc->priv->trans_meta_context);
+			for (i = 0; i < cnc->priv->trans_meta_context->len; i++) {
+				GdaMetaContext *context;
+				context = g_array_index (cnc->priv->trans_meta_context, GdaMetaContext*, i);
+				auto_update_meta_context_free (context);
+			}
+			g_array_free (cnc->priv->trans_meta_context, TRUE);
+			cnc->priv->trans_meta_context = NULL;
+		}
+		return;
+	}
+	else if (type != GDA_SQL_STATEMENT_UNKNOWN)
+		return;
+
+	GdaMetaContext *context;
+	context = meta_data_context_from_statement (cnc, stmt, params);
+	if (context) {
+		GError *lerror = NULL;
+		if (! gda_connection_update_meta_store (cnc, context, &lerror))
+			add_connection_event_from_error (cnc, &lerror);
+		
+		if (cnc->priv->trans_meta_context)
+			g_array_prepend_val (cnc->priv->trans_meta_context, context);
+		else
+			auto_update_meta_context_free (context);
+	}
+}
+
+/*
+ * Free @context which must have been created by meta_data_context_from_statement()
+ */
+static void
+auto_update_meta_context_free (GdaMetaContext *context)
+{
+	gint i;
+	context->table_name = NULL; /* don't free */
+	g_free (context->column_names); /* don't free the strings in the array */
+	for (i = 0; i < context->size; i++)
+		gda_value_free (context->column_values[i]);
+	g_free (context->column_values);
+	g_free (context);
+}
diff --git a/libgda/gda-connection.h b/libgda/gda-connection.h
index dfe9946..e487491 100644
--- a/libgda/gda-connection.h
+++ b/libgda/gda-connection.h
@@ -97,7 +97,10 @@ struct _GdaConnectionClass {
  * @GDA_CONNECTION_OPTIONS_THREAD_ISOLATED: this flag specifies that the connection to open will be used 
  *                                     by several threads at once and requests that the real connection be used
  *                                     only in a sub thread created specifically for it
- *                                    
+ * @GDA_CONNECTION_OPTIONS_AUTO_META_DATA: this flags specifies that if a #GdaMetaStore has been associated
+ *                                     to the connection, then it is kept up to date with the evolutions in the
+ *                                     database's structure. Be aware however that there are some drawbacks
+ *                                     explained below.
  *
  * Specifies some aspects of a connection when opening it.
  *
@@ -129,13 +132,22 @@ struct _GdaConnectionClass {
  * Having a specific thread and a "wrapper connection" definitely has an impact on the performances (because it involves
  * messages passing between threads for every method call), so using the
  * GDA_CONNECTION_OPTIONS_THREAD_SAFE or GDA_CONNECTION_OPTIONS_THREAD_ISOLATED flags should be carefully considered.
+ *
+ * Note about the @GDA_CONNECTION_OPTIONS_AUTO_META_DATA flag:
+ * <itemizedlist>
+ *  <listitem><para>Every time a DDL statement is successfully executed, the associated mate data, if
+              defined, will be updated, which has a impact on performances</para></listitem>
+ *  <listitem><para>If a transaction is started and some DDL statements are executed and the transaction
+ *            is not rolled back or committed, then the meta data may end up being wrong</para></listitem>
+ * </itemizedlist>
  */
 typedef enum {
 	GDA_CONNECTION_OPTIONS_NONE = 0,
 	GDA_CONNECTION_OPTIONS_READ_ONLY = 1 << 0,
 	GDA_CONNECTION_OPTIONS_SQL_IDENTIFIERS_CASE_SENSITIVE = 1 << 1,
 	GDA_CONNECTION_OPTIONS_THREAD_SAFE = 1 << 2,
-	GDA_CONNECTION_OPTIONS_THREAD_ISOLATED = 1 << 3
+	GDA_CONNECTION_OPTIONS_THREAD_ISOLATED = 1 << 3,
+	GDA_CONNECTION_OPTIONS_AUTO_META_DATA = 1 << 4
 } GdaConnectionOptions;
 
 
diff --git a/tools/browser/auth-dialog.c b/tools/browser/auth-dialog.c
index 3aa932c..340d71f 100644
--- a/tools/browser/auth-dialog.c
+++ b/tools/browser/auth-dialog.c
@@ -233,12 +233,14 @@ sub_thread_open_cnc (AuthData *ad, GError **error)
 	GdaDsnInfo *info = &(ad->cncinfo);
 	if (info->name)
 		cnc = gda_connection_open_from_dsn (info->name, ad->auth_string ? ad->auth_string->str : NULL,
-						    GDA_CONNECTION_OPTIONS_THREAD_SAFE,
+						    GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+						    GDA_CONNECTION_OPTIONS_AUTO_META_DATA,
 						    error);
 	else
 		cnc = gda_connection_open_from_string (info->provider, info->cnc_string,
 						       ad->auth_string ? ad->auth_string->str : NULL,
-						       GDA_CONNECTION_OPTIONS_THREAD_SAFE,
+						       GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+						       GDA_CONNECTION_OPTIONS_AUTO_META_DATA,
 						       error);
 	return cnc;
 #else
diff --git a/tools/browser/browser-connection-priv.h b/tools/browser/browser-connection-priv.h
index 0cce524..dd599bd 100644
--- a/tools/browser/browser-connection-priv.h
+++ b/tools/browser/browser-connection-priv.h
@@ -26,8 +26,14 @@ struct _BrowserConnectionPrivate {
 	GdaThreadWrapper *wrapper;
 	GSList           *wrapper_jobs;
 	guint             wrapper_results_timer;
+	gboolean          long_timer;
+	gint              nb_no_job_waits; /* number of times check_for_wrapper_result() has been
+					      called without any job */
+
 	GHashTable       *executed_statements; /* key = guint exec ID, value = a StatementResult pointer */
 
+	gulong            meta_store_signal;
+
 	gchar         *name;
 	GdaConnection *cnc;
 	gchar         *dict_file_name;
@@ -35,7 +41,8 @@ struct _BrowserConnectionPrivate {
 
 	GdaDsnInfo     dsn_info;
 	GMutex        *p_mstruct_mutex;
-	GdaMetaStruct *p_mstruct; /* private GdaMetaStruct: while it is being created */
+	GSList        *p_mstruct_list; /* private GdaMetaStruct list: while they are being created */
+	GdaMetaStruct *c_mstruct; /* last GdaMetaStruct up to date, ready to be passed as @mstruct */
 	GdaMetaStruct *mstruct; /* public GdaMetaStruct: once it has been created and is no more modified */
 
 	BrowserFavorites *bfav;
diff --git a/tools/browser/browser-connection.c b/tools/browser/browser-connection.c
index b8a75ae..5dbc2b8 100644
--- a/tools/browser/browser-connection.c
+++ b/tools/browser/browser-connection.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Vivien Malerba
+ * Copyright (C) 2009 - 2010 Vivien Malerba
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -31,6 +31,9 @@
 /* code inclusion */
 #include "../dict-file-name.c"
 
+#define CHECK_RESULTS_SHORT_TIMER 200
+#define CHECK_RESULTS_LONG_TIMER 2
+
 typedef struct {
 	GObject *result;
 	GError  *error;
@@ -89,10 +92,21 @@ static void
 push_wrapper_job (BrowserConnection *bcnc, guint job_id, JobType job_type, const gchar *reason)
 {
 	/* setup timer */
-	if (bcnc->priv->wrapper_results_timer == 0)
-		bcnc->priv->wrapper_results_timer = g_timeout_add (200,
+	if (bcnc->priv->wrapper_results_timer == 0) {
+		bcnc->priv->long_timer = FALSE;
+		bcnc->priv->wrapper_results_timer = g_timeout_add (CHECK_RESULTS_SHORT_TIMER,
 								   (GSourceFunc) check_for_wrapper_result,
 								   bcnc);
+	}
+	else if (bcnc->priv->long_timer) {
+		/* switch to a short timer to check for results */
+		g_source_remove (bcnc->priv->wrapper_results_timer);
+		bcnc->priv->wrapper_results_timer = g_timeout_add (CHECK_RESULTS_SHORT_TIMER,
+								   (GSourceFunc) check_for_wrapper_result,
+								   bcnc);
+		bcnc->priv->long_timer = FALSE;
+		bcnc->priv->nb_no_job_waits = 0;
+	}
 
 	/* add WrapperJob structure */
 	WrapperJob *wj;
@@ -241,6 +255,8 @@ browser_connection_init (BrowserConnection *bcnc)
 	bcnc->priv->wrapper = gda_thread_wrapper_new ();
 	bcnc->priv->wrapper_jobs = NULL;
 	bcnc->priv->wrapper_results_timer = 0;
+	bcnc->priv->long_timer = FALSE;
+	bcnc->priv->nb_no_job_waits = 0;
 	bcnc->priv->executed_statements = NULL;
 
 	bcnc->priv->name = g_strdup_printf (_("c%u"), index++);
@@ -249,9 +265,12 @@ browser_connection_init (BrowserConnection *bcnc)
 	bcnc->priv->variables = NULL;
 	memset (&(bcnc->priv->dsn_info), 0, sizeof (GdaDsnInfo));
 	bcnc->priv->p_mstruct_mutex = g_mutex_new ();
-	bcnc->priv->p_mstruct = NULL;
+	bcnc->priv->p_mstruct_list = NULL;
+	bcnc->priv->c_mstruct = NULL;
 	bcnc->priv->mstruct = NULL;
 
+	bcnc->priv->meta_store_signal = 0;
+
 	bcnc->priv->bfav = NULL;
 
 	bcnc->priv->store_cnc = NULL;
@@ -280,19 +299,61 @@ wrapper_meta_store_update (BrowserConnection *bcnc, GError **error)
 static gpointer
 wrapper_meta_struct_sync (BrowserConnection *bcnc, GError **error)
 {
-	gboolean retval;
+	gboolean retval = TRUE;
+	GdaMetaStruct *mstruct;
 
 	g_mutex_lock (bcnc->priv->p_mstruct_mutex);
-	g_assert (bcnc->priv->p_mstruct);
-	g_object_ref (G_OBJECT (bcnc->priv->p_mstruct));
-	retval = gda_meta_struct_complement_all (bcnc->priv->p_mstruct, error);
-	g_object_unref (G_OBJECT (bcnc->priv->p_mstruct));
+	g_assert (bcnc->priv->p_mstruct_list);
+	mstruct = (GdaMetaStruct *) bcnc->priv->p_mstruct_list->data;
+	bcnc->priv->p_mstruct_list = g_slist_delete_link (bcnc->priv->p_mstruct_list,
+							  bcnc->priv->p_mstruct_list);
+	if (bcnc->priv->p_mstruct_list)
+		/* don't care about this one */
+		g_object_unref (G_OBJECT (mstruct));
+	else {
+		if (bcnc->priv->c_mstruct)
+			g_object_unref (bcnc->priv->c_mstruct);
+		bcnc->priv->c_mstruct = mstruct;
+		/*g_print ("Meta struct sync for %p\n", mstruct);*/
+		retval = gda_meta_struct_complement_all (mstruct, error);
+	}
 	g_mutex_unlock (bcnc->priv->p_mstruct_mutex);
 
 	return GINT_TO_POINTER (retval ? 2 : 1);
 }
 
 static void
+meta_changed_cb (GdaThreadWrapper *wrapper,
+		 GdaMetaStore *store,
+		 const gchar *signame,
+		 gint n_param_values,
+		 const GValue *param_values,
+		 gpointer gda_reserved,
+		 BrowserConnection *bcnc)
+{
+	guint job_id;
+	GError *lerror = NULL;
+	GdaMetaStruct *mstruct;
+	
+	g_mutex_lock (bcnc->priv->p_mstruct_mutex);
+	mstruct = gda_meta_struct_new (gda_connection_get_meta_store (bcnc->priv->cnc),
+				       GDA_META_STRUCT_FEATURE_ALL);
+	bcnc->priv->p_mstruct_list = g_slist_append (bcnc->priv->p_mstruct_list, mstruct);
+	g_mutex_unlock (bcnc->priv->p_mstruct_mutex);
+	job_id = gda_thread_wrapper_execute (bcnc->priv->wrapper,
+					     (GdaThreadWrapperFunc) wrapper_meta_struct_sync,
+					     g_object_ref (bcnc), g_object_unref, &lerror);
+	if (job_id > 0)
+		push_wrapper_job (bcnc, job_id, JOB_TYPE_META_STRUCT_SYNC,
+				  _("Analysing database schema"));
+	else if (lerror) {
+		browser_show_error (NULL, _("Error while fetching meta data from the connection: %s"),
+				    lerror->message ? lerror->message : _("No detail"));
+		g_error_free (lerror);
+	}
+}
+
+static void
 browser_connection_set_property (GObject *object,
 				 guint param_id,
 				 const GValue *value,
@@ -354,8 +415,11 @@ browser_connection_set_property (GObject *object,
 			else {
 				guint job_id;
 				GError *lerror = NULL;
-				
-				bcnc->priv->p_mstruct = gda_meta_struct_new (store, GDA_META_STRUCT_FEATURE_ALL);
+				GdaMetaStruct *mstruct;
+
+				mstruct = gda_meta_struct_new (store, GDA_META_STRUCT_FEATURE_ALL);
+				bcnc->priv->p_mstruct_list = g_slist_append (bcnc->priv->p_mstruct_list,
+									     mstruct);
 				job_id = gda_thread_wrapper_execute (bcnc->priv->wrapper,
 								     (GdaThreadWrapperFunc) wrapper_meta_struct_sync,
 								     g_object_ref (bcnc), g_object_unref, &lerror);
@@ -369,11 +433,18 @@ browser_connection_set_property (GObject *object,
 				}
 				g_object_unref (store);
 			}
+			bcnc->priv->meta_store_signal =
+				gda_thread_wrapper_connect_raw (bcnc->priv->wrapper, store, "meta-changed",
+								FALSE, FALSE,
+								(GdaThreadWrapperCallback) meta_changed_cb,
+								bcnc);
                         break;
                 }
         }
 }
 
+
+
 static void
 browser_connection_get_property (GObject *object,
 				 guint param_id,
@@ -448,14 +519,22 @@ browser_connection_dispose (GObject *object)
 		if (bcnc->priv->wrapper_results_timer > 0)
 			g_source_remove (bcnc->priv->wrapper_results_timer);
 
+		if (bcnc->priv->meta_store_signal)
+			gda_thread_wrapper_disconnect (bcnc->priv->wrapper,
+						       bcnc->priv->meta_store_signal);
 		g_object_unref (bcnc->priv->wrapper);
 		g_free (bcnc->priv->name);
+		if (bcnc->priv->c_mstruct)
+			g_object_unref (bcnc->priv->c_mstruct);
 		if (bcnc->priv->mstruct)
 			g_object_unref (bcnc->priv->mstruct);
-		if (bcnc->priv->p_mstruct)
-			g_object_unref (bcnc->priv->p_mstruct);
+		if (bcnc->priv->p_mstruct_list) {
+			g_slist_foreach (bcnc->priv->p_mstruct_list, (GFunc) g_object_unref, NULL);
+			g_slist_free (bcnc->priv->p_mstruct_list);
+		}
 		if (bcnc->priv->p_mstruct_mutex)
 			g_mutex_free (bcnc->priv->p_mstruct_mutex);
+
 		if (bcnc->priv->cnc) {
 			g_signal_handlers_disconnect_by_func (bcnc->priv->cnc,
 							      G_CALLBACK (transaction_status_changed_cb),
@@ -485,9 +564,34 @@ check_for_wrapper_result (BrowserConnection *bcnc)
 	GError *lerror = NULL;
 	gpointer exec_res = NULL;
 	WrapperJob *wj;
+	gboolean retval = TRUE; /* return FALSE to interrupt current timer */
 
-	if (!bcnc->priv->wrapper_jobs)
-		goto out;
+	if (!bcnc->priv->wrapper_jobs) {
+		gda_thread_wrapper_iterate (bcnc->priv->wrapper, FALSE);
+		if (! bcnc->priv->long_timer) {
+			if (bcnc->priv->nb_no_job_waits > 100) {
+				/* switch to a long timer to check for results */
+				bcnc->priv->wrapper_results_timer = g_timeout_add_seconds (CHECK_RESULTS_LONG_TIMER,
+											   (GSourceFunc) check_for_wrapper_result,
+											   bcnc);
+				bcnc->priv->nb_no_job_waits = 0;
+				bcnc->priv->long_timer = TRUE;
+				return FALSE;
+			}
+			else
+				bcnc->priv->nb_no_job_waits ++;
+		}
+		return TRUE;
+	}
+	else if (bcnc->priv->long_timer) {
+		/* switch to a short timer to check for results */
+		bcnc->priv->wrapper_results_timer = g_timeout_add (CHECK_RESULTS_SHORT_TIMER,
+								   (GSourceFunc) check_for_wrapper_result,
+								   bcnc);
+		retval = FALSE;
+		bcnc->priv->long_timer = FALSE;
+		bcnc->priv->nb_no_job_waits = 0;
+	}
 
 	wj = (WrapperJob*) bcnc->priv->wrapper_jobs->data;
 	exec_res = gda_thread_wrapper_fetch_result (bcnc->priv->wrapper,
@@ -501,26 +605,17 @@ check_for_wrapper_result (BrowserConnection *bcnc)
 						    lerror && lerror->message ? lerror->message : _("No detail"));
 				g_clear_error (&lerror);
 			}
-			else {
-				guint job_id;
-				
-				g_mutex_lock (bcnc->priv->p_mstruct_mutex);
-				if (bcnc->priv->p_mstruct)
-					g_object_unref (G_OBJECT (bcnc->priv->p_mstruct));
-				bcnc->priv->p_mstruct = gda_meta_struct_new (gda_connection_get_meta_store (bcnc->priv->cnc),
-									     GDA_META_STRUCT_FEATURE_ALL);
-				g_mutex_unlock (bcnc->priv->p_mstruct_mutex);
-				job_id = gda_thread_wrapper_execute (bcnc->priv->wrapper,
-								     (GdaThreadWrapperFunc) wrapper_meta_struct_sync,
-								     g_object_ref (bcnc), g_object_unref, &lerror);
-				if (job_id > 0)
-					push_wrapper_job (bcnc, job_id, JOB_TYPE_META_STRUCT_SYNC,
-							  _("Analysing database schema"));
-				else if (lerror) {
-					browser_show_error (NULL, _("Error while fetching meta data from the connection: %s"),
-							    lerror->message ? lerror->message : _("No detail"));
-					g_error_free (lerror);
-				}
+			else if (! bcnc->priv->meta_store_signal) {
+				GdaMetaStore *store;
+				store = gda_connection_get_meta_store (bcnc->priv->cnc);
+				meta_changed_cb (bcnc->priv->wrapper, store,
+						 NULL, 0, NULL, NULL, bcnc);
+				bcnc->priv->meta_store_signal =
+					gda_thread_wrapper_connect_raw (bcnc->priv->wrapper, store, "meta-changed",
+									FALSE, FALSE,
+									(GdaThreadWrapperCallback) meta_changed_cb,
+									bcnc);
+
 			}
 			break;
 		}
@@ -532,24 +627,27 @@ check_for_wrapper_result (BrowserConnection *bcnc)
 			}
 			else {
 				g_mutex_lock (bcnc->priv->p_mstruct_mutex);
-				GdaMetaStruct *old_mstruct;
-				old_mstruct = bcnc->priv->mstruct;
-				bcnc->priv->mstruct = bcnc->priv->p_mstruct;
-				bcnc->priv->p_mstruct = NULL;
-				if (old_mstruct)
-					g_object_unref (old_mstruct);
+				
+				if (bcnc->priv->c_mstruct) {
+					GdaMetaStruct *old_mstruct;
+					old_mstruct = bcnc->priv->mstruct;
+					bcnc->priv->mstruct = bcnc->priv->c_mstruct;
+					bcnc->priv->c_mstruct = NULL;
+					if (old_mstruct)
+						g_object_unref (old_mstruct);
 #ifdef GDA_DEBUG_NO
-				GSList *all, *list;
-				all = gda_meta_struct_get_all_db_objects (bcnc->priv->mstruct);
-				for (list = all; list; list = list->next) {
-					GdaMetaDbObject *dbo = (GdaMetaDbObject *) list->data;
-					g_print ("DBO, Type %d: short=>[%s] schema=>[%s] full=>[%s]\n", dbo->obj_type,
-						 dbo->obj_short_name, dbo->obj_schema, dbo->obj_full_name);
-				}
-				g_slist_free (all);
+					GSList *all, *list;
+					g_print ("For GdaMetaStruct %p:\n", bcnc->priv->mstruct);
+					all = gda_meta_struct_get_all_db_objects (bcnc->priv->mstruct);
+					for (list = all; list; list = list->next) {
+						GdaMetaDbObject *dbo = (GdaMetaDbObject *) list->data;
+						g_print ("DBO, Type %d: short=>[%s] schema=>[%s] full=>[%s]\n", dbo->obj_type,
+							 dbo->obj_short_name, dbo->obj_schema, dbo->obj_full_name);
+					}
+					g_slist_free (all);
 #endif
-
-				g_signal_emit (bcnc, browser_connection_signals [META_CHANGED], 0, bcnc->priv->mstruct);
+					g_signal_emit (bcnc, browser_connection_signals [META_CHANGED], 0, bcnc->priv->mstruct);
+				}
 				g_mutex_unlock (bcnc->priv->p_mstruct_mutex);
 			}
 			break;
@@ -581,17 +679,12 @@ check_for_wrapper_result (BrowserConnection *bcnc)
 		pop_wrapper_job (bcnc, wj);
 	}
 
- out:
-	if (!bcnc->priv->wrapper_jobs) {
-		bcnc->priv->wrapper_results_timer = 0;
-		return FALSE;
-	}
-	else {
+	if (bcnc->priv->wrapper_jobs) {
 		wj = (WrapperJob*) bcnc->priv->wrapper_jobs->data;
 		if (exec_res)
 			g_signal_emit (bcnc, browser_connection_signals [BUSY], 0, TRUE, wj->reason);
-		return TRUE;
 	}
+	return retval;
 }
 
 /**
@@ -721,6 +814,12 @@ browser_connection_update_meta_data (BrowserConnection *bcnc)
 		}
 	}
 
+	if (bcnc->priv->meta_store_signal) {
+		gda_thread_wrapper_disconnect (bcnc->priv->wrapper,
+					       bcnc->priv->meta_store_signal);
+		bcnc->priv->meta_store_signal = 0;
+	}
+
 	guint job_id;
 	GError *lerror = NULL;
 	job_id = gda_thread_wrapper_execute (bcnc->priv->wrapper,
diff --git a/tools/browser/doc/tmpl/browser-connection.sgml b/tools/browser/doc/tmpl/browser-connection.sgml
index a6dc66d..72a8c5f 100644
--- a/tools/browser/doc/tmpl/browser-connection.sgml
+++ b/tools/browser/doc/tmpl/browser-connection.sgml
@@ -29,14 +29,18 @@ An opened connection
 @wrapper: 
 @wrapper_jobs: 
 @wrapper_results_timer: 
+ long_timer: 
+ nb_no_job_waits: 
 @executed_statements: 
+ meta_store_signal: 
 @name: 
 @cnc: 
 @dict_file_name: 
 @parser: 
 @dsn_info: 
 @p_mstruct_mutex: 
- p_mstruct: 
+ p_mstruct_list: 
+ c_mstruct: 
 @mstruct: 
 @bfav: 
 @busy: 
diff --git a/tools/browser/login-dialog.c b/tools/browser/login-dialog.c
index ccd5d64..deec133 100644
--- a/tools/browser/login-dialog.c
+++ b/tools/browser/login-dialog.c
@@ -281,12 +281,14 @@ sub_thread_open_cnc (GdaDsnInfo *info, GError **error)
 	GdaConnection *cnc;
 	if (info->name)
 		cnc = gda_connection_open_from_dsn (info->name, info->auth_string,
-						    GDA_CONNECTION_OPTIONS_THREAD_SAFE,
+						    GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+						    GDA_CONNECTION_OPTIONS_AUTO_META_DATA,
 						    error);
 	else
 		cnc = gda_connection_open_from_string (info->provider, info->cnc_string,
 						       info->auth_string,
-						       GDA_CONNECTION_OPTIONS_THREAD_SAFE,
+						       GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+						       GDA_CONNECTION_OPTIONS_AUTO_META_DATA,
 						       error);
 	return cnc;
 #else
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index c836709..db88b8f 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -1376,10 +1376,12 @@ open_connection (SqlConsole *console, const gchar *cnc_name, const gchar *cnc_st
 	
 	if (info && !real_provider)
 		newcnc = gda_connection_open_from_dsn (real_cnc_string, real_auth_string,
-						       GDA_CONNECTION_OPTIONS_THREAD_SAFE, error);
+						       GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+						       GDA_CONNECTION_OPTIONS_AUTO_META_DATA, error);
 	else 
 		newcnc = gda_connection_open_from_string (NULL, real_cnc_string, real_auth_string,
-							  GDA_CONNECTION_OPTIONS_THREAD_SAFE, error);
+							  GDA_CONNECTION_OPTIONS_THREAD_SAFE |
+							  GDA_CONNECTION_OPTIONS_AUTO_META_DATA, error);
 
 	g_free (real_cnc_string);
 	g_free (real_cnc);
@@ -1775,7 +1777,7 @@ output_string (const gchar *str)
 	}
 
 	length = strlen (str);
-	if (str[length - 1] != '\n')
+	if (*str && (str[length - 1] != '\n'))
 		append_nl = TRUE;
 
 	if (main_data->output_stream)



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