Esempio n. 1
0
        /// <summary>
        /// Initializes a command with the given SQL, connection and transaction
        /// </summary>
        /// <param name="commandText">The SQL command text</param>
        /// <param name="connection">The connection to associate with the command</param>
        /// <param name="transaction">The transaction the command should be associated with</param>
        public SQLiteCommand(string commandText, SQLiteConnection connection, SQLiteTransaction transaction)
        {
            _commandTimeout      = 30;
            _parameterCollection = new SQLiteParameterCollection(this);
            _designTimeVisible   = true;
            _updateRowSource     = UpdateRowSource.None;

            if (commandText != null)
            {
                CommandText = commandText;
            }

            if (connection != null)
            {
                DbConnection    = connection;
                _commandTimeout = connection.DefaultTimeout;
            }

            if (transaction != null)
            {
                Transaction = transaction;
            }

            SQLiteConnection.OnChanged(connection, new ConnectionEventArgs(
                                           SQLiteConnectionEventType.NewCommand, null, transaction, this,
                                           null, null, null, null));
        }
Esempio n. 2
0
        internal override void Open(string strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool)
        {
            //
            // NOTE: If the database connection is currently open, attempt to
            //       close it now.  This must be done because the file name or
            //       other parameters that may impact the underlying database
            //       connection may have changed.
            //
            if (_sql != null)
            {
                Close(true);
            }

            //
            // NOTE: If the connection was not closed successfully, throw an
            //       exception now.
            //
            if (_sql != null)
            {
                throw new SQLiteException("connection handle is still active");
            }

            _usePool  = usePool;
            _fileName = strFilename;

            if (usePool)
            {
                _sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out _poolVersion);

#if !NET_COMPACT_20 && TRACE_CONNECTION
                Trace.WriteLine(String.Format("Open16 (Pool): {0}", (_sql != null) ? _sql.ToString() : "<null>"));
#endif
            }

            if (_sql == null)
            {
                try
                {
                    // do nothing.
                }
                finally /* NOTE: Thread.Abort() protection. */
                {
                    IntPtr          db;
                    SQLiteErrorCode n;

#if !SQLITE_STANDARD
                    if ((connectionFlags & SQLiteConnectionFlags.NoExtensionFunctions) != SQLiteConnectionFlags.NoExtensionFunctions)
                    {
                        n = UnsafeNativeMethods.sqlite3_open16_interop(ToUTF8(strFilename), openFlags, out db);
                    }
                    else
#endif
                    {
                        //
                        // NOTE: This flag check is designed to enforce the constraint that opening
                        //       a database file that does not already exist requires specifying the
                        //       "Create" flag, even when a native API is used that does not accept
                        //       a flags parameter.
                        //
                        if (((openFlags & SQLiteOpenFlagsEnum.Create) != SQLiteOpenFlagsEnum.Create) && !File.Exists(strFilename))
                        {
                            throw new SQLiteException(SQLiteErrorCode.CantOpen, strFilename);
                        }

                        n = UnsafeNativeMethods.sqlite3_open16(strFilename, out db);
                    }

#if !NET_COMPACT_20 && TRACE_CONNECTION
                    Trace.WriteLine(String.Format("Open16: {0}", db));
#endif

                    if (n != SQLiteErrorCode.Ok)
                    {
                        throw new SQLiteException(n, null);
                    }
                    _sql = new SQLiteConnectionHandle(db, true);
                }
                lock (_sql) { /* HACK: Force the SyncBlock to be "created" now. */ }

                SQLiteConnection.OnChanged(null, new ConnectionEventArgs(
                                               SQLiteConnectionEventType.NewCriticalHandle, null, null,
                                               null, null, _sql, strFilename, new object[] { strFilename,
                                                                                             connectionFlags, openFlags, maxPoolSize, usePool }));
            }

            // Bind functions to this connection.  If any previous functions of the same name
            // were already bound, then the new bindings replace the old.
            if ((connectionFlags & SQLiteConnectionFlags.NoBindFunctions) != SQLiteConnectionFlags.NoBindFunctions)
            {
                if (_functions == null)
                {
                    _functions = new List <SQLiteFunction>();
                }

                _functions.AddRange(new List <SQLiteFunction>(SQLiteFunction.BindFunctions(this, connectionFlags)));
            }

            SetTimeout(0);
            GC.KeepAlive(_sql);
        }
Esempio n. 3
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Disposes of the command and clears all member variables
        /// </summary>
        /// <param name="disposing">Whether or not the class is being explicitly or implicitly disposed</param>
        protected override void Dispose(bool disposing)
        {
            SQLiteConnection.OnChanged(_cnn, new ConnectionEventArgs(
                                           SQLiteConnectionEventType.DisposingCommand, null, _transaction, this,
                                           null, null, null, new object[] { disposing, disposed }));

            bool skippedDispose = false;

            try
            {
                if (!disposed)
                {
                    if (disposing)
                    {
                        ////////////////////////////////////
                        // dispose managed resources here...
                        ////////////////////////////////////

                        // If a reader is active on this command, don't destroy the command, instead let the reader do it
                        SQLiteDataReader reader = null;
                        if (_activeReader != null)
                        {
                            try
                            {
                                reader = _activeReader.Target as SQLiteDataReader;
                            }
                            catch (InvalidOperationException)
                            {
                            }
                        }

                        if (reader != null)
                        {
                            reader._disposeCommand = true;
                            _activeReader          = null;
                            skippedDispose         = true;
                            return;
                        }

                        Connection = null;
                        _parameterCollection.Clear();
                        _commandText = null;
                    }

                    //////////////////////////////////////
                    // release unmanaged resources here...
                    //////////////////////////////////////
                }
            }
            finally
            {
                if (!skippedDispose)
                {
                    base.Dispose(disposing);

                    //
                    // NOTE: Everything should be fully disposed at this point.
                    //
                    disposed = true;
                }
            }
        }