?? sqliteconnection.cs
字號:
?/********************************************************
* 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;
using System.Collections.Generic;
using System.Globalization;
using System.ComponentModel;
/// <summary>
/// SQLite implentation of DbConnection.
/// </summary>
/// <remarks>
/// The <see cref="ConnectionString">ConnectionString</see> property of the SQLiteConnection class can contain the following parameter(s), delimited with a semi-colon:
/// <list type="table">
/// <listheader>
/// <term>Parameter</term>
/// <term>Values</term>
/// <term>Required</term>
/// <term>Default</term>
/// </listheader>
/// <item>
/// <description>Data Source</description>
/// <description>{filename}</description>
/// <description>Y</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Version</description>
/// <description>3</description>
/// <description>N</description>
/// <description>3</description>
/// </item>
/// <item>
/// <description>UseUTF16Encoding</description>
/// <description><b>True</b><br/><b>False</b></description>
/// <description>N</description>
/// <description>False</description>
/// </item>
/// <item>
/// <description>DateTimeFormat</description>
/// <description><b>Ticks</b> - Use DateTime.Ticks<br/><b>ISO8601</b> - Use ISO8601 DateTime format</description>
/// <description>N</description>
/// <description>ISO8601</description>
/// </item>
/// <item>
/// <description>BinaryGUID</description>
/// <description><b>True</b> - Store GUID columns in binary form<br/><b>False</b> - Store GUID columns as text</description>
/// <description>N</description>
/// <description>True</description>
/// </item>
/// <item>
/// <description>Cache Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>2000</description>
/// </item>
/// <item>
/// <description>Synchronous</description>
/// <description><b>Normal</b> - Normal file flushing behavior<br/><b>Full</b> - Full flushing after all writes<br/><b>Off</b> - Underlying OS flushes I/O's</description>
/// <description>N</description>
/// <description>Normal</description>
/// </item>
/// <item>
/// <description>Page Size</description>
/// <description>{size in bytes}</description>
/// <description>N</description>
/// <description>1024</description>
/// </item>
/// <item>
/// <description>Password</description>
/// <description>{password}</description>
/// <description>N</description>
/// <description></description>
/// </item>
/// <item>
/// <description>Enlist</description>
/// <description><B>Y</B> - Automatically enlist in distributed transactions<br/><b>N</b> - No automatic enlistment</description>
/// <description>N</description>
/// <description>Y</description>
/// </item>
/// </list>
/// </remarks>
public sealed class SQLiteConnection : DbConnection, ICloneable
{
/// <summary>
/// State of the current connection
/// </summary>
private ConnectionState _connectionState;
/// <summary>
/// The connection string
/// </summary>
private string _connectionString;
/// <summary>
/// Nesting level of the transactions open on the connection
/// </summary>
internal int _transactionLevel;
#if !PLATFORM_COMPACTFRAMEWORK
/// <summary>
/// Whether or not the connection is enlisted in a distrubuted transaction
/// </summary>
internal SQLiteEnlistment _enlistment;
#endif
/// <summary>
/// The base SQLite object to interop with
/// </summary>
internal SQLiteBase _sql;
/// <summary>
/// Commands associated with this connection
/// </summary>
internal List<SQLiteCommand> _commandList;
/// <summary>
/// The database filename minus path and extension
/// </summary>
private string _dataSource;
/// <summary>
/// Temporary password storage, emptied after the database has been opened
/// </summary>
private byte[] _password;
internal bool _binaryGuid;
internal long _version;
private event SQLiteUpdateEventHandler _updateHandler;
private event SQLiteCommitHandler _commitHandler;
private event EventHandler _rollbackHandler;
private SQLiteUpdateCallback _updateCallback;
private SQLiteCommitCallback _commitCallback;
private SQLiteRollbackCallback _rollbackCallback;
/// <summary>
/// This event is raised whenever the database is opened or closed.
/// </summary>
public override event StateChangeEventHandler StateChange;
/// <summary>
/// This event is raised whenever SQLite makes an update/delete/insert into the database on
/// this connection. It only applies to the given connection.
/// </summary>
public event SQLiteUpdateEventHandler Update
{
add
{
if (_updateHandler == null)
{
_updateCallback = new SQLiteUpdateCallback(UpdateCallback);
_sql.SetUpdateHook(_updateCallback);
}
_updateHandler += value;
}
remove
{
_updateHandler -= value;
if (_updateHandler == null)
{
_sql.SetUpdateHook(null);
_updateCallback = null;
}
}
}
private void UpdateCallback(int type, IntPtr database, int databaseLen, IntPtr table, int tableLen, Int64 rowid)
{
_updateHandler(this, new UpdateEventArgs(
_sql.UTF8ToString(database, databaseLen),
_sql.UTF8ToString(table, tableLen),
(UpdateEventType)type,
rowid));
}
/// <summary>
/// This event is raised whenever SQLite is committing a transaction.
/// Return non-zero to trigger a rollback
/// </summary>
public event SQLiteCommitHandler Commit
{
add
{
if (_commitHandler == null)
{
_commitCallback = new SQLiteCommitCallback(CommitCallback);
_sql.SetCommitHook(_commitCallback);
}
_commitHandler += value;
}
remove
{
_commitHandler -= value;
if (_commitHandler == null)
{
_sql.SetCommitHook(null);
_commitCallback = null;
}
}
}
/// <summary>
/// This event is raised whenever SQLite is committing a transaction.
/// Return non-zero to trigger a rollback
/// </summary>
public event EventHandler RollBack
{
add
{
if (_rollbackHandler == null)
{
_rollbackCallback = new SQLiteRollbackCallback(RollbackCallback);
_sql.SetRollbackHook(_rollbackCallback);
}
_rollbackHandler += value;
}
remove
{
_rollbackHandler -= value;
if (_rollbackHandler == null)
{
_sql.SetRollbackHook(null);
_rollbackCallback = null;
}
}
}
private int CommitCallback()
{
CommitEventArgs e = new CommitEventArgs();
_commitHandler(this, e);
return (e.AbortTransaction == true) ? 1 : 0;
}
private void RollbackCallback()
{
_rollbackHandler(this, EventArgs.Empty);
}
///<overloads>
/// Constructs a new SQLiteConnection object
/// </overloads>
/// <summary>
/// Default constructor
/// </summary>
public SQLiteConnection() : this("")
{
}
/// <summary>
/// Initializes the connection with the specified connection string
/// </summary>
/// <param name="connectionString">The connection string to use on the connection</param>
public SQLiteConnection(string connectionString)
{
_sql = null;
_connectionState = ConnectionState.Closed;
_connectionString = "";
_transactionLevel = 0;
_version = 0;
_commandList = new List<SQLiteCommand>();
if (connectionString != null)
ConnectionString = connectionString;
}
/// <summary>
/// Clones the settings and connection string from an existing connection. If the existing connection is already open, this
/// function will open its own connection, enumerate any attached databases of the original connection, and automatically
/// attach to them.
/// </summary>
/// <param name="connection"></param>
public SQLiteConnection(SQLiteConnection connection) : this(connection.ConnectionString)
{
string str;
if (connection.State == ConnectionState.Open)
{
Open();
// Reattach all attached databases from the existing connection
using (DataTable tbl = connection.GetSchema("Catalogs"))
{
foreach (DataRow row in tbl.Rows)
{
str = row[0].ToString();
if (String.Compare(str, "main", true, CultureInfo.InvariantCulture) != 0
&& String.Compare(str, "temp", true, CultureInfo.InvariantCulture) != 0)
{
using (SQLiteCommand cmd = CreateCommand())
{
cmd.CommandText = String.Format(CultureInfo.InvariantCulture, "ATTACH DATABASE '{0}' AS [{1}]", row[1], row[0]);
cmd.ExecuteNonQuery();
}
}
}
}
}
}
#if PLATFORM_COMPACTFRAMEWORK
/// <summary>
/// Obsolete
/// </summary>
public override int ConnectionTimeout
{
get
{
return 30;
}
}
#endif
/// <summary>
/// Creates a clone of the connection. All attached databases and user-defined functions are cloned. If the existing connection is open, the cloned connection
/// will also be opened.
/// </summary>
/// <returns></returns>
public object Clone()
{
return new SQLiteConnection(this);
}
/// <summary>
/// Disposes of the SQLiteConnection, closing it if it is active.
/// </summary>
/// <param name="disposing">True if the connection is being explicitly closed.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -