List:Commits« Previous MessageNext Message »
From:rburnett Date:October 31 2006 8:02pm
Subject:Connector/NET commit: r447 - in trunk: . mysqlclient
View as plain text  
Modified:
   trunk/CHANGES
   trunk/mysqlclient/Resources.Designer.cs
   trunk/mysqlclient/Resources.resx
   trunk/mysqlclient/parameter_collection.cs
Log:
The code now issues a nicer exception if the user has added a parameter without the parameter marker included.

Modified: trunk/CHANGES
===================================================================
--- trunk/CHANGES	2006-10-31 20:01:59 UTC (rev 446)
+++ trunk/CHANGES	2006-10-31 20:02:14 UTC (rev 447)
@@ -21,6 +21,7 @@
          statements application-wide
     Added Installer class to provide custom install type procedures such as modifying
          machine.config
+    A nicer exception is displayed if you have added a parameter without the parameter marker.
          
 Version 5.0.1 (Beta)
 

Modified: trunk/mysqlclient/Resources.Designer.cs
===================================================================
--- trunk/mysqlclient/Resources.Designer.cs	2006-10-31 20:01:59 UTC (rev 446)
+++ trunk/mysqlclient/Resources.Designer.cs	2006-10-31 20:02:14 UTC (rev 447)
@@ -590,5 +590,14 @@
                 return ResourceManager.GetString("WriteToStreamFailed", resourceCulture);
             }
         }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Parameter &apos;{0}&apos; is not found but a parameter with the name &apos;{1}&apos; is found. Parameter names must include the leading parameter marker..
+        /// </summary>
+        internal static string WrongParameterName {
+            get {
+                return ResourceManager.GetString("WrongParameterName", resourceCulture);
+            }
+        }
     }
 }

Modified: trunk/mysqlclient/Resources.resx
===================================================================
--- trunk/mysqlclient/Resources.resx	2006-10-31 20:01:59 UTC (rev 446)
+++ trunk/mysqlclient/Resources.resx	2006-10-31 20:02:14 UTC (rev 447)
@@ -294,4 +294,7 @@
   <data name="PerfMonSoftProcName" xml:space="preserve">
     <value>Soft Procedure Queries</value>
   </data>
+  <data name="WrongParameterName" xml:space="preserve">
+    <value>Parameter '{0}' is not found but a parameter with the name '{1}' is found. Parameter names must include the leading parameter marker.</value>
+  </data>
 </root>
\ No newline at end of file

Modified: trunk/mysqlclient/parameter_collection.cs
===================================================================
--- trunk/mysqlclient/parameter_collection.cs	2006-10-31 20:01:59 UTC (rev 446)
+++ trunk/mysqlclient/parameter_collection.cs	2006-10-31 20:02:14 UTC (rev 447)
@@ -52,23 +52,14 @@
 #endif
 		}
 
-		internal char ParameterMarker 
+		internal char ParameterMarker
 		{
 			get { return paramMarker; }
 			set { paramMarker = value; }
 		}
 
-		private int InternalIndexOf(string name)
-		{
-			int index = IndexOf(name);
-			if (index != -1) return index;
-			throw new MySqlException("A MySqlParameter with ParameterName '" + name + 
-				"' is not contained by this MySqlParameterCollection.");
-		}
+		#region Public Methods
 
-
-#region Public Methods
-
 		/// <summary>
 		/// Gets the <see cref="MySqlParameter"/> at the specified index.
 		/// </summary>
