Browse Source

2009-07-25 Veerapuram Varadhan <[email protected]>

	* Fixes #507663
	* SqlCommandBuilder.cs: From 2.0 onwards, use base's
	implementation for handling auto-generated commands.
	* SqlDataAdapter.cs: -- ditto --

svn path=/trunk/mcs/; revision=139061
Veerapuram Varadhan 16 years ago
parent
commit
cee0485905

+ 7 - 0
mcs/class/System.Data/System.Data.SqlClient/ChangeLog

@@ -7,6 +7,13 @@
 
 	* SqlParameter.cs: Improved support for SqlValue and SqlTypes.
 
+2009-07-25  Veerapuram Varadhan  <[email protected]>
+
+	* Fixes #507663
+	* SqlCommandBuilder.cs: From 2.0 onwards, use base's 
+	implementation for handling auto-generated commands.
+	* SqlDataAdapter.cs: -- ditto --
+	
 2009-07-15  Veerapuram Varadhan  <[email protected]>
 
 	* SqlParameter.cs: Implement the "internal" FrameworkDbType, 

+ 71 - 123
mcs/class/System.Data/System.Data.SqlClient/SqlCommandBuilder.cs

@@ -53,20 +53,23 @@ namespace System.Data.SqlClient
 		bool disposed;
 
 		DataTable dbSchemaTable;
-		SqlDataAdapter adapter;
+#if ONLY_1_1
 		string quotePrefix;
 		string quoteSuffix;
+#endif
 		string tableName;
 #if NET_2_0
 		readonly string _catalogSeparator = ".";
 		readonly string _schemaSeparator = ".";
 		readonly CatalogLocation _catalogLocation = CatalogLocation.Start;
 #endif
-	
-		SqlCommand deleteCommand;
+#if ONLY_1_1
+		SqlDataAdapter adapter;
 		SqlCommand insertCommand;
+		SqlCommand deleteCommand;
 		SqlCommand updateCommand;
-
+#endif
+	
 		// Used to construct WHERE clauses
 		static readonly string clause1 = "({0} = 1 AND {1} IS NULL)";
 		static readonly string clause2 = "({0} = {1})";
@@ -80,8 +83,8 @@ namespace System.Data.SqlClient
 		public SqlCommandBuilder ()
 		{
 #if NET_2_0
-			quoteSuffix = "]";
-			quotePrefix = "[";
+			QuoteSuffix = "]";
+			QuotePrefix = "[";
 #endif
 		}
 
@@ -100,14 +103,23 @@ namespace System.Data.SqlClient
 #endif
 		[DefaultValue (null)]
 		public new SqlDataAdapter DataAdapter {
-			get { return adapter; }
-			set { 
-				if (adapter != null)
-					adapter.RowUpdating -= new SqlRowUpdatingEventHandler (RowUpdatingHandler);
+			get { 
+#if ONLY_1_1
+				return adapter;
+#else
+				return (SqlDataAdapter)base.DataAdapter;
+#endif
+			} set {
+#if ONLY_1_1
+               if (adapter != null)
+                       adapter.RowUpdating -= new SqlRowUpdatingEventHandler (RowUpdatingHandler);
 
-				adapter = value; 
-				if (adapter != null)
-					adapter.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
+               adapter = value; 
+               if (adapter != null)
+                       adapter.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
+#else
+				base.DataAdapter = value;
+#endif
 			}
 		}
 
@@ -131,23 +143,27 @@ namespace System.Data.SqlClient
 #if ONLY_1_1
 				if (quotePrefix == null)
 					return string.Empty;
-#endif
 				return quotePrefix;
+#else
+				return base.QuotePrefix;
+#endif
 			}
 			set {
+#if ONLY_1_1
 				if (dbSchemaTable != null)
 					throw new InvalidOperationException (
 						"The QuotePrefix and QuoteSuffix " +
 						"properties cannot be changed once " +
 						"an Insert, Update, or Delete " +
 						"command has been generated.");
-#if NET_2_0
+				quotePrefix = value;
+#else
 				if (value != "[" && value != "\"")
 					throw new ArgumentException ("Only '[' " +
 						"and '\"' are allowed as value " +
 						"for the 'QuoteSuffix' property.");
+				base.QuotePrefix = value;
 #endif
-				quotePrefix = value;
 			}
 		}
 
