2008-07-01 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite / SqliteParameterCollection.cs
1 //
2 // Mono.Data.Sqlite.SqliteParameterCollection.cs
3 //
4 // Represents a collection of parameters relevant to a SqliteCommand as well as 
5 // their respective mappings to columns in a DataSet.
6 //
7 //Author(s):            Vladimir Vukicevic  <vladimir@pobox.com>
8 //                      Everaldo Canuto  <everaldo_canuto@yahoo.com.br>
9 //                      Chris Turchin <chris@turchin.net>
10 //                      Jeroen Zwartepoorte <jeroen@xs4all.nl>
11 //                      Thomas Zoechling <thomas.zoechling@gmx.at>
12 //
13 // Copyright (C) 2002  Vladimir Vukicevic
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 #if !NET_2_0
35 using System;
36 using System.Data;
37 using System.Collections;
38
39 namespace Mono.Data.Sqlite
40 {
41         public class SqliteParameterCollection : IDataParameterCollection, IList
42         {
43         
44                 #region Fields
45                 
46                 ArrayList numeric_param_list = new ArrayList();
47                 Hashtable named_param_hash = new Hashtable();
48                 
49                 #endregion
50
51                 #region Private Methods
52
53                 private void CheckSqliteParam (object value)
54                 {
55                         if (!(value is SqliteParameter))
56                                 throw new InvalidCastException ("Can only use SqliteParameter objects");
57                         SqliteParameter sqlp = value as SqliteParameter;
58                         if (sqlp.ParameterName == null || sqlp.ParameterName.Length == 0)
59                                 sqlp.ParameterName = this.GenerateParameterName();
60                  }
61
62                 private void RecreateNamedHash ()
63                 {
64                         for (int i = 0; i < numeric_param_list.Count; i++) 
65                         {
66                                 named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
67                         }
68                 }
69
70                 //FIXME: if the user is calling Insert at various locations with unnamed parameters, this is not going to work....
71                 private string GenerateParameterName()
72                 {
73                         int             index   = this.Count + 1;
74                         string  name    = String.Empty;
75
76                         while (index > 0)
77                         {
78                                 name = ":" + index.ToString();
79                                         if (this.IndexOf(name) == -1)
80                                         index = -1;
81                                 else
82                                 index++;
83                         }
84                         return name;
85                 }
86
87                 #endregion
88
89                 #region Properties
90                 
91                 object IList.this[int index] {
92                         get 
93                         {
94                                 return this[index];
95                         }
96                         set 
97                         {
98                                 CheckSqliteParam (value);
99                                 this[index] = (SqliteParameter) value;
100                         }
101                 }
102
103                 object IDataParameterCollection.this[string parameterName] {
104                         get 
105                         {
106                                 return this[parameterName];
107                         }
108                         set 
109                         {
110                                 CheckSqliteParam (value);
111                                 this[parameterName] = (SqliteParameter) value;
112                         }
113                 }
114                 
115                 private bool isPrefixed (string parameterName)
116                 {
117                         return parameterName.Length > 1 && (parameterName[0] == ':' || parameterName[0] == '$');
118                 }
119
120                 SqliteParameter GetParameter (int parameterIndex)
121                 {
122                         if (this.Count >= parameterIndex+1)
123                                 return (SqliteParameter) numeric_param_list[parameterIndex];
124                         else          
125                                 throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
126                 }
127
128                 SqliteParameter GetParameter (string parameterName)
129                 {
130                         if (this.Contains(parameterName))
131                                 return this[(int) named_param_hash[parameterName]];
132                         else if (isPrefixed(parameterName) && this.Contains(parameterName.Substring(1)))
133                                 return this[(int) named_param_hash[parameterName.Substring(1)]];
134                         else
135                                 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
136                 }
137
138                 void SetParameter (int parameterIndex, SqliteParameter parameter)
139                 {
140                         if (this.Count >= parameterIndex+1)
141                                 numeric_param_list[parameterIndex] = parameter;
142                         else          
143                                 throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
144                 }
145
146                 void SetParameter (string parameterName, SqliteParameter parameter)
147                 {
148                         if (this.Contains(parameterName))
149                                 numeric_param_list[(int) named_param_hash[parameterName]] = parameter;
150                         else if (parameterName.Length > 1 && this.Contains(parameterName.Substring(1)))
151                                 numeric_param_list[(int) named_param_hash[parameterName.Substring(1)]] = parameter;
152                         else
153                                 throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
154                 }
155
156                 public SqliteParameter this[string parameterName] 
157                 {
158                         get { return GetParameter (parameterName); }
159                         set { SetParameter (parameterName, value); }
160                 }
161
162                 public SqliteParameter this[int parameterIndex]
163                 {
164                         get { return GetParameter (parameterIndex); }
165                         set { SetParameter (parameterIndex, value); }
166                 }
167
168                 public int Count {
169                         get { return this.numeric_param_list.Count; }
170                 }
171
172                 bool IList.IsFixedSize
173                 {
174                         get
175                         {
176                                 return this.numeric_param_list.IsFixedSize;
177                         }
178                 }
179
180                 bool IList.IsReadOnly {
181                         get { return this.numeric_param_list.IsReadOnly; }
182                 }
183
184
185                 bool ICollection.IsSynchronized {
186                         get { return this.numeric_param_list.IsSynchronized; }
187                 }               
188
189                 object ICollection.SyncRoot {
190                         get { return this.numeric_param_list.SyncRoot; }
191                 }
192
193                 #endregion
194
195                 #region Public Methods
196
197                 public int Add (object value)
198                 {
199                         CheckSqliteParam (value);
200                         SqliteParameter sqlp = value as SqliteParameter;
201                         if (named_param_hash.Contains (sqlp.ParameterName))
202                                 throw new DuplicateNameException ("Parameter collection already contains the a SqliteParameter with the given ParameterName.");
203                         named_param_hash[sqlp.ParameterName] = numeric_param_list.Add(value);
204                                 return (int) named_param_hash[sqlp.ParameterName];
205                 }
206
207                 public SqliteParameter Add (SqliteParameter param)
208                 {
209                         Add ((object)param);
210                         return param;
211                 }
212                 
213                 public SqliteParameter Add (string name, object value)
214                 {
215                         return Add (new SqliteParameter (name, value));
216                 }
217                 
218                 public SqliteParameter Add (string name, DbType type)
219                 {
220                         return Add (new SqliteParameter (name, type));
221                 }
222                 
223                 public void Clear ()
224                 {
225                         numeric_param_list.Clear ();
226                         named_param_hash.Clear ();
227                 }
228         
229                 public void CopyTo (Array array, int index)
230                 {
231                         this.numeric_param_list.CopyTo(array, index);
232                 }
233                 
234                 bool IList.Contains (object value)
235                 {
236                         return Contains ((SqliteParameter) value);
237                 }
238                 
239                 public bool Contains (string parameterName)
240                 {
241                         return named_param_hash.Contains (parameterName);
242                 }
243                 
244                 public bool Contains (SqliteParameter param)
245                 {
246                         return Contains (param.ParameterName);
247                 }
248                 
249                 public IEnumerator GetEnumerator ()
250                 {
251                         return this.numeric_param_list.GetEnumerator();
252                 }
253                 
254                 int IList.IndexOf (object param)
255                 {
256                         return IndexOf ((SqliteParameter) param);
257                 }
258                 
259                 public int IndexOf (string parameterName)
260                 {
261                         if (isPrefixed (parameterName)){
262                                 string sub = parameterName.Substring (1);
263                                 if (named_param_hash.Contains(sub))
264                                         return (int) named_param_hash [sub];
265                         }
266                         if (named_param_hash.Contains(parameterName))
267                                 return (int) named_param_hash[parameterName];
268                         else 
269                                 return -1;
270                 }
271                 
272                 public int IndexOf (SqliteParameter param)
273                 {
274                         return IndexOf (param.ParameterName);
275                 }
276                 
277                 public void Insert (int index, object value)
278                 {
279                         CheckSqliteParam (value);
280                         if (numeric_param_list.Count == index) 
281                         {
282                                 Add (value);
283                                 return;
284                         }
285                         
286                         numeric_param_list.Insert (index, value);
287                         RecreateNamedHash ();
288                 }
289                 
290                 public void Remove (object value)
291                 {
292                         CheckSqliteParam (value);
293                         RemoveAt ((SqliteParameter) value);
294                 }
295                 
296                 public void RemoveAt (int index)
297                 {
298                         RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
299                 }
300                 
301                 public void RemoveAt (string parameterName)
302                 {
303                         if (!named_param_hash.Contains (parameterName))
304                                 throw new ApplicationException ("Parameter " + parameterName + " not found");
305                         
306                         numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
307                         named_param_hash.Remove (parameterName);
308                         
309                         RecreateNamedHash ();
310                 }
311                 
312                 public void RemoveAt (SqliteParameter param)
313                 {
314                         RemoveAt (param.ParameterName);
315                 }
316
317                 #endregion
318         }
319 }
320 #endif