[libgda] Code cleanups and improvements



commit 56fe9307c1a9f8ed6fcaa89498be3ec492a23e7f
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Jan 23 21:57:55 2013 +0100

    Code cleanups and improvements

 libgda/gda-statement.c                             |    6 +-
 libgda/sqlite/gda-sqlite-meta.c                    |   85 ++++++++++----------
 libgda/sqlite/gda-sqlite-provider.c                |    9 +-
 providers/firebird/gda-firebird-meta.c             |   48 ++++++------
 providers/firebird/gda-firebird-provider.c         |   26 ++++---
 providers/jdbc/gda-jdbc-provider.c                 |   27 ++++---
 providers/mysql/gda-mysql-provider.c               |   40 ++++++----
 providers/oracle/gda-oracle-meta.c                 |   39 +++++-----
 providers/oracle/gda-oracle-provider.c             |   27 ++++---
 providers/postgres/gda-postgres-provider.c         |   27 ++++---
 providers/skel-implementation/capi/gda-capi-meta.c |   37 +++++----
 .../skel-implementation/capi/gda-capi-provider.c   |   27 ++++---
 12 files changed, 220 insertions(+), 178 deletions(-)
---
diff --git a/libgda/gda-statement.c b/libgda/gda-statement.c
index bac7768..e806f57 100644
--- a/libgda/gda-statement.c
+++ b/libgda/gda-statement.c
@@ -1492,8 +1492,8 @@ default_render_expr (GdaSqlExpr *expr, GdaSqlRenderingContext *context, gboolean
 	if (is_null)
 		*is_null = FALSE;
 
-	/* can't have: 
-	 *  - expr->cast_as && expr->param_spec 
+	/* can't have:
+	 *  - expr->cast_as && expr->param_spec
 	 */
 	if (!gda_sql_any_part_check_structure (GDA_SQL_ANY_PART (expr), error)) return NULL;
 
@@ -1639,7 +1639,7 @@ default_render_expr (GdaSqlExpr *expr, GdaSqlRenderingContext *context, gboolean
 
 	if (!str) goto err;
 
-	if (expr->cast_as) 
+	if (expr->cast_as)
 		g_string_append_printf (string, "CAST (%s AS %s)", str, expr->cast_as);
 	else
 		g_string_append (string, str);
diff --git a/libgda/sqlite/gda-sqlite-meta.c b/libgda/sqlite/gda-sqlite-meta.c
index f394a21..379936d 100644
--- a/libgda/sqlite/gda-sqlite-meta.c
+++ b/libgda/sqlite/gda-sqlite-meta.c
@@ -83,23 +83,24 @@ static gchar *internal_sql[] = {
 /*
  * predefined statements' GdaStatement
  */
-static GdaStatement **internal_stmt;
-static GdaSet        *internal_params;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
+static GdaSet        *internal_params = NULL;
 
 /* 
  * global static values
  */
 static GdaSqlParser *internal_parser = NULL;
-static GValue       *catalog_value;
-static GValue       *table_type_value;
-static GValue       *view_type_value;
-static GValue       *view_check_option;
-static GValue       *false_value;
-static GValue       *true_value;
-static GValue       *zero_value;
-static GValue       *rule_value_none;
-static GValue       *rule_value_action;
-static GdaSet       *pragma_set;
+static GValue       *catalog_value = NULL;
+static GValue       *table_type_value = NULL;
+static GValue       *view_type_value = NULL;
+static GValue       *view_check_option = NULL;
+static GValue       *false_value = NULL;
+static GValue       *true_value = NULL;
+static GValue       *zero_value = NULL;
+static GValue       *rule_value_none = NULL;
+static GValue       *rule_value_action = NULL;
+static GdaSet       *pragma_set = NULL;
 
 static GValue *
 new_caseless_value (const GValue *cvalue)
@@ -152,39 +153,39 @@ to_caseless_string (gchar *string)
 void
 _gda_sqlite_provider_meta_init (GdaServerProvider *provider)
 {
-	static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
-	InternalStatementItem i;
-
 	g_static_mutex_lock (&init_mutex);
 
-        internal_parser = gda_server_provider_internal_get_parser (provider);
-	internal_params = gda_set_new (NULL);
-
-        internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-        for (i = I_PRAGMA_DATABASE_LIST; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		GdaSet *set;
-                internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
-                if (!internal_stmt[i])
-                        g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
-		g_assert (gda_statement_get_parameters (internal_stmt[i], &set, NULL));
-		if (set) {
-			gda_set_merge_with_set (internal_params, set);
-			g_object_unref (set);
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		internal_parser = gda_server_provider_internal_get_parser (provider);
+		internal_params = gda_set_new (NULL);
+
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = I_PRAGMA_DATABASE_LIST; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			GdaSet *set;
+			internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+			g_assert (gda_statement_get_parameters (internal_stmt[i], &set, NULL));
+			if (set) {
+				gda_set_merge_with_set (internal_params, set);
+				g_object_unref (set);
+			}
 		}
-        }
 
-	g_value_set_string ((catalog_value = gda_value_new (G_TYPE_STRING)), "main");
-	g_value_set_string ((table_type_value = gda_value_new (G_TYPE_STRING)), "BASE TABLE");
-	g_value_set_string ((view_type_value = gda_value_new (G_TYPE_STRING)), "VIEW");
-	g_value_set_string ((view_check_option = gda_value_new (G_TYPE_STRING)), "NONE");
-	g_value_set_boolean ((false_value = gda_value_new (G_TYPE_BOOLEAN)), FALSE);
-	g_value_set_boolean ((true_value = gda_value_new (G_TYPE_BOOLEAN)), TRUE);
-	g_value_set_int ((zero_value = gda_value_new (G_TYPE_INT)), 0);
-	rule_value_none = view_check_option;
-	g_value_set_string ((rule_value_action = gda_value_new (G_TYPE_STRING)), "NO ACTION");
-
-	pragma_set = gda_set_new_inline (2, "tblname", G_TYPE_STRING, "",
-					 "idxname", G_TYPE_STRING, "");
+		g_value_set_string ((catalog_value = gda_value_new (G_TYPE_STRING)), "main");
+		g_value_set_string ((table_type_value = gda_value_new (G_TYPE_STRING)), "BASE TABLE");
+		g_value_set_string ((view_type_value = gda_value_new (G_TYPE_STRING)), "VIEW");
+		g_value_set_string ((view_check_option = gda_value_new (G_TYPE_STRING)), "NONE");
+		g_value_set_boolean ((false_value = gda_value_new (G_TYPE_BOOLEAN)), FALSE);
+		g_value_set_boolean ((true_value = gda_value_new (G_TYPE_BOOLEAN)), TRUE);
+		g_value_set_int ((zero_value = gda_value_new (G_TYPE_INT)), 0);
+		rule_value_none = view_check_option;
+		g_value_set_string ((rule_value_action = gda_value_new (G_TYPE_STRING)), "NO ACTION");
+
+		pragma_set = gda_set_new_inline (2, "tblname", G_TYPE_STRING, "",
+						 "idxname", G_TYPE_STRING, "");
+	}
 
 	g_static_mutex_unlock (&init_mutex);
 }
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index 7c307a2..faa89ae 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -568,17 +568,16 @@ gda_sqlite_provider_class_init (GdaSqliteProviderClass *klass)
 static void
 gda_sqlite_provider_init (GdaSqliteProvider *sqlite_prv, G_GNUC_UNUSED GdaSqliteProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
 	g_static_mutex_lock (&init_mutex);
 
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) sqlite_prv);
-
 	if (!internal_stmt) {
 		/* configure multi threading environment */
 		SQLITE3_CALL (sqlite3_config) (SQLITE_CONFIG_SERIALIZED);
 
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) sqlite_prv);
+
 		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
 		for (i = INTERNAL_PRAGMA_INDEX_LIST; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
 			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
diff --git a/providers/firebird/gda-firebird-meta.c b/providers/firebird/gda-firebird-meta.c
index f1aebcc..4afc12c 100644
--- a/providers/firebird/gda-firebird-meta.c
+++ b/providers/firebird/gda-firebird-meta.c
@@ -214,9 +214,10 @@ static gchar *internal_sql[] = {
  * global static values, and
  * predefined statements' GdaStatement, all initialized in _gda_postgres_provider_meta_init()
  */
-static GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 static GdaSqlParser *internal_parser = NULL;
-static GdaSet       *i_set;
+static GdaSet       *i_set = NULL;
 
 /* TO_ADD: other static values */
 
@@ -227,36 +228,35 @@ static GdaSet       *i_set;
 void
 _gda_firebird_provider_meta_init (GdaServerProvider *provider)
 {
-	static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
-	InternalStatementItem i;
-
 	g_static_mutex_lock (&init_mutex);
 
-	internal_parser = gda_server_provider_internal_get_parser (provider);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = I_STMT_CATALOG; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		internal_parser = gda_server_provider_internal_get_parser (provider);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = I_STMT_CATALOG; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
 			internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
 			if (!internal_stmt[i])
 					g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
-	}
+		}
 
-	/* initialize static values here */
-	
-	i_set = gda_set_new_inline (5, "tblname", G_TYPE_STRING, "", 
-				    "schema", G_TYPE_STRING, "",
-				    "constraint_name", G_TYPE_STRING, "",
-				    "field_name", G_TYPE_STRING, ""
-				    , "char_set_name", G_TYPE_STRING, "");
-	g_static_mutex_unlock (&init_mutex);
+		/* initialize static values here */
+		i_set = gda_set_new_inline (5, "tblname", G_TYPE_STRING, "",
+					    "schema", G_TYPE_STRING, "",
+					    "constraint_name", G_TYPE_STRING, "",
+					    "field_name", G_TYPE_STRING, ""
+					    , "char_set_name", G_TYPE_STRING, "");
+		g_static_mutex_unlock (&init_mutex);
 	
-	/* initialize static values here */
-	/*
-	i_set = gda_set_new_inline (3, "name", G_TYPE_STRING, "",
-				    "schema", G_TYPE_STRING, "",
-                                    "name2", G_TYPE_STRING, "");
+		/* initialize static values here */
+		/*
+		  i_set = gda_set_new_inline (3, "name", G_TYPE_STRING, "",
+		  "schema", G_TYPE_STRING, "",
+		  "name2", G_TYPE_STRING, ""); */
 	
+	}
+
 	g_static_mutex_unlock (&init_mutex);
