Example #1
0
    /// <summary>
    /// Executes the operation asynchronously.
    /// </summary>
    /// <param name="executionToken">The execution token.</param>
    /// <param name="implementation">The implementation that handles processing the result of the command.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <param name="state">User supplied state.</param>
    /// <returns>Task.</returns>
    /// <exception cref="NotImplementedException"></exception>
    protected override async Task <int?> ExecuteAsync(CommandExecutionToken <SqlCommand, SqlParameter> executionToken, CommandImplementationAsync <SqlCommand> implementation, CancellationToken cancellationToken, object?state)
    {
        if (executionToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
        }
        if (implementation == null)
        {
            throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
        }

        var startTime = DateTimeOffset.Now;

        OnExecutionStarted(executionToken, startTime, state);

        try
        {
            using (var cmd = new SqlCommand())
            {
                cmd.Connection = m_Connection;
                if (m_Transaction != null)
                {
                    cmd.Transaction = m_Transaction;
                }

                executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

                var rows = await implementation(cmd).ConfigureAwait(false);

                executionToken.RaiseCommandExecuted(cmd, rows);
                OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                return(rows);
            }
        }
        catch (Exception ex)
        {
            if (cancellationToken.IsCancellationRequested)             //convert Exception into a OperationCanceledException
            {
                var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                OnExecutionCanceled(executionToken, startTime, DateTimeOffset.Now, state);
                throw ex2;
            }
            else
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
    }
Example #2
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="state">User supplied state.</param>
        /// <returns>System.Nullable&lt;System.Int32&gt;.</returns>
        /// <exception cref="ArgumentNullException">executionToken;executionToken is null.
        /// or
        /// implementation;implementation is null.</exception>
        protected override int?Execute(CommandExecutionToken <MySqlCommand, MySqlParameter> executionToken, CommandImplementation <MySqlCommand> implementation, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var con = CreateConnection())
                {
                    using (var cmd = new MySqlCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                        {
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        }
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                        {
                            cmd.Parameters.Add(param);
                        }

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = implementation(cmd);

                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return(rows);
                    }
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="state">User supplied state.</param>
        protected override int?Execute(CommandExecutionToken <NpgsqlCommand, NpgsqlParameter> executionToken, CommandImplementation <NpgsqlCommand> implementation, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var cmd = new NpgsqlCommand())
                {
                    cmd.Connection = m_Connection;
                    if (m_Transaction != null)
                    {
                        cmd.Transaction = m_Transaction;
                    }
                    executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

                    int?rows;

                    if (((PostgreSqlCommandExecutionToken)executionToken).DereferenceCursors)
                    {
                        rows = DereferenceCursors(cmd, implementation);
                    }
                    else
                    {
                        rows = implementation(cmd);
                    }

                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
Example #4
0
        /// <summary>
        /// Executes the specified operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="state">User supplied state.</param>
        /// <exception cref="ArgumentNullException">
        /// executionToken;executionToken is null.
        /// or
        /// implementation;implementation is null.
        /// </exception>
        protected override int?Execute(CommandExecutionToken <OleDbCommand, OleDbParameter> executionToken, CommandImplementation <OleDbCommand> implementation, object state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException("implementation", "implementation is null.");
            }

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var cmd = new OleDbCommand())
                {
                    cmd.Connection = m_Connection;
                    if (m_Transaction != null)
                    {
                        cmd.Transaction = m_Transaction;
                    }
                    if (DefaultCommandTimeout.HasValue)
                    {
                        cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                    }
                    cmd.CommandText = executionToken.CommandText;
                    cmd.CommandType = executionToken.CommandType;
                    foreach (var param in executionToken.Parameters)
                    {
                        cmd.Parameters.Add(param);
                    }

                    var rows = implementation(cmd);
                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
Example #5
0
    /// <summary>
    /// Executes the specified operation.
    /// </summary>
    /// <param name="executionToken">The execution token.</param>
    /// <param name="implementation">The implementation that handles processing the result of the command.</param>
    /// <param name="state">User supplied state.</param>
    /// <returns>System.Nullable&lt;System.Int32&gt;.</returns>
    /// <exception cref="ArgumentNullException">executionToken;executionToken is null.
    /// or
    /// implementation;implementation is null.</exception>
    protected override int?Execute(CommandExecutionToken <MySqlCommand, MySqlParameter> executionToken, CommandImplementation <MySqlCommand> implementation, object?state)
    {
        if (executionToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
        }
        if (implementation == null)
        {
            throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
        }

        var startTime = DateTimeOffset.Now;

        OnExecutionStarted(executionToken, startTime, state);

        try
        {
            using (var con = CreateConnection())
            {
                using (var cmd = new MySqlCommand())
                {
                    cmd.Connection = con;
                    executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

                    var rows = implementation(cmd);

                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
        }
        catch (Exception ex)
        {
            OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
            throw;
        }
    }
        /// <summary>
        /// Executes the operation asynchronously.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="state">User supplied state.</param>
        /// <returns>
        /// Task.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// executionToken;executionToken is null.
        /// or
        /// implementation;implementation is null.
        /// </exception>
        protected override async Task <int?> ExecuteAsync(CommandExecutionToken <NpgsqlCommand, NpgsqlParameter> executionToken, CommandImplementationAsync <NpgsqlCommand> implementation, CancellationToken cancellationToken, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var cmd = new NpgsqlCommand())
                {
                    cmd.Connection  = m_Connection;
                    cmd.Transaction = m_Transaction;
                    if (DefaultCommandTimeout.HasValue)
                    {
                        cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                    }
                    cmd.CommandText = executionToken.CommandText;
                    cmd.CommandType = executionToken.CommandType;
                    foreach (var param in executionToken.Parameters)
                    {
                        cmd.Parameters.Add(param);
                    }

                    int?rows;
                    if (((PostgreSqlCommandExecutionToken)executionToken).DereferenceCursors)
                    {
                        rows = await DereferenceCursorsAsync(cmd, implementation).ConfigureAwait(false);
                    }
                    else
                    {
                        rows = await implementation(cmd).ConfigureAwait(false);
                    }

                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionCanceled(executionToken, startTime, DateTimeOffset.Now, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
        }
Example #7
0
    /// <summary>
    /// Executes the specified operation asynchronously.
    /// </summary>
    /// <param name="executionToken"></param>
    /// <param name="implementation"></param>
    /// <param name="cancellationToken"></param>
    /// <param name="state"></param>
    /// <returns></returns>
    protected override async Task <int?> ExecuteAsync(CommandExecutionToken <OleDbCommand, OleDbParameter> executionToken, CommandImplementationAsync <OleDbCommand> implementation, CancellationToken cancellationToken, object?state)
    {
        if (executionToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
        }
        if (implementation == null)
        {
            throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
        }
        var currentToken = executionToken as AccessCommandExecutionToken;

        if (currentToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), "only AccessCommandExecutionToken is supported.");
        }

        var startTime = DateTimeOffset.Now;

        try
        {
            using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
            {
                int?rows = null;
                while (currentToken != null)
                {
                    OnExecutionStarted(currentToken, startTime, state);
                    using (var cmd = new OleDbCommand())
                    {
                        cmd.Connection = con;
                        currentToken.PopulateCommand(cmd, DefaultCommandTimeout);

                        if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer)
                        {
                            rows = await implementation(cmd).ConfigureAwait(false);
                        }
                        else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward)
                        {
                            if (currentToken.ForwardResult == null)
                            {
                                throw new InvalidOperationException("currentToken.ExecutionMode is ExecuteScalarAndForward, but currentToken.ForwardResult is null.");
                            }

                            currentToken.ForwardResult(await cmd.ExecuteScalarAsync().ConfigureAwait(false));
                        }
                        else
                        {
                            rows = await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
                        }
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(currentToken, startTime, DateTimeOffset.Now, rows, state);
                    }
                    currentToken = currentToken.NextCommand;
                }
                return(rows);
            }
        }
        catch (Exception ex)
        {
            if (cancellationToken.IsCancellationRequested)             //convert Exception into a OperationCanceledException
            {
                var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                throw ex2;
            }
            else
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
    }
Example #8
0
        /// <summary>
        /// Executes the specified operation asynchronously.
        /// </summary>
        /// <param name="executionToken"></param>
        /// <param name="implementation"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override async Task <int?> ExecuteAsync(CommandExecutionToken <OleDbCommand, OleDbParameter> executionToken, CommandImplementationAsync <OleDbCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException("implementation", "implementation is null.");
            }
            var currentToken = executionToken as AccessCommandExecutionToken;

            if (currentToken == null)
            {
                throw new ArgumentNullException("executionToken", "only AccessCommandExecutionToken is supported.");
            }

            var startTime = DateTimeOffset.Now;

            try
            {
                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    int?rows = null;
                    while (currentToken != null)
                    {
                        OnExecutionStarted(currentToken, startTime, state);
                        using (var cmd = new OleDbCommand())
                        {
                            cmd.Connection = con;
                            if (DefaultCommandTimeout.HasValue)
                            {
                                cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                            }
                            cmd.CommandText = currentToken.CommandText;
                            cmd.CommandType = currentToken.CommandType;
                            foreach (var param in currentToken.Parameters)
                            {
                                cmd.Parameters.Add(param);
                            }

                            currentToken.ApplyCommandOverrides(cmd);

                            if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer)
                            {
                                rows = await implementation(cmd);
                            }
                            else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward)
                            {
                                currentToken.ForwardResult(await cmd.ExecuteScalarAsync());
                            }
                            else
                            {
                                rows = await cmd.ExecuteNonQueryAsync();
                            }
                            executionToken.RaiseCommandExecuted(cmd, rows);
                            OnExecutionFinished(currentToken, startTime, DateTimeOffset.Now, rows, state);
                        }
                        currentToken = currentToken.NextCommand;
                    }
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
        }
