wasCSharpSQLite – Blame information for rev 4

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