@@ -167,23 +183,27 @@ namespace System.Data.SqlClient
 #if ONLY_1_1
 				if (quoteSuffix == null)
 					return string.Empty;
-#endif
 				return quoteSuffix;
+#else
+				return base.QuoteSuffix;
+#endif
 			}
 			set {
+#if ONLY_1_1
 				if (dbSchemaTable != null)
 					throw new InvalidOperationException (
 						"The QuotePrefix and QuoteSuffix " +
 						"properties cannot be changed once " +
 						"an Insert, Update, or Delete " +
 						"command has been generated.");
-#if NET_2_0
+				quoteSuffix = value;
+#else
 				if (value != "]" && value != "\"")
 					throw new ArgumentException ("Only ']' " +
 						"and '\"' are allowed as value " +
 						"for the 'QuoteSuffix' property.");
+				base.QuoteSuffix = value;
 #endif
-				quoteSuffix = value;
 			}
 		}
 
@@ -241,6 +261,7 @@ namespace System.Data.SqlClient
 
 #endif // NET_2_0
 
+#if ONLY_1_1
 		private SqlCommand SourceCommand {
 			get {
 				if (adapter != null)
@@ -248,11 +269,13 @@ namespace System.Data.SqlClient
 				return null;
 			}
 		}
+#endif
 
 		#endregion // Properties
 
 		#region Methods
 
