Skip to content

Commit

Permalink
try fix no parameters bug of Dapper. see DapperLib/Dapper#1169
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny-wantgoo committed Sep 27, 2023
1 parent 6d31f93 commit c6eff25
Show file tree
Hide file tree
Showing 2 changed files with 713 additions and 535 deletions.
@@ -0,0 +1,246 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Dapper;

namespace Chef.DbAccess.SqlServer.Extensions
{
internal static class IDbConnectionExtension
{
public static async Task<T> TryQuerySingleOrDefaultAsync<T>(
this IDbConnection cnn,
string sql,
object param = null,
IDbTransaction transaction = null,
int? commandTimeout = null,
CommandType? commandType = null)
{
T result;

try
{
result = await cnn.QuerySingleOrDefaultAsync<T>(sql, param, transaction, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QuerySingleOrDefaultAsync<T>(sql, param, transaction, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TThird, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TThird, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TThird, TFourth, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TThird, TFourth, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<TReturn>> TryQueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(
this IDbConnection cnn,
string sql,
Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map,
object param = null,
IDbTransaction transaction = null,
bool buffered = true,
string splitOn = "Id",
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<TReturn> result;

try
{
result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync(sql, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
}

return result;
}

public static async Task<IEnumerable<T>> TryQueryAsync<T>(
this IDbConnection cnn,
string sql,
object param = null,
IDbTransaction transaction = null,
int? commandTimeout = null,
CommandType? commandType = null)
{
IEnumerable<T> result;

try
{
result = await cnn.QueryAsync<T>(sql, param, transaction, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.QueryAsync<T>(sql, param, transaction, commandTimeout, commandType);
}

return result;
}

public static async Task<int> TryExecuteAsync(
this IDbConnection cnn,
string sql,
object param = null,
IDbTransaction transaction = null,
int? commandTimeout = null,
CommandType? commandType = null)
{
int result;

try
{
result = await cnn.ExecuteAsync(sql, param, transaction, commandTimeout, commandType);
}
catch (SqlException sqlEx) when (sqlEx.Number is 137 or 102)
{
SqlMapper.PurgeQueryCache();

result = await cnn.ExecuteAsync(sql, param, transaction, commandTimeout, commandType);
}

return result;
}
}
}

0 comments on commit c6eff25

Please sign in to comment.