2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Mono.Data.MySql / Mono.Data.MySql / MySqlParameterCollection.cs
1 //
2 // Mono.Data.MySql.MySqlParameterCollection.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //
8 // (C) Ximian, Inc 2002
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.ComponentModel;
34 using System.Data;
35 using System.Data.Common;
36 using System.Collections;
37
38 namespace Mono.Data.MySql
39 {
40         /// <summary>
41         /// Collects all parameters relevant to a Command object 
42         /// and their mappings to DataSet columns.
43         /// </summary>
44         public sealed class MySqlParameterCollection : MarshalByRefObject,
45                 IDataParameterCollection, IList, ICollection, IEnumerable
46         {
47                 private ArrayList parameterList = new ArrayList();
48
49                 [MonoTODO]
50                 public IEnumerator GetEnumerator()
51                 {
52                         return new MySqlParameterEnumerator (parameterList);
53                 }
54                 
55                 public int Add( object value)
56                 {
57                         // Call the add version that receives a SqlParameter 
58                         
59                         // Check if value is a MySqlParameter.
60                         CheckType(value);
61                         Add((MySqlParameter) value);
62
63                         return IndexOf (value);
64                 }
65
66                 
67                 public MySqlParameter Add(MySqlParameter value)
68                 {
69                         parameterList.Add(value);
70                         return value;
71                 }
72
73                 
74                 public MySqlParameter Add(string parameterName, object value)
75                 {
76                         MySqlParameter sqlparam = new MySqlParameter();
77                         sqlparam.Value = value;
78                         // TODO: Get the DbType from system type of value.
79                         
80                         return Add(sqlparam);
81                 }
82
83                 
84                 public MySqlParameter Add(string parameterName, DbType dbType)
85                 {
86                         MySqlParameter sqlparam = new MySqlParameter();
87                         sqlparam.ParameterName = parameterName;
88                         sqlparam.DbType = dbType;
89                         return Add(sqlparam);                   
90                 }
91
92                 
93                 public MySqlParameter Add(string parameterName,
94                         DbType dbType, int size)
95                 {
96                         MySqlParameter sqlparam = new MySqlParameter();
97                         sqlparam.ParameterName = parameterName;
98                         sqlparam.DbType = dbType;
99                         sqlparam.Size = size;
100                         return Add(sqlparam);                   
101                 }
102
103                 
104                 public MySqlParameter Add(string parameterName,
105                         DbType dbType, int size, string sourceColumn)
106                 {
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);                   
113                 }
114
115                 [MonoTODO]
116                 public void Clear()
117                 {
118                         throw new NotImplementedException ();
119                 }
120
121                 
122                 public bool Contains(object value)
123                 {
124                         // Check if value is a SqlParameter
125                         CheckType(value);
126                         return Contains(((MySqlParameter)value).ParameterName);
127                 }
128
129
130                 [MonoTODO]
131                 public bool Contains(string value)
132                 {
133                         for(int p = 0; p < parameterList.Count; p++) {
134                                 if(((MySqlParameter)parameterList[p]).ParameterName.Equals(value))
135                                         return true;
136                         }
137                         return false;
138                 }
139
140                 [MonoTODO]
141                 public void CopyTo(Array array, int index)
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 
147                 public int IndexOf(object value)
148                 {
149                         // Check if value is a SqlParameter
150                         CheckType(value);
151                         return IndexOf(((MySqlParameter)value).ParameterName);
152                 }
153
154                 
155                 public int IndexOf(string parameterName)
156                 {
157                         int p = -1;
158
159                         for(p = 0; p < parameterList.Count; p++) {
160                                 if(((MySqlParameter)parameterList[p]).ParameterName.Equals(parameterName))
161                                         return p;
162                         }
163                         return p;
164                 }
165
166                 [MonoTODO]
167                 public void Insert(int index, object value)
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 [MonoTODO]
173                 public void Remove(object value)
174                 {
175                         throw new NotImplementedException ();
176                 }
177
178                 [MonoTODO]
179                 public void RemoveAt(int index)
180                 {
181                         throw new NotImplementedException ();
182                 }
183
184                 [MonoTODO]
185                 public void RemoveAt(string parameterName)
186                 {
187                         throw new NotImplementedException ();
188                 }
189         
190                 [MonoTODO]
191                 public int Count {
192                         get {   
193                                 return parameterList.Count;
194                         }                         
195                 }
196
197                 object IList.this[int index] {
198                         [MonoTODO]
199                         get { 
200                                 return (MySqlParameter) this[index];
201                         }
202                         
203                         [MonoTODO]
204                         set { 
205                                 this[index] = (MySqlParameter) value;
206                         }
207                 }
208
209                 public MySqlParameter this[int index] {
210                         get {   
211                                 return (MySqlParameter) parameterList[index];
212                         }                         
213                         
214                         set {   
215                                 parameterList[index] = (MySqlParameter) value;
216                         }                         
217                 }
218
219                 object IDataParameterCollection.this[string parameterName] {
220                         [MonoTODO]
221                         get { 
222                                 return this[parameterName];
223                         }
224                         
225                         [MonoTODO]
226                         set { 
227                                 CheckType(value);
228                                 this[parameterName] = (MySqlParameter) value;
229                         }
230                 }
231
232                 public MySqlParameter this[string parameterName] {
233                         get {   
234                                 for(int p = 0; p < parameterList.Count; p++) {
235                                         if(parameterName.Equals(((MySqlParameter)parameterList[p]).ParameterName))
236                                                 return (MySqlParameter) parameterList[p];
237                                 }
238                                 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
239                         }         
240                         
241                         set {   
242                                 for(int p = 0; p < parameterList.Count; p++) {
243                                         if(parameterName.Equals(((MySqlParameter)parameterList[p]).ParameterName))
244                                                 parameterList[p] = value;
245                                 }
246                                 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
247                         }                         
248                 }
249
250                 bool IList.IsFixedSize {
251                         get {   
252                                 throw new NotImplementedException ();
253                         }                         
254                 }
255
256                 bool IList.IsReadOnly {
257                         get {   
258                                 throw new NotImplementedException ();
259                         }                         
260                 }
261
262                 bool ICollection.IsSynchronized {
263                         get {   
264                                 throw new NotImplementedException ();
265                         }                         
266                 }
267
268                 object ICollection.SyncRoot {
269                         get {   
270                                 throw new NotImplementedException ();
271                         }                         
272                 }
273                 
274                 /// <summary>
275                 /// This method checks if the parameter value is of 
276                 /// MySqlParameter type. If it doesn't, throws an InvalidCastException.
277                 /// </summary>
278                 private void CheckType(object value)
279                 {
280                         if(!(value is MySqlParameter))
281                                 throw new InvalidCastException("Only MySqlParameter objects can be used.");
282                 }
283
284                 private class MySqlParameterEnumerator : IEnumerator {\r
285                         public MySqlParameterEnumerator (IList list) {\r
286                                 this.list = list;\r
287                                 Reset ();\r
288                         }\r
289 \r
290                         public object Current {\r
291                                 get {\r
292                                         if (ptr >= list.Count)\r
293                                                 throw new InvalidOperationException ();\r
294 \r
295                                         return list[ptr];\r
296                                 }\r
297                         }\r
298 \r
299                         public bool MoveNext () {\r
300                                 if (ptr > list.Count)\r
301                                         throw new InvalidOperationException ();\r
302                                 \r
303                                 return ++ ptr < list.Count;\r
304                         }\r
305 \r
306                         public void Reset () {\r
307                                 ptr = -1;\r
308                         }\r
309 \r
310                         private IList list;\r
311                         private int ptr;\r
312                 }
313         }
314 }