[libgda] Corrections after commit #dacaa9e33b62c5ba2118e2b248a5ab4c56bf49a6



commit 85dad0c5bc69d7f33cfdddd361538da02d24e98a
Author: Vivien Malerba <malerba gnome-db org>
Date:   Mon Feb 20 20:36:13 2012 +0100

    Corrections after commit #dacaa9e33b62c5ba2118e2b248a5ab4c56bf49a6
    
    - the added _schemata.schema_default column has to be allowed to be NULL
      because it will be NULL for some providers
    - the GdaMetaStore must add that column when migrating schema versions

 doc/C/i_s_doc.xml                                |    2 +-
 libgda/gda-meta-store.c                          |  113 ++++++++++++++++++++--
 libgda/information_schema.xml                    |    4 +-
 libgda/providers-support/gda-meta-column-types.h |    1 +
 libgda/sqlite/gda-sqlite-ddl.c                   |    1 -
 tests/meta-store/common.c                        |   12 +-
 tests/meta-store/data_schemata.csv               |    6 +-
 tests/meta-store/data_schemata_1.csv             |    2 +-
 8 files changed, 118 insertions(+), 23 deletions(-)
---
diff --git a/doc/C/i_s_doc.xml b/doc/C/i_s_doc.xml
index 26e13c1..59ff9d3 100644
--- a/doc/C/i_s_doc.xml
+++ b/doc/C/i_s_doc.xml
@@ -17,7 +17,7 @@ do not modify-->
   <sect3 id="is:_schemata">
     <title>_schemata table</title>
     <para>List of schemas</para>
