/Rhino.Etl.Core/Operations/InputCommandOperation.cs

http://github.com/ayende/rhino-etl · C# · 73 lines · 41 code · 7 blank · 25 comment · 3 complexity · 6a8f7bb9a7c293f15de7ff70a7aae38f MD5 · raw file

  1. using System.Configuration;
  2. using Rhino.Etl.Core.Infrastructure;
  3. namespace Rhino.Etl.Core.Operations
  4. {
  5. using System.Collections.Generic;
  6. using System.Data;
  7. /// <summary>
  8. /// Generic input command operation
  9. /// </summary>
  10. public abstract class InputCommandOperation : AbstractCommandOperation
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="OutputCommandOperation"/> class.
  14. /// </summary>
  15. /// <param name="connectionStringName">Name of the connection string.</param>
  16. public InputCommandOperation(string connectionStringName)
  17. : 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 InputCommandOperation(ConnectionStringSettings connectionStringSettings)
  25. : base(connectionStringSettings)
  26. {
  27. UseTransaction = true;
  28. }
  29. /// <summary>
  30. /// Executes this operation
  31. /// </summary>
  32. /// <param name="rows">The rows.</param>
  33. /// <returns></returns>
  34. public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
  35. {
  36. using (IDbConnection connection = Use.Connection(ConnectionStringSettings))
  37. using (IDbTransaction transaction = BeginTransaction(connection))
  38. {
  39. using (currentCommand = connection.CreateCommand())
  40. {
  41. currentCommand.Transaction = transaction;
  42. PrepareCommand(currentCommand);
  43. using (IDataReader reader = currentCommand.ExecuteReader())
  44. {
  45. while (reader.Read())
  46. {
  47. yield return CreateRowFromReader(reader);
  48. }
  49. }
  50. }
  51. if (transaction != null) transaction.Commit();
  52. }
  53. }
  54. /// <summary>
  55. /// Creates a row from the reader.
  56. /// </summary>
  57. /// <param name="reader">The reader.</param>
  58. /// <returns></returns>
  59. protected abstract Row CreateRowFromReader(IDataReader reader);
  60. /// <summary>
  61. /// Prepares the command for execution, set command text, parameters, etc
  62. /// </summary>
  63. /// <param name="cmd">The command.</param>
  64. protected abstract void PrepareCommand(IDbCommand cmd);
  65. }
  66. }