@@ -97,22 +88,22 @@
 		/// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
 		public MySqlParameter Add(MySqlParameter value)
 		{
-			if (value == null) 
+			if (value == null)
 				throw new ArgumentException("The MySqlParameterCollection only accepts non-null MySqlParameter type objects.", "value");
 
-            if (value.Direction == ParameterDirection.ReturnValue)
-                return AddReturnParameter(value);
+			if (value.Direction == ParameterDirection.ReturnValue)
+				return AddReturnParameter(value);
 
 			string inComingName = value.ParameterName.ToLower();
 			if (inComingName[0] == paramMarker)
-				inComingName = inComingName.Substring(1, inComingName.Length-1);
+				inComingName = inComingName.Substring(1, inComingName.Length - 1);
 
-			for (int i=0; i < items.Count; i++)
+			for (int i = 0; i < items.Count; i++)
 			{
 				MySqlParameter p = (MySqlParameter)items[i];
 				string name = p.ParameterName.ToLower();
 				if (name[0] == paramMarker)
-					name = name.Substring(1, name.Length-1);
+					name = name.Substring(1, name.Length - 1);
 				if (name == inComingName)
 				{
 					items[i] = value;
@@ -122,32 +113,32 @@
 
 			int index = items.Add(value);
 			hash.Add(value.ParameterName, index);
-			ciHash.Add(value.ParameterName, index); 
+			ciHash.Add(value.ParameterName, index);
 			return value;
 		}
 
-        private MySqlParameter AddReturnParameter(MySqlParameter value)
-        {
-            for (int i = 0; i < items.Count; i++)
-            {
-                MySqlParameter p = (MySqlParameter)items[i];
-                if (p.Direction != ParameterDirection.ReturnValue) continue;
-                items[i] = value;
-                return value;
-            }
-            items.Add(value);
-            return value;
-        }
+		private MySqlParameter AddReturnParameter(MySqlParameter value)
+		{
+			for (int i = 0; i < items.Count; i++)
+			{
+				MySqlParameter p = (MySqlParameter)items[i];
+				if (p.Direction != ParameterDirection.ReturnValue) continue;
+				items[i] = value;
+				return value;
+			}
+			items.Add(value);
+			return value;
+		}
 
-        /// <summary>
-        /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
-        /// </summary>
-        /// <param name="parameterName">The name of the parameter.</param>
-        /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
-        /// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
-        public MySqlParameter Add(string parameterName, object value)
+		/// <summary>
+		/// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
+		/// </summary>
+		/// <param name="parameterName">The name of the parameter.</param>
+		/// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
+		/// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
+		public MySqlParameter Add(string parameterName, object value)
 		{
-			return Add( new MySqlParameter( parameterName, value ) );
+			return Add(new MySqlParameter(parameterName, value));
 		}
 
 		/// <summary>
@@ -170,7 +161,7 @@
 		/// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
 		public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size)
 		{
-			return Add(new MySqlParameter(parameterName, dbType, size ));
+			return Add(new MySqlParameter(parameterName, dbType, size));
 		}
 
 		/// <summary>
@@ -186,15 +177,15 @@
 			return Add(new MySqlParameter(parameterName, dbType, size, sourceColumn));
 		}
 
-#endregion
+		#endregion
 
-#region DbParameterCollection Implementation
+		#region DbParameterCollection Implementation
 
-        public override void AddRange(Array values)
-        {
-            foreach (DbParameter p in values)
-                Add(p);
-        }
+		public override void AddRange(Array values)
+		{
+			foreach (DbParameter p in values)
+				Add(p);
+		}
 
 		void CheckIndex(int index)
 		{
@@ -202,194 +193,213 @@
 				throw new IndexOutOfRangeException("Parameter index is out of range.");
 		}
 
+		/// <summary>
+		/// Retrieve the parameter with the given name.
+		/// </summary>
+		/// <param name="parameterName"></param>
+		/// <returns></returns>
 		protected override DbParameter GetParameter(string parameterName)
-        {
+		{
 			int index = this.IndexOf(parameterName);
 			if (index < 0)
+			{
+				// check to see if the user has added the parameter without a
+				// parameter marker.  If so, kindly tell them what they did.
+				if (parameterName.StartsWith(ParameterMarker.ToString()))
+				{
+					string newParameterName = parameterName.Substring(1);
+					index = IndexOf(newParameterName);
+					if (index != -1)
+						throw new ArgumentException(
+							String.Format(Resources.WrongParameterName, parameterName,
+							newParameterName));
+				}
 				throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
-            return (DbParameter)items[index];
-        }
+			}
+			return (DbParameter)items[index];
+		}
 
-        protected override DbParameter GetParameter(int index)
-        {
+		protected override DbParameter GetParameter(int index)
+		{
 			CheckIndex(index);
-            return (DbParameter)items[index];
-        }
+			return (DbParameter)items[index];
+		}
 
-        protected override void SetParameter(string parameterName, DbParameter value)
-        {
+		protected override void SetParameter(string parameterName, DbParameter value)
+		{
 			int index = this.IndexOf(parameterName);
 			if (index < 0)
 				throw new ArgumentException("Parameter '" + parameterName + "' not found in the collection.");
 			//changed = true;
 			items[index] = (MySqlParameter)value;
-        }
+		}
 
-        protected override void SetParameter(int index, DbParameter value)
-        {
+		protected override void SetParameter(int index, DbParameter value)
+		{
 			CheckIndex(index);
 			//changed = true;
 			items[index] = (MySqlParameter)value;
-        }
+		}
 