-    <para>The following table describes the columns:<informaltable frame="all"><tgroup cols="5" colsep="1" rowsep="1" align="justify"><thead><row><entry>Column name</entry><entry>Type</entry><entry>Key</entry><entry>Can be NULL</entry><entry>description</entry></row></thead><tbody><row><entry>catalog_name</entry><entry>string</entry><entry>Yes</entry><entry>No</entry><entry>Name of the catalog that contains the schema</entry></row><row><entry>schema_name</entry><entry>string</entry><entry>Yes</entry><entry>No</entry><entry></entry></row><row><entry>schema_owner</entry><entry>string</entry><entry></entry><entry>Yes</entry><entry>Name of the schema</entry></row><row><entry>schema_internal</entry><entry>boolean</entry><entry></entry><entry>No</entry><entry>Tells if the schema is specific to the database implementation (and usually can't be modified)</entry></row></tbody></tgroup></informaltable></para>
+    <para>The following table describes the columns:<informaltable frame="all"><tgroup cols="5" colsep="1" rowsep="1" align="justify"><thead><row><entry>Column name</entry><entry>Type</entry><entry>Key</entry><entry>Can be NULL</entry><entry>description</entry></row></thead><tbody><row><entry>catalog_name</entry><entry>string</entry><entry>Yes</entry><entry>No</entry><entry>Name of the catalog that contains the schema</entry></row><row><entry>schema_name</entry><entry>string</entry><entry>Yes</entry><entry>No</entry><entry></entry></row><row><entry>schema_owner</entry><entry>string</entry><entry></entry><entry>Yes</entry><entry>Name of the schema</entry></row><row><entry>schema_internal</entry><entry>boolean</entry><entry></entry><entry>No</entry><entry>Tells if the schema is specific to the database implementation (and usually can't be modified)</entry></row><row><entry>schema_default</entry><entry>boolean</entry><entry></entry><entry>Yes</entry><entry>Tells if the schema i
 s used by default when creating a new table without schema name</entry></row></tbody></tgroup></informaltable></para>
     <para>
       <itemizedlist>
         <listitem>
diff --git a/libgda/gda-meta-store.c b/libgda/gda-meta-store.c
index 6118890..14e4c0d 100644
--- a/libgda/gda-meta-store.c
+++ b/libgda/gda-meta-store.c
@@ -85,9 +85,9 @@ gda_meta_context_get_type (void)
 		static GStaticMutex registering = G_STATIC_MUTEX_INIT;
 		g_static_mutex_lock (&registering);
                 if (type == 0)
-			type = type = g_boxed_type_register_static ("GdaMetaContext",
-                                                    (GBoxedCopyFunc) gda_meta_context_copy,
-                                                    (GBoxedFreeFunc) gda_meta_context_free);
+			type = g_boxed_type_register_static ("GdaMetaContext",
+							     (GBoxedCopyFunc) gda_meta_context_copy,
+							     (GBoxedFreeFunc) gda_meta_context_free);
 		g_static_mutex_unlock (&registering);
 	}
 
@@ -227,19 +227,18 @@ gda_meta_context_set_columns (GdaMetaContext *ctx, GHashTable *columns, GdaConne
 	gpointer key, value;
 	gint i = 0; // FIXME: Code to remove
 	g_hash_table_iter_init (&iter, ctx->columns);
-	while (g_hash_table_iter_next (&iter, &key, &value))
-	{
+	while (g_hash_table_iter_next (&iter, &key, &value)) {
 		// Normalize identifier quote
-		if (G_VALUE_HOLDS_STRING((GValue*)value)) 
-		{
+		if (G_VALUE_HOLDS_STRING((GValue*)value)) {
 			GValue *v = gda_value_new (G_TYPE_STRING);
 			g_value_take_string (v, gda_sql_identifier_quote (g_value_get_string ((GValue*)value), cnc, NULL,
 											TRUE, FALSE));
 			g_hash_table_insert (ctx->columns, key, (gpointer) v);
 		}
+
 		// FIXME: Code to remove
 		ctx->column_names[i] = (gchar*) key;
-		ctx->column_names[i] = (GValue*) value;
+		ctx->column_values[i] = (GValue*) value;
 		i++;
 	}
 }
@@ -2343,6 +2342,72 @@ create_a_dbobj (GdaMetaStore *store, const gchar *obj_name, GError **error)
 	return retval;
 }
 
+/*
+ * Create a database object from its name,
+ * to be used by functions implementing versions migrations
+ */
+static gboolean
+add_a_column (GdaMetaStore *store, const gchar *table_name, const gchar *column_name, GError **error)
+{
+	DbObject *dbobj;
+	GdaMetaStoreClass *klass;
+	GdaServerProvider *prov;
+	gboolean retval = FALSE;
+
+	klass = (GdaMetaStoreClass *) G_OBJECT_GET_CLASS (store);
+	dbobj = g_hash_table_lookup (klass->cpriv->db_objects_hash, table_name);
+	if (!dbobj) {
+		g_set_error (error, GDA_META_STORE_ERROR, GDA_META_STORE_SCHEMA_OBJECT_NOT_FOUND_ERROR,
+			     _("Schema description does not contain the object '%s', check installation"),
+			     table_name);
+		return FALSE;
+	}
+	prov = gda_connection_get_provider (store->priv->cnc);
+	
+	GdaServerOperation *op;
+	op = gda_server_provider_create_operation (prov, store->priv->cnc, GDA_SERVER_OPERATION_ADD_COLUMN, NULL, error);
+	if (!op)
+		return FALSE;
+
+	GSList *list;
+	if (! gda_server_operation_set_value_at (op, table_name, error, "/COLUMN_DEF_P/TABLE_NAME"))
+		goto out;
+
+	for (list = TABLE_INFO (dbobj)->columns; list; list = list->next) {
+		TableColumn *tcol = TABLE_COLUMN (list->data);
+		if (!strcmp (tcol->column_name, column_name)) {
+			const gchar *repl;
+			if (! gda_server_operation_set_value_at (op, tcol->column_name,
+								 error, "/COLUMN_DEF_P/COLUMN_NAME"))
+				goto out;
+			repl = provider_specific_match (klass->cpriv->provider_specifics, prov,
+							tcol->column_type ? tcol->column_type : "string",
+							"/FIELDS_A/@COLUMN_TYPE");
+			if (! gda_server_operation_set_value_at (op, repl ? repl : "string", error,
+								 "/COLUMN_DEF_P/COLUMN_TYPE"))
+				goto out;
+			if (! gda_server_operation_set_value_at (op, NULL, error,
+								 "/COLUMN_DEF_P/COLUMN_SIZE"))
+				goto out;
+			if (! gda_server_operation_set_value_at (op, tcol->nullok ? "FALSE" : "TRUE", error,
+								 "/COLUMN_DEF_P/COLUMN_NNUL"))
+				goto out;
+			break;
+		}
+	}
+	if (!list) {
+		g_set_error (error, GDA_META_STORE_ERROR, GDA_META_STRUCT_UNKNOWN_OBJECT_ERROR,
+			     _("Could not find description for column named '%s'"), column_name);
+		goto out;
+	}
+
+	retval = gda_server_provider_perform_operation (prov, store->priv->cnc, op, error);
+
+ out:
+	g_object_unref (op);
+	return retval;
+}
+
 /* migrate schema from version 1 to 2 */
 static void
 migrate_schema_from_v1_to_v2 (GdaMetaStore *store, GError **error)
@@ -2403,6 +2468,34 @@ migrate_schema_from_v2_to_v3 (GdaMetaStore *store, GError **error)
 	}
 }
 
