/********************************************************
* 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 System.Data.SQLite
{
using System;
using System.Data;
using System.Data.Common;
///
/// SQLite implementation of DbParameter.
///
public sealed class SQLiteParameter : DbParameter
{
///
/// The data type of the parameter
///
private int _dbType;
///
/// The version information for mapping the parameter
///
private DataRowVersion _rowVersion;
///
/// The value of the data in the parameter
///
private Object _objValue;
///
/// The source column for the parameter
///
private string _sourceColumn;
///
/// The column name
///
private string _columnName;
///
/// The data size, unused by SQLite
///
private int _dataSize;
///
/// Default constructor
///
public SQLiteParameter()
{
Initialize(null, -1, 0, null, DataRowVersion.Current);
}
///
/// Constructs a named parameter given the specified parameter name
///
/// The parameter name
public SQLiteParameter(string parameterName)
{
Initialize(parameterName, -1, 0, null, DataRowVersion.Current);
}
///
/// Constructs a named parameter given the specified parameter name and initial value
///
/// The parameter name
/// The initial value of the parameter
public SQLiteParameter(string parameterName, object value)
{
Initialize(parameterName, -1, 0, null, DataRowVersion.Current);
Value = value;
}
///
/// Constructs a named parameter of the specified type
///
/// The parameter name
/// The datatype of the parameter
public SQLiteParameter(string parameterName, DbType dbType)
{
Initialize(parameterName, (int)dbType, 0, null, DataRowVersion.Current);
}
///
/// Constructs a named parameter of the specified type and source column reference
///
/// The parameter name
/// The data type
/// The source column
public SQLiteParameter(string parameterName, DbType dbType, string sourceColumn)
{
Initialize(parameterName, (int)dbType, 0, sourceColumn, DataRowVersion.Current);
}
///
/// Constructs a named parameter of the specified type, source column and row version
///
/// The parameter name
/// The data type
/// The source column
/// The row version information
public SQLiteParameter(string parameterName, DbType dbType, string sourceColumn, DataRowVersion rowVersion)
{
Initialize(parameterName, (int)dbType, 0, sourceColumn, rowVersion);
}
///
/// Constructs an unnamed parameter of the specified data type
///
/// The datatype of the parameter
public SQLiteParameter(DbType dbType)
{
Initialize(null, (int)dbType, 0, null, DataRowVersion.Current);
}
///
/// Constructs an unnamed parameter of the specified data type and sets the initial value
///
/// The datatype of the parameter
/// The initial value of the parameter
public SQLiteParameter(DbType dbType, object value)
{
Initialize(null, (int)dbType, 0, null, DataRowVersion.Current);
Value = value;
}
///
/// Constructs an unnamed parameter of the specified data type and source column
///
/// The datatype of the parameter
/// The source column
public SQLiteParameter(DbType dbType, string sourceColumn)
{
Initialize(null, (int)dbType, 0, sourceColumn, DataRowVersion.Current);
}
///
/// Constructs an unnamed parameter of the specified data type, source column and row version
///
/// The data type
/// The source column
/// The row version information
public SQLiteParameter(DbType dbType, string sourceColumn, DataRowVersion rowVersion)
{
Initialize(null, (int)dbType, 0, sourceColumn, rowVersion);
}
///
/// Constructs a named parameter of the specified type and size
///
/// The parameter name
/// The data type
/// The size of the parameter
public SQLiteParameter(string parameterName, DbType parameterType, int parameterSize)
{
Initialize(parameterName, (int)parameterType, parameterSize, null, DataRowVersion.Current);
}
///
/// Constructs a named parameter of the specified type, size and source column
///
/// The name of the parameter
/// The data type
/// The size of the parameter
/// The source column
public SQLiteParameter(string parameterName, DbType parameterType, int parameterSize, string sourceColumn)
{
Initialize(parameterName, (int)parameterType, parameterSize, sourceColumn, DataRowVersion.Current);
}
///
/// Constructs a named parameter of the specified type, size, source column and row version
///
/// The name of the parameter
/// The data type
/// The size of the parameter
/// The source column
/// The row version information
public SQLiteParameter(string parameterName, DbType parameterType, int parameterSize, string sourceColumn, DataRowVersion rowVersion)
{
Initialize(parameterName, (int)parameterType, parameterSize, sourceColumn, rowVersion);
}
///
/// Constructs a named parameter of the specified type, size, source column and row version
///
/// The name of the parameter
/// The data type
/// The size of the parameter
/// Only input parameters are supported in SQLite
/// Ignored
/// Ignored
/// Ignored
/// The source column
/// The row version information
/// The initial value to assign the parameter
public SQLiteParameter(string parameterName, DbType parameterType, int parameterSize, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion rowVersion, object value)
{
Initialize(parameterName, (int)parameterType, parameterSize, sourceColumn, rowVersion);
Direction = direction;
IsNullable = isNullable;
Value = value;
}
///
/// Constructs an unnamed parameter of the specified type and size
///
/// The data type
/// The size of the parameter
public SQLiteParameter(DbType parameterType, int parameterSize)
{
Initialize(null, (int)parameterType, parameterSize, null, DataRowVersion.Current);
}
///
/// Constructs an unnamed parameter of the specified type, size, and source column
///
/// The data type
/// The size of the parameter
/// The source column
public SQLiteParameter(DbType parameterType, int parameterSize, string sourceColumn)
{
Initialize(null, (int)parameterType, parameterSize, sourceColumn, DataRowVersion.Current);
}
///
/// Constructs an unnamed parameter of the specified type, size, source column and row version
///
/// The data type
/// The size of the parameter
/// The source column
/// The row version information
public SQLiteParameter(DbType parameterType, int parameterSize, string sourceColumn, DataRowVersion rowVersion)
{
Initialize(null, (int)parameterType, parameterSize, sourceColumn, rowVersion);
}
///
/// Initializes the parameter member variables
///
/// The parameter name
/// The data type
/// The size
/// The source column
/// The row version
private void Initialize(string parameterName, int paramType, int nSize, string sourceColumn, DataRowVersion rowVersion)
{
_columnName = parameterName;
_dbType = paramType;
_sourceColumn = sourceColumn;
_rowVersion = rowVersion;
_objValue = null;
_dataSize = nSize;
}
///
/// Returns True.
///
public override bool IsNullable
{
get
{
return true;
}
set
{
}
}
///
/// Returns the datatype of the parameter
///
public override DbType DbType
{
get
{
if (_dbType == -1) return DbType.String; // Unassigned default value is String
return (DbType)_dbType;
}
set
{
_dbType = (int)value;
}
}
///
/// Supports only input parameters
///
public override ParameterDirection Direction
{
get
{
return ParameterDirection.Input;
}
set
{
if (value != ParameterDirection.Input)
throw new NotSupportedException();
}
}
///
/// Not implemented
///
public override int Offset
{
get
{
throw new NotImplementedException();
}
set
{
}
}
///
/// Returns the parameter name
///
public override string ParameterName
{
get
{
return _columnName;
}
set
{
_columnName = value;
}
}
///
/// Not implemented
///
public override void ResetDbType()
{
throw new NotImplementedException();
}
///
/// Returns the size of the parameter
///
public override int Size
{
get
{
return _dataSize;
}
set
{
_dataSize = value;
}
}
///
/// Gets/sets the source column
///
public override string SourceColumn
{
get
{
return _sourceColumn;
}
set
{
_sourceColumn = value;
}
}
///
/// Returns false, ignores any set value
///
public override bool SourceColumnNullMapping
{
get
{
return false;
}
set
{
}
}
///
/// Gets and sets the row version
///
public override DataRowVersion SourceVersion
{
get
{
return _rowVersion;
}
set
{
_rowVersion = value;
}
}
///
/// Gets and sets the parameter value. If no datatype was specified, the datatype will assume the type from the value given.
///
public override object Value
{
get
{
return _objValue;
}
set
{
_objValue = value;
if (_dbType == -1 && _objValue != null && _objValue != DBNull.Value) // If the DbType has never been assigned, try to glean one from the value's datatype
_dbType = (int)SQLiteConvert.TypeToDbType(_objValue.GetType());
}
}
}
}