[libgda] GdaBrowser: added browser_connection_execute_statement_cb()
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] GdaBrowser: added browser_connection_execute_statement_cb()
- Date: Wed, 21 Jul 2010 19:18:28 +0000 (UTC)
commit ceeb932d0e360e3d8c07f0ad07c305cf6c861ed0
Author: Vivien Malerba <malerba gnome-db org>
Date: Wed Jul 21 17:14:09 2010 +0200
GdaBrowser: added browser_connection_execute_statement_cb()
tools/browser/browser-connection-priv.h | 3 +
tools/browser/browser-connection.c | 121 +++++++++++++++++++++++-
tools/browser/browser-connection.h | 20 ++++
tools/browser/doc/gda-browser-sections.txt | 2 +
tools/browser/doc/tmpl/browser-connection.sgml | 31 ++++++
tools/browser/schema-browser/table-info.c | 58 ++++--------
6 files changed, 192 insertions(+), 43 deletions(-)
---
diff --git a/tools/browser/browser-connection-priv.h b/tools/browser/browser-connection-priv.h
index dd599bd..e91062d 100644
--- a/tools/browser/browser-connection-priv.h
+++ b/tools/browser/browser-connection-priv.h
@@ -53,6 +53,9 @@ struct _BrowserConnectionPrivate {
GdaConnection *store_cnc;
GdaSet *variables;
+
+ GSList *results_list; /* list of #ExecCallbackData pointers */
+ gulong results_timer_id;
};
void browser_connection_set_busy_state (BrowserConnection *bcnc, gboolean busy, const gchar *busy_reason);
diff --git a/tools/browser/browser-connection.c b/tools/browser/browser-connection.c
index 52f271f..c4d7036 100644
--- a/tools/browser/browser-connection.c
+++ b/tools/browser/browser-connection.c
@@ -498,6 +498,16 @@ browser_connection_dispose (GObject *object)
bcnc = BROWSER_CONNECTION (object);
if (bcnc->priv) {
+ if (bcnc->priv->results_timer_id) {
+ g_source_remove (bcnc->priv->results_timer_id);
+ bcnc->priv->results_timer_id = 0;
+ }
+ if (bcnc->priv->results_list) {
+ g_slist_foreach (bcnc->priv->results_list, (GFunc) g_free, NULL);
+ g_slist_free (bcnc->priv->results_list);
+ bcnc->priv->results_list = NULL;
+ }
+
if (bcnc->priv->variables)
g_object_unref (bcnc->priv->variables);
@@ -1050,7 +1060,8 @@ wrapper_statement_execute (StmtExecData *data, GError **error)
* @need_last_insert_row: %TRUE if the values of the last interted row must be computed
* @error: a place to store errors, or %NULL
*
- * Executes @stmt by @bcnc
+ * Executes @stmt by @bcnc. Unless specific requirements, it's easier to use
+ * browser_connection_execute_statement_cb().
*
* Returns: a job ID, to be used with browser_connection_execution_get_result(), or %0 if an
* error occurred
@@ -1175,11 +1186,115 @@ browser_connection_execution_get_result (BrowserConnection *bcnc, guint exec_id,
}
g_hash_table_remove (bcnc->priv->executed_statements, &id);
- if (GDA_IS_DATA_MODEL (retval))
- gda_data_model_dump (GDA_DATA_MODEL (retval), NULL);
+ /*if (GDA_IS_DATA_MODEL (retval))
+ gda_data_model_dump (GDA_DATA_MODEL (retval), NULL);*/
return retval;
}
+static gboolean query_exec_fetch_cb (BrowserConnection *bcnc);
+
+typedef struct {
+ guint exec_id;
+ gboolean need_last_insert_row;
+ BrowserConnectionExecuteCallback callback;
+ gpointer cb_data;
+} ExecCallbackData;
+
+/**
+ * browser_connection_execute_statement_cb
+ * @bcnc: a #BrowserConnection
+ * @stmt: a #GdaStatement
+ * @params: a #GdaSet as parameters, or %NULL
+ * @model_usage: how the returned data model (if any) will be used
+ * @need_last_insert_row: %TRUE if the values of the last interted row must be computed
+ * @callback: the function to call when statement has been executed
+ * @data: data to pass to @callback, or %NULL
+ * @error: a place to store errors, or %NULL
+ *
+ * Executes @stmt by @bcnc and calls @callback when done. This occurs in the UI thread and avoids
+ * having to set up a waiting mechanism to call browser_connection_execution_get_result()
+ * repeatedly.
+ *
+ * Returns: a job ID, or %0 if an error occurred
+ */
+guint
+browser_connection_execute_statement_cb (BrowserConnection *bcnc,
+ GdaStatement *stmt,
+ GdaSet *params,
+ GdaStatementModelUsage model_usage,
+ gboolean need_last_insert_row,
+ BrowserConnectionExecuteCallback callback,
+ gpointer data,
+ GError **error)
+{
+ guint exec_id;
+ g_return_val_if_fail (callback, 0);
+
+ exec_id = browser_connection_execute_statement (bcnc, stmt, params, model_usage,
+ need_last_insert_row, error);
+ if (!exec_id)
+ return 0;
+ ExecCallbackData *cbdata;
+ cbdata = g_new0 (ExecCallbackData, 1);
+ cbdata->exec_id = exec_id;
+ cbdata->need_last_insert_row = need_last_insert_row;
+ cbdata->callback = callback;
+ cbdata->cb_data = data;
+
+ bcnc->priv->results_list = g_slist_append (bcnc->priv->results_list, cbdata);
+ if (! bcnc->priv->results_timer_id)
+ bcnc->priv->results_timer_id = g_timeout_add (200,
+ (GSourceFunc) query_exec_fetch_cb,
+ bcnc);
+ return exec_id;
+}
+
+static gboolean
+query_exec_fetch_cb (BrowserConnection *bcnc)
+{
+ GObject *res;
+ GError *lerror = NULL;
+ ExecCallbackData *cbdata;
+ GdaSet *last_inserted_row = NULL;
+
+ g_print (".");
+ if (!bcnc->priv->results_list)
+ goto out;
+
+ cbdata = (ExecCallbackData *) bcnc->priv->results_list->data;
+
+ if (cbdata->need_last_insert_row)
+ res = browser_connection_execution_get_result (bcnc,
+ cbdata->exec_id,
+ &last_inserted_row,
+ &lerror);
+ else
+ res = browser_connection_execution_get_result (bcnc,
+ cbdata->exec_id, NULL,
+ &lerror);
+
+ if (res || lerror) {
+ cbdata->callback (bcnc, cbdata->exec_id, res, last_inserted_row, lerror, cbdata->cb_data);
+ if (res)
+ g_object_unref (res);
+ if (last_inserted_row)
+ g_object_unref (last_inserted_row);
+ g_clear_error (&lerror);
+
+ bcnc->priv->results_list = g_slist_remove (bcnc->priv->results_list, cbdata);
+ g_free (cbdata);
+ }
+
+ out:
+ if (! bcnc->priv->results_list) {
+ bcnc->priv->results_timer_id = 0;
+ return FALSE;
+ }
+ else
+ return TRUE; /* keep timer */
+}
+
+
/**
* browser_connection_normalize_sql_statement
* @bcnc: a #BrowserConnection
diff --git a/tools/browser/browser-connection.h b/tools/browser/browser-connection.h
index 327239f..fd599d9 100644
--- a/tools/browser/browser-connection.h
+++ b/tools/browser/browser-connection.h
@@ -94,6 +94,26 @@ GObject *browser_connection_execution_get_result (BrowserConnection
GdaSet **last_insert_row, GError **error);
gboolean browser_connection_normalize_sql_statement(BrowserConnection *bcnc,
GdaSqlStatement *sqlst, GError **error);
+/**
+ * BrowserConnectionExecuteCallback
+ *
+ * Callback function called by browser_connection_execute_statement_cb(). If you need to keep
+ * some of the arguments for a later usage, you need to ref/copy them.
+ */
+typedef void (*BrowserConnectionExecuteCallback) (BrowserConnection *bcnc,
+ guint exec_id,
+ GObject *out_result,
+ GdaSet *out_last_inserted_row, GError *error,
+ gpointer data);
+
+guint browser_connection_execute_statement_cb (BrowserConnection *bcnc,
+ GdaStatement *stmt,
+ GdaSet *params,
+ GdaStatementModelUsage model_usage,
+ gboolean need_last_insert_row,
+ BrowserConnectionExecuteCallback callback,
+ gpointer data,
+ GError **error);
/*
* transactions
diff --git a/tools/browser/doc/gda-browser-sections.txt b/tools/browser/doc/gda-browser-sections.txt
index abd65dc..879e772 100644
--- a/tools/browser/doc/gda-browser-sections.txt
+++ b/tools/browser/doc/gda-browser-sections.txt
@@ -86,6 +86,8 @@ browser_connection_create_parser
browser_connection_render_pretty_sql
browser_connection_execute_statement
browser_connection_execution_get_result
+BrowserConnectionExecuteCallback
+browser_connection_execute_statement_cb
browser_connection_normalize_sql_statement
<SUBSECTION>
browser_connection_begin
diff --git a/tools/browser/doc/tmpl/browser-connection.sgml b/tools/browser/doc/tmpl/browser-connection.sgml
index 72a8c5f..4eb3302 100644
--- a/tools/browser/doc/tmpl/browser-connection.sgml
+++ b/tools/browser/doc/tmpl/browser-connection.sgml
@@ -47,6 +47,8 @@ An opened connection
@busy_reason:
@store_cnc:
@variables:
+ results_list:
+ results_timer_id:
<!-- ##### STRUCT BrowserConnection ##### -->
<para>
@@ -239,6 +241,35 @@ An opened connection
@Returns:
+<!-- ##### USER_FUNCTION BrowserConnectionExecuteCallback ##### -->
+<para>
+
+</para>
+
+ bcnc:
+ exec_id:
+ out_result:
+ out_last_inserted_row:
+ error:
+ data:
+
+
+<!-- ##### FUNCTION browser_connection_execute_statement_cb ##### -->
+<para>
+
+</para>
+
+ bcnc:
+ stmt:
+ params:
+ model_usage:
+ need_last_insert_row:
+ callback:
+ data:
+ error:
+ Returns:
+
+
<!-- ##### FUNCTION browser_connection_normalize_sql_statement ##### -->
<para>
diff --git a/tools/browser/schema-browser/table-info.c b/tools/browser/schema-browser/table-info.c
index 15c5f48..ddf6693 100644
--- a/tools/browser/schema-browser/table-info.c
+++ b/tools/browser/schema-browser/table-info.c
@@ -53,8 +53,6 @@ struct _TableInfoPrivate {
GtkWidget *pages; /* notebook to store individual pages */
GtkWidget *insert_popup;
- guint exec_id;
- guint timeout_id; /* timout ID to fetch execution results */
};
static void table_info_class_init (TableInfoClass *klass);
@@ -129,8 +127,6 @@ table_info_dispose (GObject *object)
/* free memory */
if (tinfo->priv) {
- if (tinfo->priv->timeout_id)
- g_source_remove (tinfo->priv->timeout_id);
if (tinfo->priv->insert_popup)
gtk_widget_destroy (tinfo->priv->insert_popup);
g_free (tinfo->priv->schema);
@@ -505,32 +501,21 @@ insert_form_params_changed_cb (GdauiBasicForm *form, GdaHolder *param,
gdaui_basic_form_is_valid (form));
}
-static gboolean
-query_exec_fetch_cb (TableInfo *tinfo)
+static void statement_executed_cb (BrowserConnection *bcnc,
+ guint exec_id,
+ GObject *out_result,
+ GdaSet *out_last_inserted_row, GError *error,
+ TableInfo *tinfo)
{
- GObject *res;
- GError *lerror = NULL;
- gboolean alldone = FALSE;
-
- res = browser_connection_execution_get_result (tinfo->priv->bcnc,
- tinfo->priv->exec_id, NULL,
- &lerror);
- if (res) {
- alldone = TRUE;
- }
- else if (lerror) {
+ if (error)
browser_show_error (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tinfo)),
_("Error executing query:\n%s"),
- lerror && lerror->message ?
- lerror->message : _("No detail"));
- g_clear_error (&lerror);
- alldone = TRUE;
- }
-
- if (alldone)
- tinfo->priv->timeout_id = 0;
-
- return alldone;
+ error->message ?
+ error->message : _("No detail"));
+ else
+ browser_show_notice (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tinfo)),
+ "DataInsertQuery",
+ _("Data successfully inserted"));
}
static void
@@ -539,29 +524,22 @@ insert_response_cb (GtkWidget *dialog, gint response_id, TableInfo *tinfo)
if (response_id == GTK_RESPONSE_ACCEPT) {
GdaStatement *stmt;
GdaSet *params;
- guint exec_id;
GError *lerror = NULL;
stmt = g_object_get_data (G_OBJECT (dialog), "stmt");
params = g_object_get_data (G_OBJECT (dialog), "params");
- exec_id = browser_connection_execute_statement (tinfo->priv->bcnc, stmt, params,
- GDA_STATEMENT_MODEL_RANDOM_ACCESS,
- FALSE, &lerror);
- if (!exec_id) {
+ if (! browser_connection_execute_statement_cb (tinfo->priv->bcnc,
+ stmt, params,
+ GDA_STATEMENT_MODEL_RANDOM_ACCESS,
+ FALSE,
+ (BrowserConnectionExecuteCallback) statement_executed_cb,
+ tinfo, &lerror)) {
browser_show_error (GTK_WINDOW (gtk_widget_get_toplevel ((GtkWidget*) tinfo)),
_("Error executing query: %s"),
lerror && lerror->message ? lerror->message : _("No detail"));
g_clear_error (&lerror);
}
- else {
- tinfo->priv->exec_id = exec_id;
-
- if (! tinfo->priv->timeout_id)
- tinfo->priv->timeout_id = g_timeout_add (200,
- (GSourceFunc) query_exec_fetch_cb,
- tinfo);
- }
}
gtk_widget_hide (dialog);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]