/Rhino.Etl.Core/Operations/OutputCommandOperation.cs

http://github.com/ayende/rhino-etl · C# · 75 lines · 49 code · 5 blank · 21 comment · 5 complexity · 9efa0378d64fa14ce59a1cf1c222e933 MD5 · raw file

  1. using System.Configuration;
  2. using Rhino.Etl.Core.Enumerables;
  3. using Rhino.Etl.Core.Infrastructure;
  4. namespace Rhino.Etl.Core.Operations
  5. {
  6. using System.Collections.Generic;
  7. using System.Data;
  8. /// <summary>
  9. /// Generic output command operation
  10. /// </summary>
  11. public abstract class OutputCommandOperation : AbstractCommandOperation
  12. {
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="OutputCommandOperation"/> class.
  15. /// </summary>
  16. /// <param name="connectionStringName">Name of the connection string.</param>
  17. public OutputCommandOperation(string connectionStringName) : this(ConfigurationManager.ConnectionStrings[connectionStringName])
  18. {
  19. }
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="OutputCommandOperation"/> class.
  22. /// </summary>
  23. /// <param name="connectionStringSettings">Connection string settings to use.</param>
  24. public OutputCommandOperation(ConnectionStringSettings connectionStringSettings)
  25. : base(connectionStringSettings)
  26. {
  27. }
  28. /// <summary>
  29. /// Executes this operation
  30. /// </summary>
  31. /// <param name="rows">The rows.</param>
  32. /// <returns></returns>
  33. public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
  34. {
  35. using (IDbConnection connection = Use.Connection(ConnectionStringSettings))
  36. using (IDbTransaction transaction = BeginTransaction(connection))
  37. {
  38. foreach (Row row in new SingleRowEventRaisingEnumerator(this, rows))
  39. {
  40. using (IDbCommand cmd = connection.CreateCommand())
  41. {
  42. currentCommand = cmd;
  43. currentCommand.Transaction = transaction;
  44. PrepareCommand(currentCommand, row);
  45. currentCommand.ExecuteNonQuery();
  46. }
  47. }
  48. if (PipelineExecuter.HasErrors)
  49. {
  50. Warn("Rolling back transaction in {0}", Name);
  51. if (transaction != null) transaction.Rollback();
  52. Warn("Rolled back transaction in {0}", Name);
  53. }
  54. else
  55. {
  56. Debug("Committing {0}", Name);
  57. if (transaction != null) transaction.Commit();
  58. Debug("Committed {0}", Name);
  59. }
  60. }
  61. yield break;
  62. }
  63. /// <summary>
  64. /// Prepares the command for execution, set command text, parameters, etc
  65. /// </summary>
  66. /// <param name="cmd">The command.</param>
  67. /// <param name="row">The row.</param>
  68. protected abstract void PrepareCommand(IDbCommand cmd, Row row);
  69. }
  70. }