Merge pull request #1200 from akoeplinger/remove-jvm
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleParameterCollection.cs
1 //
2 // OracleParameterCollection.cs
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors:
11 //    Tim Coleman <tim@timcoleman.com>
12 //
13 // Copyright (C) Tim Coleman , 2003
14 //
15 // Licensed under the MIT/X11 License.
16 //
17
18 using System;
19 using System.Collections;
20 using System.ComponentModel;
21 using System.Data;
22 #if NET_2_0
23 using System.Data.Common;
24 #endif
25 using System.Data.OracleClient.Oci;
26 using System.Drawing.Design;
27 using System.Globalization;
28 using System.Reflection;
29
30 namespace System.Data.OracleClient
31 {
32         [ListBindable (false)]
33         [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, typeof(UITypeEditor))]
34         public sealed class OracleParameterCollection :
35 #if NET_2_0
36                 DbParameterCollection
37 #else
38                 MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
39 #endif
40         {
41                 #region Fields
42
43                 readonly ArrayList list;
44
45                 #endregion // Fields
46
47                 #region Constructors
48
49                 public OracleParameterCollection ()
50                 {
51                         list = new ArrayList ();
52                 }
53
54                 #endregion // Constructors
55
56                 #region Properties
57
58                 public
59 #if NET_2_0
60                 override
61 #endif
62                 int Count {
63                         get { return list.Count; }
64                 }
65
66                 public
67 #if NET_2_0
68                 override
69 #endif
70                 bool IsFixedSize {
71                         get { return list.IsFixedSize; }
72                 }
73
74                 public
75 #if NET_2_0
76                 override
77 #endif
78                 bool IsReadOnly {
79                         get { return list.IsReadOnly; }
80                 }
81
82                 public
83 #if NET_2_0
84                 override
85 #endif
86                 bool IsSynchronized {
87                         get { return list.IsSynchronized; }
88                 }
89
90                 public
91 #if NET_2_0
92                 new
93 #endif
94                 OracleParameter this [string parameterName] {
95                         get {
96                                 return (OracleParameter) GetParameter (parameterName);
97                         }
98                         set {
99                                 SetParameter (parameterName, value);
100                         }
101                 }
102
103                 public
104 #if NET_2_0
105                 override
106 #endif
107                 object SyncRoot {
108                         get { return this; }
109                 }
110
111                 public
112 #if NET_2_0
113                 new
114 #endif
115                 OracleParameter this [int index]
116                 {
117                         get {
118                                 return (OracleParameter) GetParameter (index);
119                         }
120                         set {
121                                 SetParameter (index, value);
122                         }
123                 }
124
125 #if !NET_2_0
126                 object IList.this [int index] {
127                         get { return this [index]; }
128                         set { this [index] = (OracleParameter) value; }
129                 }
130
131                 object IDataParameterCollection.this [string index] {
132                         get { return this [index]; }
133                         set {
134                                 if (!(value is OracleParameter))
135                                         throw new InvalidCastException ("The parameter was not an OracleParameter.");
136                                 this [index] = (OracleParameter) value;
137                         }
138                 }
139 #endif
140
141                 #endregion // Properties
142
143                 #region Methods
144
145 #if NET_2_0
146                 [EditorBrowsable (EditorBrowsableState.Never)]
147 #endif
148                 public
149 #if NET_2_0
150                 override
151 #endif
152                 int Add (object value)
153                 {
154                         AssertParameterValid (value);
155
156                         Add ((OracleParameter) value);
157                         return IndexOf (value);
158                 }
159
160                 public OracleParameter Add (OracleParameter value)
161                 {
162 #if NET_2_0
163                         if (value == null)
164                                 throw CreateParameterNullException ();
165 #endif
166                         if (value.Container != null)
167                                 throw new ArgumentException ("The OracleParameter specified in the value parameter is already added to this or another OracleParameterCollection.");
168                         value.Container = this;
169                         list.Add (value);
170                         return value;
171                 }
172
173                 public OracleParameter Add (string parameterName, object value)
174                 {
175                         return Add (new OracleParameter (parameterName, value));
176                 }
177
178                 public OracleParameter Add (string parameterName, OracleType dataType)
179                 {
180                         return Add (new OracleParameter (parameterName, dataType));
181                 }
182
183                 public OracleParameter Add (string parameterName, OracleType dataType, int size)
184                 {
185                         return Add (new OracleParameter (parameterName, dataType, size));
186                 }
187
188                 public OracleParameter Add (string parameterName, OracleType dataType, int size, string srcColumn)
189                 {
190                         return Add (new OracleParameter (parameterName, dataType, size, srcColumn));
191                 }
192
193 #if NET_2_0
194                 public override void AddRange (Array values)
195                 {
196                         if (values == null)
197                                 throw new ArgumentNullException ("values");
198
199                         foreach (object param in values)
200                                 AssertParameterValid (param);
201
202                         foreach (OracleParameter param in values)
203                                 Add (param);
204                 }
205
206                 public void AddRange (OracleParameter [] values)
207                 {
208                         if (values == null)
209                                 throw new ArgumentNullException ("values");
210
211                         foreach (OracleParameter param in values)
212                                 if (param == null)
213                                         throw CreateParameterNullException ();
214
215                         foreach (OracleParameter param in values)
216                                 Add (param);
217                 }
218 #endif
219
220                 public
221 #if NET_2_0
222                 override
223 #endif
224                 void Clear ()
225                 {
226                         foreach (OracleParameter param in list)
227                                 param.Container = null;
228                         list.Clear ();
229                 }
230
231                 public
232 #if NET_2_0
233                 override
234 #endif
235                 bool Contains (object value)
236                 {
237                         return (IndexOf (value) != -1);
238                 }
239
240 #if NET_2_0
241                 public bool Contains (OracleParameter value)
242                 {
243                         return (IndexOf (value) != -1);
244                 }
245 #endif
246
247                 public
248 #if NET_2_0
249                 override
250 #endif
251                 bool Contains (string parameterName)
252                 {
253                         return (IndexOf (parameterName) != -1);
254                 }
255
256                 public
257 #if NET_2_0
258                 override
259 #endif
260                 void CopyTo (Array array, int index)
261                 {
262                         list.CopyTo (array, index);
263                 }
264
265                 public
266 #if NET_2_0
267                 override
268 #endif
269                 IEnumerator GetEnumerator ()
270                 {
271                         return list.GetEnumerator ();
272                 }
273
274 #if NET_2_0
275                 protected override DbParameter GetParameter (int index)
276 #else
277                 object GetParameter (int index)
278 #endif
279                 {
280                         AssertIndex (index);
281                         return (OracleParameter) list [index];
282                 }
283
284 #if NET_2_0
285                 protected override DbParameter GetParameter (string parameterName)
286 #else
287                 object GetParameter (string parameterName)
288 #endif
289                 {
290                         int index = IndexOf (parameterName);
291                         if (index == -1)
292                                 throw ParameterNotFoundException (parameterName, index);
293                         return (OracleParameter) list [index];
294                 }
295
296 #if NET_2_0
297                 protected override void SetParameter (int index, DbParameter value)
298 #else
299                 void SetParameter (int index, IDbDataParameter value)
300 #endif
301                 {
302                         AssertIndex (index);
303                         AssertParameterValid (value);
304
305                         OracleParameter new_value = (OracleParameter) value;
306                         OracleParameter old_value = (OracleParameter) list [index];
307
308 #if !NET_2_0
309                         old_value.Container = null;
310 #endif
311
312                         if (new_value.Container != null) {
313                                 if (new_value.Container != this)
314                                         throw ParameterAlreadyOwnedException ();
315                                 if (IndexOf (new_value) != index)
316                                         throw ParameterAlreadyOwnedException ();
317                         }
318
319                         list [index] = new_value;
320                         new_value.Container = this;
321 #if NET_2_0
322                         old_value.Container = null;
323 #endif
324                 }
325
326 #if NET_2_0
327                 protected override void SetParameter (string parameterName, DbParameter value)
328 #else
329                 void SetParameter (string parameterName, IDbDataParameter value)
330 #endif
331                 {
332                         int index = IndexOf (parameterName);
333                         if (index == -1)
334                                 throw ParameterNotFoundException (parameterName, index);
335
336                         AssertParameterValid (value);
337
338                         OracleParameter new_value = (OracleParameter) value;
339                         OracleParameter old_value = (OracleParameter) list [index];
340
341 #if !NET_2_0
342                         old_value.Container = null;
343 #endif
344
345                         if (new_value.Container != null) {
346                                 if (new_value.Container != this)
347                                         throw ParameterAlreadyOwnedException ();
348                                 if (IndexOf (new_value) != index)
349                                         throw ParameterAlreadyOwnedException ();
350                         }
351
352                         list [index] = new_value;
353                         new_value.Container = this;
354 #if NET_2_0
355                         old_value.Container = null;
356 #endif
357                 }
358
359                 public
360 #if NET_2_0
361                 override
362 #endif
363                 int IndexOf (object value)
364                 {
365                         if (value != null)
366                                 AssertParameterValid (value);
367
368                         for (int i = 0; i < Count; i += 1)
369                                 if (list [i] == value)
370                                         return i;
371                         return -1;
372                 }
373
374 #if NET_2_0
375                 public int IndexOf (OracleParameter value)
376                 {
377                         for (int i = 0; i < Count; i += 1)
378                                 if (list [i] == value)
379                                         return i;
380                         return -1;
381                 }
382 #endif
383
384                 public
385 #if NET_2_0
386                 override
387 #endif
388                 int IndexOf (string parameterName)
389                 {
390 #if NET_2_0
391                         // case-sensitive lookup
392                         for (int i = 0; i < Count; i += 1) {
393                                 OracleParameter param = (OracleParameter) list [i];
394                                 if (string.Compare (param.ParameterName, parameterName, false, CultureInfo.CurrentCulture) == 0)
395                                         return i;
396                         }
397 #endif
398
399                         // case-insensitive lookup
400                         for (int i = 0; i < Count; i += 1) {
401                                 OracleParameter param = (OracleParameter) list [i];
402                                 if (string.Compare (param.ParameterName, parameterName, true, CultureInfo.CurrentCulture) == 0)
403                                         return i;
404                         }
405
406                         return -1;
407                 }
408
409                 public
410 #if NET_2_0
411                 override
412 #endif
413                 void Insert (int index, object value)
414                 {
415                         AssertParameterValid (value);
416
417                         OracleParameter new_value = (OracleParameter) value;
418
419                         if (new_value.Container != null) {
420                                 if (new_value.Container != this)
421                                         throw ParameterAlreadyOwnedException ();
422                                 if (IndexOf (value) != -1)
423                                         throw ParameterAlreadyOwnedException ();
424                         }
425
426                         list.Insert (index, new_value);
427                         new_value.Container = this;
428                 }
429
430 #if NET_2_0
431                 public void Insert (int index, OracleParameter value)
432                 {
433                         Insert (index, (object) value);
434                 }
435 #endif
436
437                 public
438 #if NET_2_0
439                 override
440 #endif
441                 void Remove (object value)
442                 {
443                         AssertParameterValid (value);
444
445                         int index = IndexOf (value);
446                         if (index == -1)
447                                 throw ParameterNotOwnedException ();
448
449                         ((OracleParameter) value).Container = null;
450                         list.RemoveAt (index);
451                 }
452
453 #if NET_2_0
454                 public void Remove (OracleParameter value)
455                 {
456                         if (value == null)
457                                 throw CreateParameterNullException ();
458
459                         int index = IndexOf (value);
460                         if (index == -1)
461                                 throw ParameterNotOwnedException ();
462
463                         value.Container = null;
464                         list.RemoveAt (index);
465                 }
466 #endif
467
468                 public
469 #if NET_2_0
470                 override
471 #endif
472                 void RemoveAt (int index)
473                 {
474                         AssertIndex (index);
475
476                         OracleParameter param = (OracleParameter) list [index];
477                         param.Container = null;
478                         list.RemoveAt (index);
479                 }
480
481                 public
482 #if NET_2_0
483                 override
484 #endif
485                 void RemoveAt (string parameterName)
486                 {
487                         int index = IndexOf (parameterName);
488                         if (index == -1)
489                                 throw ParameterNotOwnedException (parameterName);
490
491                         OracleParameter param = (OracleParameter) list [index];
492                         param.Container = null;
493                         list.RemoveAt (index);
494                 }
495
496                 static void AssertParameterValid (object value)
497                 {
498                         if (value == null)
499                                 throw CreateParameterNullException ();
500
501                         if (value is OracleParameter)
502                                 return;
503
504                         string msg = string.Format (CultureInfo.InvariantCulture,
505 #if NET_2_0
506                                 "Only non-null {0} instances are valid for " +
507                                 "the {1}, not {2} instances.",
508                                 typeof (OracleParameter).Name,
509                                 typeof (OracleParameterCollection).Name,
510                                 value.GetType ().Name);
511 #else
512                                 "Value is not {0}.",
513                                 typeof (OracleParameter).Name);
514 #endif
515                         throw new InvalidCastException (msg);
516                 }
517
518                 static Exception CreateParameterNullException ()
519                 {
520 #if NET_2_0
521                         string msg = string.Format (CultureInfo.InvariantCulture,
522                                 "Only non-null {0} instances are valid for " +
523                                 "{1}.", typeof (OracleParameter).Name,
524                                 typeof (OracleParameterCollection).Name);
525                         return new ArgumentNullException ("value", msg);
526 #else
527                         return new ArgumentNullException ("value");
528 #endif
529                 }
530
531                 static Exception ParameterAlreadyOwnedException ()
532                 {
533                         string msg = string.Format (CultureInfo.InvariantCulture,
534                                 "The specified {0} is already owned by this " +
535                                 "or another {1}.", typeof (OracleParameter).Name,
536                                 typeof (OracleParameterCollection).Name);
537                         throw new ArgumentException (msg);
538                 }
539
540                 Exception ParameterNotFoundException (string name, int index)
541                 {
542                         string msg = string.Format (CultureInfo.InvariantCulture,
543 #if NET_2_0
544                                 "Index {0} is not valid for this {1}.",
545                                 index, typeof (OracleParameterCollection).Name);
546 #else
547                                 "Parameter '{0}' not found.", name);
548 #endif
549                         throw new IndexOutOfRangeException (msg);
550                 }
551
552                 Exception ParameterNotOwnedException ()
553                 {
554                         throw new ArgumentException (string.Format (
555                                 CultureInfo.InvariantCulture,
556                                 "An {0} instance that is not contained " +
557                                 "by this {1} cannot be removed.",
558                                 typeof (OracleParameter).Name,
559                                 this.GetType ().Name));
560                 }
561
562                 Exception ParameterNotOwnedException (string name)
563                 {
564 #if NET_2_0
565                         throw new IndexOutOfRangeException (string.Format (
566                                 CultureInfo.InvariantCulture,
567                                 "{0} parameter '{1}' is not contained by " +
568                                 "this {2}.", typeof (OracleParameter).Name,
569                                 name, this.GetType ().Name));
570 #else
571                         throw new IndexOutOfRangeException (string.Format (
572                                 CultureInfo.InvariantCulture,
573                                 "Parameter '{0}' does not exist.", name));
574 #endif
575                 }
576
577                 void AssertIndex (int index)
578                 {
579                         if (index < 0 || index >= Count)
580                                 throw new IndexOutOfRangeException (string.Format (
581                                         CultureInfo.InvariantCulture, "Index {0} " +
582                                         "is not valid for this {1}.", index,
583                                         typeof (OracleParameterCollection).Name));
584                 }
585
586                 #endregion // Methods
587         }
588 }