+/* migrate schema from version 2 to 3 */
+static void
+migrate_schema_from_v3_to_v4 (GdaMetaStore *store, GError **error)
+{
+	g_return_if_fail (GDA_IS_CONNECTION (store->priv->cnc));
+	g_return_if_fail (gda_connection_is_opened (store->priv->cnc));
+
+	/* begin a transaction if possible */
+	gboolean transaction_started = FALSE;
+	if (! check_transaction_started (store->priv->cnc, &transaction_started))
+		return;
+
+	if (! add_a_column (store, "_schemata", "schema_default", error))
+		goto out;
+
+	/* set version info to CURRENT_SCHEMA_VERSION */
+	update_schema_version (store, "4", error);
+
+ out:
+	if (transaction_started) {
+		/* handle transaction started if necessary */
+		if (store->priv->version != 4)
+			gda_connection_rollback_transaction (store->priv->cnc, NULL, NULL);
+		else
+			gda_connection_commit_transaction (store->priv->cnc, NULL, NULL);
+	}
+}
+
 static gboolean
 handle_schema_version (GdaMetaStore *store, gboolean *schema_present, GError **error)
 {
@@ -2444,7 +2537,9 @@ handle_schema_version (GdaMetaStore *store, gboolean *schema_present, GError **e
 			case 2:
 				migrate_schema_from_v2_to_v3 (store, error);
 			case 3:
-				/* function call for migration from V3 will be here */
+				migrate_schema_from_v3_to_v4 (store, error);
+			case 4:
+				/* function call for migration from V4 will be here */
 				break;
 			default:
 				/* no downgrade to do */
diff --git a/libgda/information_schema.xml b/libgda/information_schema.xml
index cff4fda..11383d0 100644
--- a/libgda/information_schema.xml
+++ b/libgda/information_schema.xml
@@ -4,7 +4,7 @@
     This is the base schema and can be extended by applications by adding new
     DBMS objects, but: the '_' prefix is reserved for Libgda's internals...
 
-    NOTE: when modified, please:
+    IMPORTANT NOTE: when modified, please:
     * run tools/information-schema-doc and move the generated file to doc/C/
     * run tools/information-schema-types and move the generated file to libgda/providers-support/
     * increment the CURRENT_SCHEMA_VERSION define in libgda/gda-meta-store.c and implement the corresponding
@@ -48,7 +48,7 @@
     <column name="schema_name" pkey="TRUE" ident="TRUE"/>
     <column name="schema_owner" nullok="TRUE" descr="Name of the schema"/>
     <column name="schema_internal" type="boolean"  descr="Tells if the schema is specific to the database implementation (and usually can't be modified)"/>
-    <column name="schema_default" type="boolean"  descr="Tells if the schema is used by default when creating a new table without schema name"/>
+    <column name="schema_default" nullok="TRUE" type="boolean"  descr="Tells if the schema is used by default when creating a new table without schema name"/>
     <fkey ref_table="_information_schema_catalog_name">
       <part column="catalog_name" ref_column="catalog_name"/>
     </fkey>
diff --git a/libgda/providers-support/gda-meta-column-types.h b/libgda/providers-support/gda-meta-column-types.h
index 764f287..6e84a22 100644
--- a/libgda/providers-support/gda-meta-column-types.h
+++ b/libgda/providers-support/gda-meta-column-types.h
@@ -44,6 +44,7 @@ static GType _col_types_schemata[] = {
 , G_TYPE_STRING  /* column: schema_name */
 , G_TYPE_STRING  /* column: schema_owner */
 , G_TYPE_BOOLEAN  /* column: schema_internal */
+, G_TYPE_BOOLEAN  /* column: schema_default */
 , G_TYPE_NONE /* end of array marker */
 };
 
diff --git a/libgda/sqlite/gda-sqlite-ddl.c b/libgda/sqlite/gda-sqlite-ddl.c
index dec0dac..6860778 100644
--- a/libgda/sqlite/gda-sqlite-ddl.c
+++ b/libgda/sqlite/gda-sqlite-ddl.c
@@ -320,7 +320,6 @@ _gda_sqlite_render_ADD_COLUMN (GdaServerProvider *provider, GdaConnection *cnc,
 	}
 
 	value = gda_server_operation_get_value_at (op, "/COLUMN_DEF_P/COLUMN_DEFAULT");
-	value = gda_server_operation_get_value_at (op, "/COLUMN_DEF_P/COLUMN_DEFAULT");
 	if (value && G_VALUE_HOLDS (value, G_TYPE_STRING)) {
 		const gchar *str = g_value_get_string (value);
 		if (str && *str) {
diff --git a/tests/meta-store/common.c b/tests/meta-store/common.c
index a76f2f6..ea16cae 100644
--- a/tests/meta-store/common.c
+++ b/tests/meta-store/common.c
@@ -454,10 +454,10 @@ test_schemata_1 (GdaMetaStore *store)
 #define TNAME "_schemata"
 	TEST_HEADER;
 
-	import = common_load_csv_file ("data_schemata.csv", 3, "gboolean", -1);
-	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "pg_catalog", "+2", "a user", "+3", "TRUE", NULL);
-	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "pub", "+2", "postgres", "+3", "FALSE", NULL);
-	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "information_schema", "+2", "postgres", "+3", "TRUE", NULL);
+	import = common_load_csv_file ("data_schemata.csv", 3, "gboolean", 4, "gboolean", -1);
+	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "pg_catalog", "+2", "a user", "+3", "TRUE", "+4", "FALSE", NULL);
+	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "pub", "+2", "postgres", "+3", "FALSE", "+4", "FALSE", NULL);
+	DECL_CHANGE (TNAME, GDA_META_STORE_ADD, "+0", "meta", "+1", "information_schema", "+2", "postgres", "+3", "TRUE", "+4", "TRUE", NULL);
 	TEST_MODIFY (store, TNAME, import, NULL, &error, NULL);
 	TEST_END (import);
 #undef TNAME
