2
// Mono.Data.SqliteClient.SqliteParameterCollection.cs
4
// Represents a collection of parameters relevant to a SqliteCommand as well as
5
// their respective mappings to columns in a DataSet.
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>
13
// Copyright (C) 2002 Vladimir Vukicevic
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:
23
// The above copyright notice and this permission notice shall be
24
// included in all copies or substantial portions of the Software.
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.
37
using System.Collections;
39
namespace Mono.Data.SqliteClient
41
public class SqliteParameterCollection : IDataParameterCollection, IList
46
ArrayList numeric_param_list = new ArrayList();
47
Hashtable named_param_hash = new Hashtable();
51
#region Private Methods
53
private void CheckSqliteParam (object value)
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();
62
private void RecreateNamedHash ()
64
for (int i = 0; i < numeric_param_list.Count; i++)
66
named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
70
//FIXME: if the user is calling Insert at various locations with unnamed parameters, this is not going to work....
71
private string GenerateParameterName()
73
int index = this.Count + 1;
74
string name = String.Empty;
78
name = ":" + index.ToString();
79
if (this.IndexOf(name) == -1)
91
object IList.this[int index] {
98
CheckSqliteParam (value);
99
this[index] = (SqliteParameter) value;
103
object IDataParameterCollection.this[string parameterName] {
106
return this[parameterName];
110
CheckSqliteParam (value);
111
this[parameterName] = (SqliteParameter) value;
115
private bool isPrefixed (string parameterName)
117
return parameterName.Length > 1 && (parameterName[0] == ':' || parameterName[0] == '$');
120
public SqliteParameter this[string parameterName]
124
if (this.Contains(parameterName))
125
return this[(int) named_param_hash[parameterName]];
126
else if (isPrefixed(parameterName) && this.Contains(parameterName.Substring(1)))
127
return this[(int) named_param_hash[parameterName.Substring(1)]];
129
throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
133
if (this.Contains(parameterName))
134
numeric_param_list[(int) named_param_hash[parameterName]] = value;
135
else if (parameterName.Length > 1 && this.Contains(parameterName.Substring(1)))
136
numeric_param_list[(int) named_param_hash[parameterName.Substring(1)]] = value;
138
throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
142
public SqliteParameter this[int parameterIndex]
146
if (this.Count >= parameterIndex+1)
147
return (SqliteParameter) numeric_param_list[parameterIndex];
149
throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
153
if (this.Count >= parameterIndex+1)
154
numeric_param_list[parameterIndex] = value;
156
throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
165
return this.numeric_param_list.Count;
169
bool IList.IsFixedSize
173
return this.numeric_param_list.IsFixedSize;
177
bool IList.IsReadOnly
181
return this.numeric_param_list.IsReadOnly;
186
bool ICollection.IsSynchronized
190
return this.numeric_param_list.IsSynchronized;
195
object ICollection.SyncRoot
199
return this.numeric_param_list.SyncRoot;
205
#region Public Methods
207
public int Add (object value)
209
CheckSqliteParam (value);
210
SqliteParameter sqlp = value as SqliteParameter;
211
if (named_param_hash.Contains (sqlp.ParameterName))
212
throw new DuplicateNameException ("Parameter collection already contains the a SqliteParameter with the given ParameterName.");
213
named_param_hash[sqlp.ParameterName] = numeric_param_list.Add(value);
214
return (int) named_param_hash[sqlp.ParameterName];
217
public SqliteParameter Add (SqliteParameter param)
223
public SqliteParameter Add (string name, object value)
225
return Add (new SqliteParameter (name, value));
228
public SqliteParameter Add (string name, DbType type)
230
return Add (new SqliteParameter (name, type));
235
numeric_param_list.Clear ();
236
named_param_hash.Clear ();
239
public void CopyTo (Array array, int index)
241
this.numeric_param_list.CopyTo(array, index);
244
bool IList.Contains (object value)
246
return Contains ((SqliteParameter) value);
249
public bool Contains (string parameterName)
251
return named_param_hash.Contains (parameterName);
254
public bool Contains (SqliteParameter param)
256
return Contains (param.ParameterName);
259
public IEnumerator GetEnumerator ()
261
return this.numeric_param_list.GetEnumerator();
264
int IList.IndexOf (object param)
266
return IndexOf ((SqliteParameter) param);
269
public int IndexOf (string parameterName)
271
if (named_param_hash.Contains(parameterName))
272
return (int) named_param_hash[parameterName];
277
public int IndexOf (SqliteParameter param)
279
return IndexOf (param.ParameterName);
282
public void Insert (int index, object value)
284
CheckSqliteParam (value);
285
if (numeric_param_list.Count == index)
291
numeric_param_list.Insert (index, value);
292
RecreateNamedHash ();
295
public void Remove (object value)
297
CheckSqliteParam (value);
298
RemoveAt ((SqliteParameter) value);
301
public void RemoveAt (int index)
303
RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
306
public void RemoveAt (string parameterName)
308
if (!named_param_hash.Contains (parameterName))
309
throw new ApplicationException ("Parameter " + parameterName + " not found");
311
numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
312
named_param_hash.Remove (parameterName);
314
RecreateNamedHash ();
317
public void RemoveAt (SqliteParameter param)
319
RemoveAt (param.ParameterName);