[libgda] Implemented GdaData.Table.append and Unit Tests * Now you can add new tables to a dabase using GdaDa



commit 6d60697b92464e7c0d9a26b85745d3302b7ddaf2
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Fri Feb 24 11:20:56 2012 -0600

    Implemented GdaData.Table.append and Unit Tests
    * Now you can add new tables to a dabase using GdaData.Table.append method.
    You need to setup, by adding fields and its attributes. No support tested
    for Foreignkeys

 libgda/data/DbFieldInfo.vala |   48 ++++++++++++++++----
 libgda/data/DbTable.vala     |    7 +++-
 libgda/data/FieldInfo.vala   |   52 ++++++++++++---------
 libgda/data/GdaData-5.0.gir  |   63 ++++++++++++++++++++++----
 libgda/data/Table.vala       |  101 +++++++++++++++++++++++++++++++++++++++--
 tests/vala/CheckTable.vala   |  102 +++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 325 insertions(+), 48 deletions(-)
---
diff --git a/libgda/data/DbFieldInfo.vala b/libgda/data/DbFieldInfo.vala
index 7287b47..0fe44b3 100644
--- a/libgda/data/DbFieldInfo.vala
+++ b/libgda/data/DbFieldInfo.vala
@@ -18,10 +18,13 @@
  */
 
 using Gda;
