프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / AbstractDatabase.cs @ b37ef4b3

이력 | 보기 | 이력해설 | 다운로드 (12.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
using System.Data;
6
using System.Data.Common;
7

    
8
namespace KCOM
9
{
10
    public class ColInfo
11
    {
12
        public string Name { get; set; }
13
        public string DataType { get; set; }
14
    }
15

    
16
    /// <summary>
17
    /// Abstract base class for encapsulating provider independant database interactin logic.
18
    /// </summary>
19
    /// <remarks>Version 2.0</remarks>
20
    /// <typeparam name="CONNECTION_TYPE"><see cref="DbConnection"/> derived Connection type.</typeparam>
21
    /// <typeparam name="COMMAND_TYPE"><see cref="DbCommand"/> derived Command type.</typeparam>
22
    /// <typeparam name="ADAPTER_TYPE"><see cref="DbDataAdapater"/> derived Data Adapter type.</typeparam>
23
    public abstract class AbstractDatabase<CONNECTION_TYPE, COMMAND_TYPE, ADAPTER_TYPE> : IDisposable, IAbstractDatabase
24
        where CONNECTION_TYPE : DbConnection, new()
25
        where COMMAND_TYPE : DbCommand
26
        where ADAPTER_TYPE : DbDataAdapter, new()
27
    {
28
        #region : Connection :
29

    
30
        /// <summary>Gets the Connection object associated with the current instance.</summary>
31
        public DbConnection Connection
32
        {
33
            get
34
            {
35
                if (internal_currentConnection == null)
36
                {
37
                    internal_currentConnection = new CONNECTION_TYPE() { ConnectionString = GetConnectionString() };
38
                }
39
                return internal_currentConnection;
40
            }
41
        }
42
        private DbConnection internal_currentConnection = null;
43

    
44
        /// <summary>When overridden in derived classes returns the connection string for the database.</summary>
45
        /// <returns>The connection string for the database.</returns>
46
        protected abstract string GetConnectionString();
47

    
48
        #endregion
49

    
50
        #region : Commands :
51

    
52
        /// <summary>Gets a DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</summary>
53
        /// <param name="sqlString">The SQL string.</param>
54
        /// <returns>A DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</returns>
55
        public DbCommand GetSqlStringCommand(string sqlString)
56
        {
57
            if (this.Connection.State != ConnectionState.Open)
58
                this.Connection.Open();
59

    
60
            DbCommand cmd = this.Connection.CreateCommand();
61
            cmd.CommandType = CommandType.Text;
62
            cmd.CommandText = sqlString;
63
            cmd.CommandTimeout = 600; // 15.11.04 added by soohyun - Extend Time out of query
64
            return cmd;
65
        }
66

    
67
        /// <summary>Gets a DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</summary>
68
        /// <param name="sqlStringFormat">The SQL string format.</param>
69
        /// <param name="args">The format arguments.</param>
70
        /// <returns>A DbCommand object with the specified <see cref="DbCommand.CommandText"/>.</returns>
71
        public DbCommand GetSqlStringCommand(string sqlStringFormat, params object[] args)
72
        {
73
            return GetSqlStringCommand(string.Format(sqlStringFormat, args));
74
        }
75

    
76
        /// <summary>Gets a DbCommand object for the specified Stored Procedure.</summary>
77
        /// <param name="storedProcName">The name of the stored procedure.</param>
78
        /// <returns>A DbCommand object for the specified Stored Procedure.</returns>
79
        public DbCommand GetStoredProcedureCommand(string storedProcName)
80
        {
81
            if (this.Connection.State != ConnectionState.Open)
82
                this.Connection.Open();
83

    
84
            DbCommand cmd = this.Connection.CreateCommand();
85
            cmd.CommandType = CommandType.StoredProcedure;
86
            cmd.CommandText = storedProcName;
87
            return cmd;
88
        }
89

    
90
        #region : Parameters :
91

    
92
        /// <summary>Adds an input parameter to the given <see cref="DbCommand"/>.</summary>
93
        /// <param name="cmd">The command object the parameter should be added to.</param>
94
        /// <param name="paramName">The identifier of the parameter.</param>
95
        /// <param name="paramType">The type of the parameter.</param>
96
        /// <param name="value">The value of the parameter.</param>
97
        /// <returns>The <see cref="DbParameter"/> that was created.</returns>
98
        public DbParameter AddInParameter(DbCommand cmd, string paramName, DbType paramType, object value)
99
        {
100
            return AddParameter(cmd, paramName, paramType, ParameterDirection.Input, value);
101
        }
102

    
103
        /// <summary>Adds an input parameter to the given <see cref="DbCommand"/>.</summary>
104
        /// <param name="cmd">The command object the parameter should be added to.</param>
105
        /// <param name="paramName">The identifier of the parameter.</param>
106
        /// <param name="paramType">The type of the parameter.</param>
107
        /// <param name="size">The maximum size in bytes, of the data table column to be affected.</param>
108
        /// <param name="value">The value of the parameter.</param>
109
        /// <returns>The <see cref="DbParameter"/> that was created.</returns>
110
        public DbParameter AddInParameter(DbCommand cmd, string paramName, DbType paramType, int size, object value)
111
        {
112
            DbParameter param = AddInParameter(cmd, paramName, paramType, value);
113
            param.Size = size;
114
            cmd.Parameters.Add(param);
115
            return param;
116
        }
117

    
118
        /// <summary>Adds the out parameter to the given <see cref="DbCommand"/></summary>
119
        /// <param name="cmd">The command object the parameter should be added to.</param>
120
        /// <param name="paramName">The identifier of the parameter.</param>
121
        /// <param name="paramType">The type of the parameter.</param>
122
        /// <param name="value">The value of the parameter.</param>
123
        /// <returns>The <see cref="DbParameter"/> that was created.</returns>
124
        public DbParameter AddOutParameter(DbCommand cmd, string paramName, DbType paramType, object value)
125
        {
126
            return AddParameter(cmd, paramName, paramType, ParameterDirection.Output, value);
127
        }
128

    
129
        /// <summary>Adds a parameter to the given <see cref="DbCommand"/>.</summary>
130
        /// <param name="cmd">The command object the parameter should be added to.</param>
131
        /// <param name="paramName">The identifier of the parameter.</param>
132
        /// <param name="paramType">The type of the parameter.</param>
133
        /// <param name="direction"><see cref="ParameterDirection"/> of the parameter.</param>
134
        /// <param name="value">The value of the parameter.</param>
135
        /// <returns>The <see cref="DbParameter"/> that was created.</returns>
136
        public DbParameter AddParameter(DbCommand cmd, string paramName,
137
                                            DbType paramType,
138
                                            ParameterDirection direction,
139
                                            object value)
140
        {
141
            DbParameter param = cmd.CreateParameter();
142
            param.DbType = paramType;
143
            param.ParameterName = paramName;
144
            param.Value = value;
145
            param.Direction = direction;
146
            cmd.Parameters.Add(param);
147
            return param;
148
        }
149

    
150
        /// <summary>Adds a parameter to the given <see cref="DbCommand"/>.</summary>
151
        /// <param name="cmd">The command object the parameter should be added to.</param>
152
        /// <param name="paramName">The identifier of the parameter.</param>
153
        /// <param name="paramType">The type of the parameter.</param>
154
        /// <param name="direction"><see cref="ParameterDirection"/> of the parameter.</param>
155
        /// <param name="size">The maximum size in bytes, of the data table column to be affected.</param>
156
        /// <param name="value">The value of the parameter.</param>
157
        /// <returns>The <see cref="DbParameter"/> that was created.</returns>
158
        public DbParameter AddParameter(DbCommand cmd, string paramName,
159
                                            DbType paramType,
160
                                            ParameterDirection direction,
161
                                            int size,
162
                                            object value)
163
        {
164
            DbParameter param = AddParameter(cmd, paramName, paramType, direction, value);
165
            param.Size = size;
166
            return param;
167
        }
168

    
169
        #endregion
170

    
171
        #region : Executes :
172

    
173
        /// <summary>Executes the specified command against the current connection.</summary>
174
        /// <param name="cmd">The command to be executed.</param>
175
        /// <returns>Result returned by the database engine.</returns>
176
        public int ExecuteNonQuery(DbCommand cmd)
177
        {
178
            if (this.Connection.State != ConnectionState.Open)
179
                this.Connection.Open();
180

    
181
            return cmd.ExecuteNonQuery();
182
        }
183

    
184
        public int ExecuteNonQuery(string sSql)
185
        {
186
            if (this.Connection.State != ConnectionState.Open)
187
                this.Connection.Open();
188

    
189
            return GetSqlStringCommand(sSql).ExecuteNonQuery();
190
        }
191

    
192
        /// <summary>Executes the specified command against the current connection.</summary>
193
        /// <param name="cmd">The command to be executed.</param>
194
        /// <param name="txn">The database transaction inside which the command should be executed.</param>
195
        /// <returns>Result returned by the database engine.</returns>
196
        public int ExecuteNonQuery(DbCommand cmd, DbTransaction txn)
197
        {
198
            if (this.Connection.State != ConnectionState.Open)
199
                this.Connection.Open();
200

    
201
            cmd.Transaction = txn;
202
            return cmd.ExecuteNonQuery();
203
        }
204

    
205
        /// <summary>Executes the specified command against the current connection.</summary>
206
        /// <param name="cmd">The command to be executed.</param>
207
        /// <returns>Result returned by the database engine.</returns>
208
        public DbDataReader ExecuteReader(DbCommand cmd)
209
        {
210
            if (this.Connection.State != ConnectionState.Open)
211
                this.Connection.Open();
212

    
213
            return cmd.ExecuteReader();
214
        }
215

    
216
        /// <summary>Executes the specified command against the current connection.</summary>
217
        /// <param name="cmd">The command to be executed.</param>
218
        /// <param name="behavior">One of the <see cref="System.Data.CommandBehavior"/> values.</param>
219
        /// <returns>Result returned by the database engine.</returns>
220
        public DbDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
221
        {
222
            if (this.Connection.State != ConnectionState.Open)
223
                this.Connection.Open();
224

    
225
            return cmd.ExecuteReader(behavior);
226
        }
227

    
228
        /// <summary>Executes the specified command against the current connection.</summary>
229
        /// <param name="cmd">The command to be executed.</param>
230
        /// <returns>Result returned by the database engine.</returns>
231
        public T ExecuteScalar<T>(DbCommand cmd, T defaultValue)
232
        {
233
            if (this.Connection.State != ConnectionState.Open)
234
                this.Connection.Open();
235

    
236
            object retVal = cmd.ExecuteScalar();
237
            if (null == retVal || DBNull.Value == retVal)
238
                return defaultValue;
239
            else
240
                return (T)retVal;
241
        }
242

    
243
        /// <summary>Executes the specified command against the current connection.</summary>
244
        /// <param name="cmd">The command to be executed.</param>
245
        /// <returns>Result returned by the database engine.</returns>
246
        public DataSet ExecuteDataSet(DbCommand cmd, DbTransaction txn = null)
247
        {
248
            ADAPTER_TYPE adapter = new ADAPTER_TYPE();
249
            if (null != txn) cmd.Transaction = txn;
250
            adapter.SelectCommand = (COMMAND_TYPE)cmd;
251

    
252
            DataSet retVal = new DataSet()
253
            {
254
                Locale = System.Globalization.CultureInfo.InvariantCulture
255
            };
256
            adapter.Fill(retVal);
257
            return retVal;
258
        }
259

    
260
        #endregion
261

    
262
        #endregion
263

    
264
        /// <summary>Begins a transaction.</summary>
265
        /// <returns>Created transaction.</returns>
266
        public DbTransaction BeginTransaction()
267
        {
268
            if (this.Connection.State != ConnectionState.Open)
269
                this.Connection.Open();
270
            return Connection.BeginTransaction();
271
        }
272

    
273
        #region : Consturction / Destruction :
274

    
275
        /// <summary>Disposes the resources associated with the current database connection.</summary>
276
        ~AbstractDatabase()
277
        {
278
            Dispose();
279
        }
280

    
281
        #region IDisposable Members
282

    
283
        /// <summary>Disposes the resources associated with the current database connection.</summary>
284
        public void Dispose()
285
        {
286
            if (null != internal_currentConnection)
287
            {
288
                internal_currentConnection.Dispose();
289
                internal_currentConnection = null;
290
            }
291
        }
292

    
293
        #endregion
294

    
295
        #endregion
296
    }
297
}
클립보드 이미지 추가 (최대 크기: 500 MB)