[libgda] Web provider: use only one connection for all data models



commit 2c1cf91d7df861c3fc101e7be7c2d43295693024
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sun Aug 21 21:26:37 2011 +0200

    Web provider: use only one connection for all data models

 providers/web/gda-web-recordset.c |   45 ++++++++++++++++++++++--------------
 providers/web/gda-web-util.c      |    3 ++
 providers/web/gda-web.h           |    2 +
 3 files changed, 32 insertions(+), 18 deletions(-)
---
diff --git a/providers/web/gda-web-recordset.c b/providers/web/gda-web-recordset.c
index dfd7b78..5e476c9 100644
--- a/providers/web/gda-web-recordset.c
+++ b/providers/web/gda-web-recordset.c
@@ -161,29 +161,35 @@ gda_web_recordset_new (GdaConnection *cnc, GdaWebPStmt *ps, GdaSet *exec_params,
         gint i;
 	GdaDataModelAccessFlags rflags;
 	static guint counter = 0;
+	WebConnectionData *cdata;
 
         g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
         g_return_val_if_fail (ps != NULL, NULL);
 
+	cdata = (WebConnectionData*) gda_connection_internal_get_provider_data (cnc);
+	if (!cdata)
+		return FALSE;
+
 	/* prepare internal connection which will be used to store
 	 * the recordset's data
 	 */
-	GdaConnection *rs_cnc;
 	gchar *fname, *tmp;
 	
 	for (fname = (gchar*) session_id; *fname && (*fname != '='); fname++);
 	g_assert (*fname == '=');
 	fname++;
 	tmp = g_strdup_printf ("%s%u.db", fname, counter++);
-	rs_cnc = gda_connection_open_sqlite (NULL, tmp, TRUE);
-	if (!rs_cnc) {
-		fname = g_build_filename (g_get_tmp_dir(), tmp, NULL);
-		g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
-			     GDA_SERVER_PROVIDER_INTERNAL_ERROR,
-			     _("Can't create temporary file '%s'"), fname);
-		g_free (tmp);
-		g_free (fname);
-		return NULL;
+	if (! cdata->rs_cnc) {
+		cdata->rs_cnc = gda_connection_open_sqlite (NULL, tmp, TRUE);
+		if (!cdata->rs_cnc) {
+			fname = g_build_filename (g_get_tmp_dir(), tmp, NULL);
+			g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
+				     GDA_SERVER_PROVIDER_INTERNAL_ERROR,
+				     _("Can't create temporary file '%s'"), fname);
+			g_free (tmp);
+			g_free (fname);
+			return NULL;
+		}
 	}
 	g_free (tmp);
 
@@ -229,10 +235,6 @@ gda_web_recordset_new (GdaConnection *cnc, GdaWebPStmt *ps, GdaSet *exec_params,
 		
 		/* fill GdaColumn's data */
 		xmlNodePtr child;
-		WebConnectionData *cdata;
-		cdata = (WebConnectionData*) gda_connection_internal_get_provider_data (cnc);
-		if (!cdata) 
-			return FALSE;
 
 		for (child = data_node->children, i = 0, list = _GDA_PSTMT (ps)->tmpl_columns; 
 		     child && (i < GDA_PSTMT (ps)->ncols);
@@ -309,7 +311,7 @@ gda_web_recordset_new (GdaConnection *cnc, GdaWebPStmt *ps, GdaSet *exec_params,
 			      "model-usage", rflags, 
 			      "exec-params", exec_params, NULL);
         model->priv->cnc = cnc;
-	model->priv->rs_cnc = rs_cnc;
+	model->priv->rs_cnc = g_object_ref (G_OBJECT (cdata->rs_cnc));
 	g_object_ref (cnc);
 
         return GDA_DATA_MODEL (model);
@@ -326,16 +328,23 @@ create_table (GdaWebRecordset *rs, GError **error)
 	GString *string;
 	gint i, ncols;
 	gboolean retval = FALSE;
+	static guint64 counter = 0;
+	gchar *tname;
 
 	GdaSqlBuilder *sb, *ib;
 
 	ib = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
 	sb = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
 
-	gda_sql_builder_set_table (ib, TABLE_NAME);
-	gda_sql_builder_select_add_target (sb, TABLE_NAME, NULL);
+	tname = g_strdup_printf (TABLE_NAME "%lu", counter++);
+	gda_sql_builder_set_table (ib, tname);
+	gda_sql_builder_select_add_target (sb, tname, NULL);
+
+	string = g_string_new ("CREATE table ");
+	g_string_append (string, tname);
+	g_string_append (string, " (");
+	g_free (tname);
 
-	string = g_string_new ("CREATE table " TABLE_NAME " (");
 	ncols = gda_data_model_get_n_columns ((GdaDataModel*) rs);
 	for (i = 0; i < ncols; i++) {
 		GdaColumn *column;
diff --git a/providers/web/gda-web-util.c b/providers/web/gda-web-util.c
index 40fed1c..9ee1887 100644
--- a/providers/web/gda-web-util.c
+++ b/providers/web/gda-web-util.c
@@ -659,5 +659,8 @@ _gda_web_free_cnc_data (WebConnectionData *cdata)
 	g_free (cdata->key);
 	g_free (cdata->next_challenge);
 
+	if (cdata->rs_cnc)
+		g_object_unref (G_OBJECT (cdata->rs_cnc));
+
 	g_free (cdata);
 }
diff --git a/providers/web/gda-web.h b/providers/web/gda-web.h
index a464bcb..a5ff822 100644
--- a/providers/web/gda-web.h
+++ b/providers/web/gda-web.h
@@ -64,6 +64,8 @@ typedef struct {
 	/* front and others attributes */
 	SoupSession *front_session;
 	guint last_exec_counter; /* the worker counter which replied to the last EXEC command */
+
+	GdaConnection *rs_cnc; /* SQLite connection used by GdaWebRecordsetObjects */
 } WebConnectionData;
 
 #endif



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