Example #9
0
        protected override int?Execute(CommandExecutionToken <OleDbCommand, OleDbParameter> executionToken, CommandImplementation <OleDbCommand> implementation, object state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException("implementation", "implementation is null.");
            }
            var currentToken = executionToken as AccessCommandExecutionToken;

            if (currentToken == null)
            {
                throw new ArgumentNullException("executionToken", "only AccessCommandExecutionToken is supported.");
            }

            var startTime = DateTimeOffset.Now;

            try
            {
                using (var con = CreateConnection())
                {
                    int?rows = null;
                    while (currentToken != null)
                    {
                        OnExecutionStarted(currentToken, startTime, state);
                        using (var cmd = new OleDbCommand())
                        {
                            cmd.Connection = con;
                            if (DefaultCommandTimeout.HasValue)
                            {
                                cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                            }
                            cmd.CommandText = currentToken.CommandText;
                            cmd.CommandType = currentToken.CommandType;
                            foreach (var param in currentToken.Parameters)
                            {
                                cmd.Parameters.Add(param);
                            }

                            currentToken.ApplyCommandOverrides(cmd);

                            if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer)
                            {
                                rows = implementation(cmd);
                            }
                            else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward)
                            {
                                currentToken.ForwardResult(cmd.ExecuteScalar());
                            }
                            else
                            {
                                rows = cmd.ExecuteNonQuery();
                            }
                            executionToken.RaiseCommandExecuted(cmd, rows);
                            OnExecutionFinished(currentToken, startTime, DateTimeOffset.Now, rows, state);
                        }
                        currentToken = currentToken.NextCommand;
                    }
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
Example #10
0
        /// <summary>
        /// Executes the specified operation asynchronously.
        /// </summary>
        /// <param name="executionToken"></param>
        /// <param name="implementation"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override async Task<int?> ExecuteAsync(CommandExecutionToken<OleDbCommand, OleDbParameter> executionToken, CommandImplementationAsync<OleDbCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");
            var currentToken = executionToken as AccessCommandExecutionToken;
            if (currentToken == null)
                throw new ArgumentNullException("executionToken", "only AccessCommandExecutionToken is supported.");

            var startTime = DateTimeOffset.Now;

            try
            {
                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    int? rows = null;
                    while (currentToken != null)
                    {
                        OnExecutionStarted(currentToken, startTime, state);
                        using (var cmd = new OleDbCommand())
                        {
                            cmd.Connection = con;
                            if (DefaultCommandTimeout.HasValue)
                                cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                            cmd.CommandText = currentToken.CommandText;
                            cmd.CommandType = currentToken.CommandType;
                            foreach (var param in currentToken.Parameters)
                                cmd.Parameters.Add(param);

                            currentToken.ApplyCommandOverrides(cmd);

                            if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer)
                                rows = await implementation(cmd);
                            else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward)
                                currentToken.ForwardResult(await cmd.ExecuteScalarAsync());
                            else
                                rows = await cmd.ExecuteNonQueryAsync();
                            executionToken.RaiseCommandExecuted(cmd, rows);
                            OnExecutionFinished(currentToken, startTime, DateTimeOffset.Now, rows, state);
                        }
                        currentToken = currentToken.NextCommand;
                    }
                    return rows;
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException 
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
        }
        protected override int? Execute(CommandExecutionToken<OleDbCommand, OleDbParameter> executionToken, CommandImplementation<OleDbCommand> implementation, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");

            var startTime = DateTimeOffset.Now;
            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var con = CreateConnection())
                {
                    using (var cmd = new OleDbCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                            cmd.Parameters.Add(param);

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = implementation(cmd);
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return rows;
                    }
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }

        }