+#if ONLY_1_1
 		private void BuildCache (bool closeConnection)
 		{
 			SqlCommand sourceCommand = SourceCommand;
@@ -303,13 +326,7 @@ namespace System.Data.SqlClient
 
 			CreateNewCommand (ref deleteCommand);
 
-			string command = String.Format (
-#if NET_2_0
-				"DELETE FROM {0}",
-#else
-				"DELETE FROM  {0}",
-#endif
-				QuotedTableName);
+			string command = String.Format ("DELETE FROM  {0}", QuotedTableName);
 			StringBuilder whereClause = new StringBuilder ();
 			bool keyFound = false;
 			int parmIndex = 1;
@@ -330,11 +347,7 @@ namespace System.Data.SqlClient
 					keyFound = true;
 
 				bool allowNull = (bool) schemaRow ["AllowDBNull"];
-#if NET_2_0
-				if (!isKey && allowNull) {
-#else
 				if (!isKey) {
-#endif
 					string sourceColumnName = (string) schemaRow ["BaseColumnName"];
 					if (useColumnsForParameterNames) {
 						parameter = deleteCommand.Parameters.Add (
@@ -345,21 +358,9 @@ namespace System.Data.SqlClient
 							GetParameterName (parmIndex++),
 							SqlDbType.Int);
 					}
-#if ONLY_1_1
 					parameter.IsNullable = allowNull;
-#endif
-#if NET_2_0
-					parameter.SourceColumn = sourceColumnName;
-					parameter.SourceColumnNullMapping = true;
-					parameter.SourceVersion = DataRowVersion.Original;
-#else
 					parameter.SourceVersion = DataRowVersion.Current;
-#endif
-#if NET_2_0
-					parameter.SqlValue = new SqlInt32 (1);
-#else
 					parameter.Value = 1;
-#endif
 
 					whereClause.Append ("(");
 					whereClause.Append (String.Format (clause1, parameter.ParameterName,
@@ -373,32 +374,19 @@ namespace System.Data.SqlClient
 					parameter = CreateParameter (parmIndex++, schemaRow);
 				deleteCommand.Parameters.Add (parameter);
 				ApplyParameterInfo (parameter, schemaRow, StatementType.Delete, true);
-#if ONLY_1_1
 				parameter.IsNullable = allowNull;
-#endif
 				parameter.SourceVersion = DataRowVersion.Original;
 
 				whereClause.Append (String.Format (clause2, GetQuotedString (parameter.SourceColumn), parameter.ParameterName));
 
-#if NET_2_0
-				if (!isKey && allowNull)
-#else
 				if (!isKey)
-#endif
 					whereClause.Append (")");
 			}
 			if (!keyFound)
 				throw new InvalidOperationException ("Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.");
 
 			// We're all done, so bring it on home
-			string sql = String.Format (
-#if NET_2_0
-				"{0} WHERE ({1})",
-#else
-				"{0} WHERE ( {1} )",
-#endif
-				command,
-				whereClause.ToString ());
+			string sql = String.Format ("{0} WHERE ( {1} )", command, whereClause.ToString ());
 			deleteCommand.CommandText = sql;
 			return deleteCommand;
 		}
@@ -421,13 +409,8 @@ namespace System.Data.SqlClient
 					continue;
 
 				if (parmIndex > 1) {
-#if NET_2_0
-					columns.Append (", ");
-					values.Append (", ");
-#else
 					columns.Append (" , ");
 					values.Append (" , ");
-#endif
 				}
 
 				SqlParameter parameter = null;
@@ -440,9 +423,7 @@ namespace System.Data.SqlClient
 				insertCommand.Parameters.Add (parameter);
 				ApplyParameterInfo (parameter, schemaRow, StatementType.Insert, false);
 				parameter.SourceVersion = DataRowVersion.Current;
-#if ONLY_1_1
 				parameter.IsNullable = (bool) schemaRow ["AllowDBNull"];
-#endif
 
 				columns.Append (GetQuotedString (parameter.SourceColumn));
 				values.Append (parameter.ParameterName);
@@ -450,15 +431,7 @@ namespace System.Data.SqlClient
 				parmIndex++;
 			}
 
-			sql = String.Format (
-#if NET_2_0
-				"{0} ({1}) VALUES ({2})",
-#else
-				"{0}( {1} ) VALUES ( {2} )",
-#endif
-				command,
-				columns.ToString (),
-				values.ToString ());
+			sql = String.Format ("{0}( {1} ) VALUES ( {2} )", command, columns.ToString (), values.ToString ());
 			insertCommand.CommandText = sql;
 			return insertCommand;
 		}
@@ -495,11 +468,7 @@ namespace System.Data.SqlClient
 				if (!IncludedInUpdate (schemaRow))
 					continue;
 				if (columns.Length > 0)
-#if NET_2_0
-					columns.Append (", ");
-#else
 					columns.Append (" , ");
-#endif
 
 				SqlParameter parameter = null;
 				if (useColumnsForParameterNames) {
@@ -509,9 +478,7 @@ namespace System.Data.SqlClient
 				}
 				updateCommand.Parameters.Add (parameter);
 				ApplyParameterInfo (parameter, schemaRow, StatementType.Update, false);
-#if ONLY_1_1
 				parameter.IsNullable = (bool) schemaRow ["AllowDBNull"];
-#endif
 				parameter.SourceVersion = DataRowVersion.Current;
 
 				columns.Append (String.Format ("{0} = {1}", GetQuotedString (parameter.SourceColumn), parameter.ParameterName));
@@ -536,11 +503,7 @@ namespace System.Data.SqlClient
 					keyFound = true;
 
 				bool allowNull = (bool) schemaRow ["AllowDBNull"];
-#if NET_2_0
-				if (!isKey && allowNull) {
-#else
 				if (!isKey) {
-#endif
 					string sourceColumnName = (string) schemaRow ["BaseColumnName"];
 					if (useColumnsForParameterNames) {
 						parameter = updateCommand.Parameters.Add (
@@ -551,25 +514,15 @@ namespace System.Data.SqlClient
 							GetParameterName (parmIndex++),
 							SqlDbType.Int);
 					}
-#if ONLY_1_1
 					parameter.IsNullable = allowNull;
-#endif
-#if NET_2_0
-					parameter.SourceColumn = sourceColumnName;
-					parameter.SourceColumnNullMapping = true;
-					parameter.SourceVersion = DataRowVersion.Original;
-#else
 					parameter.SourceVersion = DataRowVersion.Current;
-#endif
-#if NET_2_0
-					parameter.SqlValue = new SqlInt32 (1);
-#else
 					parameter.Value = 1;
-#endif
+					
 					whereClause.Append ("(");
 					whereClause.Append (String.Format (clause1, parameter.ParameterName,
 									GetQuotedString (sourceColumnName)));
 					whereClause.Append (" OR ");
+					
 				}
 
 				if (useColumnsForParameterNames) {
@@ -579,33 +532,19 @@ namespace System.Data.SqlClient
 				}
 				updateCommand.Parameters.Add (parameter);
 				ApplyParameterInfo (parameter, schemaRow, StatementType.Update, true);
-#if ONLY_1_1
 				parameter.IsNullable = allowNull;
-#endif
 				parameter.SourceVersion = DataRowVersion.Original;
 
 				whereClause.Append (String.Format (clause2, GetQuotedString (parameter.SourceColumn), parameter.ParameterName));
 
-#if NET_2_0
-				if (!isKey && allowNull)
-#else
 				if (!isKey)
-#endif
 					whereClause.Append (")");
 			}
 			if (!keyFound)
 				throw new InvalidOperationException ("Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.");
 
 			// We're all done, so bring it on home
-			string sql = String.Format (
-#if NET_2_0
-				"{0}{1} WHERE ({2})",
-#else
-				"{0}{1} WHERE ( {2} )",
-#endif
-				command,
-				columns.ToString (),
-				whereClause.ToString ());
+			string sql = String.Format ("{0}{1} WHERE ( {2} )", command, columns.ToString (), whereClause.ToString ());
 			updateCommand.CommandText = sql;
 			return updateCommand;
 		}
