/// <summary> /// Executes the specified implementation. /// </summary> /// <param name="executionToken">The execution token.</param> /// <param name="implementation">The implementation.</param> /// <param name="state">The state.</param> /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> /// <exception cref="System.NotImplementedException"></exception> public override StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <OleDbCommand, OleDbParameter> executionToken, StreamingCommandImplementation <OleDbCommand> 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 { var cmd = new OleDbCommand(); cmd.Connection = m_Connection; cmd.Transaction = m_Transaction; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); implementation(cmd); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd)); } catch (Exception ex) { OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state); throw; } }
/// <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 <OleDbCommand, OleDbParameter> executionToken, CommandImplementation <OleDbCommand> 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 OleDbCommand()) { cmd.Connection = m_Connection; cmd.Transaction = m_Transaction; 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 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 <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 cmd = new SQLiteCommand()) { cmd.Connection = m_Connection; if (m_Transaction != null) { cmd.Transaction = m_Transaction; } 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; } finally { if (lockToken != null) { lockToken.Dispose(); } } }
/// <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; } } }
/// <summary> /// Executes the specified implementation asynchronously. /// </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>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> /// <exception cref="System.NotImplementedException"></exception> public override async Task <StreamingCommandCompletionToken> ExecuteStreamAsync(CommandExecutionToken <SqlCommand, SqlParameter> executionToken, StreamingCommandImplementationAsync <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); SqlConnection?con = null; try { con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false); var cmd = new SqlCommand(); cmd.Connection = con; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); await implementation(cmd).ConfigureAwait(false); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con)); } catch (Exception ex) { #if NET6_0_OR_GREATER if (con != null) { await con.DisposeAsync().ConfigureAwait(false); } #else con?.Dispose(); #endif 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; } } }
/// <summary> /// Executes the specified implementation. /// </summary> /// <param name="executionToken">The execution token.</param> /// <param name="implementation">The implementation.</param> /// <param name="state">The state.</param> /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> public override StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, StreamingCommandImplementation <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; SQLiteConnection?con = null; try { switch (mode) { case LockType.Read: lockToken = SyncLock.ReaderLock(); break; case LockType.Write: lockToken = SyncLock.WriterLock(); break; } con = CreateConnection(); var cmd = new SQLiteCommand(); cmd.Connection = con; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); implementation(cmd); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con) { LockToken = lockToken }); } catch (Exception ex) { lockToken?.Dispose(); con?.Dispose(); OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state); throw; } }
/// <summary> /// Executes the specified implementation asynchronously. /// </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>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> /// <exception cref="System.NotImplementedException"></exception> public override async Task <StreamingCommandCompletionToken> ExecuteStreamAsync(CommandExecutionToken <NpgsqlCommand, NpgsqlParameter> executionToken, StreamingCommandImplementationAsync <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 { var cmd = new NpgsqlCommand(); cmd.Connection = m_Connection; if (m_Transaction != null) { cmd.Transaction = m_Transaction; } executionToken.PopulateCommand(cmd, DefaultCommandTimeout); if (((PostgreSqlCommandExecutionToken)executionToken).DereferenceCursors) { await DereferenceCursorsAsync(cmd, implementation).ConfigureAwait(false); } else { await implementation(cmd).ConfigureAwait(false); } return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd)); } 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; } } }
/// <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; } }
/// <summary> /// Executes the specified implementation. /// </summary> /// <param name="executionToken">The execution token.</param> /// <param name="implementation">The implementation.</param> /// <param name="state">The state.</param> /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> /// <exception cref="System.NotImplementedException"></exception> public override StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <NpgsqlCommand, NpgsqlParameter> executionToken, StreamingCommandImplementation <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); NpgsqlConnection?con = null; try { con = CreateConnection(); var cmd = new NpgsqlCommand(); NpgsqlTransaction?transactionToClose = null; cmd.Connection = con; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); if (((PostgreSqlCommandExecutionToken)executionToken).DereferenceCursors) { transactionToClose = DereferenceCursors(cmd, implementation); } else { implementation(cmd); } return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con) { Transaction = transactionToClose }); } catch (Exception ex) { con?.Dispose(); OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state); throw; } }
/// <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; } } }
/// <summary> /// Executes the specified implementation. /// </summary> /// <param name="executionToken">The execution token.</param> /// <param name="implementation">The implementation.</param> /// <param name="state">The state.</param> /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> /// <exception cref="System.NotImplementedException"></exception> public override StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <MySqlCommand, MySqlParameter> executionToken, StreamingCommandImplementation <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); MySqlConnection?con = null; try { con = CreateConnection(); var cmd = new MySqlCommand(); cmd.Connection = con; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); implementation(cmd); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con)); } catch (Exception ex) { con?.Dispose(); 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="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> /// Executes the specified implementation asynchronously. /// </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>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns> public override async Task <StreamingCommandCompletionToken> ExecuteStreamAsync(CommandExecutionToken <SQLiteCommand, SQLiteParameter> executionToken, StreamingCommandImplementationAsync <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; SQLiteConnection?con = null; try { switch (mode) { case LockType.Read: lockToken = await SyncLock.ReaderLockAsync().ConfigureAwait(false); break; case LockType.Write: lockToken = await SyncLock.WriterLockAsync().ConfigureAwait(false); break; } con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false); var cmd = new SQLiteCommand(); cmd.Connection = con; executionToken.PopulateCommand(cmd, DefaultCommandTimeout); await implementation(cmd).ConfigureAwait(false); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con) { LockToken = lockToken }); } catch (Exception ex) { lockToken?.Dispose(); #if NET6_0_OR_GREATER if (con != null) { await con.DisposeAsync().ConfigureAwait(false); } #else con?.Dispose(); #endif 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; } } }