2 // System.Data.Common.AbstractDbParameterCollection
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Boris Kirzner <borisk@mainsoft.com>
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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.
34 using System.Data.Common;
35 using System.Collections;
37 namespace System.Data.ProviderBase
39 public abstract class AbstractDbParameterCollection : DbParameterCollection
43 readonly AbstractDbCommand _parent;
44 readonly ArrayList _list = new ArrayList();
50 public AbstractDbParameterCollection(DbCommand parent)
52 _parent = (AbstractDbCommand)parent;
55 #endregion // Constructors
59 public override int Count {
60 get { return _list.Count; }
63 public override bool IsFixedSize {
64 get { return _list.IsFixedSize; }
67 public override bool IsReadOnly {
68 get { return _list.IsReadOnly; }
71 public override bool IsSynchronized {
72 get { return _list.IsSynchronized; }
75 protected abstract Type ItemType { get; }
77 public override object SyncRoot {
78 get { return _list.SyncRoot; }
81 #endregion // Properties
85 public override int Add (object value) {
88 ((AbstractDbParameter)value).Parent = this;
89 return _list.Add (value);
93 public override void AddRange (Array values)
95 foreach (object value in values)
101 public override void Clear () {
103 if (_list != null && Count != 0) {
104 for (int i = 0; i < _list.Count; i++) {
105 ((AbstractDbParameter)_list [i]).Parent = null;
111 public override bool Contains (object value) {
112 if (IndexOf (value) != -1)
118 public override bool Contains (string value) {
119 if (IndexOf (value) != -1)
125 public override void CopyTo (Array array, int index) {
126 _list.CopyTo (array, index);
129 public override IEnumerator GetEnumerator () {
130 return _list.GetEnumerator ();
133 protected override DbParameter GetParameter (int index) {
134 return (DbParameter) _list [index];
138 protected override DbParameter GetParameter (string parameterName) {
139 return GetParameter (IndexOf (parameterName));
142 protected override void SetParameter (string parameterName, DbParameter value) {
143 SetParameter (IndexOf (parameterName), value);
147 public override int IndexOf (object value) {
148 ValidateType (value);
149 return _list.IndexOf (value);
152 public override int IndexOf (string parameterName) {
156 for (int i = 0; i < _list.Count; i++) {
157 string name = ((DbParameter)_list [i]).ParameterName;
158 if (String.Compare (name, parameterName, StringComparison.OrdinalIgnoreCase) == 0) {
165 public override void Insert (int index, object value) {
166 Validate(-1, (DbParameter)value);
168 ((AbstractDbParameter)value).Parent = this;
169 _list.Insert (index, value);
172 public override void Remove (object value) {
173 ValidateType (value);
174 int index = IndexOf (value);
178 public override void RemoveAt (int index) {
182 public override void RemoveAt (string parameterName) {
183 int index = IndexOf (parameterName);
187 protected override void SetParameter (int index, DbParameter value) {
188 Replace (index, value);
191 void Validate (int index, object value) {
192 ValidateType (value);
193 AbstractDbParameter parameter = (AbstractDbParameter) value;
195 if (parameter.Parent != null) {
196 if (parameter.Parent.Equals (this)) {
197 if (IndexOf (parameter) != index)
198 throw ExceptionHelper.CollectionAlreadyContains (ItemType,"ParameterName",parameter.ParameterName,this);
201 // FIXME : The OleDbParameter with ParameterName 'MyParam2' is already contained by another OleDbParameterCollection.
202 throw new ArgumentException ("");
206 if (parameter.ParameterName == null || parameter.ParameterName == String.Empty) {
208 string parameterName;
211 parameterName = "Parameter" + newIndex;
214 while(IndexOf (parameterName) != -1);
216 parameter.ParameterName = parameterName;
220 void ValidateType (object value) {
222 throw ExceptionHelper.CollectionNoNullsAllowed (this,ItemType);
224 Type objectType = value.GetType ();
225 Type itemType = ItemType;
227 if (itemType.IsInstanceOfType(objectType)) {
228 Type thisType = this.GetType ();
229 string err = String.Format ("The {0} only accepts non-null {1} type objects, not {2} objects.", thisType.Name, itemType.Name, objectType.Name);
230 throw new InvalidCastException (err);
234 private void RemoveIndex (int index) {
236 AbstractDbParameter oldItem = (AbstractDbParameter)_list [index];
237 oldItem.Parent = null;
238 _list.RemoveAt (index);
241 private void Replace (int index, DbParameter value) {
242 Validate (index, value);
243 AbstractDbParameter oldItem = (AbstractDbParameter)this [index];
244 oldItem.Parent = null;
246 ((AbstractDbParameter)value).Parent = this;
247 _list [index] = value;
250 protected internal void OnSchemaChanging()
252 if (_parent != null) {
253 _parent.OnSchemaChanging();
257 #endregion // Methods