/// <summary> /// Executes this operation /// </summary> /// <param name="rows">The rows.</param> /// <param name="cancellationToken">A CancellationToken to stop execution</param> /// <returns></returns> public override IAsyncEnumerable <Row> Execute(IAsyncEnumerable <Row> rows, CancellationToken cancellationToken = default) { return(new AsyncEnumerable <Row>(async yield => { Guard.Against <ArgumentException>(rows == null, "SqlBatchOperation cannot accept a null enumerator"); using (SqlConnection connection = (SqlConnection)await Database.Connection(ConnectionStringSettings, cancellationToken)) using (SqlTransaction transaction = (SqlTransaction)BeginTransaction(connection)) { SqlCommandSet commandSet = null; CreateCommandSet(connection, transaction, ref commandSet, timeout); await rows.ForEachAsync(row => { SqlCommand command = new SqlCommand(); PrepareCommand(row, command); if (command.Parameters.Count == 0 && (RuntimeInfo.Version.Contains("2.0") || RuntimeInfo.Version.Contains("1.1")) ) //workaround around a framework bug { Guid guid = Guid.NewGuid(); command.Parameters.AddWithValue(guid.ToString(), guid); } commandSet.Append(command); if (commandSet.CountOfCommands >= batchSize) { Debug("Executing batch of {CountOfCommands} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); CreateCommandSet(connection, transaction, ref commandSet, timeout); } }, cancellationToken); Debug("Executing final batch of {CountOfCommands} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); if (PipelineExecuter.HasErrors) { Warn(null, "Rolling back transaction in {OperationName}", Name); if (transaction != null) { transaction.Rollback(); } Warn(null, "Rolled back transaction in {OperationName}", Name); } else { Debug("Committing {OperationName}", Name); if (transaction != null) { transaction.Commit(); } Debug("Committed {OperationName}", Name); } } yield.Break(); })); }
/// <summary> /// Executes this operation /// </summary> /// <param name="rows">The rows.</param> /// <returns></returns> public override IEnumerable <Row> Execute(IEnumerable <Row> rows) { Guard.Against <ArgumentException>(rows == null, "SqlBatchOperation cannot accept a null enumerator"); using (SqlConnection connection = (SqlConnection)Use.Connection(ConnectionStringSettings)) using (SqlTransaction transaction = (SqlTransaction)BeginTransaction(connection)) { SqlCommandSet commandSet = null; CreateCommandSet(connection, transaction, ref commandSet, timeout); foreach (Row row in rows) { SqlCommand command = new SqlCommand(); PrepareCommand(row, command); if (command.Parameters.Count == 0 && (RuntimeInfo.Version.Contains("2.0") || RuntimeInfo.Version.Contains("1.1"))) //workaround around a framework bug { Guid guid = Guid.NewGuid(); command.Parameters.AddWithValue(guid.ToString(), guid); } commandSet.Append(command); if (commandSet.CountOfCommands >= batchSize) { Debug("Executing batch of {0} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); CreateCommandSet(connection, transaction, ref commandSet, timeout); } } Debug("Executing final batch of {0} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); if (PipelineExecuter.HasErrors) { Warn(null, "Rolling back transaction in {0}", Name); if (transaction != null) { transaction.Rollback(); } Warn(null, "Rolled back transaction in {0}", Name); } else { Debug("Committing {0}", Name); if (transaction != null) { transaction.Commit(); } Debug("Committed {0}", Name); } } yield break; }
/// <summary> /// Executes this operation /// </summary> /// <param name="rows">The rows.</param> /// <returns></returns> public override IEnumerable <Row> Execute(IEnumerable <Row> rows) { Guard.Against <ArgumentException>(rows == null, "SqlBatchOperation cannot accept a null enumerator"); using (var cn = (SqlConnection)Use.Connection(Connection)) using (var transaction = BeginTransaction(cn)) { SqlCommandSet commandSet = null; CreateCommandSet(cn, transaction, ref commandSet, _timeout); foreach (var row in rows) { var command = new SqlCommand(); PrepareCommand(row, command); //if (command.Parameters.Count == 0) //workaround around a framework bug //{ // var guid = Guid.NewGuid(); // command.Parameters.AddWithValue(guid.ToString(), guid); //} commandSet.Append(command); if (commandSet.CountOfCommands >= _batchSize) { Debug("Executing batch of {0} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); CreateCommandSet(cn, transaction, ref commandSet, _timeout); } } Debug("Executing final batch of {0} commands", commandSet.CountOfCommands); commandSet.ExecuteNonQuery(); if (transaction != null) { if (PipelineExecuter.HasErrors) { Warn(null, "Rolling back transaction in {0}", Name); transaction.Rollback(); Warn(null, "Rolled back transaction in {0}", Name); } else { Debug("Committing {0}", Name); transaction.Commit(); Debug("Committed {0}", Name); } } } yield break; }
/// <summary> /// Executes the SqlCommandSet, calls OnErrorExecute on Exception. /// </summary> /// <param name="commandSet">The SqlCommandSet to execute.</param> protected virtual void Execute(SqlCommandSet commandSet) { try { commandSet.ExecuteNonQuery(); } catch (Exception e) { if (OnErrorExecute(commandSet, e)) { commandSet.ExecuteNonQuery(); } else { System.Diagnostics.Debug.WriteLine(commandSet.DumpRows()); // To Preserve this StackTrace in InnerException throw new Exception(this + ". " + e.Message, e); } } }