New test.
[mono.git] / mcs / class / Mono.Data.SybaseClient / Mono.Data.SybaseClient / SybaseParameterCollection.cs
1 //
2 // Mono.Data.SybaseClient.SybaseParameterCollection.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //   Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc 2002
10 // Copyright (C) Tim Coleman, 2002
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using Mono.Data.Tds;
35 using System;
36 using System.ComponentModel;
37 using System.Data;
38 using System.Data.Common;
39 using System.Collections;
40
41 namespace Mono.Data.SybaseClient {
42         [ListBindable (false)]
43         public sealed class SybaseParameterCollection : MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
44         {
45                 #region Fields
46
47                 ArrayList list = new ArrayList();
48                 TdsMetaParameterCollection metaParameters;
49                 SybaseCommand command;
50
51                 #endregion // Fields
52
53                 #region Constructors
54
55                 internal SybaseParameterCollection (SybaseCommand command)
56                 {
57                         this.command = command;
58                         metaParameters = new TdsMetaParameterCollection ();
59                 }
60
61                 #endregion // Constructors
62
63                 #region Properties
64
65                 public int Count {
66                         get { return list.Count; }                        
67                 }
68
69                 public SybaseParameter this [int index] {
70                         get { return (SybaseParameter) list [index]; }                    
71                         set { list [index] = (SybaseParameter) value; }                   
72                 }
73
74                 object IDataParameterCollection.this [string parameterName] {
75                         get { return this[parameterName]; }
76                         set { 
77                                 if (!(value is SybaseParameter))
78                                         throw new InvalidCastException ("Only SQLParameter objects can be used.");
79                                 this [parameterName] = (SybaseParameter) value;
80                         }
81                 }
82
83                 public SybaseParameter this [string parameterName] {
84                         get {
85                                 foreach (SybaseParameter p in list)
86                                         if (p.ParameterName.Equals (parameterName))
87                                                 return p;
88                                 throw new IndexOutOfRangeException ("The specified name does not exist: " + parameterName);
89                         }         
90                         set {   
91                                 if (!Contains (parameterName))
92                                         throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
93                                 this [IndexOf (parameterName)] = value;
94                         }                         
95                 }
96
97                 object IList.this [int index] {
98                         get { return (SybaseParameter) this [index]; }
99                         set { this [index] = (SybaseParameter) value; }
100                 }
101
102                 bool IList.IsFixedSize {
103                         get { return list.IsFixedSize; }
104                 }
105
106                 bool IList.IsReadOnly {
107                         get { return list.IsReadOnly; }
108                 }
109
110                 bool ICollection.IsSynchronized {
111                         get { return list.IsSynchronized; }
112                 }
113
114                 object ICollection.SyncRoot {
115                         get { return list.SyncRoot; }
116                 }
117
118                 internal TdsMetaParameterCollection MetaParameters {
119                         get { return metaParameters; }
120                 }
121                 
122                 #endregion // Properties
123
124                 #region Methods
125
126                 public int Add (object value)
127                 {
128                         if (!(value is SybaseParameter))
129                                 throw new InvalidCastException ("The parameter was not an SybaseParameter.");
130                         Add ((SybaseParameter) value);
131                         return IndexOf (value);
132                 }
133                 
134                 public SybaseParameter Add (SybaseParameter value)
135                 {
136                         if (value.Container != null)
137                                 throw new ArgumentException ("The SybaseParameter specified in the value parameter is already added to this or another SybaseParameterCollection.");
138                         
139                         value.Container = this;
140                         list.Add (value);
141                         metaParameters.Add (value.MetaParameter);
142                         return value;
143                 }
144                 
145                 public SybaseParameter Add (string parameterName, object value)
146                 {
147                         return Add (new SybaseParameter (parameterName, value));
148                 }
149                 
150                 public SybaseParameter Add (string parameterName, SybaseType sybaseType)
151                 {
152                         return Add (new SybaseParameter (parameterName, sybaseType));
153                 }
154
155                 public SybaseParameter Add (string parameterName, SybaseType sybaseType, int size)
156                 {
157                         return Add (new SybaseParameter (parameterName, sybaseType, size));
158                 }
159
160                 public SybaseParameter Add (string parameterName, SybaseType sybaseType, int size, string sourceColumn)
161                 {
162                         return Add (new SybaseParameter (parameterName, sybaseType, size, sourceColumn));
163                 }
164
165                 public void Clear()
166                 {
167                         metaParameters.Clear ();
168                         list.Clear ();
169                 }
170                 
171                 public bool Contains (object value)
172                 {
173                         if (!(value is SybaseParameter))
174                                 throw new InvalidCastException ("The parameter was not an SybaseParameter.");
175                         return Contains (((SybaseParameter) value).ParameterName);
176                 }
177
178                 public bool Contains (string value)
179                 {
180                         foreach (SybaseParameter p in list)
181                                 if (p.ParameterName.Equals (value))
182                                         return true;
183                         return false;
184                 }
185
186                 public void CopyTo (Array array, int index)
187                 {
188                         list.CopyTo (array, index);
189                 }
190
191                 public IEnumerator GetEnumerator()
192                 {
193                         return list.GetEnumerator ();
194                 }
195                 
196                 public int IndexOf (object value)
197                 {
198                         if (!(value is SybaseParameter))
199                                 throw new InvalidCastException ("The parameter was not an SybaseParameter.");
200                         return IndexOf (((SybaseParameter) value).ParameterName);
201                 }
202                 
203                 public int IndexOf (string parameterName)
204                 {
205                         return list.IndexOf (parameterName);
206                 }
207
208                 public void Insert (int index, object value)
209                 {
210                         list.Insert (index, value);
211                 }
212
213                 public void Remove (object value)
214                 {
215                         metaParameters.Remove (((SybaseParameter) value).MetaParameter);
216                         list.Remove (value);
217                 }
218
219                 public void RemoveAt (int index)
220                 {
221                         metaParameters.RemoveAt (index);
222                         list.RemoveAt (index);
223                 }
224
225                 public void RemoveAt (string parameterName)
226                 {
227                         RemoveAt (IndexOf (parameterName));
228                 }
229
230                 #endregion // Methods   
231         }
232 }