Browse Source

2006-02-13 Senganal T <[email protected]>

	* Test/ProviderTests/System.Data.SqlClient/SqlAdapterTest.cs : Added testcases for #77480
	* System.Data.Common/DbDataAdapter.cs :
		- FillSchema  : 
			- Add table to schema only if MissingSchemaAction is not Ignore
			Add schema to table if MissingSchemaAction is set to either of
			Add or AddWithKey
		- BuildSchema :
			- Set the Schema values only if MissingSchemaAction is set to
			AddWithKey


svn path=/trunk/mcs/; revision=56830
Senganal T 20 years ago
parent
commit
cfae80d105

+ 11 - 0
mcs/class/System.Data/System.Data.Common/ChangeLog

@@ -1,3 +1,14 @@
+2006-02-13  Senganal T  <[email protected]>
+
+	* DbDataAdapter.cs :
+		- FillSchema  : 
+			- Add table to schema only if MissingSchemaAction is not Ignore
+			Add schema to table if MissingSchemaAction is set to either of
+			Add or AddWithKey
+		- BuildSchema :
+			- Set the Schema values only if MissingSchemaAction is set to
+			AddWithKey
+
 2006-01-31  Senganal T  <[email protected]>
 
 	* DbDataAdapter.cs :

+ 45 - 22
mcs/class/System.Data/System.Data.Common/DbDataAdapter.cs

@@ -513,7 +513,15 @@ namespace System.Data.Common {
 				string tableName =  SetupSchema (schemaType, dataTable.TableName);
 				if (tableName != null)
 				{
-					BuildSchema (reader, dataTable, schemaType);
+					// FillSchema should add the KeyInfo unless MissingSchemaAction
+					// is set to Ignore or Error.
+					MissingSchemaAction schemaAction = MissingSchemaAction;
+					if (!(schemaAction == MissingSchemaAction.Ignore ||
+						schemaAction == MissingSchemaAction.Error))
+						schemaAction = MissingSchemaAction.AddWithKey;
+
+					BuildSchema (reader, dataTable, schemaType, schemaAction,
+						MissingMappingAction, TableMappings);
 				}
 			}
 			finally
@@ -542,6 +550,13 @@ namespace System.Data.Common {
 			DataTable table;
 			try
 			{
+				// FillSchema should add the KeyInfo unless MissingSchemaAction
+				// is set to Ignore or Error.
+				MissingSchemaAction schemaAction = MissingSchemaAction;
+				if (!(MissingSchemaAction == MissingSchemaAction.Ignore ||
+					MissingSchemaAction == MissingSchemaAction.Error))
+					schemaAction = MissingSchemaAction.AddWithKey;
+
 				do {
 					tableName = SetupSchema (schemaType, tableName);
 					if (tableName != null)
@@ -550,10 +565,14 @@ namespace System.Data.Common {
 							table = dataSet.Tables [tableName];	
 						else
 						{
-							table = new DataTable(tableName);
-							dataSet.Tables.Add (table);
+							// Do not create schema if MissingSchemAction is set to Ignore
+							if (this.MissingSchemaAction == MissingSchemaAction.Ignore)
+								continue;
+							table =  dataSet.Tables.Add (tableName);
 						}
-						BuildSchema (reader, table, schemaType);
+						
+						BuildSchema (reader, table, schemaType, schemaAction,
+							MissingMappingAction, TableMappings);
 						output.Add (table);
 						tableName = String.Format ("{0}{1}", srcTable, ++index);
 					}
@@ -703,21 +722,25 @@ namespace System.Data.Common {
 								mapping = tmp;
 							}				
 
-							object value = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
-							bool allowDBNull = value is bool ? (bool)value : true;
-							col.AllowDBNull = allowDBNull; 
-							value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
-							bool isKey = value is bool ? (bool)value : false;
 
 							if (missingSchAction == MissingSchemaAction.AddWithKey) {
 	                            
+								object value = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
+								bool allowDBNull = value is bool ? (bool)value : true;
+
+								value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
+								bool isKey = value is bool ? (bool)value : false;
+
 								value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null;
 								bool isAutoIncrement = value is bool ? (bool)value : false;
+
 								value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null;
 								bool isReadOnly = value is bool ? (bool)value : false;
+
 								value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null;
 								bool isUnique = value is bool ? (bool)value : false;
 								
+								col.AllowDBNull = allowDBNull;
 								// fill woth key info								
 								if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType)) {
 									col.AutoIncrement = true;
@@ -739,20 +762,20 @@ namespace System.Data.Common {
 									if (!allowDBNull)
 										col.AllowDBNull = false;
 								}
+								
+								// This might not be set by all DataProviders
+								bool isHidden = false;
+								if (schemaTable.Columns.Contains ("IsHidden")) {
+									value = schemaRow["IsHidden"];
+									isHidden = ((value is bool) ? (bool)value : false);
+								}
+
+								if (isKey && !isHidden) {
+									primaryKey.Add (col);
+									if (allowDBNull)
+										createPrimaryKey = false;
+								}
 							}
-						
-							bool isHidden = false;
-							if (schemaTable.Columns.Contains ("IsHidden")) {
-								value = schemaRow["IsHidden"];
-								isHidden = ((value is bool) ? (bool)value : false);
-							}
-							
-							if (isKey && !isHidden) {
-								primaryKey.Add (col);
-								if (allowDBNull)
-									createPrimaryKey = false;
-							}
-							
 							// add the ordinal of the column as a key and the index of the column in the datareader as a value.
 							mapping[col.Ordinal] = readerIndex++;
 						}

+ 4 - 0
mcs/class/System.Data/Test/ProviderTests/System.Data.SqlClient/ChangeLog

@@ -1,3 +1,7 @@
+2006-02-13  Senganal T  <[email protected]>
+
+	* SqlAdapterTest.cs : Added testcases for #77480
+
 2006-02-10  Senganal T  <[email protected]>
 
 	* SqlDataReaderTest.cs : Added more tests to verify Sequential

+ 76 - 0
mcs/class/System.Data/Test/ProviderTests/System.Data.SqlClient/SqlDataAdapterTest.cs

@@ -657,5 +657,81 @@ namespace MonoTests.System.Data.SqlClient
 				Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
 			}
 		}
