hytos / ID2.Manager / ID2.Manager.Dapper / Repository / BaseRepository.cs @ 1018a498
이력 | 보기 | 이력해설 | 다운로드 (8.36 KB)
1 | 8164f84e | yoush97 | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | |||
7 | using System.Data; |
||
8 | using System.Data.SqlClient; |
||
9 | using System.ComponentModel.DataAnnotations.Schema; |
||
10 | c7f3eb42 | taeseongkim | #if !MARKUS_IMAGE_CREATE |
11 | fdecb3be | yoush97 | using System.Data.SQLite; |
12 | c7f3eb42 | taeseongkim | #endif |
13 | 8164f84e | yoush97 | using Dapper; |
14 | |||
15 | namespace ID2.Manager.Dapper.Repository |
||
16 | { |
||
17 | public abstract class BaseRepository : IBaseRepository |
||
18 | { |
||
19 | fdecb3be | yoush97 | protected readonly IDbConnection _DbConnection; |
20 | d2d4f84b | yoush97 | protected IDbTransaction _DbTransaction = null; |
21 | 8164f84e | yoush97 | |
22 | e8b34346 | yoush97 | protected BaseRepository(string connectionStr) : this(connectionStr, DatabaseType.MSSQL) { } |
23 | 8164f84e | yoush97 | |
24 | fdecb3be | yoush97 | protected BaseRepository(string connectionStr, DatabaseType dbType) |
25 | d2d4f84b | yoush97 | { |
26 | fdecb3be | yoush97 | switch (dbType) |
27 | d2d4f84b | yoush97 | { |
28 | c7f3eb42 | taeseongkim | #if !MARKUS_IMAGE_CREATE |
29 | fdecb3be | yoush97 | case DatabaseType.SQLITE: |
30 | this._DbConnection = this.SQLiteDBConnection(connectionStr); |
||
31 | break; |
||
32 | c7f3eb42 | taeseongkim | #endif |
33 | fdecb3be | yoush97 | case DatabaseType.MSSQL: |
34 | default: |
||
35 | this._DbConnection = this.DBConnection(connectionStr); |
||
36 | break; |
||
37 | d2d4f84b | yoush97 | } |
38 | |||
39 | 8164f84e | yoush97 | SqlMapper.TypeMapProvider = type => |
40 | { |
||
41 | // create fallback default type map |
||
42 | var fallback = new DefaultTypeMap(type); |
||
43 | return new CustomPropertyTypeMap(type, (t, column) => |
||
44 | { |
||
45 | var property = t.GetProperties().FirstOrDefault(prop => |
||
46 | prop.GetCustomAttributes(typeof(ColumnAttribute), true) |
||
47 | .Cast<ColumnAttribute>() |
||
48 | .Any(attr => attr.Name == column)); |
||
49 | |||
50 | // if no property matched - fall back to default type map |
||
51 | if (property == null) |
||
52 | { |
||
53 | property = fallback.GetMember(column)?.Property; |
||
54 | } |
||
55 | |||
56 | return property; |
||
57 | }); |
||
58 | }; |
||
59 | be2dacfc | yoush97 | } |
60 | fdecb3be | yoush97 | |
61 | be2dacfc | yoush97 | public SqlConnection DBConnection(string connectionStr) |
62 | { |
||
63 | fdecb3be | yoush97 | return new SqlConnection(connectionStr); |
64 | } |
||
65 | c7f3eb42 | taeseongkim | #if !MARKUS_IMAGE_CREATE |
66 | fdecb3be | yoush97 | public SQLiteConnection SQLiteDBConnection(string connectionStr) |
67 | { |
||
68 | be2dacfc | yoush97 | return new SQLiteConnection(connectionStr, true); |
69 | fdecb3be | yoush97 | } |
70 | c7f3eb42 | taeseongkim | #endif |
71 | fdecb3be | yoush97 | |
72 | public IDbTransaction BeginTransaction() |
||
73 | { |
||
74 | if (this._DbConnection.State != ConnectionState.Open) |
||
75 | { |
||
76 | this._DbConnection.Open(); |
||
77 | } |
||
78 | |||
79 | return this._DbConnection.BeginTransaction(); |
||
80 | } |
||
81 | |||
82 | public void Dispose() |
||
83 | { |
||
84 | if (this._DbTransaction != null) |
||
85 | { |
||
86 | this._DbTransaction.Commit(); |
||
87 | } |
||
88 | |||
89 | if (this._DbConnection != null) |
||
90 | { |
||
91 | this._DbConnection.Close(); |
||
92 | } |
||
93 | 8164f84e | yoush97 | } |
94 | |||
95 | public IEnumerable<T> Query<T>(string sql) |
||
96 | { |
||
97 | if (this._DbConnection.State != ConnectionState.Open) |
||
98 | { |
||
99 | this._DbConnection.Open(); |
||
100 | } |
||
101 | |||
102 | return this._DbConnection.Query<T>(sql); |
||
103 | } |
||
104 | |||
105 | 08499f5f | taeseongkim | public IEnumerable<TReturn> MultiQuery<TFirst, TSecond, TFourth, TReturn>(string sql, Func<TFirst, TSecond, TFourth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) |
106 | e458a996 | taeseongkim | { |
107 | if (this._DbConnection.State != ConnectionState.Open) |
||
108 | { |
||
109 | this._DbConnection.Open(); |
||
110 | } |
||
111 | 08499f5f | taeseongkim | |
112 | return this._DbConnection.Query<TFirst, TSecond, TFourth, TReturn >(sql, map: map, param: param, transaction: transaction, buffered: buffered, splitOn: splitOn, commandTimeout: commandTimeout, commandType: commandType); |
||
113 | e458a996 | taeseongkim | } |
114 | |||
115 | 9096bc6d | taeseongkim | public IEnumerable<TReturn> MultiQuery<TFirst, TSecond, TFourth, TFifth, TReturn>(string sql, Func<TFirst, TSecond, TFourth, TFifth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) |
116 | { |
||
117 | if (this._DbConnection.State != ConnectionState.Open) |
||
118 | { |
||
119 | this._DbConnection.Open(); |
||
120 | } |
||
121 | |||
122 | return this._DbConnection.Query<TFirst, TSecond, TFourth, TFifth, TReturn>(sql, map: map, param: param, transaction: transaction, buffered: buffered, splitOn: splitOn, commandTimeout: commandTimeout, commandType: commandType); |
||
123 | } |
||
124 | |||
125 | |||
126 | 098748fa | taeseongkim | public T QueryFirst<T>(string sql) |
127 | { |
||
128 | if (this._DbConnection.State != ConnectionState.Open) |
||
129 | { |
||
130 | this._DbConnection.Open(); |
||
131 | } |
||
132 | |||
133 | return this._DbConnection.QueryFirst<T>(sql); |
||
134 | } |
||
135 | |||
136 | 978488b0 | yoush97 | public T QueryFirstOrDefault<T>(string sql, object param, IDbTransaction dbTran = null) |
137 | { |
||
138 | if (this._DbConnection.State != ConnectionState.Open) |
||
139 | { |
||
140 | this._DbConnection.Open(); |
||
141 | } |
||
142 | |||
143 | e458a996 | taeseongkim | return this._DbConnection.QueryFirstOrDefault<T>(sql, param, transaction: dbTran); |
144 | 978488b0 | yoush97 | } |
145 | |||
146 | 098748fa | taeseongkim | |
147 | 8164f84e | yoush97 | public IEnumerable<T> Query<T>(string sql, object param) |
148 | { |
||
149 | if (this._DbConnection.State != ConnectionState.Open) |
||
150 | { |
||
151 | this._DbConnection.Open(); |
||
152 | } |
||
153 | |||
154 | return this._DbConnection.Query<T>(sql, param); |
||
155 | } |
||
156 | e458a996 | taeseongkim | public IEnumerable<T> Query<T>(string sql, object param, CommandType commandType) |
157 | 0b008ea6 | taeseongkim | { |
158 | if (this._DbConnection.State != ConnectionState.Open) |
||
159 | { |
||
160 | this._DbConnection.Open(); |
||
161 | } |
||
162 | |||
163 | e458a996 | taeseongkim | return this._DbConnection.Query<T>(sql, param, commandType: commandType); |
164 | 0b008ea6 | taeseongkim | } |
165 | 8164f84e | yoush97 | |
166 | public async Task<IEnumerable<T>> QueryAsync<T>(string sql) |
||
167 | { |
||
168 | if (this._DbConnection.State != ConnectionState.Open) |
||
169 | { |
||
170 | this._DbConnection.Open(); |
||
171 | } |
||
172 | |||
173 | return await this._DbConnection.QueryAsync<T>(sql); |
||
174 | } |
||
175 | |||
176 | public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param) |
||
177 | { |
||
178 | if (this._DbConnection.State != ConnectionState.Open) |
||
179 | { |
||
180 | this._DbConnection.Open(); |
||
181 | } |
||
182 | |||
183 | return await this._DbConnection.QueryAsync<T>(sql, param); |
||
184 | } |
||
185 | |||
186 | 709c1971 | yoush97 | public int Execute(string query, IDbTransaction dbTran) |
187 | { |
||
188 | d2d4f84b | yoush97 | return this.Execute(query, null, dbTran); |
189 | 709c1971 | yoush97 | } |
190 | |||
191 | 8164f84e | yoush97 | public int Execute(string query, object param, IDbTransaction dbTran) |
192 | { |
||
193 | if (this._DbConnection.State != ConnectionState.Open) |
||
194 | { |
||
195 | this._DbConnection.Open(); |
||
196 | } |
||
197 | |||
198 | return this._DbConnection.Execute(query, param, dbTran); |
||
199 | } |
||
200 | |||
201 | c112c3c3 | taeseongkim | |
202 | public int Execute(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType) |
||
203 | { |
||
204 | if (_DbConnection.State != System.Data.ConnectionState.Open) |
||
205 | { |
||
206 | _DbConnection.Open(); |
||
207 | } |
||
208 | |||
209 | |||
210 | return _DbConnection.Execute(SQL, parameters, transaction, commandType: commandType); |
||
211 | } |
||
212 | |||
213 | 8164f84e | yoush97 | public async Task<int> ExecuteAsync(string query, object param, IDbTransaction dbTran) |
214 | { |
||
215 | if (this._DbConnection.State != ConnectionState.Open) |
||
216 | { |
||
217 | this._DbConnection.Open(); |
||
218 | } |
||
219 | |||
220 | return await this._DbConnection.ExecuteAsync(query, param, dbTran); |
||
221 | } |
||
222 | 1a88080d | taeseongkim | |
223 | |||
224 | public async Task<int> ExecuteAsync(string SQL, SqlMapper.IDynamicParameters parameters, IDbTransaction transaction, CommandType commandType) |
||
225 | { |
||
226 | if (_DbConnection.State != System.Data.ConnectionState.Open) |
||
227 | { |
||
228 | _DbConnection.Open(); |
||
229 | } |
||
230 | |||
231 | |||
232 | return await _DbConnection.ExecuteAsync(SQL, parameters, transaction, commandType: commandType); |
||
233 | } |
||
234 | ff990784 | yoush97 | public T ExecuteScalar<T>(string query, object param) |
235 | { |
||
236 | if (this._DbConnection.State != ConnectionState.Open) |
||
237 | { |
||
238 | this._DbConnection.Open(); |
||
239 | } |
||
240 | |||
241 | return this._DbConnection.ExecuteScalar<T>(query, param); |
||
242 | } |
||
243 | |||
244 | a23d0a0c | yoush97 | |
245 | public T ExecuteScalar<T>(string query, object param, IDbTransaction dbTran) |
||
246 | { |
||
247 | if (_DbConnection.State != ConnectionState.Open) |
||
248 | { |
||
249 | _DbConnection.Open(); |
||
250 | } |
||
251 | |||
252 | return this._DbConnection.ExecuteScalar<T>(query, param, dbTran); |
||
253 | } |
||
254 | 8164f84e | yoush97 | } |
255 | } |