PageRenderTime 363ms CodeModel.GetById 181ms app.highlight 3ms RepoModel.GetById 178ms app.codeStats 0ms

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