Ver código fonte

2006-02-22 Chris Toshok <[email protected]>

	* ParameterCollection.cs: add an EditorAttribute to the class.

	* Parameter.cs: cleanup, fix the Value property, and implement the
	Size property.

	* SqlDataSourceView.cs: clean things up a bit, and add handling
	for parameters.


svn path=/trunk/mcs/; revision=57191
Chris Toshok 20 anos atrás
pai
commit
885cbe5bda

+ 10 - 0
mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog

@@ -1,3 +1,13 @@
+2006-02-22  Chris Toshok  <[email protected]>
+
+	* ParameterCollection.cs: add an EditorAttribute to the class.
+
+	* Parameter.cs: cleanup, fix the Value property, and implement the
+	Size property.
+
+	* SqlDataSourceView.cs: clean things up a bit, and add handling
+	for parameters.
+
 2006-02-22  Chris Toshok  <[email protected]>
 
 	* SqlDataSourceFilteringEventArgs.cs: formatting.

+ 31 - 35
mcs/class/System.Web/System.Web.UI.WebControls/Parameter.cs

@@ -123,15 +123,16 @@ namespace System.Web.UI.WebControls {
 		
 		public override string ToString ()
 		{
-			return Name;
+			object o = GetValue (HttpContext.Current, null);
+			if (o != null) return o.ToString();
+			return "";
 		}
 		
-		[WebCategoryAttribute ("Parameter"), DefaultValueAttribute (""),
-		WebSysDescriptionAttribute ("Default value to be used in case value is null.") ]
+		[WebCategoryAttribute ("Parameter")]
+		[DefaultValueAttribute (null)]
+		[WebSysDescriptionAttribute ("Default value to be used in case value is null.")]
 		public string DefaultValue {
-			get {
-				return ViewState ["DefaultValue"] as string;
-			}
+			get { return ViewState.GetString ("DefaultValue", null); }
 			set {
 				
 				if (DefaultValue != value) {
@@ -141,17 +142,12 @@ namespace System.Web.UI.WebControls {
 			}
 		}
 
-		[WebCategoryAttribute ("Parameter"), DefaultValueAttribute ("Input"),
-		WebSysDescriptionAttribute ("Parameter's direction.")]
+		[WebCategoryAttribute ("Parameter")]
+		[DefaultValueAttribute ("Input")]
+		[WebSysDescriptionAttribute ("Parameter's direction.")]
 		public ParameterDirection Direction
 		{
-			get {
-				object o = ViewState ["Direction"];
-				if (o != null)
-					return (ParameterDirection) o;
-				
-				return ParameterDirection.Input;
-			}
+			get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
 			set {				
 				if (Direction != value) {
 					ViewState ["Direction"] = value;
@@ -161,8 +157,9 @@ namespace System.Web.UI.WebControls {
 		}
 
 
-		[WebCategoryAttribute ("Parameter"), DefaultValueAttribute (""),
-		WebSysDescriptionAttribute ("Parameter's name.")]
+		[WebCategoryAttribute ("Parameter")]
+		[DefaultValueAttribute ("")]
+		[WebSysDescriptionAttribute ("Parameter's name.")]
 		public string Name
 		{
 			get {
@@ -181,17 +178,12 @@ namespace System.Web.UI.WebControls {
 			}
 		}
 
-		[WebCategoryAttribute ("Parameter"), DefaultValueAttribute (true),
-	        WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
+		[WebCategoryAttribute ("Parameter")]
+		[DefaultValueAttribute (true)]
+	        [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
 		public bool ConvertEmptyStringToNull
 		{
-			get {
-				object o = ViewState["ConvertEmptyStringToNull"];
-				if (o != null)
-					return (bool) o;
-				
-				return true;
-			}
+			get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
 			set {
 				if (ConvertEmptyStringToNull != value) {
 					ViewState["ConvertEmptyStringToNull"] = value;
@@ -200,18 +192,23 @@ namespace System.Web.UI.WebControls {
 			}
 		}
 
-	    [DefaultValueAttribute (TypeCode.Object)]
+		[DefaultValue (0)]
+		public int Size {
+			get { return ViewState.GetInt ("Size", 0); }
+			set {
+				if (Size != value) {
+					ViewState["Size"] = value;
+					OnParameterChanged ();
+				}
+			}
+		}
+
+		[DefaultValueAttribute (TypeCode.Empty)]
 		[WebCategoryAttribute ("Parameter"), 
 		WebSysDescriptionAttribute("Represents type of the parameter.")]
 		public TypeCode Type
 		{
-			get {
-				object o = ViewState ["Type"];
-				if (o != null)
-					return (TypeCode) o;
-				
-				return TypeCode.Object;
-			}
+			get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
 			set {
 				
 				if (Type != value) {
@@ -222,7 +219,6 @@ namespace System.Web.UI.WebControls {
 		}
 		
 		StateBag viewState;
-		
 		[BrowsableAttribute (false), 
 		DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
 		protected StateBag ViewState {

+ 2 - 0
mcs/class/System.Web/System.Web.UI.WebControls/ParameterCollection.cs

@@ -31,10 +31,12 @@
 using System.Web.UI;
 using System.Collections;
 using System.Collections.Specialized;
+using System.ComponentModel;
 
 namespace System.Web.UI.WebControls
 {
 
+	[EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
 	public class ParameterCollection : StateManagedCollection
 	{
 

+ 65 - 21
mcs/class/System.Web/System.Web.UI.WebControls/SqlDataSourceView.cs

@@ -55,9 +55,9 @@ namespace System.Web.UI.WebControls {
 
 		void InitConnection ()
 		{
-			if (connection == null) {
-				factory = owner.GetDbProviderFactoryInternal ();
-				connection = factory.CreateConnection ();
+                        if (factory == null) factory = owner.GetDbProviderFactoryInternal ();
+                        if (connection == null) {
+				connection = factory.CreateConnection();
 				connection.ConnectionString = owner.ConnectionString;
 			}
 		}
@@ -80,10 +80,17 @@ namespace System.Web.UI.WebControls {
 			DbCommand command = factory.CreateCommand ();
 			command.CommandText = DeleteCommand;
 			command.Connection = connection;
+			command.CommandType = CommandType.Text;
+
+                        if (DeleteParameters.Count > 0)
+                                InitializeParameters (command, DeleteParameters, null);
 
 			OnDeleting (new SqlDataSourceCommandEventArgs (command));
 
-			connection.Open ();
+			bool closed = connection.State == ConnectionState.Closed;
+
+			if (closed)
+				connection.Open();
 			Exception exception = null; 
 			int result = -1;;
 			try {
@@ -92,6 +99,9 @@ namespace System.Web.UI.WebControls {
 				exception = e;
 			}
 
+			if (closed)
+				connection.Close ();
+
 			OnDeleted (new SqlDataSourceStatusEventArgs (command, result, exception));
 
 			if (exception != null)
@@ -115,18 +125,28 @@ namespace System.Web.UI.WebControls {
 			DbCommand command = factory.CreateCommand ();
 			command.CommandText = InsertCommand;
 			command.Connection = connection;
+			command.CommandType = CommandType.Text;
+
+                        if (InsertParameters.Count > 0)
+                                InitializeParameters (command, InsertParameters, null);
 
 			OnInserting (new SqlDataSourceCommandEventArgs (command));
 
-			connection.Open();
+			bool closed = connection.State == ConnectionState.Closed;
+
+			if (closed)
+				connection.Open();
 			Exception exception = null;
 			int result = -1;
 			try {
 				result = command.ExecuteNonQuery();
-			}catch (Exception e) {
+			} catch (Exception e) {
 				exception = e;
 			}
 
+			if (closed)
+				connection.Close ();
+
 			OnInserted (new SqlDataSourceStatusEventArgs (command, result, exception));
 
 			if (exception != null)
@@ -139,40 +159,39 @@ namespace System.Web.UI.WebControls {
 			return ExecuteSelect (arguments);
 		}
 
-		[MonoTODO ("Handle @arguments, do replacement of command parameters")]
+		[MonoTODO ("Handle @arguments")]
 		protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
 		{
-			DbProviderFactory f = owner.GetDbProviderFactoryInternal ();
-			DbConnection c = f.CreateConnection ();
-
-			c.ConnectionString = owner.ConnectionString;
-
-			DbCommand command = f.CreateCommand ();
+			InitConnection ();
 
+			DbCommand command = factory.CreateCommand ();
 			command.CommandText = SelectCommand;
-			command.Connection = c;
+			command.Connection = connection;
 			command.CommandType = CommandType.Text;
 
-			/* XXX do replacement of command parameters */
+                        if (SelectParameters.Count > 0)
+                                InitializeParameters (command, SelectParameters, null);
 
 			OnSelecting (new SqlDataSourceSelectingEventArgs (command, arguments));
 
 			if (owner.DataSourceMode == SqlDataSourceMode.DataSet) {
-				DbDataAdapter adapter = f.CreateDataAdapter ();
+				DbDataAdapter adapter = factory.CreateDataAdapter ();
 
 				adapter.SelectCommand = command;
 
 				DataSet dataset = new DataSet ();
 
-				/* XXX MS calls Fill (DataSet dataset, string srcTable) - find out what the source table is */
 				adapter.Fill (dataset, name);
 
-				dataset.WriteXml (Console.Out);
-
 				return dataset.CreateDataReader ();
 			}
 			else {
-				throw new NotImplementedException ();
+				bool closed = connection.State == ConnectionState.Closed;
+
+                                if (closed)
+                                        connection.Open ();
+
+                                return command.ExecuteReader (closed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
 			}
 		}
 
@@ -194,10 +213,17 @@ namespace System.Web.UI.WebControls {
 			DbCommand command = factory.CreateCommand ();
 			command.CommandText = UpdateCommand;
 			command.Connection = connection;
+			command.CommandType = CommandType.Text;
+
+                        if (UpdateParameters.Count > 0)
+                                InitializeParameters (command, UpdateParameters, null);
 
 			OnUpdating (new SqlDataSourceCommandEventArgs (command));
 
-			connection.Open ();
+			bool closed = connection.State == ConnectionState.Closed;
+
+			if (closed)
+				connection.Open();
 			Exception exception = null;
 			int result = -1;
 			try {
@@ -206,6 +232,9 @@ namespace System.Web.UI.WebControls {
 				exception = e;
 			}
 
+			if (closed)
+				connection.Close ();
+
 			OnUpdated (new SqlDataSourceStatusEventArgs (command, result, exception));
 
 			if (exception != null)
@@ -213,6 +242,21 @@ namespace System.Web.UI.WebControls {
 			return result;
 		}
 
+                void InitializeParameters (DbCommand command, ParameterCollection parameters, IDictionary exclusionList)
+                {
+                        IOrderedDictionary values = parameters.GetValues (HttpContext.Current, owner);
+
+                        foreach (Parameter p in parameters) {
+                                DbParameter dbp = factory.CreateParameter ();
+
+                                dbp.ParameterName = p.Name;
+                                dbp.Value = values[p.Name];
+                                dbp.Direction = p.Direction;
+                                dbp.Size = p.Size;
+                                command.Parameters.Add (dbp);
+                        }
+                }
+
 		void IStateManager.LoadViewState (object savedState)
 		{
 			LoadViewState (savedState);