|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Data.Common; |
| 5 | + |
| 6 | +namespace StackExchange.Exceptional.Dapper |
| 7 | +{ |
| 8 | + internal static class DapperExtensions |
| 9 | + { |
| 10 | + public static int Execute(this DbConnection conn, string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null) |
| 11 | + { |
| 12 | + using (conn.EnsureOpen()) |
| 13 | + { |
| 14 | + return SqlMapper.Execute(conn, sql, param as object, transaction, commandTimeout: commandTimeout); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public static IEnumerable<T> Query<T>(this DbConnection conn, string sql, dynamic param = null, bool buffered = true, int? commandTimeout = null, IDbTransaction transaction = null) |
| 19 | + { |
| 20 | + using (conn.EnsureOpen()) |
| 21 | + { |
| 22 | + return SqlMapper.Query<T>(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(this DbConnection conn, string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) |
| 27 | + { |
| 28 | + using (conn.EnsureOpen()) |
| 29 | + { |
| 30 | + return SqlMapper.Query<TReturn>(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(this DbConnection conn, string sql, Func<TFirst, TSecond, TThird, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) |
| 35 | + { |
| 36 | + using (conn.EnsureOpen()) |
| 37 | + { |
| 38 | + return SqlMapper.Query<TReturn>(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(this DbConnection conn, string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) |
| 43 | + { |
| 44 | + using (conn.EnsureOpen()) |
| 45 | + { |
| 46 | + return SqlMapper.Query<TReturn>(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this DbConnection conn, string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) |
| 51 | + { |
| 52 | + using (conn.EnsureOpen()) |
| 53 | + { |
| 54 | + return SqlMapper.Query<TReturn>(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static IEnumerable<dynamic> Query(this DbConnection conn, string sql, dynamic param = null, bool buffered = true, IDbTransaction transaction = null, int? commandTimeout = null) |
| 59 | + { |
| 60 | + using (conn.EnsureOpen()) |
| 61 | + { |
| 62 | + return SqlMapper.Query(conn, sql, param as object, transaction, buffered, commandTimeout); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static IDisposable EnsureOpen(this DbConnection connection) |
| 67 | + { |
| 68 | + if (connection == null) throw new ArgumentNullException("connection"); |
| 69 | + switch (connection.State) |
| 70 | + { |
| 71 | + case ConnectionState.Open: |
| 72 | + return null; |
| 73 | + case ConnectionState.Closed: |
| 74 | + connection.Open(); |
| 75 | + try |
| 76 | + { |
| 77 | + return new ConnectionCloser(connection); |
| 78 | + } |
| 79 | + catch |
| 80 | + { |
| 81 | + try { connection.Close(); } |
| 82 | + catch { } // we're already trying to handle, kthxbye |
| 83 | + throw; |
| 84 | + } |
| 85 | + |
| 86 | + default: |
| 87 | + throw new InvalidOperationException("Cannot use EnsureOpen when connection is " + connection.State); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private class ConnectionCloser : IDisposable |
| 92 | + { |
| 93 | + DbConnection connection; |
| 94 | + public ConnectionCloser(DbConnection connection) |
| 95 | + { |
| 96 | + this.connection = connection; |
| 97 | + } |
| 98 | + public void Dispose() |
| 99 | + { |
| 100 | + var cn = connection; |
| 101 | + connection = null; |
| 102 | + if (cn != null) |
| 103 | + { |
| 104 | + try { cn.Close(); } |
| 105 | + catch { }//throwing from Dispose() is so lame |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments