2 // Mono.Data.Sqlite.SQLiteParameterCollection.cs
5 // Robert Simpson (robert@blackcastlesoft.com)
7 // Adapted and modified for the Mono Project by
8 // Marek Habersack (grendello@gmail.com)
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 /********************************************************
35 * ADO.NET 2.0 Data Provider for Sqlite Version 3.X
36 * Written by Robert Simpson (robert@blackcastlesoft.com)
38 * Released to the public domain, use at your own risk!
39 ********************************************************/
41 namespace Mono.Data.Sqlite
45 using System.Data.Common;
46 using System.Collections.Generic;
47 using System.Globalization;
48 using System.ComponentModel;
49 using System.Reflection;
52 /// Sqlite implementation of DbParameterCollection.
54 #if !PLATFORM_COMPACTFRAMEWORK
55 [Editor("Microsoft.VSDesigner.Data.Design.DBParametersEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ListBindable(false)]
57 public class SqliteParameterCollection : DbParameterCollection
60 /// The underlying command to which this collection belongs
62 private SqliteCommand _command;
64 /// The internal array of parameters in this collection
66 private List<SqliteParameter> _parameterList;
68 /// Determines whether or not all parameters have been bound to their statement(s)
70 private bool _unboundFlag;
73 /// Initializes the collection
75 /// <param name="cmd">The command to which the collection belongs</param>
76 internal SqliteParameterCollection(SqliteCommand cmd)
79 _parameterList = new List<SqliteParameter>();
86 public override bool IsSynchronized
94 public override bool IsFixedSize
102 public override bool IsReadOnly
104 get { return false; }
110 public override object SyncRoot
116 /// Retrieves an enumerator for the collection
118 /// <returns>An enumerator for the underlying array</returns>
119 public override System.Collections.IEnumerator GetEnumerator()
121 return _parameterList.GetEnumerator();
125 /// Adds a parameter to the collection
127 /// <param name="parameterName">The parameter name</param>
128 /// <param name="parameterType">The data type</param>
129 /// <param name="parameterSize">The size of the value</param>
130 /// <param name="sourceColumn">The source column</param>
131 /// <returns>A SqliteParameter object</returns>
132 public SqliteParameter Add(string parameterName, DbType parameterType, int parameterSize, string sourceColumn)
134 SqliteParameter param = new SqliteParameter(parameterName, parameterType, parameterSize, sourceColumn);
141 /// Adds a parameter to the collection
143 /// <param name="parameterName">The parameter name</param>
144 /// <param name="parameterType">The data type</param>
145 /// <param name="parameterSize">The size of the value</param>
146 /// <returns>A SqliteParameter object</returns>
147 public SqliteParameter Add(string parameterName, DbType parameterType, int parameterSize)
149 SqliteParameter param = new SqliteParameter(parameterName, parameterType, parameterSize);
156 /// Adds a parameter to the collection
158 /// <param name="parameterName">The parameter name</param>
159 /// <param name="parameterType">The data type</param>
160 /// <returns>A SqliteParameter object</returns>
161 public SqliteParameter Add(string parameterName, DbType parameterType)
163 SqliteParameter param = new SqliteParameter(parameterName, parameterType);
170 /// Adds a parameter to the collection
172 /// <param name="parameter">The parameter to add</param>
173 /// <returns>A zero-based index of where the parameter is located in the array</returns>
174 public int Add(SqliteParameter parameter)
178 if (parameter.ParameterName != null)
180 n = IndexOf(parameter.ParameterName);
185 n = _parameterList.Count;
186 _parameterList.Add((SqliteParameter)parameter);
189 SetParameter(n, parameter);
195 /// Adds a parameter to the collection
197 /// <param name="value">The parameter to add</param>
198 /// <returns>A zero-based index of where the parameter is located in the array</returns>
199 #if !PLATFORM_COMPACTFRAMEWORK
200 [EditorBrowsable(EditorBrowsableState.Never)]
202 public override int Add(object value)
204 return Add((SqliteParameter)value);
208 /// Adds a named/unnamed parameter and its value to the parameter collection.
210 /// <param name="parameterName">Name of the parameter, or null to indicate an unnamed parameter</param>
211 /// <param name="value">The initial value of the parameter</param>
212 /// <returns>Returns the SqliteParameter object created during the call.</returns>
213 public SqliteParameter AddWithValue(string parameterName, object value)
215 SqliteParameter param = new SqliteParameter(parameterName, value);
222 /// Adds an array of parameters to the collection
224 /// <param name="values">The array of parameters to add</param>
225 public void AddRange(SqliteParameter[] values)
227 int x = values.Length;
228 for (int n = 0; n < x; n++)
233 /// Adds an array of parameters to the collection
235 /// <param name="values">The array of parameters to add</param>
236 public override void AddRange(Array values)
238 int x = values.Length;
239 for (int n = 0; n < x; n++)
240 Add((SqliteParameter)(values.GetValue(n)));
244 /// Clears the array and resets the collection
246 public override void Clear()
249 _parameterList.Clear();
253 /// Determines if the named parameter exists in the collection
255 /// <param name="parameterName">The name of the parameter to check</param>
256 /// <returns>True if the parameter is in the collection</returns>
257 public override bool Contains(string parameterName)
259 return (IndexOf(parameterName) != -1);
263 /// Determines if the parameter exists in the collection
265 /// <param name="value">The SqliteParameter to check</param>
266 /// <returns>True if the parameter is in the collection</returns>
267 public override bool Contains(object value)
269 return _parameterList.Contains((SqliteParameter)value);
275 /// <param name="array"></param>
276 /// <param name="index"></param>
277 public override void CopyTo(Array array, int index)
279 throw new NotImplementedException();
283 /// Returns a count of parameters in the collection
285 public override int Count
287 get { return _parameterList.Count; }
291 /// Overloaded to specialize the return value of the default indexer
293 /// <param name="parameterName">Name of the parameter to get/set</param>
294 /// <returns>The specified named Sqlite parameter</returns>
295 public new SqliteParameter this[string parameterName]
299 return (SqliteParameter)GetParameter(parameterName);
303 SetParameter(parameterName, value);
308 /// Overloaded to specialize the return value of the default indexer
310 /// <param name="index">The index of the parameter to get/set</param>
311 /// <returns>The specified Sqlite parameter</returns>
312 public new SqliteParameter this[int index]
316 return (SqliteParameter)GetParameter(index);
320 SetParameter(index, value);
324 /// Retrieve a parameter by name from the collection
326 /// <param name="parameterName">The name of the parameter to fetch</param>
327 /// <returns>A DbParameter object</returns>
328 protected override DbParameter GetParameter(string parameterName)
330 return GetParameter(IndexOf(parameterName));
334 /// Retrieves a parameter by its index in the collection
336 /// <param name="index">The index of the parameter to retrieve</param>
337 /// <returns>A DbParameter object</returns>
338 protected override DbParameter GetParameter(int index)
340 return _parameterList[index];
344 /// Returns the index of a parameter given its name
346 /// <param name="parameterName">The name of the parameter to find</param>
347 /// <returns>-1 if not found, otherwise a zero-based index of the parameter</returns>
348 public override int IndexOf(string parameterName)
350 int x = _parameterList.Count;
351 for (int n = 0; n < x; n++)
353 if (String.Compare(parameterName, _parameterList[n].ParameterName, true, CultureInfo.InvariantCulture) == 0)
360 /// Returns the index of a parameter
362 /// <param name="value">The parameter to find</param>
363 /// <returns>-1 if not found, otherwise a zero-based index of the parameter</returns>
364 public override int IndexOf(object value)
366 return _parameterList.IndexOf((SqliteParameter)value);
370 /// Inserts a parameter into the array at the specified location
372 /// <param name="index">The zero-based index to insert the parameter at</param>
373 /// <param name="value">The parameter to insert</param>
374 public override void Insert(int index, object value)
377 _parameterList.Insert(index, (SqliteParameter)value);
381 /// Removes a parameter from the collection
383 /// <param name="value">The parameter to remove</param>
384 public override void Remove(object value)
387 _parameterList.Remove((SqliteParameter)value);
391 /// Removes a parameter from the collection given its name
393 /// <param name="parameterName">The name of the parameter to remove</param>
394 public override void RemoveAt(string parameterName)
396 RemoveAt(IndexOf(parameterName));
400 /// Removes a parameter from the collection given its index
402 /// <param name="index">The zero-based parameter index to remove</param>
403 public override void RemoveAt(int index)
406 _parameterList.RemoveAt(index);
410 /// Re-assign the named parameter to a new parameter object
412 /// <param name="parameterName">The name of the parameter to replace</param>
413 /// <param name="value">The new parameter</param>
414 protected override void SetParameter(string parameterName, DbParameter value)
416 SetParameter(IndexOf(parameterName), value);
420 /// Re-assign a parameter at the specified index
422 /// <param name="index">The zero-based index of the parameter to replace</param>
423 /// <param name="value">The new parameter</param>
424 protected override void SetParameter(int index, DbParameter value)
427 _parameterList[index] = (SqliteParameter)value;
431 /// Un-binds all parameters from their statements
433 internal void Unbind()
439 /// This function attempts to map all parameters in the collection to all statements in a Command.
440 /// Since named parameters may span multiple statements, this function makes sure all statements are bound
441 /// to the same named parameter. Unnamed parameters are bound in sequence.
443 internal void MapParameters(SqliteStatement activeStatement)
445 if (_unboundFlag == false || _parameterList.Count == 0 || _command._statementList == null) return;
451 SqliteStatement stmt;
453 foreach(SqliteParameter p in _parameterList)
459 s = String.Format(CultureInfo.InvariantCulture, ";{0}", nUnnamed);
464 bool isMapped = false;
466 if (activeStatement == null)
467 x = _command._statementList.Count;
471 stmt = activeStatement;
472 for (n = 0; n < x; n++)
475 if (stmt == null) stmt = _command._statementList[n];
476 if (stmt._paramNames != null)
478 if (stmt.MapParameter(s, p) == true)
484 // If the parameter has a name, but the SQL statement uses unnamed references, this can happen -- attempt to map
485 // the parameter by its index in the collection
486 if (isMapped == false)
488 s = String.Format(CultureInfo.InvariantCulture, ";{0}", y);
490 stmt = activeStatement;
491 for (n = 0; n < x; n++)
493 if (stmt == null) stmt = _command._statementList[n];
494 if (stmt._paramNames != null)
496 if (stmt.MapParameter(s, p) == true)
503 if (activeStatement == null) _unboundFlag = false;