/******************************************************** * 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.Common; using System.ComponentModel; /// /// SQLite implementation of DbDataAdapter. /// #if !PLATFORM_COMPACTFRAMEWORK [DefaultEvent("RowUpdated")] [ToolboxItem("SQLite.Designer.SQLiteDataAdapterToolboxItem, SQLite.Designer, Version=" + SQLite3.DesignerVersion + ", Culture=neutral, PublicKeyToken=db937bc2d44ff139")] [Designer("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] #endif public sealed class SQLiteDataAdapter : DbDataAdapter { private bool disposeSelect = true; /////////////////////////////////////////////////////////////////////////////////////////////// private static object _updatingEventPH = new object(); private static object _updatedEventPH = new object(); /////////////////////////////////////////////////////////////////////////////////////////////// #region Public Constructors /// /// This class is just a shell around the DbDataAdapter. Nothing from /// DbDataAdapter is overridden here, just a few constructors are defined. /// /// /// Default constructor. /// public SQLiteDataAdapter() { } /////////////////////////////////////////////////////////////////////////////////////////////// /// /// Constructs a data adapter using the specified select command. /// /// /// The select command to associate with the adapter. /// public SQLiteDataAdapter(SQLiteCommand cmd) { SelectCommand = cmd; disposeSelect = false; } /////////////////////////////////////////////////////////////////////////////////////////////// /// /// Constructs a data adapter with the supplied select command text and /// associated with the specified connection. /// /// /// The select command text to associate with the data adapter. /// /// /// The connection to associate with the select command. /// public SQLiteDataAdapter(string commandText, SQLiteConnection connection) { SelectCommand = new SQLiteCommand(commandText, connection); } /////////////////////////////////////////////////////////////////////////////////////////////// /// /// Constructs a data adapter with the specified select command text, /// and using the specified database connection string. /// /// /// The select command text to use to construct a select command. /// /// /// A connection string suitable for passing to a new SQLiteConnection, /// which is associated with the select command. /// public SQLiteDataAdapter( string commandText, string connectionString ) : this(commandText, connectionString, false) { // do nothing. } /////////////////////////////////////////////////////////////////////////////////////////////// /// /// Constructs a data adapter with the specified select command text, /// and using the specified database connection string. /// /// /// The select command text to use to construct a select command. /// /// /// A connection string suitable for passing to a new SQLiteConnection, /// which is associated with the select command. /// /// /// Non-zero to parse the connection string using the built-in (i.e. /// framework provided) parser when opening the connection. /// public SQLiteDataAdapter( string commandText, string connectionString, bool parseViaFramework ) { SQLiteConnection cnn = new SQLiteConnection( connectionString, parseViaFramework); SelectCommand = new SQLiteCommand(commandText, cnn); } #endregion /////////////////////////////////////////////////////////////////////////////////////////////// #region IDisposable "Pattern" Members private bool disposed; private void CheckDisposed() /* throw */ { #if THROW_ON_DISPOSED if (disposed) throw new ObjectDisposedException(typeof(SQLiteDataAdapter).Name); #endif } /////////////////////////////////////////////////////////////////////////////////////////////// protected override void Dispose(bool disposing) { try { if (!disposed) { if (disposing) { //////////////////////////////////// // dispose managed resources here... //////////////////////////////////// if (disposeSelect && (SelectCommand != null)) { SelectCommand.Dispose(); SelectCommand = null; } if (InsertCommand != null) { InsertCommand.Dispose(); InsertCommand = null; } if (UpdateCommand != null) { UpdateCommand.Dispose(); UpdateCommand = null; } if (DeleteCommand != null) { DeleteCommand.Dispose(); DeleteCommand = null; } } ////////////////////////////////////// // release unmanaged resources here... ////////////////////////////////////// } } finally { base.Dispose(disposing); // // NOTE: Everything should be fully disposed at this point. // disposed = true; } } #endregion /////////////////////////////////////////////////////////////////////////////////////////////// /// /// Row updating event handler /// public event EventHandler RowUpdating { add { CheckDisposed(); #if !PLATFORM_COMPACTFRAMEWORK EventHandler previous = (EventHandler)base.Events[_updatingEventPH]; if ((previous != null) && (value.Target is DbCommandBuilder)) { EventHandler handler = (EventHandler)FindBuilder(previous); if (handler != null) { base.Events.RemoveHandler(_updatingEventPH, handler); } } #endif base.Events.AddHandler(_updatingEventPH, value); } remove { CheckDisposed(); base.Events.RemoveHandler(_updatingEventPH, value); } } #if !PLATFORM_COMPACTFRAMEWORK internal static Delegate FindBuilder(MulticastDelegate mcd) { if (mcd != null) { Delegate[] invocationList = mcd.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { if (invocationList[i].Target is DbCommandBuilder) { return invocationList[i]; } } } return null; } #endif /// /// Row updated event handler /// public event EventHandler RowUpdated { add { CheckDisposed(); base.Events.AddHandler(_updatedEventPH, value); } remove { CheckDisposed(); base.Events.RemoveHandler(_updatedEventPH, value); } } /// /// Raised by the underlying DbDataAdapter when a row is being updated /// /// The event's specifics protected override void OnRowUpdating(RowUpdatingEventArgs value) { EventHandler handler = base.Events[_updatingEventPH] as EventHandler; if (handler != null) handler(this, value); } /// /// Raised by DbDataAdapter after a row is updated /// /// The event's specifics protected override void OnRowUpdated(RowUpdatedEventArgs value) { EventHandler handler = base.Events[_updatedEventPH] as EventHandler; if (handler != null) handler(this, value); } /// /// Gets/sets the select command for this DataAdapter /// #if !PLATFORM_COMPACTFRAMEWORK [DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, 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")] #endif public new SQLiteCommand SelectCommand { get { CheckDisposed(); return (SQLiteCommand)base.SelectCommand; } set { CheckDisposed(); base.SelectCommand = value; } } /// /// Gets/sets the insert command for this DataAdapter /// #if !PLATFORM_COMPACTFRAMEWORK [DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, 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")] #endif public new SQLiteCommand InsertCommand { get { CheckDisposed(); return (SQLiteCommand)base.InsertCommand; } set { CheckDisposed(); base.InsertCommand = value; } } /// /// Gets/sets the update command for this DataAdapter /// #if !PLATFORM_COMPACTFRAMEWORK [DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, 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")] #endif public new SQLiteCommand UpdateCommand { get { CheckDisposed(); return (SQLiteCommand)base.UpdateCommand; } set { CheckDisposed(); base.UpdateCommand = value; } } /// /// Gets/sets the delete command for this DataAdapter /// #if !PLATFORM_COMPACTFRAMEWORK [DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, 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")] #endif public new SQLiteCommand DeleteCommand { get { CheckDisposed(); return (SQLiteCommand)base.DeleteCommand; } set { CheckDisposed(); base.DeleteCommand = value; } } } }