Example #12
0
        protected override int?Execute(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, CommandImplementation <SQLiteCommand> implementation, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            IDisposable?lockToken = null;

            try
            {
                switch (mode)
                {
                case LockType.Read: lockToken = SyncLock.ReaderLock(); break;

                case LockType.Write: lockToken = SyncLock.WriterLock(); break;
                }

                using (var con = CreateConnection())
                {
                    using (var cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                        {
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        }
                        cmd.CommandText = executionToken.CommandText;
                        //TODO: add potential check for this type.
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                        {
                            cmd.Parameters.Add(param);
                        }

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = implementation(cmd);
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return(rows);
                    }
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
            finally
            {
                if (lockToken != null)
                {
                    lockToken.Dispose();
                }
            }
        }
Example #13
0
        /// <summary>
        /// Executes the specified operation asynchronously.
        /// </summary>
        /// <param name="executionToken"></param>
        /// <param name="implementation"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override async Task <int?> ExecuteAsync(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, CommandImplementationAsync <SQLiteCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException("implementation", "implementation is null.");
            }

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            IDisposable lockToken = null;

            try
            {
                switch (mode)
                {
                case LockType.Read: lockToken = await SyncLock.ReaderLockAsync().ConfigureAwait(false); break;

                case LockType.Write: lockToken = await SyncLock.WriterLockAsync().ConfigureAwait(false); break;
                }

                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    using (var cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                        {
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        }
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                        {
                            cmd.Parameters.Add(param);
                        }

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = await implementation(cmd).ConfigureAwait(false);

                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return(rows);
                    }
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert SQLiteException into a OperationCanceledException
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
            finally
            {
                if (lockToken != null)
                {
                    lockToken.Dispose();
                }
            }
        }
Example #14
0
        /// <summary>
        /// Executes the specified operation asynchronously.
        /// </summary>
        /// <param name="executionToken"></param>
        /// <param name="implementation"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override async Task<int?> ExecuteAsync(CommandExecutionToken<SQLiteCommand, SQLiteParameter> executionToken, CommandImplementationAsync<SQLiteCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;
            OnExecutionStarted(executionToken, startTime, state);

            IDisposable lockToken = null;
            try
            {
                switch (mode)
                {
                    case LockType.Read: lockToken = await SyncLock.ReaderLockAsync().ConfigureAwait(false); break;
                    case LockType.Write: lockToken = await SyncLock.WriterLockAsync().ConfigureAwait(false); break;
                }

                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    using (var cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                            cmd.Parameters.Add(param);

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = await implementation(cmd).ConfigureAwait(false);
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return rows;
                    }
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert SQLiteException into a OperationCanceledException 
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
            finally
            {
                if (lockToken != null)
                    lockToken.Dispose();
            }
        }
Example #15
0
        protected override int? Execute(CommandExecutionToken<SQLiteCommand, SQLiteParameter> executionToken, CommandImplementation<SQLiteCommand> implementation, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;
            OnExecutionStarted(executionToken, startTime, state);

            IDisposable lockToken = null;
            try
            {
                switch (mode)
                {
                    case LockType.Read: lockToken = SyncLock.ReaderLock(); break;
                    case LockType.Write: lockToken = SyncLock.WriterLock(); break;
                }

                using (var con = CreateConnection())
                {
                    using (var cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        cmd.CommandText = executionToken.CommandText;
                        //TODO: add potential check for this type.
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                            cmd.Parameters.Add(param);

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = implementation(cmd);
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return rows;
                    }
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
            finally
            {
                if (lockToken != null)
                    lockToken.Dispose();
            }
        }
Example #16
0
        /// <summary>
        /// Executes the operation asynchronously.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="state">User supplied state.</param>
        /// <returns>Task.</returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override async Task <int?> ExecuteAsync(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, CommandImplementationAsync <SQLiteCommand> implementation, CancellationToken cancellationToken, object?state)
        {
            if (executionToken == null)
            {
                throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var mode = DisableLocks ? LockType.None : (executionToken as SQLiteCommandExecutionToken)?.LockType ?? LockType.Write;

            var startTime = DateTimeOffset.Now;

            OnExecutionStarted(executionToken, startTime, state);

            IDisposable?lockToken = null;

            try
            {
                switch (mode)
                {
                case LockType.Read: lockToken = await SyncLock.ReaderLockAsync().ConfigureAwait(false); break;

                case LockType.Write: lockToken = await SyncLock.WriterLockAsync().ConfigureAwait(false); break;
                }

                using (var cmd = new SQLiteCommand())
                {
                    cmd.Connection = m_Connection;
                    if (m_Transaction != null)
                    {
                        cmd.Transaction = m_Transaction;
                    }
                    executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

                    var rows = await implementation(cmd).ConfigureAwait(false);

                    executionToken.RaiseCommandExecuted(cmd, rows);
                    OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                    return(rows);
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested)                 //convert SQLiteException into a OperationCanceledException
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex2, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }
            finally
            {
                if (lockToken != null)
                {
                    lockToken.Dispose();
                }
            }
        }
        /// <summary>
        /// Execute the operation asynchronously.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation that handles processing the result of the command.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="state">User supplied state.</param>
        /// <returns>Task.</returns>
        protected override async Task<int?> ExecuteAsync(CommandExecutionToken<OleDbCommand, OleDbParameter> executionToken, CommandImplementationAsync<OleDbCommand> implementation, CancellationToken cancellationToken, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");

            var startTime = DateTimeOffset.Now;
            OnExecutionStarted(executionToken, startTime, state);

            try
            {
                using (var con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    using (var cmd = new OleDbCommand())
                    {
                        cmd.Connection = con;
                        if (DefaultCommandTimeout.HasValue)
                            cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                        cmd.CommandText = executionToken.CommandText;
                        cmd.CommandType = executionToken.CommandType;
                        foreach (var param in executionToken.Parameters)
                            cmd.Parameters.Add(param);

                        executionToken.ApplyCommandOverrides(cmd);

                        var rows = await implementation(cmd).ConfigureAwait(false);
                        executionToken.RaiseCommandExecuted(cmd, rows);
                        OnExecutionFinished(executionToken, startTime, DateTimeOffset.Now, rows, state);
                        return rows;
                    }
                }
            }
            catch (Exception ex)
            {
                if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException 
                {
                    var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                    OnExecutionCanceled(executionToken, startTime, DateTimeOffset.Now, state);
                    throw ex2;
                }
                else
                {
                    OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                    throw;
                }
            }

        }
Example #18
0
        protected override int? Execute(CommandExecutionToken<OleDbCommand, OleDbParameter> executionToken, CommandImplementation<OleDbCommand> implementation, object state)
        {
            if (executionToken == null)
                throw new ArgumentNullException("executionToken", "executionToken is null.");
            if (implementation == null)
                throw new ArgumentNullException("implementation", "implementation is null.");
            var currentToken = executionToken as AccessCommandExecutionToken;
            if (currentToken == null)
                throw new ArgumentNullException("executionToken", "only AccessCommandExecutionToken is supported.");

            var startTime = DateTimeOffset.Now;

            try
            {
                using (var con = CreateConnection())
                {
                    int? rows = null;
                    while (currentToken != null)
                    {
                        OnExecutionStarted(currentToken, startTime, state);
                        using (var cmd = new OleDbCommand())
                        {
                            cmd.Connection = con;
                            if (DefaultCommandTimeout.HasValue)
                                cmd.CommandTimeout = (int)DefaultCommandTimeout.Value.TotalSeconds;
                            cmd.CommandText = currentToken.CommandText;
                            cmd.CommandType = currentToken.CommandType;
                            foreach (var param in currentToken.Parameters)
                                cmd.Parameters.Add(param);

                            currentToken.ApplyCommandOverrides(cmd);

                            if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer)
                                rows = implementation(cmd);
                            else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward)
                                currentToken.ForwardResult(cmd.ExecuteScalar());
                            else
                                rows = cmd.ExecuteNonQuery();
                            executionToken.RaiseCommandExecuted(cmd, rows);
                            OnExecutionFinished(currentToken, startTime, DateTimeOffset.Now, rows, state);
                        }
                        currentToken = currentToken.NextCommand;
                    }
                    return rows;
                }
            }
            catch (Exception ex)
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }