Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape id column name in Get, GetAsync #149

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.json = version.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "src\Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Tests.Contrib", "tests\Dapper.Tests.Contrib\Dapper.Tests.Contrib.csproj", "{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}"
EndProject
Expand Down
19 changes: 14 additions & 5 deletions src/Dapper.Contrib/SqlMapperExtensions.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ public static partial class SqlMapperExtensions
public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);
if (!GetQueries.TryGetValue(type.TypeHandle, out string sql))

// get query caching depends on connection specific formatting
var typeTuple = Tuple.Create(connection.GetType().TypeHandle, type.TypeHandle);

if (!GetQueries.TryGetValue(typeTuple, out string sql))
{
var key = GetSingleKey<T>(nameof(GetAsync));
var name = GetTableName(type);

sql = $"SELECT * FROM {name} WHERE {key.Name} = @id";
GetQueries[type.TypeHandle] = sql;
var adapter = GetFormatter(connection);

var sbGetQuery = new StringBuilder($"SELECT * FROM {name} WHERE ");
adapter.AppendColumnName(sbGetQuery, key.Name);
sbGetQuery.Append(" = @id");
sql = sbGetQuery.ToString();
GetQueries[typeTuple] = sql;
}

var dynParams = new DynamicParameters();
Expand Down Expand Up @@ -83,13 +92,13 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
var type = typeof(T);
var cacheType = typeof(List<T>);

if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
if (!GetAllQueries.TryGetValue(cacheType.TypeHandle, out string sql))
{
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);

sql = "SELECT * FROM " + name;
GetQueries[cacheType.TypeHandle] = sql;
GetAllQueries[cacheType.TypeHandle] = sql;
}

if (!type.IsInterface)
Expand Down
24 changes: 18 additions & 6 deletions src/Dapper.Contrib/SqlMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public interface ITableNameMapper
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> ExplicitKeyProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> TypeProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> ComputedProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetAllQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>();
/// <summary>
/// Key is a composite of connection type, entity type
/// </summary>
private static readonly ConcurrentDictionary<Tuple<RuntimeTypeHandle, RuntimeTypeHandle>, string> GetQueries = new ConcurrentDictionary<Tuple<RuntimeTypeHandle, RuntimeTypeHandle>, string>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>();

private static readonly ISqlAdapter DefaultAdapter = new SqlServerAdapter();
Expand Down Expand Up @@ -171,13 +175,21 @@ public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction
{
var type = typeof(T);

if (!GetQueries.TryGetValue(type.TypeHandle, out string sql))
// get query caching depends on connection specific formatting
var typeTuple = Tuple.Create(connection.GetType().TypeHandle, type.TypeHandle);

if (!GetQueries.TryGetValue(typeTuple, out string sql))
{
var key = GetSingleKey<T>(nameof(Get));
var name = GetTableName(type);

sql = $"select * from {name} where {key.Name} = @id";
GetQueries[type.TypeHandle] = sql;
var adapter = GetFormatter(connection);

var sbGetQuery = new StringBuilder($"SELECT * FROM {name} WHERE ");
adapter.AppendColumnName(sbGetQuery, key.Name);
sbGetQuery.Append(" = @id");
sql = sbGetQuery.ToString();
GetQueries[typeTuple] = sql;
}

var dynParams = new DynamicParameters();
Expand Down Expand Up @@ -234,13 +246,13 @@ public static IEnumerable<T> GetAll<T>(this IDbConnection connection, IDbTransac
var type = typeof(T);
var cacheType = typeof(List<T>);

if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
if (!GetAllQueries.TryGetValue(cacheType.TypeHandle, out string sql))
{
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);

sql = "select * from " + name;
GetQueries[cacheType.TypeHandle] = sql;
GetAllQueries[cacheType.TypeHandle] = sql;
}

if (!type.IsInterface) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);
Expand Down