-        /// <summary>
-        /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
-        /// </summary>
-        /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
-        /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
-        public override int Add(object value)
-        {
-            if (!(value is MySqlParameter))
-                throw new MySqlException("Only MySqlParameter objects may be stored");
+		/// <summary>
+		/// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
+		/// </summary>
+		/// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
+		/// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
+		public override int Add(object value)
+		{
+			if (!(value is MySqlParameter))
+				throw new MySqlException("Only MySqlParameter objects may be stored");
 
-            MySqlParameter p = (MySqlParameter)value;
+			MySqlParameter p = (MySqlParameter)value;
 
-            if (p.ParameterName == null || p.ParameterName == String.Empty)
-                throw new MySqlException("Parameters must be named");
+			if (p.ParameterName == null || p.ParameterName == String.Empty)
+				throw new MySqlException("Parameters must be named");
 
 			p = Add(p);
 			return IndexOf(p);
-        }
+		}
 
-        /// <summary>
-        /// Removes all items from the collection.
-        /// </summary>
-        public override void Clear()
-        {
-            items.Clear();
-            hash.Clear();
-            ciHash.Clear();
-        }
+		/// <summary>
+		/// Removes all items from the collection.
+		/// </summary>
+		public override void Clear()
+		{
+			items.Clear();
+			hash.Clear();
+			ciHash.Clear();
+		}
 
-        /// <summary>
-        /// Gets a value indicating whether a <see cref="MySqlParameter"/> with the specified parameter name exists in the collection.
-        /// </summary>
-        /// <param name="value">The name of the <see cref="MySqlParameter"/> object to find.</param>
-        /// <returns>true if the collection contains the parameter; otherwise, false.</returns>
-        public override bool Contains(string value)
-        {
-            return IndexOf(value) != -1;
-        }
+		/// <summary>
+		/// Gets a value indicating whether a <see cref="MySqlParameter"/> with the specified parameter name exists in the collection.
+		/// </summary>
+		/// <param name="value">The name of the <see cref="MySqlParameter"/> object to find.</param>
+		/// <returns>true if the collection contains the parameter; otherwise, false.</returns>
+		public override bool Contains(string value)
+		{
+			return IndexOf(value) != -1;
+		}
 
-        /// <summary>
-        /// Gets a value indicating whether a MySqlParameter exists in the collection.
-        /// </summary>
-        /// <param name="value">The value of the <see cref="MySqlParameter"/> object to find. </param>
-        /// <returns>true if the collection contains the <see cref="MySqlParameter"/> object; otherwise, false.</returns>
-        /// <overloads>Gets a value indicating whether a <see cref="MySqlParameter"/> exists in the collection.</overloads>
-        public override bool Contains(object value)
-        {
-            return items.Contains(value);
-        }
+		/// <summary>
+		/// Gets a value indicating whether a MySqlParameter exists in the collection.
+		/// </summary>
+		/// <param name="value">The value of the <see cref="MySqlParameter"/> object to find. </param>
+		/// <returns>true if the collection contains the <see cref="MySqlParameter"/> object; otherwise, false.</returns>
+		/// <overloads>Gets a value indicating whether a <see cref="MySqlParameter"/> exists in the collection.</overloads>
+		public override bool Contains(object value)
+		{
+			return items.Contains(value);
+		}
 
-        /// <summary>
-        /// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
-        /// </summary>
-        /// <param name="array"></param>
-        /// <param name="index"></param>
-        public override void CopyTo(Array array, int index)
-        {
-            items.CopyTo(array, index);
-        }
+		/// <summary>
+		/// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
+		/// </summary>
+		/// <param name="array"></param>
+		/// <param name="index"></param>
+		public override void CopyTo(Array array, int index)
+		{
+			items.CopyTo(array, index);
+		}
 
-        /// <summary>
-        /// Gets the number of MySqlParameter objects in the collection.
-        /// </summary>
-        public override int Count
-        {
-            get { return items.Count; }
-        }
+		/// <summary>
+		/// Gets the number of MySqlParameter objects in the collection.
+		/// </summary>
+		public override int Count
+		{
+			get { return items.Count; }
+		}
 
-        public override IEnumerator GetEnumerator()
-        {
-            return ((IEnumerable)items).GetEnumerator();
-        }
+		public override IEnumerator GetEnumerator()
+		{
+			return ((IEnumerable)items).GetEnumerator();
+		}
 
