ff3af6873e78d2c857466a6736ae8256697bd48c
[mono.git] / mcs / class / System.Data / System.Data.ProviderBase / DbParameterCollectionBase.cs
1 //
2 // System.Data.ProviderBase.DbParameterCollectionBase
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //       Boris Kirzner (borisk@mainsoft.com)
7 //
8 // Copyright (C) Tim Coleman, 2003
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 #if NET_2_0 || TARGET_JVM
36
37 using System.Collections;
38 using System.Data.Common;
39
40 namespace System.Data.ProviderBase {
41         public abstract class DbParameterBaseCollection : DbParameterCollection
42         {
43                 #region Fields
44
45                 ArrayList _list;
46
47                 #endregion // Fields
48
49                 #region Constructors
50         
51                 protected DbParameterBaseCollection ()
52                 {
53                         _list = new ArrayList ();
54                 }
55
56                 #endregion // Constructors
57
58                 #region Properties
59
60                 public override int Count {
61                         get { return _list.Count; }
62                 }
63
64                 public override bool IsFixedSize {
65                         get { return _list.IsFixedSize; }
66                 }
67
68                 public override bool IsReadOnly {
69                         get { return _list.IsReadOnly; }
70                 }
71
72                 public override bool IsSynchronized {
73                         get { return _list.IsSynchronized; }
74                 }
75
76                 protected abstract Type ItemType { get; }
77
78 #if NET_2_0
79                 [MonoTODO]
80                 protected virtual string ParameterNamePrefix {
81                         get { throw new NotImplementedException (); }
82                 }
83 #endif
84
85                 public override object SyncRoot {
86                         get { return _list.SyncRoot; }
87                 }
88
89                 #endregion // Properties
90
91                 #region Methods
92
93                 public override int Add (object value)
94                 {
95             Validate (-1, value);
96             ((DbParameterBase)value).Parent = this;
97                         return _list.Add (value);
98                 }
99
100 #if NET_2_0
101                 public override void AddRange (Array values)
102                 {
103                         foreach (object value in values)
104                                 Add (value);
105                 }
106
107                 [MonoTODO]
108                 protected virtual int CheckName (string parameterName)
109                 {
110                         throw new NotImplementedException ();
111                 }
112 #endif
113
114                 public override void Clear ()
115                 {
116             if (_list != null && Count != 0) {
117                                 for (int i = 0; i < _list.Count; i++) {
118                                         ((DbParameterBase)_list [i]).Parent = null;
119                                 }
120                                 _list.Clear ();
121             }
122                 }
123
124                 public override bool Contains (object value)
125                 {
126             if (IndexOf (value) != -1)
127                 return true;
128             else
129                 return false;
130                 }
131
132                 public override bool Contains (string value)
133                 {
134             if (IndexOf (value) != -1)
135                 return true;
136             else
137                 return false;
138                 }
139
140                 public override void CopyTo (Array array, int index)
141                 {
142                         _list.CopyTo (array, index);
143                 }
144
145                 public override IEnumerator GetEnumerator ()
146                 {
147                         return _list.GetEnumerator ();
148                 }
149
150                 protected override DbParameter GetParameter (int index)
151                 {
152                         return (DbParameter) _list [index];
153                 }
154
155                 public override int IndexOf (object value)
156                 {
157             ValidateType (value);
158                         return _list.IndexOf (value);
159                 }
160
161                 public override int IndexOf (string parameterName)
162                 {
163             if (_list == null)
164                 return -1;
165
166             for (int i = 0; i < _list.Count; i++) {
167                 string name = ((DbParameterBase)_list [i]).ParameterName;
168                 if (name == parameterName) {
169                     return i;
170                 }
171             }
172             return -1;
173                 }
174
175 #if NET_2_0
176                 [MonoTODO]
177                 protected internal static int IndexOf (IEnumerable items, string parameterName)
178                 {
179                         throw new NotImplementedException ();
180                 }
181 #endif
182
183                 public override void Insert (int index, object value)
184                 {
185                         Validate(-1, (DbParameterBase)value);
186                         ((DbParameterBase)value).Parent = this;
187                         _list.Insert (index, value);
188                 }
189
190 #if NET_2_0
191                 [MonoTODO]
192                 protected virtual void OnChange ()
193                 {
194                         throw new NotImplementedException ();
195                 }
196 #endif
197
198                 public override void Remove (object value)
199                 {
200             ValidateType (value);
201                         int index = IndexOf (value);
202             RemoveIndex (index);
203                 }
204
205                 public override void RemoveAt (int index)
206                 {
207                         RemoveIndex (index);
208                 }
209
210                 public override void RemoveAt (string parameterName)
211                 {
212             int index = IndexOf (parameterName);
213             RemoveIndex (index);
214                 }
215
216                 protected override void SetParameter (int index, DbParameter value)
217                 {
218                         Replace (index, value);
219                 }
220
221                 protected virtual void Validate (int index, object value)
222                 {
223                         ValidateType (value);
224                         DbParameterBase parameter = (DbParameterBase) value;
225
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);                    
230                 }
231                 else {
232                                         // FIXME :  The OleDbParameter with ParameterName 'MyParam2' is already contained by another OleDbParameterCollection.
233                     throw new ArgumentException ("");
234                 }
235             }
236
237             if (parameter.ParameterName == null  || parameter.ParameterName == String.Empty) {
238                                 int newIndex = 1;
239                                 string parameterName;
240                                 
241                                 do {
242                                         parameterName = "Parameter" + newIndex;
243                                         newIndex++;
244                                 }
245                                 while(IndexOf (parameterName) != -1);
246
247                 parameter.ParameterName = parameterName;
248             }
249                 }               
250
251                 protected virtual void ValidateType (object value)
252                 {
253                         if (value == null)
254                 throw ExceptionHelper.CollectionNoNullsAllowed (this,ItemType);
255
256                         Type objectType = value.GetType ();
257                         Type itemType = ItemType;
258
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);
263                         }
264                 }
265
266                 private void RemoveIndex (int index)
267         {
268                         DbParameterBase oldItem = (DbParameterBase)_list [index];
269             oldItem.Parent = null;
270             _list.RemoveAt (index);
271         }               
272
273                 private void Replace (int index, DbParameter value)
274         {
275             Validate (index, value);
276             DbParameterBase oldItem = (DbParameterBase)this [index];
277             oldItem.Parent = null;
278
279             ((DbParameterBase)value).Parent = this;
280             _list [index] = value;
281         }
282
283                 #endregion // Methods
284         }
285 }
286
287 #endif