📄 sqlitecommand.cs
字号:
namespace Imps.Client.Data
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public sealed class SQLiteCommand : Component, ICloneable
{
private WeakReference _activeReader;
private SQLiteConnection _cnn;
private string _commandText;
internal int _commandTimeout;
private bool _designTimeVisible;
private SQLiteParameterCollection _parameterCollection;
internal string _remainingText;
internal List<SQLiteStatement> _statementList;
private SQLiteTransaction _transaction;
private UpdateRowSource _updateRowSource;
public SQLiteCommand() : this(null, null)
{
}
private SQLiteCommand(SQLiteCommand source) : this(source.CommandText, source.Connection, source.Transaction)
{
this.CommandTimeout = source.CommandTimeout;
this.DesignTimeVisible = source.DesignTimeVisible;
this.UpdatedRowSource = source.UpdatedRowSource;
foreach (SQLiteParameter parameter in source._parameterCollection)
{
this.Parameters.Add(parameter.Clone());
}
}
public SQLiteCommand(SQLiteConnection connection) : this(null, connection, null)
{
}
public SQLiteCommand(string commandText) : this(commandText, null, null)
{
}
public SQLiteCommand(string commandText, SQLiteConnection connection) : this(commandText, connection, null)
{
}
public SQLiteCommand(string commandText, SQLiteConnection connection, SQLiteTransaction transaction)
{
this._statementList = null;
this._activeReader = null;
this._commandTimeout = 30;
this._parameterCollection = new SQLiteParameterCollection(this);
this._designTimeVisible = true;
this._updateRowSource = UpdateRowSource.FirstReturnedRecord;
this._transaction = null;
if (commandText != null)
{
this.CommandText = commandText;
}
if (connection != null)
{
this.DbConnection = connection;
}
if (transaction != null)
{
this.Transaction = transaction;
}
}
internal SQLiteStatement BuildNextCommand()
{
SQLiteStatement activeStatement = null;
SQLiteStatement statement2;
try
{
if (this._statementList == null)
{
this._remainingText = this._commandText;
}
activeStatement = this._cnn._sql.Prepare(this._remainingText, (this._statementList == null) ? null : this._statementList.get_Item(this._statementList.get_Count() - 1), out this._remainingText);
if (activeStatement != null)
{
activeStatement._command = this;
if (this._statementList == null)
{
this._statementList = new List<SQLiteStatement>();
}
this._statementList.Add(activeStatement);
this._parameterCollection.MapParameters(activeStatement);
activeStatement.BindParameters();
}
statement2 = activeStatement;
}
catch (Exception)
{
if (activeStatement != null)
{
if (this._statementList.Contains(activeStatement))
{
this._statementList.Remove(activeStatement);
}
activeStatement.Dispose();
}
this._remainingText = null;
throw;
}
return statement2;
}
public void Cancel()
{
}
internal void ClearCommands()
{
if (this._activeReader != null)
{
SQLiteDataReader reader = null;
try
{
reader = this._activeReader.Target as SQLiteDataReader;
}
catch
{
}
if (reader != null)
{
reader.Close();
}
this._activeReader = null;
}
if (this._statementList != null)
{
int num = this._statementList.get_Count();
for (int i = 0; i < num; i++)
{
this._statementList.get_Item(i).Dispose();
}
this._statementList = null;
this._parameterCollection.Unbind();
}
}
internal void ClearDataReader()
{
this._activeReader = null;
}
public object Clone()
{
return new SQLiteCommand(this);
}
public SQLiteParameter CreateParameter()
{
return new SQLiteParameter();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
SQLiteDataReader reader = null;
if (this._activeReader != null)
{
try
{
reader = this._activeReader.Target as SQLiteDataReader;
}
catch
{
}
}
if (reader != null)
{
reader._disposeCommand = true;
this._activeReader = null;
}
else
{
this.Connection = null;
this._parameterCollection.Clear();
this._commandText = null;
}
}
}
public int ExecuteNonQuery()
{
this.InitializeForReader();
int num = 0;
int index = 0;
while (true)
{
SQLiteStatement stmt = this.GetStatement(index);
index++;
if (stmt == null)
{
return num;
}
this._cnn._sql.Step(stmt);
num += this._cnn._sql.Changes;
this._cnn._sql.Reset(stmt);
}
}
public SQLiteDataReader ExecuteReader()
{
return this.ExecuteReader(CommandBehavior.Default);
}
public SQLiteDataReader ExecuteReader(CommandBehavior behavior)
{
this.InitializeForReader();
SQLiteDataReader target = new SQLiteDataReader(this, behavior);
this._activeReader = new WeakReference(target, false);
return target;
}
public object ExecuteScalar()
{
this.InitializeForReader();
int index = 0;
object obj2 = null;
SQLiteType typ = new SQLiteType();
while (true)
{
SQLiteStatement stmt = this.GetStatement(index);
index++;
if (stmt == null)
{
return obj2;
}
if (this._cnn._sql.Step(stmt) && (obj2 == null))
{
obj2 = this._cnn._sql.GetValue(stmt, 0, ref typ);
}
this._cnn._sql.Reset(stmt);
}
}
internal SQLiteStatement GetStatement(int index)
{
if (this._statementList == null)
{
return this.BuildNextCommand();
}
if (index == this._statementList.get_Count())
{
if (!string.IsNullOrEmpty(this._remainingText))
{
return this.BuildNextCommand();
}
return null;
}
SQLiteStatement statement = this._statementList.get_Item(index);
statement.BindParameters();
return statement;
}
private void InitializeForReader()
{
if ((this._activeReader != null) && this._activeReader.IsAlive)
{
throw new InvalidOperationException("DataReader already active on this command");
}
if (this._cnn == null)
{
throw new InvalidOperationException("No connection associated with this command");
}
if (this._cnn.State != ConnectionState.Open)
{
throw new InvalidOperationException("Database is not open");
}
this._parameterCollection.MapParameters(null);
this._cnn._sql.SetTimeout(this._commandTimeout * 0x3e8);
}
public void Prepare()
{
}
public string CommandText
{
get
{
return this._commandText;
}
set
{
if (this._commandText != value)
{
if ((this._activeReader != null) && this._activeReader.IsAlive)
{
throw new InvalidOperationException("Cannot set CommandText while a DataReader is active");
}
this.ClearCommands();
this._commandText = value;
if (this._cnn == null)
{
}
}
}
}
public int CommandTimeout
{
get
{
return this._commandTimeout;
}
set
{
this._commandTimeout = value;
}
}
public Imps.Client.Data.CommandType CommandType
{
get
{
return Imps.Client.Data.CommandType.Text;
}
set
{
if (value != Imps.Client.Data.CommandType.Text)
{
throw new NotSupportedException();
}
}
}
public SQLiteConnection Connection
{
get
{
return this._cnn;
}
set
{
if ((this._activeReader != null) && this._activeReader.IsAlive)
{
throw new InvalidOperationException("Cannot set Connection while a DataReader is active");
}
if (this._cnn != null)
{
this.ClearCommands();
this._cnn.RemoveCommand(this);
}
this._cnn = value;
if (this._cnn != null)
{
this._cnn.AddCommand(this);
}
}
}
private SQLiteConnection DbConnection
{
set
{
this.Connection = value;
}
}
public bool DesignTimeVisible
{
get
{
return this._designTimeVisible;
}
set
{
this._designTimeVisible = value;
}
}
public SQLiteParameterCollection Parameters
{
get
{
return this._parameterCollection;
}
}
public SQLiteTransaction Transaction
{
get
{
return this._transaction;
}
set
{
if (this._cnn != null)
{
if ((this._activeReader != null) && this._activeReader.IsAlive)
{
throw new InvalidOperationException("Cannot set Transaction while a DataReader is active");
}
if ((value != null) && (value._cnn != this._cnn))
{
throw new ArgumentException("Transaction is not associated with the command's connection");
}
this._transaction = value;
}
else if (value != null)
{
throw new ArgumentOutOfRangeException("SQLiteTransaction", "Not associated with a connection");
}
}
}
[DefaultValue(2)]
public UpdateRowSource UpdatedRowSource
{
get
{
return this._updateRowSource;
}
set
{
this._updateRowSource = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -