2 // System.Data.ProviderBase.DbParameterCollectionBase
5 // Tim Coleman (tim@timcoleman.com)
6 // Boris Kirzner (borisk@mainsoft.com)
8 // Copyright (C) Tim Coleman, 2003
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #if NET_2_0 || TARGET_JVM
37 using System.Collections;
38 using System.Data.Common;
40 namespace System.Data.ProviderBase {
41 public abstract class DbParameterBaseCollection : DbParameterCollection
51 protected DbParameterBaseCollection ()
53 _list = new ArrayList ();
56 #endregion // Constructors
60 public override int Count {
61 get { return _list.Count; }
64 public override bool IsFixedSize {
65 get { return _list.IsFixedSize; }
68 public override bool IsReadOnly {
69 get { return _list.IsReadOnly; }
72 public override bool IsSynchronized {
73 get { return _list.IsSynchronized; }
76 protected abstract Type ItemType { get; }
80 protected virtual string ParameterNamePrefix {
81 get { throw new NotImplementedException (); }
85 public override object SyncRoot {
86 get { return _list.SyncRoot; }
89 #endregion // Properties
93 public override int Add (object value)
96 ((DbParameterBase)value).Parent = this;
97 return _list.Add (value);
101 public override void AddRange (Array values)
103 foreach (object value in values)
108 protected virtual int CheckName (string parameterName)
110 throw new NotImplementedException ();
114 public override void Clear ()
116 if (_list != null && Count != 0) {
117 for (int i = 0; i < _list.Count; i++) {
118 ((DbParameterBase)_list [i]).Parent = null;
124 public override bool Contains (object value)
126 if (IndexOf (value) != -1)
132 public override bool Contains (string value)
134 if (IndexOf (value) != -1)
140 public override void CopyTo (Array array, int index)
142 _list.CopyTo (array, index);
145 public override IEnumerator GetEnumerator ()
147 return _list.GetEnumerator ();
150 protected override DbParameter GetParameter (int index)
152 return (DbParameter) _list [index];
155 public override int IndexOf (object value)
157 ValidateType (value);
158 return _list.IndexOf (value);
161 public override int IndexOf (string parameterName)
166 for (int i = 0; i < _list.Count; i++) {
167 string name = ((DbParameterBase)_list [i]).ParameterName;
168 if (name == parameterName) {
177 protected internal static int IndexOf (IEnumerable items, string parameterName)
179 throw new NotImplementedException ();
183 public override void Insert (int index, object value)
185 Validate(-1, (DbParameterBase)value);
186 ((DbParameterBase)value).Parent = this;
187 _list.Insert (index, value);
192 protected virtual void OnChange ()
194 throw new NotImplementedException ();
198 public override void Remove (object value)
200 ValidateType (value);
201 int index = IndexOf (value);
205 public override void RemoveAt (int index)
210 public override void RemoveAt (string parameterName)
212 int index = IndexOf (parameterName);
216 protected override void SetParameter (int index, DbParameter value)
218 Replace (index, value);
221 protected virtual void Validate (int index, object value)
223 ValidateType (value);
224 DbParameterBase parameter = (DbParameterBase) value;
226 if (parameter.Parent != null) {
227 if (parameter.Parent.Equals (this)) {
228 if (IndexOf (parameter) != index)
229 throw ExceptionHelper.CollectionAlreadyContains (ItemType,"ParameterName",parameter.ParameterName,this);
232 // FIXME : The OleDbParameter with ParameterName 'MyParam2' is already contained by another OleDbParameterCollection.
233 throw new ArgumentException ("");
237 if (parameter.ParameterName == null || parameter.ParameterName == String.Empty) {
239 string parameterName;
242 parameterName = "Parameter" + newIndex;
245 while(IndexOf (parameterName) != -1);
247 parameter.ParameterName = parameterName;
251 protected virtual void ValidateType (object value)
254 throw ExceptionHelper.CollectionNoNullsAllowed (this,ItemType);
256 Type objectType = value.GetType ();
257 Type itemType = ItemType;
259 if (itemType.IsInstanceOfType(objectType)) {
260 Type thisType = this.GetType ();
261 string err = String.Format ("The {0} only accepts non-null {1} type objects, not {2} objects.", thisType.Name, itemType.Name, objectType.Name);
262 throw new InvalidCastException (err);
266 private void RemoveIndex (int index)
268 DbParameterBase oldItem = (DbParameterBase)_list [index];
269 oldItem.Parent = null;
270 _list.RemoveAt (index);
273 private void Replace (int index, DbParameter value)
275 Validate (index, value);
276 DbParameterBase oldItem = (DbParameterBase)this [index];
277 oldItem.Parent = null;
279 ((DbParameterBase)value).Parent = this;
280 _list [index] = value;
283 #endregion // Methods