/// <summary> /// Dereferences cursors returned by a stored procedure. /// </summary> /// <param name="cmd">The command.</param> /// <param name="implementation">The implementation.</param> protected NpgsqlTransaction?DereferenceCursors(NpgsqlCommand cmd, StreamingCommandImplementation <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; if (cmd.Transaction == null) { cmd.Transaction = cmd.Connection.BeginTransaction(); closeTransaction = true; } var sql = new StringBuilder(); using (var reader = cmd.ExecuteReader()) while (reader.Read()) { 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; implementation(cmd2); } return(closeTransaction ? cmd.Transaction : null); }
/// <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 <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; try { switch (mode) { case LockType.Read: lockToken = SyncLock.ReaderLock(); break; case LockType.Write: lockToken = SyncLock.WriterLock(); break; } var cmd = new SQLiteCommand(); cmd.Connection = m_Connection; if (m_Transaction != null) { cmd.Transaction = m_Transaction; } executionToken.PopulateCommand(cmd, DefaultCommandTimeout); implementation(cmd); return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd) { LockToken = lockToken }); } catch (Exception ex) { lockToken?.Dispose(); 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); 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) { DereferenceCursors(cmd, implementation); } else { implementation(cmd); } return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd)); } catch (Exception ex) { OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state); throw; } }
/// <summary> /// Executes the stream. /// </summary> /// <param name="executionToken">The execution token.</param> /// <param name="implementation">The implementation.</param> /// <param name="state">The state.</param> /// <returns>StreamingCommandCompletionToken.</returns> /// <exception cref="System.ArgumentNullException">executionToken</exception> /// <exception cref="System.ArgumentNullException">implementation</exception> /// <exception cref="System.ArgumentNullException">executionToken - only AccessCommandExecutionToken is supported.</exception> /// <exception cref="System.InvalidOperationException">currentToken.ExecutionMode is ExecuteScalarAndForward, but currentToken.ForwardResult is null.</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 currentToken = executionToken as AccessCommandExecutionToken; if (currentToken == null) { throw new ArgumentNullException(nameof(executionToken), "only AccessCommandExecutionToken is supported."); } var startTime = DateTimeOffset.Now; OleDbConnection?con = null; try { con = CreateConnection(); OleDbCommand?cmdToReturn = null; while (currentToken != null) { OnExecutionStarted(currentToken, startTime, state); var cmd = new OleDbCommand(); cmd.Connection = con; currentToken.PopulateCommand(cmd, DefaultCommandTimeout); if (currentToken.ExecutionMode == AccessCommandExecutionMode.Materializer) { implementation(cmd); cmdToReturn = cmd; } else if (currentToken.ExecutionMode == AccessCommandExecutionMode.ExecuteScalarAndForward) { if (currentToken.ForwardResult == null) { throw new InvalidOperationException("currentToken.ExecutionMode is ExecuteScalarAndForward, but currentToken.ForwardResult is null."); } currentToken.ForwardResult(cmd.ExecuteScalar()); } else { cmd.ExecuteNonQuery(); } currentToken = currentToken.NextCommand; } return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmdToReturn, con)); } catch (Exception ex) { con?.Dispose(); 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 <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 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 abstract StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <TCommand, TParameter> executionToken, StreamingCommandImplementation <TCommand> implementation, object?state);
/// <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; } }