-	*/
 }
 
 gboolean
diff --git a/providers/firebird/gda-firebird-provider.c b/providers/firebird/gda-firebird-provider.c
index 683dc8d..adfd2ba 100644
--- a/providers/firebird/gda-firebird-provider.c
+++ b/providers/firebird/gda-firebird-provider.c
@@ -179,7 +179,8 @@ static gchar * firebird_render_expr (GdaSqlExpr *expr, GdaSqlRenderingContext *c
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	INTERNAL_STMT1
@@ -296,19 +297,24 @@ gda_firebird_provider_class_init (GdaFirebirdProviderClass *klass)
 static void
 gda_firebird_provider_init (GdaFirebirdProvider *firebird_prv, GdaFirebirdProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) firebird_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i]) 
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+	g_static_mutex_lock (&init_mutex);
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) firebird_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
 	_gda_firebird_provider_meta_init ((GdaServerProvider*) firebird_prv);
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType
diff --git a/providers/jdbc/gda-jdbc-provider.c b/providers/jdbc/gda-jdbc-provider.c
index 168a17d..f4da3fb 100644
--- a/providers/jdbc/gda-jdbc-provider.c
+++ b/providers/jdbc/gda-jdbc-provider.c
@@ -145,7 +145,8 @@ static void gda_jdbc_free_cnc_data (JdbcConnectionData *cdata);
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-static GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	INTERNAL_STMT1
@@ -266,21 +267,27 @@ extern JavaVM *_jdbc_provider_java_vm;
 static void
 gda_jdbc_provider_init (GdaJdbcProvider *jdbc_prv, G_GNUC_UNUSED GdaJdbcProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) jdbc_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i]) 
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+	g_static_mutex_lock (&init_mutex);
+
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) jdbc_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
 	_gda_jdbc_provider_meta_init ((GdaServerProvider*) jdbc_prv);
 
 	/* TO_ADD: any other provider's init should be added here */
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType
diff --git a/providers/mysql/gda-mysql-provider.c b/providers/mysql/gda-mysql-provider.c
index e500a2e..edd5969 100644
--- a/providers/mysql/gda-mysql-provider.c
+++ b/providers/mysql/gda-mysql-provider.c
@@ -239,7 +239,8 @@ static void gda_mysql_free_cnc_data (MysqlConnectionData  *cdata);
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	INTERNAL_STMT1
@@ -376,15 +377,19 @@ static void
 gda_mysql_provider_init (GdaMysqlProvider       *mysql_prv,
 			 G_GNUC_UNUSED GdaMysqlProviderClass  *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) mysql_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i]) 
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+	g_static_mutex_lock (&init_mutex);
+
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) mysql_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
@@ -393,6 +398,8 @@ gda_mysql_provider_init (GdaMysqlProvider       *mysql_prv,
 	/* for tests */
 	mysql_prv->test_mode = FALSE;
 	mysql_prv->test_identifiers_case_sensitive = TRUE;
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType
@@ -519,8 +526,8 @@ real_open_connection (const gchar  *host,
 	if (compress)
 		flags |= CLIENT_COMPRESS;
 	
-	MYSQL *mysql = g_new0 (MYSQL, 1);
-	mysql_init (mysql);
+	MYSQL *mysql = NULL;
+	mysql = mysql_init (NULL);
 
 	if ((port > 0) || proto) {
 		gint p = MYSQL_PROTOCOL_DEFAULT;
@@ -538,7 +545,7 @@ real_open_connection (const gchar  *host,
 			else {
 				g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_OPEN_ERROR,
 					     _("Unknown MySQL protocol '%s'"), proto);
-				g_free (mysql);
+				mysql_close (mysql);
 				return NULL;
 			}
 		}
@@ -548,7 +555,7 @@ real_open_connection (const gchar  *host,
 		if (mysql_options (mysql, MYSQL_OPT_PROTOCOL, (const char *) &p)) {
 			g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_OPEN_ERROR,
 				     "%s", mysql_error (mysql));
-			g_free (mysql);
+			mysql_close (mysql);
 			return NULL;
 		}
 	}
@@ -563,7 +570,7 @@ real_open_connection (const gchar  *host,
 	if (!return_mysql || mysql != return_mysql) {
 		g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_OPEN_ERROR,
 			     "%s", mysql_error (mysql));
-		g_free (mysql);
+		mysql_close (mysql);
 		mysql = NULL;
 	}
 
@@ -574,7 +581,7 @@ real_open_connection (const gchar  *host,
 	    mysql_select_db (mysql, db) != 0) {
 		g_set_error (error, GDA_CONNECTION_ERROR, GDA_CONNECTION_OPEN_ERROR,
 			     "%s", mysql_error (mysql));
-		g_free (mysql);
+		mysql_close (mysql);
 		mysql = NULL;
 	}
 #endif
@@ -692,6 +699,7 @@ gda_mysql_provider_open_connection (GdaServerProvider               *provider,
 	res = mysql_query (mysql, "SET NAMES 'utf8'");
 	if (res != 0) {
 		_gda_mysql_make_error (cnc, mysql, NULL, NULL);
+		mysql_close (mysql);
 		return FALSE;
 	}
 
diff --git a/providers/oracle/gda-oracle-meta.c b/providers/oracle/gda-oracle-meta.c
index 1e20560..c20a77a 100644
--- a/providers/oracle/gda-oracle-meta.c
+++ b/providers/oracle/gda-oracle-meta.c
@@ -96,11 +96,10 @@ static gchar *internal_sql[] = {
  * global static values, and
  * predefined statements' GdaStatement, all initialized in _gda_oracle_provider_meta_init()
  */
-static GdaStatement **internal_stmt;
-static GdaSet        *i_set;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
+static GdaSet        *i_set = NULL;
 static GdaSqlParser  *internal_parser = NULL;
-/* TO_ADD: other static values */
-
 
 /*
  * Meta initialization
@@ -108,24 +107,24 @@ static GdaSqlParser  *internal_parser = NULL;
 void
 _gda_oracle_provider_meta_init (GdaServerProvider *provider)
 {
-	static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
-	InternalStatementItem i;
-
 	g_static_mutex_lock (&init_mutex);
 
-        internal_parser = gda_server_provider_internal_get_parser (provider);
-        internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-        for (i = I_STMT_CATALOG; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-                internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
-                if (!internal_stmt[i])
-                        g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
-        }
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		internal_parser = gda_server_provider_internal_get_parser (provider);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = I_STMT_CATALOG; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 
-	/* initialize static values here */
-	i_set = gda_set_new_inline (4, "cat", G_TYPE_STRING, "", 
-				    "name", G_TYPE_STRING, "",
-				    "schema", G_TYPE_STRING, "",
-				    "name2", G_TYPE_STRING, "");
+		/* initialize static values here */
+		i_set = gda_set_new_inline (4, "cat", G_TYPE_STRING, "",
+					    "name", G_TYPE_STRING, "",
+					    "schema", G_TYPE_STRING, "",
+					    "name2", G_TYPE_STRING, "");
+	}
 
 	g_static_mutex_unlock (&init_mutex);
 }
diff --git a/providers/oracle/gda-oracle-provider.c b/providers/oracle/gda-oracle-provider.c
index 54e84b2..d79fe70 100644
--- a/providers/oracle/gda-oracle-provider.c
+++ b/providers/oracle/gda-oracle-provider.c
@@ -156,7 +156,8 @@ static void gda_oracle_free_cnc_data (OracleConnectionData *cdata);
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-static GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	INTERNAL_STMT1
@@ -299,19 +300,25 @@ gda_oracle_provider_class_init (GdaOracleProviderClass *klass)
 static void
 gda_oracle_provider_init (GdaOracleProvider *oracle_prv, G_GNUC_UNUSED GdaOracleProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) oracle_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i]) 
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+	g_static_mutex_lock (&init_mutex);
+
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) oracle_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
 	_gda_oracle_provider_meta_init ((GdaServerProvider*) oracle_prv);
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType
diff --git a/providers/postgres/gda-postgres-provider.c b/providers/postgres/gda-postgres-provider.c
index 0941353..ee56670 100644
--- a/providers/postgres/gda-postgres-provider.c
+++ b/providers/postgres/gda-postgres-provider.c
@@ -165,7 +165,8 @@ static void gda_postgres_free_cnc_data (PostgresConnectionData *cdata);
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-static GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	I_STMT_BEGIN,
@@ -301,19 +302,25 @@ gda_postgres_provider_class_init (GdaPostgresProviderClass *klass)
 static void
 gda_postgres_provider_init (GdaPostgresProvider *postgres_prv, G_GNUC_UNUSED GdaPostgresProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
+	g_static_mutex_lock (&init_mutex);
+
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
 
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) postgres_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = I_STMT_BEGIN; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i])
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) postgres_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = I_STMT_BEGIN; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
 	_gda_postgres_provider_meta_init ((GdaServerProvider*) postgres_prv);
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType
diff --git a/providers/skel-implementation/capi/gda-capi-meta.c b/providers/skel-implementation/capi/gda-capi-meta.c
index 6d742b5..a0fe637 100644
--- a/providers/skel-implementation/capi/gda-capi-meta.c
+++ b/providers/skel-implementation/capi/gda-capi-meta.c
@@ -56,8 +56,9 @@ static gchar *internal_sql[] = {
  * global static values, and
  * predefined statements' GdaStatement, all initialized in _gda_capi_provider_meta_init()
  */
-static GdaStatement **internal_stmt;
-static GdaSet        *i_set;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
+static GdaSet        *i_set = NULL;
 static GdaSqlParser  *internal_parser = NULL;
 /* TO_ADD: other static values */
 
@@ -68,24 +69,24 @@ static GdaSqlParser  *internal_parser = NULL;
 void
 _gda_capi_provider_meta_init (GdaServerProvider *provider)
 {
-	static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
-	InternalStatementItem i;
-
 	g_static_mutex_lock (&init_mutex);
 
-        internal_parser = gda_server_provider_internal_get_parser (provider);
-        internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-        for (i = I_STMT_1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-                internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
-                if (!internal_stmt[i])
-                        g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
-        }
-
-	/* initialize static values here */
-	i_set = gda_set_new_inline (4, "cat", G_TYPE_STRING, "", 
-				    "name", G_TYPE_STRING, "",
-				    "schema", G_TYPE_STRING, "",
-				    "name2", G_TYPE_STRING, "");
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		internal_parser = gda_server_provider_internal_get_parser (provider);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = I_STMT_1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (internal_parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
+
+		/* initialize static values here */
+		i_set = gda_set_new_inline (4, "cat", G_TYPE_STRING, "",
+					    "name", G_TYPE_STRING, "",
+					    "schema", G_TYPE_STRING, "",
+					    "name2", G_TYPE_STRING, "");
+	}
 
 	g_static_mutex_unlock (&init_mutex);
 
diff --git a/providers/skel-implementation/capi/gda-capi-provider.c b/providers/skel-implementation/capi/gda-capi-provider.c
index 8159f91..ae01cfd 100644
--- a/providers/skel-implementation/capi/gda-capi-provider.c
+++ b/providers/skel-implementation/capi/gda-capi-provider.c
@@ -144,7 +144,8 @@ static void gda_capi_free_cnc_data (CapiConnectionData *cdata);
  * TO_ADD: any prepared statement to be used internally by the provider should be
  *         declared here, as constants and as SQL statements
  */
-static GdaStatement **internal_stmt;
+static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
+static GdaStatement **internal_stmt = NULL;
 
 typedef enum {
 	INTERNAL_STMT1
@@ -262,21 +263,27 @@ gda_capi_provider_class_init (GdaCapiProviderClass *klass)
 static void
 gda_capi_provider_init (GdaCapiProvider *capi_prv, G_GNUC_UNUSED GdaCapiProviderClass *klass)
 {
-	InternalStatementItem i;
-	GdaSqlParser *parser;
-
-	parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) capi_prv);
-	internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
-	for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
-		internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
-		if (!internal_stmt[i]) 
-			g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+	g_static_mutex_lock (&init_mutex);
+
+	if (!internal_stmt) {
+		InternalStatementItem i;
+		GdaSqlParser *parser;
+
+		parser = gda_server_provider_internal_get_parser ((GdaServerProvider*) capi_prv);
+		internal_stmt = g_new0 (GdaStatement *, sizeof (internal_sql) / sizeof (gchar*));
+		for (i = INTERNAL_STMT1; i < sizeof (internal_sql) / sizeof (gchar*); i++) {
+			internal_stmt[i] = gda_sql_parser_parse_string (parser, internal_sql[i], NULL, NULL);
+			if (!internal_stmt[i])
+				g_error ("Could not parse internal statement: %s\n", internal_sql[i]);
+		}
 	}
 
 	/* meta data init */
 	_gda_capi_provider_meta_init ((GdaServerProvider*) capi_prv);
 
 	/* TO_ADD: any other provider's init should be added here */
+
+	g_static_mutex_unlock (&init_mutex);
 }
 
 GType



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