/// <summary>
        /// This method resets all the prepared statements held by this instance
        /// back to their initial states, ready to be re-executed.
        /// </summary>
        public void Reset()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);

            Reset(true, false);
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Rolls back the active transaction.
        /// </summary>
        public override void Rollback()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);
            IsValid(true);
            IssueRollback(true);
        }
        /// <summary>
        /// This method executes a query using the given execution type and command
        /// behavior and returns the results.
        /// </summary>
        /// <param name="commandText">
        /// The text of the command to be executed.
        /// </param>
        /// <param name="executeType">
        /// The execution type for the command.  This is used to determine which method
        /// of the command object to call, which then determines the type of results
        /// returned, if any.
        /// </param>
        /// <param name="commandBehavior">
        /// The command behavior flags for the command.
        /// </param>
        /// <param name="connection">
        /// The connection used to create and execute the command.
        /// </param>
        /// <param name="args">
        /// The SQL parameter values to be used when building the command object to be
        /// executed, if any.
        /// </param>
        /// <returns>
        /// The results of the query -OR- null if no results were produced from the
        /// given execution type.
        /// </returns>
        public static object Execute(
            string commandText,
            SQLiteExecuteType executeType,
            CommandBehavior commandBehavior,
            SQLiteConnection connection,
            params object[] args
            )
        {
            SQLiteConnection.Check(connection);

            using (SQLiteCommand command = connection.CreateCommand())
            {
                command.CommandText = commandText;

                if (args != null)
                {
                    foreach (object arg in args)
                    {
                        SQLiteParameter parameter = arg as SQLiteParameter;

                        if (parameter == null)
                        {
                            parameter        = command.CreateParameter();
                            parameter.DbType = DbType.Object;
                            parameter.Value  = arg;
                        }

                        command.Parameters.Add(parameter);
                    }
                }

                switch (executeType)
                {
                case SQLiteExecuteType.None:
                {
                    //
                    // NOTE: Do nothing.
                    //
                    break;
                }

                case SQLiteExecuteType.NonQuery:
                {
                    return(command.ExecuteNonQuery(commandBehavior));
                }

                case SQLiteExecuteType.Scalar:
                {
                    return(command.ExecuteScalar(commandBehavior));
                }

                case SQLiteExecuteType.Reader:
                {
                    return(command.ExecuteReader(commandBehavior));
                }
                }
            }

            return(null);
        }
Exemple #4
0
        internal static void Check(SQLiteCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            command.CheckDisposed();
            SQLiteConnection.Check(command._cnn);
        }
Exemple #5
0
        /// <summary>
        /// Overrides the default behavior to return a SQLiteDataReader specialization class
        /// </summary>
        /// <param name="behavior">The flags to be associated with the reader.</param>
        /// <returns>A SQLiteDataReader</returns>
        public new SQLiteDataReader ExecuteReader(CommandBehavior behavior)
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);
            InitializeForReader();

            SQLiteDataReader rd = new SQLiteDataReader(this, behavior);

            _activeReader = new WeakReference(rd, false);

            return(rd);
        }
        /// <summary>
        /// This method resets all the prepared statements held by this instance
        /// back to their initial states, ready to be re-executed.
        /// </summary>
        /// <param name="clearBindings">
        /// Non-zero if the parameter bindings should be cleared as well.
        /// </param>
        /// <param name="ignoreErrors">
        /// If this is zero, a <see cref="SQLiteException" /> may be thrown for
        /// any unsuccessful return codes from the native library; otherwise, a
        /// <see cref="SQLiteException" /> will only be thrown if the connection
        /// or its state is invalid.
        /// </param>
        public void Reset(
            bool clearBindings,
            bool ignoreErrors
            )
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);

            if (clearBindings && (_parameterCollection != null))
            {
                _parameterCollection.Unbind();
            }

            ClearDataReader();

            if (_statementList == null)
            {
                return;
            }

            SQLiteBase      sqlBase = _cnn._sql;
            SQLiteErrorCode rc;

            foreach (SQLiteStatement item in _statementList)
            {
                if (item == null)
                {
                    continue;
                }

                SQLiteStatementHandle stmt = item._sqlite_stmt;

                if (stmt == null)
                {
                    continue;
                }

                rc = sqlBase.Reset(item);

                if ((rc == SQLiteErrorCode.Ok) && clearBindings &&
                    (SQLite3.SQLiteVersionNumber >= 3003007))
                {
                    rc = UnsafeNativeMethods.sqlite3_clear_bindings(stmt);
                }

                if (!ignoreErrors && (rc != SQLiteErrorCode.Ok))
                {
                    throw new SQLiteException(rc, sqlBase.GetLastError());
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Execute the command and return the first column of the first row of the resultset
        /// (if present), or null if no resultset was returned.
        /// </summary>
        /// <returns>The first column of the first row of the first resultset from the query</returns>
        public override object ExecuteScalar()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);

            using (SQLiteDataReader reader = ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult))
            {
                if (reader.Read())
                {
                    return(reader[0]);
                }
            }
            return(null);
        }
