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 <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 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 = 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 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 async override 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;
                    if (m_Transaction != null)
                    {
                        cmd.Transaction = m_Transaction;
                    }
                    executionToken.PopulateCommand(cmd, DefaultCommandTimeout);
                    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 #3
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();
                }
            }
        }
Example #4
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 #5
0
        /// <summary>
        /// Dereferences cursors returned by a stored procedure.
        /// </summary>
        /// <param name="cmd">The command.</param>
        /// <param name="implementation">The implementation.</param>
        protected static async Task <int?> DereferenceCursorsAsync(NpgsqlCommand cmd, CommandImplementationAsync <NpgsqlCommand> implementation)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException(nameof(cmd), $"{nameof(cmd)} is null.");
            }
            if (cmd.Connection == null)
            {
                throw new ArgumentNullException($"{nameof(cmd)}.{nameof(cmd.Connection)}", $"{nameof(cmd)}.{nameof(cmd.Connection)} is null.");
            }
            if (implementation == null)
            {
                throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
            }

            var closeTransaction = false;

            try
            {
                if (cmd.Transaction == null)
                {
                    cmd.Transaction  = cmd.Connection.BeginTransaction();
                    closeTransaction = true;
                }

                var sql = new StringBuilder();
                using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        sql.AppendLine($"FETCH ALL IN \"{reader.GetString(0)}\";");
                    }

                using (var cmd2 = new NpgsqlCommand())
                {
                    cmd2.Connection     = cmd.Connection;
                    cmd2.Transaction    = cmd.Transaction;
                    cmd2.CommandTimeout = cmd.CommandTimeout;
                    cmd2.CommandText    = sql.ToString();
                    cmd2.CommandType    = CommandType.Text;
                    return(await implementation(cmd2).ConfigureAwait(false));
                }
            }
            finally
            {
                if (closeTransaction)
                {
                    await cmd.Transaction !.CommitAsync().ConfigureAwait(false);
                }
            }
        }
Example #6
0
 Task <int?> ICommandDataSource <TCommand, TParameter> .ExecuteAsync(CommandExecutionToken <TCommand, TParameter> executionToken, CommandImplementationAsync <TCommand> implementation, CancellationToken cancellationToken, object state)
 {
     return(ExecuteAsync(executionToken, implementation, cancellationToken, state));
 }
 /// <summary>
 /// Helper method for executing the operation.
 /// </summary>
 /// <param name="implementation">The implementation.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <param name="state">The state.</param>
 /// <returns>Task.</returns>
 protected Task ExecuteCoreAsync(CommandImplementationAsync <TCommand> implementation, CancellationToken cancellationToken, object state)
 {
     return(Prepare().ExecuteAsync(implementation, cancellationToken, state));
 }
Example #8
0
        /// <summary>
        /// execute as an asynchronous operation.
        /// </summary>
        /// <param name="executionToken">The execution token.</param>
        /// <param name="implementation">The implementation.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="state">The 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 <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);

            try
            {
                using (var cmd = new SQLiteCommand())
                {
                    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);
                    }

                    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;
                }
            }
        }
Example #9
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>
 protected internal abstract Task <int?> ExecuteAsync(CommandExecutionToken <TCommand, TParameter> executionToken, CommandImplementationAsync <TCommand> implementation, CancellationToken cancellationToken, object state);
        /// <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 #11
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;
                }
            }
        }
 /// <summary>
 /// Executes the specified implementation asynchronously.
 /// </summary>
 /// <param name="implementation">The implementation.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <param name="state">The state.</param>
 /// <returns>Task.</returns>
 public Task <int?> ExecuteAsync(CommandImplementationAsync <TCommand> implementation, CancellationToken cancellationToken, object?state)
 {
     return(m_DataSource.ExecuteAsync(this, implementation, cancellationToken, state));
 }
Example #13
0
        /// <summary>
        /// execute as an asynchronous 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="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 con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
                {
                    using (var cmd = new NpgsqlCommand())
                    {
                        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);

                        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 #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
    /// <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 <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 con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false))
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    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 #16
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;
                }
            }
        }