PageRenderTime 414ms CodeModel.GetById 141ms app.highlight 4ms RepoModel.GetById 119ms app.codeStats 0ms

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