-        /// <summary>
-        /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
-        /// </summary>
-        /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
-        /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
-        public override int IndexOf(string parameterName)
-        {
-          object o = hash[parameterName];
-          if (o == null)
-            o = ciHash[parameterName];
-          if (o == null)
-            return -1;
-          return (int)o;
-        }
+		/// <summary>
+		/// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
+		/// </summary>
+		/// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
+		/// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
+		public override int IndexOf(string parameterName)
+		{
+			object o = hash[parameterName];
+			if (o == null)
+				o = ciHash[parameterName];
+			if (o == null)
+				return -1;
+			return (int)o;
+		}
 
-        /// <summary>
-        /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
-        /// </summary>
-        /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
-        /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
-        /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
-        public override int IndexOf(object value)
-        {
-            return items.IndexOf(value);
-        }
+		/// <summary>
+		/// Gets the location of a <see cref="MySqlParameter"/> in the collection.
+		/// </summary>
+		/// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
+		/// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
+		/// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
+		public override int IndexOf(object value)
+		{
+			return items.IndexOf(value);
+		}
 
-        /// <summary>
-        /// Inserts a MySqlParameter into the collection at the specified index.
-        /// </summary>
-        /// <param name="index"></param>
-        /// <param name="value"></param>
-        public override void Insert(int index, object value)
-        {
-            items.Insert(index, value);
-        }
+		/// <summary>
+		/// Inserts a MySqlParameter into the collection at the specified index.
+		/// </summary>
+		/// <param name="index"></param>
+		/// <param name="value"></param>
+		public override void Insert(int index, object value)
+		{
+			items.Insert(index, value);
+		}
 
-        public override bool IsFixedSize
-        {
-            get { return items.IsFixedSize; }
-        }
+		public override bool IsFixedSize
+		{
+			get { return items.IsFixedSize; }
+		}
 
-        public override bool IsReadOnly
-        {
-            get { return items.IsReadOnly; }
-        }
+		public override bool IsReadOnly
+		{
+			get { return items.IsReadOnly; }
+		}
 
-        public override bool IsSynchronized
-        {
-            get { return items.IsSynchronized; }
-        }
+		public override bool IsSynchronized
+		{
+			get { return items.IsSynchronized; }
+		}
 
-        /// <summary>
-        /// Removes the specified MySqlParameter from the collection.
-        /// </summary>
-        /// <param name="value"></param>
-        public override void Remove(object value)
-        {
-            items.Remove(value);
-        }
+		/// <summary>
+		/// Removes the specified MySqlParameter from the collection.
+		/// </summary>
+		/// <param name="value"></param>
+		public override void Remove(object value)
+		{
+			items.Remove(value);
+		}
 
-        /// <summary>
-        /// Removes the specified <see cref="MySqlParameter"/> from the collection using the parameter name.
-        /// </summary>
-        /// <param name="name">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
-        public override void RemoveAt(string name)
-        {
-            items.RemoveAt(InternalIndexOf(name));
-        }
+		/// <summary>
+		/// Removes the specified <see cref="MySqlParameter"/> from the collection using the parameter name.
+		/// </summary>
+		/// <param name="name">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
+		public override void RemoveAt(string name)
+		{
+			DbParameter p = GetParameter(name);
+			items.Remove(p);
+		}
 
-        /// <summary>
-        /// Removes the specified <see cref="MySqlParameter"/> from the collection using a specific index.
-        /// </summary>
-        /// <param name="index">The zero-based index of the parameter. </param>
-        /// <overloads>Removes the specified <see cref="MySqlParameter"/> from the collection.</overloads>
-        public override void RemoveAt(int index)
-        {
-            items.RemoveAt(index);
-        }
+		/// <summary>
+		/// Removes the specified <see cref="MySqlParameter"/> from the collection using a specific index.
+		/// </summary>
+		/// <param name="index">The zero-based index of the parameter. </param>
+		/// <overloads>Removes the specified <see cref="MySqlParameter"/> from the collection.</overloads>
+		public override void RemoveAt(int index)
+		{
+			items.RemoveAt(index);
+		}
 
-        public override object SyncRoot
-        {
-            get { return items.SyncRoot; }
-        }
+		public override object SyncRoot
+		{
+			get { return items.SyncRoot; }
+		}
 
-#endregion
+		#endregion
 
-    }
+	}
 }

Thread
Connector/NET commit: r447 - in trunk: . mysqlclientrburnett31 Oct