Add licensing info
[mono.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteParameterCollection.cs
1 // -*- c-basic-offset: 8; inent-tabs-mode: nil -*-
2 //
3 //  SqliteParameterCollection.cs
4 //
5 //  Author(s): Vladimir Vukicevic  <vladimir@pobox.com>
6 //
7 //  Copyright (C) 2002  Vladimir Vukicevic
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Data;
33 using System.Collections;
34
35 namespace Mono.Data.SqliteClient
36 {
37         public class SqliteParameterCollection : IDataParameterCollection,
38                 IList
39         {
40                 ArrayList numeric_param_list = new ArrayList ();
41                 Hashtable named_param_hash = new Hashtable ();
42
43                 public IEnumerator GetEnumerator ()
44                 {
45                         throw new NotImplementedException ();
46                 }
47
48                 public void RemoveAt (string parameterName)
49                 {
50                         if (!named_param_hash.Contains (parameterName))
51                                 throw new ApplicationException ("Parameter " + parameterName + " not found");
52
53                         numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
54                         named_param_hash.Remove (parameterName);
55
56                         RecreateNamedHash ();
57                 }
58
59                 public void RemoveAt (SqliteParameter param)
60                 {
61                         RemoveAt (param.ParameterName);
62                 }
63
64                 public void RemoveAt (int index)
65                 {
66                         RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
67                 }
68
69                 int IList.IndexOf (object o)
70                 {
71                         return IndexOf ((SqliteParameter) o);
72                 }
73
74                 public int IndexOf (string parameterName)
75                 {
76                         return (int) named_param_hash[parameterName];
77                 }
78
79                 public int IndexOf (SqliteParameter param)
80                 {
81                         return IndexOf (param.ParameterName);
82                 }
83
84                 bool IList.Contains (object value)
85                 {
86                         return Contains ((SqliteParameter) value);
87                 }
88
89                 public bool Contains (string parameterName)
90                 {
91                         return named_param_hash.Contains (parameterName);
92                 }
93
94                 public bool Contains (SqliteParameter param)
95                 {
96                         return Contains (param.ParameterName);
97                 }
98
99                 object IList.this[int index] {
100                         get {
101                                 return this[index];
102                         }
103                         set {
104                                 CheckSqliteParam (value);
105                                 this[index] = (SqliteParameter) value;
106                         }
107                 }
108
109                 object IDataParameterCollection.this[string parameterName] {
110                         get {
111                                 return this[parameterName];
112                         }
113                         set {
114                                 CheckSqliteParam (value);
115                                 this[parameterName] = (SqliteParameter) value;
116                         }
117                 }
118
119                 public SqliteParameter this[string parameterName] {
120                         get {
121                                 return this[(int) named_param_hash[parameterName]];
122                         }
123                         set {
124                                 if (this.Contains (parameterName))
125                                         numeric_param_list[(int) named_param_hash[parameterName]] = value;
126                                 else          // uhm, do we add it if it doesn't exist? what does ms do?
127                                         Add (value);
128                         }
129                 }
130
131                 public SqliteParameter this[int parameterIndex] {
132                         get {
133                                 return (SqliteParameter) numeric_param_list[parameterIndex];
134                         }
135                         set {
136                                 numeric_param_list[parameterIndex] = value;
137                         }
138                 }
139
140                 public int Add (object value)
141                 {
142                         CheckSqliteParam (value);
143                         SqliteParameter sqlp = (SqliteParameter) value;
144                         if (named_param_hash.Contains (sqlp.ParameterName))
145                                 throw new DuplicateNameException ("Parameter collection already contains given value.");
146
147                         named_param_hash[value] = numeric_param_list.Add (value);
148
149                         return (int) named_param_hash[value];
150                 }
151
152                 // IList
153
154                 public SqliteParameter Add (SqliteParameter param)
155                 {
156                         Add ((object)param);
157                         return param;
158                 }
159
160                 public SqliteParameter Add (string name, object value)
161                 {
162                         return Add (new SqliteParameter (name, value));
163                 }
164
165                 public SqliteParameter Add (string name, DbType type)
166                 {
167                         return Add (new SqliteParameter (name, type));
168                 }
169
170                 public bool IsFixedSize {
171                         get {
172                                 return false;
173                         }
174                 }
175
176                 public bool IsReadOnly {
177                         get {
178                                 return false;
179                         }
180                 }
181
182                 public void Clear ()
183                 {
184                         numeric_param_list.Clear ();
185                         named_param_hash.Clear ();
186                 }
187
188                 public void Insert (int index, object value)
189                 {
190                         CheckSqliteParam (value);
191                         if (numeric_param_list.Count == index) {
192                                 Add (value);
193                                 return;
194                         }
195
196                         numeric_param_list.Insert (index, value);
197                         RecreateNamedHash ();
198                 }
199
200                 public void Remove (object value)
201                 {
202                         CheckSqliteParam (value);
203                         RemoveAt ((SqliteParameter) value);
204                 }
205
206                 // ICollection
207
208                 public int Count {
209                         get {
210                                 return numeric_param_list.Count;
211                         }
212                 }
213
214                 public bool IsSynchronized {
215                         get {
216                                 return false;
217                         }
218                 }
219
220                 public object SyncRoot {
221                         get {
222                                 return null;
223                         }
224                 }
225
226                 public void CopyTo (Array array, int index)
227                 {
228                         throw new NotImplementedException ();
229                 }
230
231                 private void CheckSqliteParam (object value)
232                 {
233                         if (!(value is SqliteParameter))
234                                 throw new InvalidCastException ("Can only use SqliteParameter objects");
235                 }
236
237                 private void RecreateNamedHash ()
238                 {
239                         for (int i = 0; i < numeric_param_list.Count; i++) {
240                                 named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
241                         }
242                 }
243         }
244 }