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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace Mono.Data.Sqlite
{
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Globalization;
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// SQLite implementation of DbParameterCollection.
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[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)]
#endif
public sealed class SqliteParameterCollection : DbParameterCollection
{
/// <summary>
/// The underlying command to which this collection belongs
/// </summary>
private SqliteCommand _command;
/// <summary>
/// The internal array of parameters in this collection
/// </summary>
private List<SqliteParameter> _parameterList;
/// <summary>
/// Determines whether or not all parameters have been bound to their statement(s)
/// </summary>
private bool _unboundFlag;
/// <summary>
/// Initializes the collection
/// </summary>
/// <param name="cmd">The command to which the collection belongs</param>
internal SqliteParameterCollection(SqliteCommand cmd)
{
_command = cmd;
_parameterList = new List<SqliteParameter>();
_unboundFlag = true;
}
/// <summary>
/// Returns true
/// </summary>
public override bool IsSynchronized
{
get { return true; }
}
/// <summary>
/// Returns false
/// </summary>
public override bool IsFixedSize
{
get { return false; }
}
/// <summary>
/// Returns false
/// </summary>
public override bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Returns null
/// </summary>
public override object SyncRoot
{
get { return null; }
}
/// <summary>
/// Retrieves an enumerator for the collection
/// </summary>
/// <returns>An enumerator for the underlying array</returns>
public override System.Collections.IEnumerator GetEnumerator()
{
return _parameterList.GetEnumerator();
}
/// <summary>
/// Adds a parameter to the collection
/// </summary>
/// <param name="parameterName">The parameter name</param>
/// <param name="parameterType">The data type</param>
/// <param name="parameterSize">The size of the value</param>
/// <param name="sourceColumn">The source column</param>
/// <returns>A SqliteParameter object</returns>
public SqliteParameter Add(string parameterName, DbType parameterType, int parameterSize, string sourceColumn)
{
SqliteParameter param = new SqliteParameter(parameterName, parameterType, parameterSize, sourceColumn);
Add(param);
return param;
}
/// <summary>
/// Adds a parameter to the collection
/// </summary>
/// <param name="parameterName">The parameter name</param>
/// <param name="parameterType">The data type</param>
/// <param name="parameterSize">The size of the value</param>
/// <returns>A SqliteParameter object</returns>
public SqliteParameter Add(string parameterName, DbType parameterType, int parameterSize)
{
SqliteParameter param = new SqliteParameter(parameterName, parameterType, parameterSize);
Add(param);
return param;
}
/// <summary>
/// Adds a parameter to the collection
/// </summary>
/// <param name="parameterName">The parameter name</param>
/// <param name="parameterType">The data type</param>
/// <returns>A SqliteParameter object</returns>
public SqliteParameter Add(string parameterName, DbType parameterType)
{
SqliteParameter param = new SqliteParameter(parameterName, parameterType);
Add(param);
return param;
}
/// <summary>
/// Adds a parameter to the collection
/// </summary>
/// <param name="parameter">The parameter to add</param>
/// <returns>A zero-based index of where the parameter is located in the array</returns>
public int Add(SqliteParameter parameter)
{
int n = -1;
if (String.IsNullOrEmpty(parameter.ParameterName) == false)
{
n = IndexOf(parameter.ParameterName);
}
if (n == -1)
{
n = _parameterList.Count;
_parameterList.Add((SqliteParameter)parameter);
}
SetParameter(n, parameter);
return n;
}
/// <summary>
/// Adds a parameter to the collection
/// </summary>
/// <param name="value">The parameter to add</param>
/// <returns>A zero-based index of where the parameter is located in the array</returns>
#if !PLATFORM_COMPACTFRAMEWORK
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public override int Add(object value)
{
return Add((SqliteParameter)value);
}
/// <summary>
/// Adds a named/unnamed parameter and its value to the parameter collection.
/// </summary>
/// <param name="parameterName">Name of the parameter, or null to indicate an unnamed parameter</param>
/// <param name="value">The initial value of the parameter</param>
/// <returns>Returns the SqliteParameter object created during the call.</returns>
public SqliteParameter AddWithValue(string parameterName, object value)
{
SqliteParameter param = new SqliteParameter(parameterName, value);
Add(param);
return param;
}
/// <summary>
/// Adds an array of parameters to the collection
/// </summary>
/// <param name="values">The array of parameters to add</param>
public void AddRange(SqliteParameter[] values)
{
int x = values.Length;
for (int n = 0; n < x; n++)
Add(values[n]);
}
/// <summary>
/// Adds an array of parameters to the collection
/// </summary>
/// <param name="values">The array of parameters to add</param>
public override void AddRange(Array values)
{
int x = values.Length;
for (int n = 0; n < x; n++)
Add((SqliteParameter)(values.GetValue(n)));
}
/// <summary>
/// Clears the array and resets the collection
/// </summary>
public override void Clear()
{
_unboundFlag = true;
_parameterList.Clear();
}
/// <summary>
/// Determines if the named parameter exists in the collection
/// </summary>
/// <param name="parameterName">The name of the parameter to check</param>
/// <returns>True if the parameter is in the collection</returns>
public override bool Contains(string parameterName)
{
return (IndexOf(parameterName) != -1);
}
/// <summary>
/// Determines if the parameter exists in the collection
/// </summary>
/// <param name="value">The SqliteParameter to check</param>
/// <returns>True if the parameter is in the collection</returns>
public override bool Contains(object value)
{
return _parameterList.Contains((SqliteParameter)value);
}
/// <summary>
/// Not implemented
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
public override void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns a count of parameters in the collection
/// </summary>
public override int Count
{
get { return _parameterList.Count; }
}
/// <summary>
/// Overloaded to specialize the return value of the default indexer
/// </summary>
/// <param name="parameterName">Name of the parameter to get/set</param>
/// <returns>The specified named SQLite parameter</returns>
public new SqliteParameter this[string parameterName]
{
get
{
return (SqliteParameter)GetParameter(parameterName);
}
set
{
SetParameter(parameterName, value);
}
}
/// <summary>
/// Overloaded to specialize the return value of the default indexer
/// </summary>
/// <param name="index">The index of the parameter to get/set</param>
/// <returns>The specified SQLite parameter</returns>
public new SqliteParameter this[int index]
{
get
{
return (SqliteParameter)GetParameter(index);
}
set
{
SetParameter(index, value);
}
}
/// <summary>
/// Retrieve a parameter by name from the collection
/// </summary>
/// <param name="parameterName">The name of the parameter to fetch</param>
/// <returns>A DbParameter object</returns>
protected override DbParameter GetParameter(string parameterName)
{
return GetParameter(IndexOf(parameterName));
}
/// <summary>
/// Retrieves a parameter by its index in the collection
/// </summary>
/// <param name="index">The index of the parameter to retrieve</param>
/// <returns>A DbParameter object</returns>
protected override DbParameter GetParameter(int index)
{
return _parameterList[index];
}
/// <summary>
/// Returns the index of a parameter given its name
/// </summary>
/// <param name="parameterName">The name of the parameter to find</param>
/// <returns>-1 if not found, otherwise a zero-based index of the parameter</returns>
public override int IndexOf(string parameterName)
{
int x = _parameterList.Count;
for (int n = 0; n < x; n++)
{
if (String.Compare(parameterName, _parameterList[n].ParameterName, true, CultureInfo.InvariantCulture) == 0)
return n;
}
return -1;
}
/// <summary>
/// Returns the index of a parameter
/// </summary>
/// <param name="value">The parameter to find</param>
/// <returns>-1 if not found, otherwise a zero-based index of the parameter</returns>
public override int IndexOf(object value)
{
return _parameterList.IndexOf((SqliteParameter)value);
}
/// <summary>
/// Inserts a parameter into the array at the specified location
/// </summary>
/// <param name="index">The zero-based index to insert the parameter at</param>
/// <param name="value">The parameter to insert</param>
public override void Insert(int index, object value)
{
_unboundFlag = true;
_parameterList.Insert(index, (SqliteParameter)value);
}
/// <summary>
/// Removes a parameter from the collection
/// </summary>
/// <param name="value">The parameter to remove</param>
public override void Remove(object value)
{
_unboundFlag = true;
_parameterList.Remove((SqliteParameter)value);
}
/// <summary>
/// Removes a parameter from the collection given its name
/// </summary>
/// <param name="parameterName">The name of the parameter to remove</param>
public override void RemoveAt(string parameterName)
{
RemoveAt(IndexOf(parameterName));
}
/// <summary>
/// Removes a parameter from the collection given its index
/// </summary>
/// <param name="index">The zero-based parameter index to remove</param>
public override void RemoveAt(int index)
{
_unboundFlag = true;
_parameterList.RemoveAt(index);
}
/// <summary>
/// Re-assign the named parameter to a new parameter object
/// </summary>
/// <param name="parameterName">The name of the parameter to replace</param>
/// <param name="value">The new parameter</param>
protected override void SetParameter(string parameterName, DbParameter value)
{
SetParameter(IndexOf(parameterName), value);
}
/// <summary>
/// Re-assign a parameter at the specified index
/// </summary>
/// <param name="index">The zero-based index of the parameter to replace</param>
/// <param name="value">The new parameter</param>
protected override void SetParameter(int index, DbParameter value)
{
_unboundFlag = true;
_parameterList[index] = (SqliteParameter)value;
}
/// <summary>
/// Un-binds all parameters from their statements
/// </summary>
internal void Unbind()
{
_unboundFlag = true;
}
/// <summary>
/// This function attempts to map all parameters in the collection to all statements in a Command.
/// Since named parameters may span multiple statements, this function makes sure all statements are bound
/// to the same named parameter. Unnamed parameters are bound in sequence.
/// </summary>
internal void MapParameters(SqliteStatement activeStatement)
{
if (_unboundFlag == false || _parameterList.Count == 0 || _command._statementList == null) return;
int nUnnamed = 0;
string s;
int n;
int y = -1;
SqliteStatement stmt;
foreach(SqliteParameter p in _parameterList)
{
y ++;
s = p.ParameterName;
if (s == null)
{
s = String.Format(CultureInfo.InvariantCulture, ";{0}", nUnnamed);
nUnnamed++;
}
int x;
bool isMapped = false;
if (activeStatement == null)
x = _command._statementList.Count;
else
x = 1;
stmt = activeStatement;
for (n = 0; n < x; n++)
{
isMapped = false;
if (stmt == null) stmt = _command._statementList[n];
if (stmt._paramNames != null)
{
if (stmt.MapParameter(s, p) == true)
isMapped = true;
}
stmt = null;
}
// If the parameter has a name, but the SQL statement uses unnamed references, this can happen -- attempt to map
// the parameter by its index in the collection
if (isMapped == false)
{
s = String.Format(CultureInfo.InvariantCulture, ";{0}", y);
stmt = activeStatement;
for (n = 0; n < x; n++)
{
if (stmt == null) stmt = _command._statementList[n];
if (stmt._paramNames != null)
{
if (stmt.MapParameter(s, p) == true)
isMapped = true;
}
stmt = null;
}
}
}
if (activeStatement == null) _unboundFlag = false;
}
}
}
|