+
+		[Test]
+		public void Fill_CheckSchema ()
+		{
+			SqlConnection conn = new SqlConnection(connectionString);
+			using (conn) {
+				conn.Open();
+
+				IDbCommand command = conn.CreateCommand();
+
+				// Create Temp Table
+				String cmd = "Create Table #tmp_TestTable (" ;
+				cmd += "id int primary key,";
+				cmd += "field int not null)";
+				command.CommandText = cmd; 
+				command.ExecuteNonQuery();
+
+				DataSet dataSet = new DataSet();
+				string selectString = "SELECT * from #tmp_TestTable";
+				IDbDataAdapter dataAdapter = new SqlDataAdapter (
+									selectString,conn);
+				dataAdapter.Fill (dataSet);
+				Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
+				Assert.AreEqual (0, dataSet.Tables[0].PrimaryKey.Length, "#2");
+
+				dataSet.Reset ();
+				dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
+				dataAdapter.Fill (dataSet);
+				Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#3");
+				Assert.AreEqual (1, dataSet.Tables[0].PrimaryKey.Length, "#4");
+			}
+		}
+
+		[Test]
+		public void FillSchema_CheckSchema ()
+		{
+			SqlConnection conn = new SqlConnection(connectionString);
+			using (conn) {
+				conn.Open();
+
+				IDbCommand command = conn.CreateCommand();
+
+				// Create Temp Table
+				String cmd = "Create Table #tmp_TestTable (" ;
+				cmd += "id int primary key,";
+				cmd += "field int not null)";
+				command.CommandText = cmd; 
+				command.ExecuteNonQuery();
+
+				DataSet dataSet = new DataSet();
+				string selectString = "SELECT * from #tmp_TestTable";
+				IDbDataAdapter dataAdapter = new SqlDataAdapter (
+									selectString,conn);
+
+				dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
+				Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
+
+				dataSet.Reset ();
+				dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
+				dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
+				Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
+
+				dataSet.Reset ();
+				dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
+				dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
+				Assert.AreEqual (0, dataSet.Tables.Count, "#3");
+
+				dataSet.Reset ();
+				dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
+				try {
+					dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
+					Assert.Fail ("#4 Error should be thrown");
+				} catch (InvalidOperationException e) {
+				}
+			}
+		}
 	}
 }