@@ -469,8 +469,8 @@ test_schemata_2 (GdaMetaStore *store)
 #define TNAME "_schemata"
 	TEST_HEADER;
 
-	import = common_load_csv_file ("data_schemata_1.csv", 3, "gboolean", -1);	
-	DECL_CHANGE (TNAME, GDA_META_STORE_MODIFY, "-0", "meta", "-1", "pg_catalog", "+0", "meta", "+1", "pg_catalog", "+2", "postgres", "+3", "TRUE", NULL);
+	import = common_load_csv_file ("data_schemata_1.csv", 3, "gboolean", 4, "gboolean", -1);	
+	DECL_CHANGE (TNAME, GDA_META_STORE_MODIFY, "-0", "meta", "-1", "pg_catalog", "+0", "meta", "+1", "pg_catalog", "+2", "postgres", "+3", "TRUE", "+4", "FALSE", NULL);
 	
 	GValue *v1, *v2;
 	g_value_set_string (v1 = gda_value_new (G_TYPE_STRING), "meta");
diff --git a/tests/meta-store/data_schemata.csv b/tests/meta-store/data_schemata.csv
index 973c88d..0c44f1a 100644
--- a/tests/meta-store/data_schemata.csv
+++ b/tests/meta-store/data_schemata.csv
@@ -1,3 +1,3 @@
-"meta","pg_catalog","a user",TRUE
-"meta","pub","postgres",FALSE
-"meta","information_schema","postgres",TRUE
+"meta","pg_catalog","a user",TRUE,FALSE
+"meta","pub","postgres",FALSE,FALSE
+"meta","information_schema","postgres",TRUE,TRUE
diff --git a/tests/meta-store/data_schemata_1.csv b/tests/meta-store/data_schemata_1.csv
index a3632bc..c051346 100644
--- a/tests/meta-store/data_schemata_1.csv
+++ b/tests/meta-store/data_schemata_1.csv
@@ -1 +1 @@
-"meta","pg_catalog","postgres",TRUE
+"meta","pg_catalog","postgres",TRUE,FALSE



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