[libgda] Test for GdaData.Table test for PostgreSQL. * Found Bug 670859 with fails on Table.update in _schema



commit 766e6213bb94571d460f2857ffb593663ae8e01e
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Sun Feb 26 23:18:53 2012 -0600

    Test for GdaData.Table test for PostgreSQL.
    * Found Bug 670859 with fails on Table.update in _schemata and _information_schema_catalog_name
    * Found Bug 670860 with fails on Table.update in _key_column_usage due to CHECK constraint not
    updated

 libgda/data/Table.vala     |  131 +++++++++++++++++++++++++++++++++++++-------
 tests/vala/CheckTable.vala |   99 ++++++++++++++++++++++++++++++---
 2 files changed, 200 insertions(+), 30 deletions(-)
---
diff --git a/libgda/data/Table.vala b/libgda/data/Table.vala
index 2680638..57c0d27 100644
--- a/libgda/data/Table.vala
+++ b/libgda/data/Table.vala
@@ -26,6 +26,7 @@ namespace GdaData
 	{
 		private bool _read_only = false;
 		private int  _n_cols = -1;
+		private bool _updated_meta = false;
 		
 		protected DbTable.TableType           _type;
 		protected DbRecordCollection          _records;
@@ -46,30 +47,60 @@ namespace GdaData
 		
 		public void update () throws Error 
 		{
-			var store = connection.get_meta_store ();
 			_fields.clear ();
+			_updated_meta = false;
 			string cond = "";
 			
 			var ctx = new Gda.MetaContext ();
 			ctx.set_table ("_columns");
 			ctx.set_column ("table_name", name, connection);
-			// May be is necesary to set schema and catalog
-			connection.update_meta_store (ctx);
+			try { connection.update_meta_store (ctx); }
+			catch (Error e) {
+				GLib.message ("Updating meta store for _columns fails with error: " + e.message+"\n");
+				GLib.message ("Trying to update all...");
+				_updated_meta = connection.update_meta_store (null);
+				_updated_meta = true;
+			}
+			
+			var store = connection.get_meta_store ();
 			
 			var vals = new HashTable<string,Value?> (str_hash,str_equal);
 			vals.set ("name", name);
 			if (schema == null)
 			{
-				var cxc = new Gda.MetaContext ();
-				cxc.set_table ("_information_schema_catalog_name");
-				connection.update_meta_store (cxc);
+				if (!_updated_meta) {
+					try { 
+						var cxc = new Gda.MetaContext ();
+						cxc.set_table ("_information_schema_catalog_name");
+						connection.update_meta_store (cxc); 
+					}
+					catch (Error e) {
+						GLib.message ("Updating meta store for _information_schema_catalog_name fails with error: "
+										 + e.message+"\n");
+						GLib.message ("Trying to update all...");
+						_updated_meta = connection.update_meta_store (null);
+						if (_updated_meta)
+							GLib.message ("Meta store update ... Ok");
+					}
+				}
 				var catm = store.extract ("SELECT * FROM _information_schema_catalog_name", null);
 				catalog = new Catalog ();
 				catalog.connection = connection;
 				catalog.name = (string) catm.get_value_at (catm.get_column_index ("catalog_name"), 0);
-				var cxs = new Gda.MetaContext ();
-				cxs.set_table ("_schemata");
-				connection.update_meta_store (cxs);
+				if (!_updated_meta) {
+					try { 
+						var cxs = new Gda.MetaContext ();
+						cxs.set_table ("_schemata");
+						connection.update_meta_store (cxs); 
+					}
+					catch (Error e) {
+						GLib.message ("Updating meta store for _schemata fails with error: " + e.message+"\n");
+						GLib.message ("Trying to update all...");
+						_updated_meta = connection.update_meta_store (null);
+						if (_updated_meta)
+							GLib.message ("Meta store update ... Ok");
+					}
+				}
 				var schm = store.extract ("SELECT * FROM _schemata WHERE schema_default = TRUE", null);
 				schema = new Schema ();
 				schema.catalog = catalog;
@@ -94,19 +125,32 @@ namespace GdaData
 			{
 				var fi = new FieldInfo ();
 				fi.name = (string) mt.get_value_at (mt.get_column_index ("column_name"), r);
-				fi.desc = (string) mt.get_value_at (mt.get_column_index ("column_comments"), r);
+				Value v = mt.get_value_at (mt.get_column_index ("column_comments"), r);
+				if (v.holds (typeof (string)))
+					fi.desc = (string) v;
+				else
+					fi.desc = "";
 				fi.ordinal = (int) mt.get_value_at (mt.get_column_index ("ordinal_position"), r);
 				// Set attributes
 				fi.attributes = DbFieldInfo.Attribute.NONE;
 				bool fcbn = (bool) mt.get_value_at (mt.get_column_index ("is_nullable"), r);
 				if (fcbn)
 					fi.attributes = fi.attributes | DbFieldInfo.Attribute.CAN_BE_NULL;
-				string fai = (string) mt.get_value_at (mt.get_column_index ("extra"), r);
+				string fai;
+				v = mt.get_value_at (mt.get_column_index ("extra"), r);
+				if (v.holds (typeof (string)))
+					fai = (string) v;
+				else
+					fai = "";
 				if (fai == "AUTO_INCREMENT")
 					fi.attributes = fi.attributes | DbFieldInfo.Attribute.AUTO_INCREMENT;
-				
 				// Default Value
-				string fdv = (string) mt.get_value_at (mt.get_column_index ("column_default"), r);
+				string fdv;
+				v = mt.get_value_at (mt.get_column_index ("column_default"), r);
+				if (v.holds (typeof (string)))
+					fdv = (string) v;
+				else
+					fdv = "";
 				Type ft = Gda.g_type_from_string ((string) mt.get_value_at (mt.get_column_index ("gtype"), r));
 				fi.value_type = ft;
 				if (fdv != null) {
@@ -127,32 +171,75 @@ namespace GdaData
 				_fields.set (fi.name, fi);
 			}
 			// Constraints
-			ctx.set_table ("_table_constraints");
-			connection.update_meta_store (ctx);
-			ctx.set_table ("_key_column_usage");
-			connection.update_meta_store (ctx);
+			if (!_updated_meta) {
+				try { 
+					var cxcr = new Gda.MetaContext ();
+					cxcr.set_table ("_table_constraints");
+					connection.update_meta_store (cxcr); 
+				}
+				catch (Error e) {
+					GLib.message ("Updating meta store for _table_constraints usage fails with error: " 
+									+ e.message+"\n");
+					GLib.message ("Trying to update all...");
+					_updated_meta = connection.update_meta_store (null);
+					if (_updated_meta)
+							GLib.message ("Meta store update ... Ok");
+				}
+			}
+			if (!_updated_meta) {
+				try { 
+					var cxcr2 = new Gda.MetaContext ();
+					cxcr2.set_table ("_key_column_usage");
+					connection.update_meta_store (cxcr2); 
+				}
+				catch (Error e) {
+					GLib.message ("Updating meta store for _key_column_usage usage fails with error: "
+									 + e.message+"\n");
+					GLib.message ("Trying to update all...");
+					_updated_meta = connection.update_meta_store (null);
+					if (_updated_meta)
+							GLib.message ("Meta store update ... Ok");
+				}
+			}
 			var mc = store.extract ("SELECT * FROM _table_constraints"+
 			                        " WHERE table_name  = ##name::string" + cond,
 									vals);
+			stdout.printf ("EXTRACTED _table_constraints:\n" + mc.dump_as_string ());
 			for (r = 0; r < mc.get_n_rows (); r++) 
 			{
 				string ct = (string) mc.get_value_at (mc.get_column_index ("constraint_type"), r);
 				string cn = (string) mc.get_value_at (mc.get_column_index ("constraint_name"), r);
+				GLib.message ("Constraint Name = " + cn + "Type = " + ct + "\n");
 				vals.set ("constraint_name", cn);
 				var mpk = store.extract ("SELECT * FROM _key_column_usage"+
 				                         " WHERE table_name  = ##name::string"+
 				                         " AND constraint_name = ##constraint_name::string" + cond, vals);
+				stdout.printf ("EXTRACTED _key_column_usagess:\n" + mpk.dump_as_string ());
 				var colname = (string) mpk.get_value_at (mpk.get_column_index ("column_name"), 0);
 				var f = _fields.get (colname);
 				f.attributes = f.attributes | DbFieldInfo.attribute_from_string (ct);
 				
 				if (DbFieldInfo.Attribute.FOREIGN_KEY in f.attributes) 
 				{
-					ctx.set_table ("_referential_constraints");
-					connection.update_meta_store (ctx);
+					if (!_updated_meta) {
+						try { 
+							var cxcr3 = new Gda.MetaContext ();
+							cxcr3.set_table ("_referential_constraints");
+							connection.update_meta_store (cxcr3); 
+						}
+						catch (Error e) {
+							GLib.message ("Updating for _referential_constraints usage fails with error: " 
+											+ e.message+"\n");
+							GLib.message ("Trying to update all...");
+							_updated_meta = connection.update_meta_store (null);
+							if (_updated_meta)
+									GLib.message ("Meta store update ... Ok");
+						}
+					}
 					var mfk = store.extract ("SELECT * FROM _referential_constraints"+
 					                         " WHERE table_name  = ##name::string "+
 					                         "AND constraint_name = ##constraint_name::string" + cond, vals);
+					stdout.printf ("EXTRACTED _referential_constraints:\n" + mfk.dump_as_string ());
 					f.fkey = new DbFieldInfo.ForeignKey ();
 					f.fkey.name = cn;
 					f.fkey.refname = (string) mfk.get_value_at (mfk.get_column_index ("ref_constraint_name"), 0);
@@ -185,6 +272,7 @@ namespace GdaData
 				                         " AND fk_constraint_name = ##constraint_name::string" +
 				                         " AND fk_table_catalog = ##catalog::string"+
 				                         " AND fk_table_schema = ##schema::string", vals);
+					stdout.printf ("EXTRACTED _detailed_fk 1:\n" + mfkr.dump_as_string ());
 					for (int r2 = 0; r2 < mfkr.get_n_rows (); r2++) {
 						var rc = (string) mfkr.get_value_at (mfkr.get_column_index ("ref_column"), r2);
 						f.fkey.refcol.add (rc);
@@ -203,6 +291,9 @@ namespace GdaData
 			                     	 "_detailed_fk WHERE ref_table_name  = ##name::string"+
 			                         " AND ref_table_catalog = ##catalog::string"+
 			                         " AND ref_table_schema = ##schema::string", vals);
+			if (mtr.get_n_rows () == 0)
+				GLib.message ("Rows = 0");
+			GLib.message ("EXTRACTED _detailed_fk 2:\n" + mtr.dump_as_string ());
 			for (r = 0; r < mtr.get_n_rows (); r++) {
 				var tn = (string) mtr.get_value_at (mtr.get_column_index ("fk_table_name"), r);
 				var tr = new Table ();
@@ -228,7 +319,7 @@ namespace GdaData
 			int refs = 0;
 			foreach (DbFieldInfo f in fields) {
 				op.set_value_at_path (f.name, "/FIELDS_A/@COLUMN_NAME/" + f.ordinal.to_string ());
-				op.set_value_at_path (Gda.g_type_to_string (f.value_type), 
+				op.set_value_at_path (connection.get_provider ().get_default_dbms_type (connection, f.value_type), 
 										"/FIELDS_A/@COLUMN_TYPE/" + f.ordinal.to_string ());
 				if (DbFieldInfo.Attribute.PRIMARY_KEY in f.attributes) {
 					op.set_value_at_path ("TRUE", "/FIELDS_A/@COLUMN_PKEY/" + f.ordinal.to_string ());
diff --git a/tests/vala/CheckTable.vala b/tests/vala/CheckTable.vala
index 965ac57..1651ab6 100644
--- a/tests/vala/CheckTable.vala
+++ b/tests/vala/CheckTable.vala
@@ -30,10 +30,82 @@ namespace Check {
 		{
 			try {
 				GLib.FileUtils.unlink("table.db");
+				bool usepg = false;
+				stdout.printf ("Trying to use PostgreSQL provider\n");
+				try {
+					this.connection = Connection.open_from_string ("PostgreSQL", 
+										"DB_NAME=test", null,
+										Gda.ConnectionOptions.NONE);
+					if (this.connection.is_opened ()) {
+						usepg = true;
+						init_pg ();
+					}
+				}
+			 	catch (Error e) { 
+					stdout.printf ("ERROR: Not using PostgreSQL provider. Message: " + e.message); 
+				}
 				stdout.printf("Creating Database...\n");
-				this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=table", null, Gda.ConnectionOptions.NONE);
+				if (!usepg) {
+					this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=table", null, 
+									Gda.ConnectionOptions.NONE);
+					Init_sqlite ();
+				}
+			}
+			catch (Error e) {
+				stdout.printf ("Couln't initalize database...\nERROR: %s\n", e.message);
+			}
+		}
+		
+		private void init_pg () throws Error
+		{
+			try {
+				try {this.connection.execute_non_select_command("DROP TABLE IF EXISTS company CASCADE");}
+				catch (Error e) { stdout.printf ("Error on dopping table company: "+e.message+"\n"); }
 				stdout.printf("Creating table 'company'...\n");
-				this.connection.execute_non_select_command("CREATE TABLE company (id int PRIMARY KEY, name string, responsability string)");
+				this.connection.execute_non_select_command("CREATE TABLE company (id serial PRIMARY KEY, " +
+				"name text, responsability text)");
+				this.connection.execute_non_select_command("INSERT INTO company (name, responsability) " + 
+					"VALUES (\'Telcsa\', \'Programing\')");
+				this.connection.execute_non_select_command("INSERT INTO company (name, responsability) " + 
+					"VALUES (\'Viasa\', \'Accessories\')");
+			}
+			catch (Error e) { stdout.printf ("Error on Create company table: " + e.message+"\n"); }
+			try {
+				try {this.connection.execute_non_select_command("DROP TABLE IF EXISTS customer CASCADE");}
+				catch (Error e) { stdout.printf ("Error on dopping table customer: "+e.message+"\n"); }
+				stdout.printf("Creating table 'customer'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE customer (id serial PRIMARY KEY, " +
+							"name text UNIQUE,"+
+							" city text DEFAULT \'New Yield\',"+
+							" company integer REFERENCES company (id) ON DELETE SET NULL ON UPDATE CASCADE)");
+				this.connection.execute_non_select_command("INSERT INTO customer (name, city, company) " +
+					"VALUES (\'Daniel\', \'Mexico\', 1)");
+				this.connection.execute_non_select_command("INSERT INTO customer (name, city) VALUES " +
+					"(\'Jhon\', \'Springfield\')");
+				this.connection.execute_non_select_command("INSERT INTO customer (name) VALUES (\'Jack\')");
+			}
+			catch (Error e) { stdout.printf ("Error on Create customer table: " + e.message + "\n"); }
+			try {
+				try {this.connection.execute_non_select_command("DROP TABLE IF EXISTS salary CASCADE");}
+				catch (Error e) { stdout.printf ("Error on dopping table salary: "+e.message+"\n"); }
+				stdout.printf("Creating table 'salary'...\n");
+				this.connection.execute_non_select_command("CREATE TABLE salary (id serial PRIMARY KEY,"+
+				                                           " customer integer REFERENCES customer (id) "+
+				                                           " ON DELETE CASCADE ON UPDATE CASCADE,"+
+				                                           " income float DEFAULT 10.0)");
+				this.connection.execute_non_select_command("INSERT INTO salary (customer, income) VALUES " +
+					" (1,55.0)");
+				this.connection.execute_non_select_command("INSERT INTO salary (customer, income) VALUES " +
+					" (2,65.0)");
+				this.connection.execute_non_select_command("INSERT INTO salary (customer) VALUES (3)");
+			}
+			catch (Error e) { stdout.printf ("Error on Create company table: " + e.message + "\n"); }
+			this.connection.update_meta_store (null);
+		}
+		
+		private void Init_sqlite () throws Error
+		{
+			this.connection.execute_non_select_command("CREATE TABLE company (id int PRIMARY KEY, name string, responsability string)");
 				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (1, \"Telcsa\", \"Programing\")");
 				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (2, \"Viasa\", \"Accessories\")");
 				
@@ -51,16 +123,13 @@ namespace Check {
 				this.connection.execute_non_select_command("INSERT INTO salary (id, customer, income) VALUES (1,1,55.0)");
 				this.connection.execute_non_select_command("INSERT INTO salary (id, customer, income) VALUES (2,2,65.0)");
 				this.connection.execute_non_select_command("INSERT INTO salary (customer) VALUES (3)");
-			}
-			catch (Error e) {
-				stdout.printf ("Couln't create temporary database...\nERROR: %s\n", e.message);
-			}
 		}
 		
+		
 		public void init ()
 			throws Error
 		{
-			stdout.printf("Creating new table\n");
+			stdout.printf("\nCreating new table\n");
 			table = new Table ();
 			stdout.printf("Setting connection\n");
 			table.connection = this.connection;
@@ -73,7 +142,11 @@ namespace Check {
 			stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: GdaData.DbTable -- Update\n");
 			int fails = 0;
 			stdout.printf(">>>>>> Updating meta information\n");
-			table.update ();
+			try { table.update (); }
+			catch (Error e) { 
+				fails++; 
+				stdout.printf ("Error on Updating: "+e.message+"\n");
+			}
 			if (fails > 0)
 				stdout.printf (">>>>>>>> FAIL <<<<<<<<<<<\n");
 			else
@@ -252,7 +325,13 @@ namespace Check {
 				stdout.printf ("Check Ordinal position: FAILED\n");
 			}
 			
-			connection.execute_non_select_command ("INSERT INTO created_table (name) VALUES (\"Nancy\")");
+			var r = new Record ();
+			r.connection = connection;
+			var nt = new Table ();
+			nt.name = "created_table";
+			r.table = nt;
+			r.set_field_value ("name", "Nancy");
+			r.append ();
 			
 			var m2 = connection.execute_select_command ("SELECT * FROM created_table");
 			bool f2 = false;
@@ -279,7 +358,7 @@ namespace Check {
 		}
 		
 		public static int main (string[] args) {
-			stdout.printf ("\n\n\n>>>>>>>>>>>>>>>> NEW TEST: Checking GdaData.DbRecord implementation... <<<<<<<<<< \n");
+			stdout.printf ("\n\n\n>>>>>>>>>>>>>>>> NEW TEST: Checking GdaData.DbTable implementation... <<<<<<<<<< \n");
 			int failures = 0;
 			var app = new Tests ();
 			try {



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