2 // OracleParameterCollection.cs
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
11 // Tim Coleman <tim@timcoleman.com>
13 // Copyright (C) Tim Coleman , 2003
15 // Licensed under the MIT/X11 License.
19 using System.Collections;
20 using System.ComponentModel;
23 using System.Data.Common;
25 using System.Data.OracleClient.Oci;
26 using System.Drawing.Design;
27 using System.Globalization;
28 using System.Reflection;
30 namespace System.Data.OracleClient
32 [ListBindable (false)]
33 [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, typeof(UITypeEditor))]
34 public sealed class OracleParameterCollection :
38 MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
43 readonly ArrayList list;
49 public OracleParameterCollection ()
51 list = new ArrayList ();
54 #endregion // Constructors
63 get { return list.Count; }
71 get { return list.IsFixedSize; }
79 get { return list.IsReadOnly; }
87 get { return list.IsSynchronized; }
94 OracleParameter this [string parameterName] {
96 return (OracleParameter) GetParameter (parameterName);
99 SetParameter (parameterName, value);
115 OracleParameter this [int index]
118 return (OracleParameter) GetParameter (index);
121 SetParameter (index, value);
126 object IList.this [int index] {
127 get { return this [index]; }
128 set { this [index] = (OracleParameter) value; }
131 object IDataParameterCollection.this [string index] {
132 get { return this [index]; }
134 if (!(value is OracleParameter))
135 throw new InvalidCastException ("The parameter was not an OracleParameter.");
136 this [index] = (OracleParameter) value;
141 #endregion // Properties
146 [EditorBrowsable (EditorBrowsableState.Never)]
152 int Add (object value)
154 AssertParameterValid (value);
156 Add ((OracleParameter) value);
157 return IndexOf (value);
160 public OracleParameter Add (OracleParameter value)
164 throw CreateParameterNullException ();
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;
173 public OracleParameter Add (string parameterName, object value)
175 return Add (new OracleParameter (parameterName, value));
178 public OracleParameter Add (string parameterName, OracleType dataType)
180 return Add (new OracleParameter (parameterName, dataType));
183 public OracleParameter Add (string parameterName, OracleType dataType, int size)
185 return Add (new OracleParameter (parameterName, dataType, size));
188 public OracleParameter Add (string parameterName, OracleType dataType, int size, string srcColumn)
190 return Add (new OracleParameter (parameterName, dataType, size, srcColumn));
194 public override void AddRange (Array values)
197 throw new ArgumentNullException ("values");
199 foreach (object param in values)
200 AssertParameterValid (param);
202 foreach (OracleParameter param in values)
206 public void AddRange (OracleParameter [] values)
209 throw new ArgumentNullException ("values");
211 foreach (OracleParameter param in values)
213 throw CreateParameterNullException ();
215 foreach (OracleParameter param in values)
226 foreach (OracleParameter param in list)
227 param.Container = null;
235 bool Contains (object value)
237 return (IndexOf (value) != -1);
241 public bool Contains (OracleParameter value)
243 return (IndexOf (value) != -1);
251 bool Contains (string parameterName)
253 return (IndexOf (parameterName) != -1);
260 void CopyTo (Array array, int index)
262 list.CopyTo (array, index);
269 IEnumerator GetEnumerator ()
271 return list.GetEnumerator ();
275 protected override DbParameter GetParameter (int index)
277 object GetParameter (int index)
281 return (OracleParameter) list [index];
285 protected override DbParameter GetParameter (string parameterName)
287 object GetParameter (string parameterName)
290 int index = IndexOf (parameterName);
292 throw ParameterNotFoundException (parameterName, index);
293 return (OracleParameter) list [index];
297 protected override void SetParameter (int index, DbParameter value)
299 void SetParameter (int index, IDbDataParameter value)
303 AssertParameterValid (value);
305 OracleParameter new_value = (OracleParameter) value;
306 OracleParameter old_value = (OracleParameter) list [index];
309 old_value.Container = null;
312 if (new_value.Container != null) {
313 if (new_value.Container != this)
314 throw ParameterAlreadyOwnedException ();
315 if (IndexOf (new_value) != index)
316 throw ParameterAlreadyOwnedException ();
319 list [index] = new_value;
320 new_value.Container = this;
322 old_value.Container = null;
327 protected override void SetParameter (string parameterName, DbParameter value)
329 void SetParameter (string parameterName, IDbDataParameter value)
332 int index = IndexOf (parameterName);
334 throw ParameterNotFoundException (parameterName, index);
336 AssertParameterValid (value);
338 OracleParameter new_value = (OracleParameter) value;
339 OracleParameter old_value = (OracleParameter) list [index];
342 old_value.Container = null;
345 if (new_value.Container != null) {
346 if (new_value.Container != this)
347 throw ParameterAlreadyOwnedException ();
348 if (IndexOf (new_value) != index)
349 throw ParameterAlreadyOwnedException ();
352 list [index] = new_value;
353 new_value.Container = this;
355 old_value.Container = null;
363 int IndexOf (object value)
366 AssertParameterValid (value);
368 for (int i = 0; i < Count; i += 1)
369 if (list [i] == value)
375 public int IndexOf (OracleParameter value)
377 for (int i = 0; i < Count; i += 1)
378 if (list [i] == value)
388 int IndexOf (string parameterName)
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)
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)
413 void Insert (int index, object value)
415 AssertParameterValid (value);
417 OracleParameter new_value = (OracleParameter) value;
419 if (new_value.Container != null) {
420 if (new_value.Container != this)
421 throw ParameterAlreadyOwnedException ();
422 if (IndexOf (value) != -1)
423 throw ParameterAlreadyOwnedException ();
426 list.Insert (index, new_value);
427 new_value.Container = this;
431 public void Insert (int index, OracleParameter value)
433 Insert (index, (object) value);
441 void Remove (object value)
443 AssertParameterValid (value);
445 int index = IndexOf (value);
447 throw ParameterNotOwnedException ();
449 ((OracleParameter) value).Container = null;
450 list.RemoveAt (index);
454 public void Remove (OracleParameter value)
457 throw CreateParameterNullException ();
459 int index = IndexOf (value);
461 throw ParameterNotOwnedException ();
463 value.Container = null;
464 list.RemoveAt (index);
472 void RemoveAt (int index)
476 OracleParameter param = (OracleParameter) list [index];
477 param.Container = null;
478 list.RemoveAt (index);
485 void RemoveAt (string parameterName)
487 int index = IndexOf (parameterName);
489 throw ParameterNotOwnedException (parameterName);
491 OracleParameter param = (OracleParameter) list [index];
492 param.Container = null;
493 list.RemoveAt (index);
496 static void AssertParameterValid (object value)
499 throw CreateParameterNullException ();
501 if (value is OracleParameter)
504 string msg = string.Format (CultureInfo.InvariantCulture,
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);
513 typeof (OracleParameter).Name);
515 throw new InvalidCastException (msg);
518 static Exception CreateParameterNullException ()
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);
527 return new ArgumentNullException ("value");
531 static Exception ParameterAlreadyOwnedException ()
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);
540 Exception ParameterNotFoundException (string name, int index)
542 string msg = string.Format (CultureInfo.InvariantCulture,
544 "Index {0} is not valid for this {1}.",
545 index, typeof (OracleParameterCollection).Name);
547 "Parameter '{0}' not found.", name);
549 throw new IndexOutOfRangeException (msg);
552 Exception ParameterNotOwnedException ()
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));
562 Exception ParameterNotOwnedException (string name)
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));
571 throw new IndexOutOfRangeException (string.Format (
572 CultureInfo.InvariantCulture,
573 "Parameter '{0}' does not exist.", name));
577 void AssertIndex (int index)
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));
586 #endregion // Methods