PageRenderTime 224ms CodeModel.GetById 141ms app.highlight 7ms RepoModel.GetById 74ms app.codeStats 0ms

/Rhino.Etl.Core/Operations/AbstractDatabaseOperation.cs

http://github.com/ayende/rhino-etl
C# | 157 lines | 98 code | 16 blank | 43 comment | 7 complexity | a0f4be9571812b34e6041197e3cf64a0 MD5 | raw file
  1using System.Configuration;
  2
  3namespace Rhino.Etl.Core.Operations
  4{
  5    using System;
  6    using System.Collections;
  7    using System.Data;
  8
  9    /// <summary>
 10    /// Represent an operation that uses the database can occure during the ETL process
 11    /// </summary>
 12    public abstract class AbstractDatabaseOperation : AbstractOperation
 13    {
 14        private readonly ConnectionStringSettings connectionStringSettings;
 15        private static Hashtable supportedTypes;
 16        ///<summary>
 17        ///The parameter prefix to use when adding parameters
 18        ///</summary>
 19        protected string paramPrefix = "";
 20
 21        /// <summary>
 22        /// Gets the connection string settings.
 23        /// </summary>
 24        /// <value>The connection string settings.</value>
 25        public ConnectionStringSettings ConnectionStringSettings
 26        {
 27            get { return connectionStringSettings; }
 28        }
 29
 30        /// <summary>
 31        /// Gets the name of the connection string.
 32        /// </summary>
 33        /// <value>The name of the connection string.</value>
 34        public string ConnectionStringName
 35        {
 36            get { return connectionStringSettings.Name; }
 37        }
 38
 39        /// <summary>
 40        /// Initializes a new instance of the <see cref="AbstractDatabaseOperation"/> class.
 41        /// </summary>
 42        /// <param name="connectionStringName">Name of the connection string.</param>
 43        protected AbstractDatabaseOperation(string connectionStringName)
 44        {
 45            Guard.Against<ArgumentException>(string.IsNullOrEmpty(connectionStringName),
 46                                             "Connection string name must have a value");            
 47
 48            Guard.Against<ArgumentException>(ConfigurationManager.ConnectionStrings[connectionStringName] == null,
 49                                             "Cannot resolve connection strings with name: " + connectionStringName);
 50
 51            connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName];
 52        }
 53
 54        /// <summary>
 55        /// Initializes a new instance of the <see cref="AbstractDatabaseOperation"/> class.
 56        /// </summary>
 57        /// <param name="connectionStringSettings">Name of the connection string.</param>
 58        protected AbstractDatabaseOperation(ConnectionStringSettings connectionStringSettings)
 59        {
 60            Guard.Against<ArgumentException>(connectionStringSettings == null,
 61                                             "connectionStringSettings must resolve to a value");
 62                        
 63            this.connectionStringSettings = connectionStringSettings;
 64        }
 65
 66         private static void InitializeSupportedTypes()
 67        {
 68            supportedTypes = new Hashtable();
 69            supportedTypes[typeof (byte[])] = typeof (byte[]);
 70            supportedTypes[typeof (Guid)] = typeof (Guid);
 71            supportedTypes[typeof (Object)] = typeof (Object);
 72            supportedTypes[typeof (Boolean)] = typeof (Boolean);
 73            supportedTypes[typeof (SByte)] = typeof (SByte);
 74            supportedTypes[typeof (SByte)] = typeof (SByte);
 75            supportedTypes[typeof (Byte)] = typeof (Byte);
 76            supportedTypes[typeof (Int16)] = typeof (Int16);
 77            supportedTypes[typeof (UInt16)] = typeof (UInt16);
 78            supportedTypes[typeof (Int32)] = typeof (Int32);
 79            supportedTypes[typeof (UInt32)] = typeof (UInt32);
 80            supportedTypes[typeof (Int64)] = typeof (Int64);
 81            supportedTypes[typeof (UInt64)] = typeof (UInt64);
 82            supportedTypes[typeof (Single)] = typeof (Single);
 83            supportedTypes[typeof (Double)] = typeof (Double);
 84            supportedTypes[typeof (Decimal)] = typeof (Decimal);
 85            supportedTypes[typeof (DateTime)] = typeof (DateTime);
 86            supportedTypes[typeof (String)] = typeof (String);
 87        }
 88
 89        private static Hashtable SupportedTypes
 90        {
 91            get
 92            {
 93                if (supportedTypes == null)
 94                {
 95                    InitializeSupportedTypes();
 96                }
 97                return supportedTypes;
 98            }
 99        }
100
101        /// <summary>
102        /// Copies the row values to command parameters.
103        /// </summary>
104        /// <param name="command">The command.</param>
105        /// <param name="row">The row.</param>
106        protected void CopyRowValuesToCommandParameters(IDbCommand command, Row row)
107        {
108            foreach (string column in row.Columns)
109            {
110                object value = row[column];
111                if (CanUseAsParameter(value))
112                    AddParameter(command, column, value);
113            }
114        }
115
116        /// <summary>
117        /// Adds the parameter the specifed command
118        /// </summary>
119        /// <param name="command">The command.</param>
120        /// <param name="name">The name.</param>
121        /// <param name="val">The val.</param>
122        protected void AddParameter(IDbCommand command, string name, object val)
123        {
124            IDbDataParameter parameter = command.CreateParameter();
125            parameter.ParameterName = paramPrefix + name;
126            parameter.Value = val ?? DBNull.Value;
127            command.Parameters.Add(parameter);
128        }
129
130        /// <summary>
131        /// Determines whether this value can be use as a parameter to ADO.Net provider.
132        /// This perform a simple heuristic 
133        /// </summary>
134        /// <param name="value">The value.</param>
135        private static bool CanUseAsParameter(object value)
136        {
137            if(value==null)
138                return true;
139            return SupportedTypes.ContainsKey(value.GetType());
140        }
141
142        /// <summary>
143        /// Begins a transaction conditionally based on the UseTransaction property
144        /// </summary>
145        /// <param name="connection">The IDbConnection object you are working with</param>
146        /// <returns>An open IDbTransaction object or null.</returns>
147        protected IDbTransaction BeginTransaction(IDbConnection connection)
148        {
149            if (UseTransaction)
150            {
151                return connection.BeginTransaction();
152            }
153
154            return null;
155        }
156    }
157}