**** Merged from MCS ****
[mono-project.git] / mcs / class / ByteFX.Data / mysqlclient / parameter_collection.cs
blob68eee7fe5593f31034d3c844f6c7679b7a28dfd2
1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 using System;
19 using System.Data;
20 using System.Collections;
21 using System.ComponentModel;
23 namespace ByteFX.Data.MySqlClient
25 /// <summary>
26 /// Represents a collection of parameters relevant to a <see cref="MySqlCommand"/> as well as their respective mappings to columns in a <see cref="DataSet"/>. This class cannot be inherited.
27 /// </summary>
28 /// <include file='docs/MySqlParameterCollection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
29 [Editor(typeof(ByteFX.Data.Common.DBParametersEditor), typeof(System.Drawing.Design.UITypeEditor))]
30 [ListBindable(true)]
31 public sealed class MySqlParameterCollection : MarshalByRefObject, IDataParameterCollection,
32 IList, ICollection, IEnumerable
34 private ArrayList _parms = new ArrayList();
36 #region ICollection support
38 /// <summary>
39 /// Gets the number of MySqlParameter objects in the collection.
40 /// </summary>
41 public int Count
43 get { return _parms.Count; }
46 /// <summary>
47 /// Copies MySqlParameter objects from the MySqlParameterCollection to the specified array.
48 /// </summary>
49 /// <param name="array"></param>
50 /// <param name="index"></param>
51 public void CopyTo( Array array, int index )
53 _parms.CopyTo(array, index);
56 bool ICollection.IsSynchronized
58 get { return _parms.IsSynchronized; }
61 object ICollection.SyncRoot
63 get { return _parms.SyncRoot; }
65 #endregion
67 #region IList
69 /// <summary>
70 /// Removes all items from the collection.
71 /// </summary>
72 public void Clear()
74 _parms.Clear();
77 /// <summary>
78 /// Gets a value indicating whether a MySqlParameter exists in the collection.
79 /// </summary>
80 /// <param name="value">The value of the <see cref="MySqlParameter"/> object to find. </param>
81 /// <returns>true if the collection contains the <see cref="MySqlParameter"/> object; otherwise, false.</returns>
82 /// <overloads>Gets a value indicating whether a <see cref="MySqlParameter"/> exists in the collection.</overloads>
83 public bool Contains(object value)
85 return _parms.Contains(value);
88 /// <summary>
89 /// Gets the location of a <see cref="MySqlParameter"/> in the collection.
90 /// </summary>
91 /// <param name="value">The <see cref="MySqlParameter"/> object to locate. </param>
92 /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
93 /// <overloads>Gets the location of a <see cref="MySqlParameter"/> in the collection.</overloads>
94 public int IndexOf(object value)
96 return _parms.IndexOf(value);
99 /// <summary>
100 /// Inserts a MySqlParameter into the collection at the specified index.
101 /// </summary>
102 /// <param name="index"></param>
103 /// <param name="value"></param>
104 public void Insert(int index, object value)
106 _parms.Insert( index, value );
109 bool IList.IsFixedSize
111 get { return _parms.IsFixedSize; }
114 bool IList.IsReadOnly
116 get { return _parms.IsReadOnly; }
119 /// <summary>
120 /// Removes the specified MySqlParameter from the collection.
121 /// </summary>
122 /// <param name="value"></param>
123 public void Remove( object value )
125 _parms.Remove( value );
128 /// <summary>
129 /// Removes the specified <see cref="MySqlParameter"/> from the collection using a specific index.
130 /// </summary>
131 /// <param name="index">The zero-based index of the parameter. </param>
132 /// <overloads>Removes the specified <see cref="MySqlParameter"/> from the collection.</overloads>
133 public void RemoveAt( int index )
135 _parms.RemoveAt( index );
138 object IList.this[int index]
140 get { return this[index]; }
141 set
143 if (! (value is MySqlParameter)) throw new MySqlException("Only MySqlParameter objects may be stored");
144 this[index] = (MySqlParameter)value;
148 /// <summary>
149 /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
150 /// </summary>
151 /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
152 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
153 public int Add( object value )
155 if (! (value is MySqlParameter)) throw new MySqlException("Only MySqlParameter objects may be stored");
157 MySqlParameter p = (MySqlParameter)value;
159 if (p.ParameterName == null || p.ParameterName == String.Empty)
160 throw new MySqlException("Parameters must be named");
162 return _parms.Add(value);
165 #endregion
167 #region IDataParameterCollection
169 /// <summary>
170 /// Gets a value indicating whether a <see cref="MySqlParameter"/> with the specified parameter name exists in the collection.
171 /// </summary>
172 /// <param name="name">The name of the <see cref="MySqlParameter"/> object to find.</param>
173 /// <returns>true if the collection contains the parameter; otherwise, false.</returns>
174 public bool Contains(string name)
176 if (name[0] == '@')
177 name = name.Substring(1, name.Length-1);
178 foreach (MySqlParameter p in _parms)
180 if (p.ParameterName.ToLower().Equals( name.ToLower() )) return true;
182 return false;
185 /// <summary>
186 /// Gets the location of the <see cref="MySqlParameter"/> in the collection with a specific parameter name.
187 /// </summary>
188 /// <param name="parameterName">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
189 /// <returns>The zero-based location of the <see cref="MySqlParameter"/> in the collection.</returns>
190 public int IndexOf( string parameterName )
192 if (parameterName[0] == '@')
193 parameterName = parameterName.Substring(1, parameterName.Length-1);
194 for (int x=0; x < _parms.Count; x++)
196 MySqlParameter p = (MySqlParameter)_parms[x];
197 if (p.ParameterName.ToLower().Equals( parameterName.ToLower() )) return x;
199 throw new MySqlException("Parameter '" + parameterName + "' not found in collection");
202 /// <summary>
203 /// Removes the specified <see cref="MySqlParameter"/> from the collection using the parameter name.
204 /// </summary>
205 /// <param name="name">The name of the <see cref="MySqlParameter"/> object to retrieve. </param>
206 public void RemoveAt( string name )
208 int index = IndexOf( name );
209 _parms.RemoveAt(index);
212 object IDataParameterCollection.this[string name]
214 get { return this[name]; }
215 set
217 if (! (value is MySqlParameter)) throw new MySqlException("Only MySqlParameter objects may be stored");
218 this[name] = (MySqlParameter)value;
221 #endregion
223 #region IEnumerable
224 IEnumerator IEnumerable.GetEnumerator()
226 return ((IEnumerable)_parms).GetEnumerator();
228 #endregion
230 #region Public Methods
232 /// <summary>
233 /// Gets the <see cref="MySqlParameter"/> at the specified index.
234 /// </summary>
235 /// <overloads>Gets the <see cref="MySqlParameter"/> with a specified attribute.
236 /// [C#] In C#, this property is the indexer for the <see cref="MySqlParameterCollection"/> class.
237 /// </overloads>
238 public MySqlParameter this[int index]
240 get { return (MySqlParameter)_parms[index]; }
241 set { _parms[index] = value; }
244 /// <summary>
245 /// Gets the <see cref="MySqlParameter"/> with the specified name.
246 /// </summary>
247 public MySqlParameter this[string name]
249 get { return (MySqlParameter)_parms[ IndexOf( name ) ]; }
250 set { _parms[ IndexOf( name ) ] = value; }
253 /// <summary>
254 /// Adds the specified <see cref="MySqlParameter"/> object to the <see cref="MySqlParameterCollection"/>.
255 /// </summary>
256 /// <param name="value">The <see cref="MySqlParameter"/> to add to the collection.</param>
257 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
258 public MySqlParameter Add(MySqlParameter value)
260 if ( value.ParameterName == null ) throw new ArgumentException("parameter must be named");
262 _parms.Add(value);
263 return value;
266 /// <summary>
267 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
268 /// </summary>
269 /// <param name="parameterName">The name of the parameter.</param>
270 /// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
271 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
272 public MySqlParameter Add( string parameterName, object value )
274 return Add( new MySqlParameter( parameterName, value ) );
277 /// <summary>
278 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the parameter name and the data type.
279 /// </summary>
280 /// <param name="parameterName">The name of the parameter.</param>
281 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
282 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
283 public MySqlParameter Add(string parameterName, MySqlDbType dbType)
285 return Add(new MySqlParameter(parameterName, dbType));
288 /// <summary>
289 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, and the column length.
290 /// </summary>
291 /// <param name="parameterName">The name of the parameter.</param>
292 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
293 /// <param name="size">The length of the column.</param>
294 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
295 public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size)
297 return Add(new MySqlParameter(parameterName, dbType, size ));
300 /// <summary>
301 /// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> with the parameter name, the data type, the column length, and the source column name.
302 /// </summary>
303 /// <param name="parameterName">The name of the parameter.</param>
304 /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
305 /// <param name="size">The length of the column.</param>
306 /// <param name="sourceColumn">The name of the source column.</param>
307 /// <returns>The index of the new <see cref="MySqlParameter"/> object.</returns>
308 public MySqlParameter Add(string parameterName, MySqlDbType dbType, int size, string sourceColumn)
310 return Add(new MySqlParameter(parameterName, dbType, size, sourceColumn));
313 #endregion