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