Exemple #8
0
        /// <summary>
        /// Execute the command and return the number of rows inserted/updated affected by it.
        /// </summary>
        /// <returns></returns>
        public override int ExecuteNonQuery()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);

            using (SQLiteDataReader reader = ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult))
            {
                while (reader.NextResult())
                {
                    ;
                }
                return(reader.RecordsAffected);
            }
        }
Exemple #9
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Commits the current transaction.
        /// </summary>
        public override void Commit()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);
            IsValid(true);

            if (_cnn._transactionLevel - 1 == 0)
            {
                using (SQLiteCommand cmd = _cnn.CreateCommand())
                {
                    cmd.CommandText = "COMMIT";
                    cmd.ExecuteNonQuery();
                }
            }
            _cnn._transactionLevel--;
            _cnn = null;
        }
Exemple #10
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Commits the current transaction.
        /// </summary>
        public override void Commit()
        {
            CheckDisposed();
            SQLiteConnection.Check(_cnn);
            IsValid(true);

            if (_beginLevel == 0)
            {
                using (SQLiteCommand cmd = _cnn.CreateCommand())
                {
                    cmd.CommandText = "COMMIT;";
                    cmd.ExecuteNonQuery();
                }

                _cnn._transactionLevel = 0;
                _cnn = null;
            }
            else
            {
                using (SQLiteCommand cmd = _cnn.CreateCommand())
                {
                    if (String.IsNullOrEmpty(_savePointName))
                    {
                        throw new SQLiteException("Cannot commit, unknown SAVEPOINT");
                    }

                    cmd.CommandText = String.Format(
                        "RELEASE {0};", _savePointName);

                    cmd.ExecuteNonQuery();
                }

                _cnn._transactionLevel--;
                _cnn = null;
            }
        }
Exemple #11
0
 /// <summary>
 /// Does nothing.  Commands are prepared as they are executed the first time, and kept in prepared state afterwards.
 /// </summary>
 public override void Prepare()
 {
     CheckDisposed();
     SQLiteConnection.Check(_cnn);
 }
Exemple #12
0
 /// <summary>
 /// Execute the command and return the first column of the first row of the resultset
 /// (if present), or null if no resultset was returned.
 /// </summary>
 /// <returns>The first column of the first row of the first resultset from the query.</returns>
 public override object ExecuteScalar()
 {
     CheckDisposed();
     SQLiteConnection.Check(_cnn);
     return(ExecuteScalar(CommandBehavior.Default));
 }
Exemple #13
0
 /// <summary>
 /// Execute the command and return the number of rows inserted/updated affected by it.
 /// </summary>
 /// <returns>The number of rows inserted/updated affected by it.</returns>
 public override int ExecuteNonQuery()
 {
     CheckDisposed();
     SQLiteConnection.Check(_cnn);
     return(ExecuteNonQuery(CommandBehavior.Default));
 }
Exemple #14
0
 /// <summary>
 /// Overrides the default behavior of DbDataReader to return a specialized SQLiteDataReader class
 /// </summary>
 /// <returns>A SQLiteDataReader</returns>
 public new SQLiteDataReader ExecuteReader()
 {
     CheckDisposed();
     SQLiteConnection.Check(_cnn);
     return(ExecuteReader(CommandBehavior.Default));
 }
        /// <summary>
        /// Verifies that all SQL queries associated with the current command text
        /// can be successfully compiled.  A <see cref="SQLiteException" /> will be
        /// raised if any errors occur.
        /// </summary>
        public void VerifyOnly()
        {
            CheckDisposed();

            SQLiteConnection connection = _cnn;

            SQLiteConnection.Check(connection); /* throw */
            SQLiteBase sqlBase = connection._sql;

            if ((connection == null) || (sqlBase == null))
            {
                throw new SQLiteException("invalid or unusable connection");
            }

            List <SQLiteStatement> statements       = null;
            SQLiteStatement        currentStatement = null;

            try
            {
                string          text              = _commandText;
                uint            timeout           = (uint)(_commandTimeout * 1000);
                SQLiteStatement previousStatement = null;

                while ((text != null) && (text.Length > 0))
                {
                    currentStatement = sqlBase.Prepare(
                        connection, text, previousStatement, timeout,
                        ref text); /* throw */

                    previousStatement = currentStatement;

                    if (currentStatement != null)
                    {
                        if (statements == null)
                        {
                            statements = new List <SQLiteStatement>();
                        }

                        statements.Add(currentStatement);
                        currentStatement = null;
                    }

                    if (text == null)
                    {
                        continue;
                    }
                    text = text.Trim();
                }
            }
            finally
            {
                if (currentStatement != null)
                {
                    currentStatement.Dispose();
                    currentStatement = null;
                }

                if (statements != null)
                {
                    foreach (SQLiteStatement statement in statements)
                    {
                        if (statement == null)
                        {
                            continue;
                        }

                        statement.Dispose();
                    }

                    statements.Clear();
                    statements = null;
                }
            }
        }