@@ -635,7 +574,8 @@ namespace System.Data.SqlClient
 			param.SourceColumn = sourceColumn;
 			return param;
 		}
-
+#endif // ONLY_1_1
+				
 		public static void DeriveParameters (SqlCommand command)
 		{
 			command.DeriveParameters ();
@@ -650,12 +590,14 @@ namespace System.Data.SqlClient
 		{
 			if (!disposed) {
 				if (disposing) {
+#if ONLY_1_1
 					if (insertCommand != null)
 						insertCommand.Dispose ();
 					if (deleteCommand != null)
 						deleteCommand.Dispose ();
 					if (updateCommand != null)
 						updateCommand.Dispose ();
+#endif
 					if (dbSchemaTable != null)
 						dbSchemaTable.Dispose ();
 				}
@@ -669,10 +611,14 @@ namespace System.Data.SqlClient
 #endif // NET_2_0
 		SqlCommand GetDeleteCommand ()
 		{
+#if NET_2_0 
+			return (SqlCommand) base.GetDeleteCommand (false);
+#else
 			BuildCache (true);
 			if (deleteCommand == null)
 				return CreateDeleteCommand (false);
 			return deleteCommand;
+#endif
 		}
 
 		public
@@ -681,10 +627,14 @@ namespace System.Data.SqlClient
 #endif // NET_2_0
 		SqlCommand GetInsertCommand ()
 		{
+#if NET_2_0
+			return (SqlCommand) base.GetInsertCommand (false);
+#else
 			BuildCache (true);
 			if (insertCommand == null)
 				return CreateInsertCommand (false);
 			return insertCommand;
+#endif
 		}
 
 		private string GetQuotedString (string value)
@@ -706,35 +656,30 @@ namespace System.Data.SqlClient
 #endif // NET_2_0
 		SqlCommand GetUpdateCommand ()
 		{
+#if NET_2_0
+			return (SqlCommand) base.GetUpdateCommand (false);
+#else
 			BuildCache (true);
 			if (updateCommand == null)
 				return CreateUpdateCommand (false);
 			return updateCommand;
+#endif
 		}
 
 #if NET_2_0
 		public new SqlCommand GetUpdateCommand (bool useColumnsForParameterNames)
 		{
-			BuildCache (true);
-			if (updateCommand == null || useColumnsForParameterNames)
-				return CreateUpdateCommand (useColumnsForParameterNames);
-			return updateCommand;
+			return (SqlCommand) base.GetUpdateCommand (useColumnsForParameterNames);
 		}
 
 		public new SqlCommand GetDeleteCommand (bool useColumnsForParameterNames)
 		{
-			BuildCache (true);
-			if (deleteCommand == null || useColumnsForParameterNames)
-				return CreateDeleteCommand (useColumnsForParameterNames);
-			return deleteCommand;
+			return (SqlCommand) base.GetDeleteCommand (useColumnsForParameterNames);
 		}
 
 		public new SqlCommand GetInsertCommand (bool useColumnsForParameterNames)
 		{
-			BuildCache (true);
-			if (insertCommand == null || useColumnsForParameterNames)
-				return CreateInsertCommand (useColumnsForParameterNames);
-			return insertCommand;
+			return (SqlCommand) base.GetInsertCommand (useColumnsForParameterNames);
 		}
 		
 		public override string QuoteIdentifier (string unquotedIdentifier)
@@ -813,9 +758,12 @@ namespace System.Data.SqlClient
 			// FIXME: "Figure out what else needs to be cleaned up when we refresh."
 			tableName = String.Empty;
 			dbSchemaTable = null;
+			base.RefreshSchema ();
+#if ONLY_1_1
 			deleteCommand = null;
 			insertCommand = null;
 			updateCommand = null;
+#endif
 		}
 
 #if NET_2_0
@@ -915,7 +863,7 @@ namespace System.Data.SqlClient
 
 		protected override DataTable GetSchemaTable (DbCommand srcCommand)
 		{
-			using (SqlDataReader rdr = (SqlDataReader) srcCommand.ExecuteReader ())
+			using (SqlDataReader rdr = (SqlDataReader) srcCommand.ExecuteReader (CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
 				return rdr.GetSchemaTable ();
 		}
 

+ 29 - 37
mcs/class/System.Data/System.Data.SqlClient/SqlDataAdapter.cs

@@ -40,17 +40,13 @@ namespace System.Data.SqlClient {
 	[DefaultEvent ("RowUpdated")]
 	[DesignerAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
 	[ToolboxItemAttribute ("Microsoft.VSDesigner.Data.VS.SqlDataAdapterToolboxItem, "+ Consts.AssemblyMicrosoft_VSDesigner)]
-	public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter, ICloneable
+	public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
 	{
 		#region Fields
 
 #if !NET_2_0
 		bool disposed;
 #endif
-		SqlCommand deleteCommand;
-		SqlCommand insertCommand;
-		SqlCommand selectCommand;
-		SqlCommand updateCommand;
 #if NET_2_0
 		int updateBatchSize;
 #endif
@@ -89,9 +85,13 @@ namespace System.Data.SqlClient {
 #endif
 		[DefaultValue (null)]
 		[EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
-		public new SqlCommand DeleteCommand {
-			get { return deleteCommand; }
-			set { deleteCommand = value; }
+		public
+#if ONLY_1_1
+		new 
+#endif 
+		SqlCommand DeleteCommand {
+			get { return (SqlCommand)base.DeleteCommand; }
+			set { base.DeleteCommand = value; }
 		}
 
 #if !NET_2_0
@@ -99,9 +99,13 @@ namespace System.Data.SqlClient {
 #endif
 		[DefaultValue (null)]
 		[EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
-		public new SqlCommand InsertCommand {
-			get { return insertCommand; }
-			set { insertCommand = value; }
+		public
+#if ONLY_1_1
+		new 
+#endif 
+		SqlCommand InsertCommand {
+			get { return (SqlCommand)base.InsertCommand; }
+			set { base.InsertCommand = value; }
 		}
 
 #if !NET_2_0
@@ -109,9 +113,13 @@ namespace System.Data.SqlClient {
 #endif
 		[DefaultValue (null)]
 		[EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
-		public new SqlCommand SelectCommand {
-			get { return selectCommand; }
-			set { selectCommand = value; }
+		public
+#if ONLY_1_1
+		new 
+#endif 
+		SqlCommand SelectCommand {
+			get { return (SqlCommand)base.SelectCommand; }
+			set { base.SelectCommand = value; }
 		}
 
 #if !NET_2_0
@@ -119,29 +127,13 @@ namespace System.Data.SqlClient {
 #endif
 		[DefaultValue (null)]
 		[EditorAttribute ("Microsoft.VSDesigner.Data.Design.DBCommandEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
-		public new SqlCommand UpdateCommand {
-			get { return updateCommand; }
-			set { updateCommand = value; }
-		}
-
-		IDbCommand IDbDataAdapter.DeleteCommand {
-			get { return DeleteCommand; }
-			set { DeleteCommand = (SqlCommand) value; }
-		}
-
-		IDbCommand IDbDataAdapter.InsertCommand {
-			get { return InsertCommand; }
-			set { InsertCommand = (SqlCommand) value; }
-		}
-
-		IDbCommand IDbDataAdapter.SelectCommand {
-			get { return SelectCommand; }
-			set { SelectCommand = (SqlCommand) value; }
-		}
-
-		IDbCommand IDbDataAdapter.UpdateCommand {
-			get { return UpdateCommand; }
-			set { UpdateCommand = (SqlCommand) value; }
+		public
+#if ONLY_1_1
+		new 
+#endif 
+		SqlCommand UpdateCommand {
+			get { return (SqlCommand)base.UpdateCommand; }
+			set { base.UpdateCommand = value; }
 		}
 
 #if NET_2_0