+using Gee;
 
 namespace GdaData {
 	public interface DbFieldInfo : Object
 	{
+		public abstract int                    ordinal           { get; set; }
+		public abstract Type                   value_type        { get; set; }
 		public abstract DbFieldInfo.Attribute  attributes        { get; set; }
 		public abstract Value?                 default_value     { get; set; }
 		public abstract string                 name              { get; set; }
@@ -33,6 +36,9 @@ namespace GdaData {
 		
 		// Check clause expression
 //		public abstract DbSqlExpression        check_expression  { get; set; }
+
+		// Constrains
+		public abstract ForeignKey  fkey   { get; set; }
 		
 		public static Attribute attribute_from_string (string str)
 		{
@@ -71,16 +77,21 @@ namespace GdaData {
 			AUTO_INCREMENT
 		}
 		
-		// Constrains
-		public abstract ForeignKey  fkey   { get; set; }
+		
 		
 		public class ForeignKey {
-			public string    name          { get; set; }
-			public string    refname       { get; set; }
-			public DbTable   reftable      { get; set; }
-			public Match     match         { get; set; }
-			public Rule      update_rule   { get; set; }
-			public Rule      delete_rule   { get; set; }
+			public string                 name        { get; set; }
+			public string                 refname     { get; set; }
+			public DbTable                reftable    { get; set; }
+			public ArrayList<string>      refcol      { get; set; }
+			public Match                  match       { get; set; }
+			public Rule                   update_rule { get; set; }
+			public Rule                   delete_rule { get; set; }
+			
+			public ForeignKey ()
+			{
+				refcol = new ArrayList<string> ();
+			}
 			
 			public static Match match_from_string (string str)
 			{
@@ -113,18 +124,35 @@ namespace GdaData {
 	
 				return Rule.NONE;
 			}
+			
+			public static string rule_to_string (Rule r)
+			{
+				if (r == Rule.CASCADE)
+					return "CASCADE";
+				if (r == Rule.SET_NULL)
+					return "SET NULL";
+				if (r == Rule.SET_DEFAULT)
+					return "SET DEFAULT";
+				if (r == Rule.RESTRICT)
+					return "RESTRICT";
+				if (r == Rule.NO_ACTION)
+					return "NO ACTION";
+	
+				return "NONE";
+			}
+			
 			public enum Match {
 				FULL,
 				PARTIAL,
 				NONE
 			}
 			public enum Rule {
+				NONE,
 				CASCADE,
 				SET_NULL,
 				SET_DEFAULT,
 				RESTRICT,
-				NO_ACTION,
-				NONE
+				NO_ACTION
 			} 
 		}
 	}
diff --git a/libgda/data/DbTable.vala b/libgda/data/DbTable.vala
index 59b4589..a2f7153 100644
--- a/libgda/data/DbTable.vala
+++ b/libgda/data/DbTable.vala
@@ -27,7 +27,7 @@ namespace GdaData
 		public abstract DbCatalog                   catalog       { get; set; }
 		public abstract DbSchema                    schema        { get; set; }
 		public abstract TableType                   table_type    { get; set; }
-		public abstract DbRecordCollection          records       { owned get; }
+		public abstract Collection<DbRecord>        records       { owned get; }
 		public abstract Collection<DbTable>         depends       { owned get; }
 		public abstract Collection<DbTable>         referenced    { owned get; }
 		public abstract Collection<DbFieldInfo>     fields        { owned get; }
@@ -64,4 +64,9 @@ namespace GdaData
 			return TableType.NONE;
 		}
 	}
+	
+	errordomain DbTableError {
+		READ_ONLY,
+		FIELD
+	}
 }
diff --git a/libgda/data/FieldInfo.vala b/libgda/data/FieldInfo.vala
index 3306fe4..2423dd6 100644
--- a/libgda/data/FieldInfo.vala
+++ b/libgda/data/FieldInfo.vala
@@ -22,33 +22,41 @@ using Gda;
 namespace GdaData {
 	public class FieldInfo : Object, DbFieldInfo
 	{
-		private DbFieldInfo.Attribute      _attr;
-		private Value?                 _default_value;
-		private string                 _name;
-		private string                 _desc;
-		private int                    _precision = -1;
-		private int                    _scale = -1;
-		private DbFieldInfo.ForeignKey _fk;
+		private Value?                  _default_value;
+		private DbFieldInfo.ForeignKey  _fkey;
 		
-		public DbFieldInfo.Attribute  attributes {
-			get { return _attr; } set { _attr = value; }
-		}
-		
-		public Value? default_value { 
+		public int                    ordinal           { get; set; }
+		public Type                   value_type        { get; set; }
+		public DbFieldInfo.Attribute  attributes        { get; set; }
+		public Value?                 default_value     
+		{ 
 			get { return _default_value; }
-			set { _default_value = value; }
-		}
-		
-		public string name { 
-			get { return _name; }
-			set { _name = value; }
+			set {
+				attributes = attributes | DbFieldInfo.Attribute.HAVE_DEFAULT;
+				_default_value = value;
+			}
 		}
+		public string                 name              { get; set; }
+		public string                 desc              { get; set; }
 		
-		public string desc { get { return _desc; } set { _desc = value; } }
+		// Numeric and Datetime attributes
+		public int                    precision         { get; set; }
+		public int                    scale             { get; set; }
 		
-		public int precision   { get { return _precision; } set { _precision = value; } }
-		public int scale       { get { return _scale; } set { _scale = value; } }
+		// Constrains
+		public DbFieldInfo.ForeignKey  fkey   
+		{ 
+			get { return _fkey; }
+			set {
+				attributes = attributes | DbFieldInfo.Attribute.FOREIGN_KEY;
+				_fkey = value;
+			}
+		}
 		
-		public DbFieldInfo.ForeignKey  fkey   { get  { return _fk; } set { _fk = value; } }
+		public FieldInfo ()
+		{
+			ordinal = -1;
+		}
+
 	}
 }
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index d30a21c..c8a5bd0 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -141,6 +141,26 @@
 				<type name="GdaData.Table" c:type="GdaDataTable*"/>
 			</return-value>
 		</constructor>
+		<method name="set_field" c:identifier="gda_data_table_set_field" throws="1">
+			<parameters>
+				<parameter name="field" transfer-ownership="none">
+					<type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+				</parameter>
+			</parameters>
+			<return-value transfer-ownership="full">
+				<type name="none"/>
+			</return-value>
+		</method>
+		<method name="get_field" c:identifier="gda_data_table_get_field" throws="1">
+			<parameters>
+				<parameter name="name" transfer-ownership="none">
+					<type name="utf8" c:type="const gchar*"/>
+				</parameter>
+			</parameters>
+			<return-value transfer-ownership="full">
+				<type name="GdaData.DbFieldInfo" c:type="GdaDataDbFieldInfo*"/>
+			</return-value>
+		</method>
 		<constructor name="new" c:identifier="gda_data_table_new">
 			<return-value transfer-ownership="full">
 				<type name="GdaData.Table" c:type="GdaDataTable*"/>
@@ -440,7 +460,9 @@
 			<type name="GdaData.DbTableTableType" c:type="GdaDataDbTableTableType"/>
 		</property>
 		<property name="records">
-			<type name="GdaData.DbRecordCollection" c:type="GdaDataDbRecordCollection*"/>
+			<type name="Gee.Collection" c:type="GeeCollection*">
+				<type name="GdaData.DbRecord" c:type="GdaDataDbRecord*"/>
+			</type>
 		</property>
 		<property name="depends">
 			<type name="Gee.Collection" c:type="GeeCollection*">
@@ -865,6 +887,12 @@
 				<type name="GdaData.DbFieldInfoAttribute" c:type="GdaDataDbFieldInfoAttribute"/>
 			</return-value>
 		</function>
+		<property name="ordinal" writable="1">
+			<type name="gint" c:type="gint"/>
+		</property>
+		<property name="value-type" writable="1">
+			<type name="GObject.Type" c:type="GType"/>
+		</property>
 		<property name="attributes" writable="1">
 			<type name="GdaData.DbFieldInfoAttribute" c:type="GdaDataDbFieldInfoAttribute"/>
 		</property>
@@ -903,6 +931,11 @@
 		<member name="auto_increment" c:identifier="GDA_DATA_DB_FIELD_INFO_ATTRIBUTE_AUTO_INCREMENT" value="128"/>
 	</bitfield>
 	<record name="DbFieldInfoForeignKey">
+		<constructor name="new" c:identifier="gda_data_db_field_info_foreign_key_new">
+			<return-value transfer-ownership="full">
+				<type name="GdaData.DbFieldInfoForeignKey" c:type="GdaDataDbFieldInfoForeignKey*"/>
+			</return-value>
+		</constructor>
 		<function name="match_from_string" c:identifier="gda_data_db_field_info_foreign_key_match_from_string">
 			<parameters>
 				<parameter name="str" transfer-ownership="none">
@@ -923,11 +956,16 @@
 				<type name="GdaData.DbFieldInfoForeignKeyRule" c:type="GdaDataDbFieldInfoForeignKeyRule"/>
 			</return-value>
 		</function>
-		<constructor name="new" c:identifier="gda_data_db_field_info_foreign_key_new">
+		<function name="rule_to_string" c:identifier="gda_data_db_field_info_foreign_key_rule_to_string">
+			<parameters>
+				<parameter name="r" transfer-ownership="none">
+					<type name="GdaData.DbFieldInfoForeignKeyRule" c:type="GdaDataDbFieldInfoForeignKeyRule"/>
+				</parameter>
+			</parameters>
 			<return-value transfer-ownership="full">
-				<type name="GdaData.DbFieldInfoForeignKey" c:type="GdaDataDbFieldInfoForeignKey*"/>
+				<type name="utf8" c:type="gchar*"/>
 			</return-value>
-		</constructor>
+		</function>
 		<property name="name" writable="1">
 			<type name="utf8" c:type="gchar*"/>
 		</property>
@@ -937,6 +975,11 @@
 		<property name="reftable" writable="1">
 			<type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
 		</property>
+		<property name="refcol" writable="1">
+			<type name="Gee.ArrayList" c:type="GeeArrayList*">
+				<type name="utf8" c:type="gchar*"/>
+			</type>
+		</property>
 		<property name="match" writable="1">
 			<type name="GdaData.DbFieldInfoForeignKeyMatch" c:type="GdaDataDbFieldInfoForeignKeyMatch"/>
 		</property>
@@ -953,12 +996,12 @@
 		<member name="none" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_MATCH_NONE" value="2"/>
 	</enumeration>
 	<enumeration name="DbFieldInfoForeignKeyRule" c:type="GdaDataDbFieldInfoForeignKeyRule" glib:type-name="GdaDataDbFieldInfoForeignKeyRule" glib:get-type="gda_data_db_field_info_foreign_key_rule_get_type">
-		<member name="cascade" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_CASCADE" value="0"/>
-		<member name="set_null" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_SET_NULL" value="1"/>
-		<member name="set_default" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_SET_DEFAULT" value="2"/>
-		<member name="restrict" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_RESTRICT" value="3"/>
-		<member name="no_action" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_NO_ACTION" value="4"/>
-		<member name="none" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_NONE" value="5"/>
+		<member name="none" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_NONE" value="0"/>
+		<member name="cascade" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_CASCADE" value="1"/>
+		<member name="set_null" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_SET_NULL" value="2"/>
+		<member name="set_default" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_SET_DEFAULT" value="3"/>
+		<member name="restrict" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_RESTRICT" value="4"/>
+		<member name="no_action" c:identifier="GDA_DATA_DB_FIELD_INFO_FOREIGN_KEY_RULE_NO_ACTION" value="5"/>
 	</enumeration>
 	<interface name="DbSchema" c:type="GdaDataDbSchema" glib:type-name="GdaDataDbSchema" glib:get-type="gda_data_db_schema_get_type" glib:type-struct="DbSchemaIface">
 		<prerequisite name="GdaData.DbObject"/>
diff --git a/libgda/data/Table.vala b/libgda/data/Table.vala
index 9afced6..2680638 100644
--- a/libgda/data/Table.vala
+++ b/libgda/data/Table.vala
@@ -1,7 +1,7 @@
 /* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 /*
  * libgdadata
- * Copyright (C) Daniel Espinosa Ortiz 2011 <esodan gmail com>
+ * Copyright (C) Daniel Espinosa Ortiz 2012 <esodan gmail com>
  * 
  * libgda is free software: you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -24,6 +24,9 @@ namespace GdaData
 {
 	public class Table : Object, DbObject, DbNamedObject, DbTable
 	{
+		private bool _read_only = false;
+		private int  _n_cols = -1;
+		
 		protected DbTable.TableType           _type;
 		protected DbRecordCollection          _records;
 		protected HashMap<string,DbFieldInfo> _fields = new HashMap<string,DbFieldInfo> ();
@@ -36,7 +39,9 @@ namespace GdaData
 				_fields.set (f.name, f);
 			}
 		}
+		
 		// DbObject Interface
+		
 		public Connection connection { get; set; }
 		
 		public void update () throws Error 
@@ -90,6 +95,7 @@ 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);
+				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);
@@ -102,6 +108,7 @@ namespace GdaData
 				// Default Value
 				string fdv = (string) mt.get_value_at (mt.get_column_index ("column_default"), r);
 				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) {
 					fi.attributes = fi.attributes | DbFieldInfo.Attribute.HAVE_DEFAULT;
 					fi.default_value = DbField.value_from_string (fdv, ft);
@@ -172,6 +179,16 @@ namespace GdaData
 					var delete_rule = (string) mfk.get_value_at (
 														mfk.get_column_index ("delete_rule"), 0);
 					f.fkey.delete_rule = DbFieldInfo.ForeignKey.rule_from_string (delete_rule);
+					
+					var mfkr = store.extract ("SELECT * FROM _detailed_fk " + 
+											" WHERE fk_table_name  = ##name::string"+
+				                         " AND fk_constraint_name = ##constraint_name::string" +
+				                         " AND fk_table_catalog = ##catalog::string"+
+				                         " AND fk_table_schema = ##schema::string", vals);
+					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);
+					}
 				}
 				if (DbFieldInfo.Attribute.CHECK in  f.attributes) 
 				{
@@ -179,7 +196,9 @@ namespace GdaData
 				}
 				_fields.set (f.name, f);
 			}
+			
 			// Table referencing this
+			
 			var mtr = store.extract ("SELECT * FROM "+
 			                     	 "_detailed_fk WHERE ref_table_name  = ##name::string"+
 			                         " AND ref_table_catalog = ##catalog::string"+
@@ -191,15 +210,70 @@ namespace GdaData
 				tr.name = tn;
 				_referenced.set (tr.name, tr);
 			}
+			_read_only = true;
+			_n_cols = _fields.size;
 		}
-		public void save () throws Error {}
 		
-		public void append () throws Error {}
+		public void save () throws Error 
+		{
+			throw new DbTableError.READ_ONLY ("Table's definition is read only");
+		}
+		
+		public void append () throws Error 
+		{
+			var op = connection.create_operation (Gda.ServerOperationType.CREATE_TABLE, null);
+			op.set_value_at_path (name, "/TABLE_DEF_P/TABLE_NAME");
+			if (DbTable.TableType.LOCAL_TEMPORARY in table_type)
+				op.set_value_at_path ("TRUE","/TABLE_DEF_P/TABLE_TEMP");
+			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), 
+										"/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 ());
+				}
+				if (DbFieldInfo.Attribute.CAN_BE_NULL in f.attributes) {
+					op.set_value_at_path ("TRUE", "/FIELDS_A/@COLUMN_NNUL/" + f.ordinal.to_string ());
+				}
+				if (DbFieldInfo.Attribute.AUTO_INCREMENT in f.attributes) {
+					op.set_value_at_path ("TRUE", "/FIELDS_A/@COLUMN_AUTOINC/" + f.ordinal.to_string ());
+				}
+				if (DbFieldInfo.Attribute.UNIQUE in f.attributes) {
+					op.set_value_at_path ("TRUE", "/FIELDS_A/@COLUMN_UNIQUE/" + f.ordinal.to_string ());
+				}
+				if (DbFieldInfo.Attribute.HAVE_DEFAULT in f.attributes) {
+					op.set_value_at_path (connection.value_to_sql_string (f.default_value),
+											"/FIELDS_A/@COLUMN_DEFAULT/" + f.ordinal.to_string ());
+				}
+				if (DbFieldInfo.Attribute.FOREIGN_KEY in f.attributes) {
+					op.set_value_at_path (f.fkey.reftable.name, "/FKEY_S/" + refs.to_string () + 
+									"/FKEY_REF_TABLE");
+					int rc = 0;
+					foreach (string fkc in f.fkey.refcol) {
+						op.set_value_at_path (f.name, "/FKEY_S/" + refs.to_string ()
+										+ "/FKEY_FIELDS_A/@FK_FIELD/" + rc.to_string ());
+						op.set_value_at_path (fkc, "/FKEY_S/" + refs.to_string ()
+										+ "/FKEY_FIELDS_A/@FK_REF_PK_FIELD/" + rc.to_string ());
+						rc++;
+					}
+
+					op.set_value_at_path (DbFieldInfo.ForeignKey.rule_to_string (f.fkey.update_rule),
+										"/FKEY_S/" + refs.to_string () + "/FKEY_ONUPDATE");
+					op.set_value_at_path (DbFieldInfo.ForeignKey.rule_to_string (f.fkey.delete_rule),
+										"/FKEY_S/" + refs.to_string () + "/FKEY_ONDELETE");
+					refs++;
+				}
+			}
+			connection.perform_operation (op);
+		}
 		
 		// DbNamedObject Interface
+		
 		public string name { get; set; }
 		
 		// DbTable Interface
+		
 		public DbTable.TableType table_type { get { return _type; } set { _type = value; } }
 
 		public Collection<DbFieldInfo> fields { 
@@ -225,7 +299,7 @@ namespace GdaData
 		
 		public DbSchema  schema { get; set; }
 		
-		public DbRecordCollection records { 
+		public Collection<DbRecord> records { 
 			owned get  {
 				try {
 					var q = new Gda.SqlBuilder (SqlStatementType.SELECT);
@@ -245,5 +319,24 @@ namespace GdaData
 		public Collection<DbTable> referenced { 
 			owned get { return _referenced.values; } 
 		}
+		
+		public void set_field (DbFieldInfo field) throws Error
+		{
+			if (_read_only) {
+				throw new DbTableError.READ_ONLY ("Table's definition is read only");
+			}
+			if (field.ordinal < 0) {
+				_n_cols++;
+				field.ordinal = _n_cols;
+			}
+			_fields.set (field.name, field);
+		}
+		
+		public DbFieldInfo get_field (string name) throws Error
+		{
+			if (!_fields.has_key (name))
+				throw new DbTableError.FIELD ("Field '" + name + "' not found");
+			return _fields.get (name);
+		}
 	}
 }
diff --git a/tests/vala/CheckTable.vala b/tests/vala/CheckTable.vala
index e04f846..965ac57 100644
--- a/tests/vala/CheckTable.vala
+++ b/tests/vala/CheckTable.vala
@@ -152,6 +152,22 @@ namespace Check {
 				}
 			}
 			
+			// DbFieldInfo
+			
+			var fl = new FieldInfo ();
+			fl.name = "FieldName1";
+			if (GLib.strcmp (fl.name, "FieldName1") != 0) {
+				fails++;
+				stdout.printf (">>>>>>>> Default Value No Match. Holded \'"+
+				               (string) fl.name + "\' But Expected \"FieldName1\" : FAIL\n");
+			}
+			fl.name = "NewFieldName";
+			if (GLib.strcmp (fl.name, "NewFieldName") != 0) {
+				fails++;
+				stdout.printf (">>>>>>>> Default Value No Match. Holded \'"+
+				               (string) fl.name + "\' But Expected \"NewFieldName\" : FAIL\n");
+			}
+			
 			if (found == 0) {
 				stdout.printf (">>>>>>>> Check Default Values: FAIL\n");
 				fails++;
@@ -178,6 +194,90 @@ namespace Check {
 			return fails;
 		}
 		
+		public int append ()
+			throws Error
+		{
+			stdout.printf("\n\n\n>>>>>>>>>>>>>>> NEW TEST: Gda.DbTable - Append...\n");
+			int fails = 0;
+			
+			var t = new Table ();
+			t.name = "created_table";
+			t.connection = connection;
+			var field = new FieldInfo ();
+			field.name = "id";
+			field.value_type = typeof (int);
+			field.attributes = DbFieldInfo.Attribute.PRIMARY_KEY | 
+								DbFieldInfo.Attribute.AUTO_INCREMENT;
+			t.set_field (field);
+			
+			var field1 = new FieldInfo ();
+			field1.name = "name";
+			field1.value_type = typeof (string);
+			field1.attributes = DbFieldInfo.Attribute.NONE;
+			t.set_field (field1);
+			
+			var field2 = new FieldInfo ();
+			field2.name = "company";
+			field2.value_type = typeof (int);
+			field2.default_value = 1;
+			var fk = new DbFieldInfo.ForeignKey ();
+			var rt = new Table ();
+			rt.name = "company";
+			fk.reftable = rt;
+			fk.refcol.add ("id");
+			fk.update_rule = DbFieldInfo.ForeignKey.Rule.CASCADE;
+			fk.delete_rule = DbFieldInfo.ForeignKey.Rule.SET_DEFAULT;
+			field2.fkey = fk;
+			t.set_field (field2);
+			
+			foreach (DbFieldInfo f in t.fields) {
+				stdout.printf ("Field: " + f.name + 
+								"\nType: " + Gda.g_type_to_string (f.value_type) +
+								"\nAttr: " + ((int)f.attributes).to_string () + "\n");
+			}
+			
+			t.append ();
+			
+			var m = connection.execute_select_command ("SELECT * FROM created_table");
+			
+			bool f = false;
+			if (m.get_column_index ("id") != 0)
+				f = true;
+			if (m.get_column_index ("name") != 1)
+				f = true;
+			if (m.get_column_index ("company") != 2)
+				f = true;
+			if (f) {
+				fails++;
+				stdout.printf ("Check Ordinal position: FAILED\n");
+			}
+			
+			connection.execute_non_select_command ("INSERT INTO created_table (name) VALUES (\"Nancy\")");
+			
+			var m2 = connection.execute_select_command ("SELECT * FROM created_table");
+			bool f2 = false;
+			if (m2.get_n_rows () != 1) 
+				f2 = true;
+			int id = (int) m2.get_value_at (0,0);
+			if (id != 1)
+				f2 = true;
+			string name = (string) m2.get_value_at (1,0);
+			if (GLib.strcmp (name, "Nancy") != 0)
+				f2 = true;
+			int company = (int) m2.get_value_at (2,0);
+			if (company != 1)
+				f2 = true;
+			if (f) {
+				fails++;
+				stdout.printf ("Check Table Values: FAILED\n");
+			}
+			if (fails > 0)
+				stdout.printf (">>>>>>>> FAIL <<<<<<<<<<<\n");
+			else
+				stdout.printf (">>>>>>>> TEST PASS <<<<<<<<<<<\n");
+			return fails;
+		}
+		
 		public static int main (string[] args) {
 			stdout.printf ("\n\n\n>>>>>>>>>>>>>>>> NEW TEST: Checking GdaData.DbRecord implementation... <<<<<<<<<< \n");
 			int failures = 0;
@@ -188,7 +288,7 @@ namespace Check {
 				failures += app.fields ();
 				failures += app.records ();
 				//failures += app.expression ();
-				//failures += app.append ();
+				failures += app.append ();
 				//failures += app.save ();
 			}
 			catch (Error e) 



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