forked from wwahammy/csharp-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteParameterCollection.cs
More file actions
300 lines (252 loc) · 8.59 KB
/
Copy pathSqliteParameterCollection.cs
File metadata and controls
300 lines (252 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// Community.CsharpSqlite.SQLiteClient.SqliteParameterCollection.cs
//
// Represents a collection of parameters relevant to a SqliteCommand as well as
// their respective mappings to columns in a DataSet.
//
//Author(s): Vladimir Vukicevic <vladimir@pobox.com>
// Everaldo Canuto <everaldo_canuto@yahoo.com.br>
// Chris Turchin <chris@turchin.net>
// Jeroen Zwartepoorte <jeroen@xs4all.nl>
// Thomas Zoechling <thomas.zoechling@gmx.at>
// Alex West <alxwest@gmail.com>
// Stewart Adcock <stewart.adcock@medit.fr>
//
// Copyright (C) 2002 Vladimir Vukicevic
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic ;
namespace Community.CsharpSqlite.SQLiteClient
{
public class SqliteParameterCollection : DbParameterCollection
{
#region Fields
List<SqliteParameter> numeric_param_list = new List<SqliteParameter>();
Dictionary<string, int> named_param_hash = new Dictionary<string, int>();
#endregion
#region Private Methods
private void CheckSqliteParam (object value)
{
if (!(value is SqliteParameter))
throw new InvalidCastException ("Can only use SqliteParameter objects");
SqliteParameter sqlp = value as SqliteParameter;
if (sqlp.ParameterName == null || sqlp.ParameterName.Length == 0)
sqlp.ParameterName = this.GenerateParameterName();
}
private void RecreateNamedHash ()
{
for (int i = 0; i < numeric_param_list.Count; i++)
{
named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
}
}
//FIXME: if the user is calling Insert at various locations with unnamed parameters, this is not going to work....
private string GenerateParameterName()
{
int index = this.Count + 1;
string name = String.Empty;
while (index > 0)
{
name = ":" + index.ToString();
if (this.IndexOf(name) == -1)
index = -1;
else
index++;
}
return name;
}
#endregion
#region Properties
private bool isPrefixed (string parameterName)
{
return parameterName.Length > 1 && (parameterName[0] == ':' || parameterName[0] == '$' || parameterName[0] == '@');
}
protected override DbParameter GetParameter (int parameterIndex)
{
if (this.Count >= parameterIndex+1)
return (SqliteParameter) numeric_param_list[parameterIndex];
else
throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
}
protected override DbParameter GetParameter (string parameterName)
{
if (this.Contains(parameterName))
return this[(int) named_param_hash[parameterName]];
else if (isPrefixed(parameterName) && this.Contains(parameterName.Substring(1)))
return this[(int) named_param_hash[parameterName.Substring(1)]];
else
throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
}
protected override void SetParameter (int parameterIndex, DbParameter parameter)
{
if (this.Count >= parameterIndex+1)
numeric_param_list[parameterIndex] = (SqliteParameter)parameter;
else
throw new IndexOutOfRangeException("The specified parameter index does not exist: " + parameterIndex.ToString());
}
protected override void SetParameter (string parameterName, DbParameter parameter)
{
if (this.Contains(parameterName))
numeric_param_list[(int)named_param_hash[parameterName]] = (SqliteParameter)parameter;
else if (parameterName.Length > 1 && this.Contains(parameterName.Substring(1)))
numeric_param_list[(int) named_param_hash[parameterName.Substring(1)]] = (SqliteParameter)parameter;
else
throw new IndexOutOfRangeException("The specified name does not exist: " + parameterName);
}
public override int Count
{
get
{
return this.numeric_param_list.Count;
}
}
public override bool IsSynchronized
{
get { return ((IList)this.numeric_param_list).IsSynchronized ; }
}
public override bool IsFixedSize
{
get { return ((IList)this.numeric_param_list).IsFixedSize; }
}
public override bool IsReadOnly
{
get { return ((IList)this.numeric_param_list).IsReadOnly; }
}
public override object SyncRoot
{
get { return ((IList)this.numeric_param_list).SyncRoot ; }
}
#endregion
#region Public Methods
public override void AddRange (Array values)
{
if (values == null || values.Length == 0)
return;
foreach (object value in values)
Add (value);
}
public override int Add (object value)
{
CheckSqliteParam (value);
SqliteParameter sqlp = value as SqliteParameter;
if (named_param_hash.ContainsKey(sqlp.ParameterName))
throw new DuplicateNameException ("Parameter collection already contains the a SqliteParameter with the given ParameterName.");
numeric_param_list.Add(sqlp);
named_param_hash.Add(sqlp.ParameterName, numeric_param_list.IndexOf(sqlp));
return (int) named_param_hash[sqlp.ParameterName];
}
public SqliteParameter Add (SqliteParameter param)
{
Add ((object)param);
return param;
}
public SqliteParameter Add (string name, object value)
{
return Add (new SqliteParameter (name, value));
}
public SqliteParameter Add (string name, DbType type)
{
return Add (new SqliteParameter (name, type));
}
public override void Clear ()
{
numeric_param_list.Clear ();
named_param_hash.Clear ();
}
public override void CopyTo (Array array, int index)
{
this.numeric_param_list.CopyTo((SqliteParameter[])array, index);
}
public override bool Contains (object value)
{
return Contains ((SqliteParameter) value);
}
public override bool Contains (string parameterName)
{
return named_param_hash.ContainsKey(parameterName);
}
public bool Contains (SqliteParameter param)
{
return Contains (param.ParameterName);
}
public override IEnumerator GetEnumerator()
{
return this.numeric_param_list.GetEnumerator();
}
public override int IndexOf (object param)
{
return IndexOf ((SqliteParameter) param);
}
public override int IndexOf (string parameterName)
{
if (isPrefixed (parameterName)){
string sub = parameterName.Substring (1);
if (named_param_hash.ContainsKey(sub))
return (int) named_param_hash [sub];
}
if (named_param_hash.ContainsKey(parameterName))
return (int) named_param_hash[parameterName];
else
return -1;
}
public int IndexOf (SqliteParameter param)
{
return IndexOf (param.ParameterName);
}
public override void Insert (int index, object value)
{
CheckSqliteParam (value);
if (numeric_param_list.Count == index)
{
Add (value);
return;
}
numeric_param_list.Insert(index,(SqliteParameter) value);
RecreateNamedHash ();
}
public override void Remove (object value)
{
CheckSqliteParam (value);
RemoveAt ((SqliteParameter) value);
}
public override void RemoveAt (int index)
{
RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
}
public override void RemoveAt (string parameterName)
{
if (!named_param_hash.ContainsKey (parameterName))
throw new ApplicationException ("Parameter " + parameterName + " not found");
numeric_param_list.RemoveAt((int) named_param_hash[parameterName]);
named_param_hash.Remove (parameterName);
RecreateNamedHash ();
}
public void RemoveAt (SqliteParameter param)
{
RemoveAt (param.ParameterName);
}
#endregion
}
}