2 // Mono.Data.MySql.MySqlParameterCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
8 // (C) Ximian, Inc 2002
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.ComponentModel;
35 using System.Data.Common;
36 using System.Collections;
38 namespace Mono.Data.MySql
41 /// Collects all parameters relevant to a Command object
42 /// and their mappings to DataSet columns.
44 public sealed class MySqlParameterCollection : MarshalByRefObject,
45 IDataParameterCollection, IList, ICollection, IEnumerable
47 private ArrayList parameterList = new ArrayList();
50 public IEnumerator GetEnumerator()
52 return new MySqlParameterEnumerator (parameterList);
55 public int Add( object value)
57 // Call the add version that receives a SqlParameter
59 // Check if value is a MySqlParameter.
61 Add((MySqlParameter) value);
63 return IndexOf (value);
67 public MySqlParameter Add(MySqlParameter value)
69 parameterList.Add(value);
74 public MySqlParameter Add(string parameterName, object value)
76 MySqlParameter sqlparam = new MySqlParameter();
77 sqlparam.Value = value;
78 // TODO: Get the DbType from system type of value.
84 public MySqlParameter Add(string parameterName, DbType dbType)
86 MySqlParameter sqlparam = new MySqlParameter();
87 sqlparam.ParameterName = parameterName;
88 sqlparam.DbType = dbType;
93 public MySqlParameter Add(string parameterName,
94 DbType dbType, int size)
96 MySqlParameter sqlparam = new MySqlParameter();
97 sqlparam.ParameterName = parameterName;
98 sqlparam.DbType = dbType;
100 return Add(sqlparam);
104 public MySqlParameter Add(string parameterName,
105 DbType dbType, int size, string sourceColumn)
107 MySqlParameter sqlparam = new MySqlParameter();
108 sqlparam.ParameterName = parameterName;
109 sqlparam.DbType = dbType;
110 sqlparam.Size = size;
111 sqlparam.SourceColumn = sourceColumn;
112 return Add(sqlparam);
118 throw new NotImplementedException ();
122 public bool Contains(object value)
124 // Check if value is a SqlParameter
126 return Contains(((MySqlParameter)value).ParameterName);
131 public bool Contains(string value)
133 for(int p = 0; p < parameterList.Count; p++) {
134 if(((MySqlParameter)parameterList[p]).ParameterName.Equals(value))
141 public void CopyTo(Array array, int index)
143 throw new NotImplementedException ();
147 public int IndexOf(object value)
149 // Check if value is a SqlParameter
151 return IndexOf(((MySqlParameter)value).ParameterName);
155 public int IndexOf(string parameterName)
159 for(p = 0; p < parameterList.Count; p++) {
160 if(((MySqlParameter)parameterList[p]).ParameterName.Equals(parameterName))
167 public void Insert(int index, object value)
169 throw new NotImplementedException ();
173 public void Remove(object value)
175 throw new NotImplementedException ();
179 public void RemoveAt(int index)
181 throw new NotImplementedException ();
185 public void RemoveAt(string parameterName)
187 throw new NotImplementedException ();
193 return parameterList.Count;
197 object IList.this[int index] {
200 return (MySqlParameter) this[index];
205 this[index] = (MySqlParameter) value;
209 public MySqlParameter this[int index] {
211 return (MySqlParameter) parameterList[index];
215 parameterList[index] = (MySqlParameter) value;
219 object IDataParameterCollection.this[string parameterName] {
222 return this[parameterName];
228 this[parameterName] = (MySqlParameter) value;
232 public MySqlParameter this[string parameterName] {
234 for(int p = 0; p < parameterList.Count; p++) {
235 if(parameterName.Equals(((MySqlParameter)parameterList[p]).ParameterName))
236 return (MySqlParameter) parameterList[p];
238 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
242 for(int p = 0; p < parameterList.Count; p++) {
243 if(parameterName.Equals(((MySqlParameter)parameterList[p]).ParameterName))
244 parameterList[p] = value;
246 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
250 bool IList.IsFixedSize {
252 throw new NotImplementedException ();
256 bool IList.IsReadOnly {
258 throw new NotImplementedException ();
262 bool ICollection.IsSynchronized {
264 throw new NotImplementedException ();
268 object ICollection.SyncRoot {
270 throw new NotImplementedException ();
275 /// This method checks if the parameter value is of
276 /// MySqlParameter type. If it doesn't, throws an InvalidCastException.
278 private void CheckType(object value)
280 if(!(value is MySqlParameter))
281 throw new InvalidCastException("Only MySqlParameter objects can be used.");
284 private class MySqlParameterEnumerator : IEnumerator {
\r
285 public MySqlParameterEnumerator (IList list) {
\r
290 public object Current {
\r
292 if (ptr >= list.Count)
\r
293 throw new InvalidOperationException ();
\r
299 public bool MoveNext () {
\r
300 if (ptr > list.Count)
\r
301 throw new InvalidOperationException ();
\r
303 return ++ ptr < list.Count;
\r
306 public void Reset () {
\r
310 private IList list;
\r