Commit b9c5d5d8 authored by Nick Craver's avatar Nick Craver

Contrib: DRY code current keys implementation

parent e40c7882
......@@ -5,7 +5,6 @@
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Dapper;
#if COREFX
......@@ -18,10 +17,8 @@
namespace Dapper.Contrib.Extensions
{
public static partial class SqlMapperExtensions
{
/// <summary>
/// Returns a single entity by a single id from table "Ts" asynchronously using .NET 4.5 Task. T must be of interface type.
/// Id must be marked with [Key] attribute.
......@@ -39,18 +36,9 @@ public static partial class SqlMapperExtensions
string sql;
if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
{
var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type);
if (keys.Count > 1 || explicitKeys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] or [ExplicitKey] property");
if (!keys.Any() && !explicitKeys.Any())
throw new DataException("Get<T> only supports an entity with a [Key] or an [ExplicitKey] property");
var key = keys.Any() ? keys.First() : explicitKeys.First();
var key = GetSingleKey<T>(nameof(GetAsync));
var name = GetTableName(type);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = $"SELECT * FROM {name} WHERE {key.Name} = @id";
GetQueries[type.TypeHandle] = sql;
}
......@@ -58,7 +46,6 @@ public static partial class SqlMapperExtensions
var dynParms = new DynamicParameters();
dynParms.Add("@id", id);
if (!type.IsInterface())
return (await connection.QueryAsync<T>(sql, dynParms, transaction, commandTimeout).ConfigureAwait(false)).FirstOrDefault();
......@@ -99,15 +86,9 @@ public static partial class SqlMapperExtensions
string sql;
if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql))
{
var keys = KeyPropertiesCache(type);
if (keys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] property");
if (!keys.Any())
throw new DataException("Get<T> only supports en entity with a [Key] property");
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = "SELECT * FROM " + name;
GetQueries[cacheType.TypeHandle] = sql;
}
......@@ -301,8 +282,7 @@ public static partial class SqlMapperExtensions
public static async Task<bool> DeleteAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);
var name = GetTableName(type);
var statement = $"DELETE FROM {name}";
var statement = "DELETE FROM " + GetTableName(type);
var deleted = await connection.ExecuteAsync(statement, null, transaction, commandTimeout).ConfigureAwait(false);
return deleted > 0;
}
......
......@@ -127,6 +127,20 @@ private static bool IsWriteable(PropertyInfo pi)
return writeAttribute.Write;
}
private static PropertyInfo GetSingleKey<T>(string method)
{
var type = typeof (T);
var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type);
var keyCount = keys.Count + explicitKeys.Count;
if (keyCount > 1)
throw new DataException($"{method}<T> only supports an entity with a single [Key] or [ExplicitKey] property");
if (keyCount == 0)
throw new DataException($"{method}<T> only supports an entity with a [Key] or an [ExplicitKey] property");
return keys.Any() ? keys.First() : explicitKeys.First();
}
/// <summary>
/// Returns a single entity by a single id from table "Ts".
/// Id must be marked with [Key] attribute.
......@@ -146,18 +160,9 @@ private static bool IsWriteable(PropertyInfo pi)
string sql;
if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
{
var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type);
if (keys.Count > 1 || explicitKeys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] or [ExplicitKey] property");
if (!keys.Any() && !explicitKeys.Any())
throw new DataException("Get<T> only supports an entity with a [Key] or an [ExplicitKey] property");
var key = keys.Any() ? keys.First() : explicitKeys.First();
var key = GetSingleKey<T>(nameof(Get));
var name = GetTableName(type);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = $"select * from {name} where {key.Name} = @id";
GetQueries[type.TypeHandle] = sql;
}
......@@ -210,21 +215,13 @@ private static bool IsWriteable(PropertyInfo pi)
string sql;
if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql))
{
var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type);
if (keys.Count > 1 || explicitKeys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] or [ExplicitKey] property");
if (!keys.Any() && !explicitKeys.Any())
throw new DataException("Get<T> only supports an entity with a [Key] or an [ExplicitKey] property");
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = "select * from " + name;
GetQueries[cacheType.TypeHandle] = sql;
}
if (!type.IsInterface()) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);
var result = connection.Query(sql);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment