Commit b9c5d5d8 authored by Nick Craver's avatar Nick Craver

Contrib: DRY code current keys implementation

parent e40c7882
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper; using Dapper;
#if COREFX #if COREFX
...@@ -18,10 +17,8 @@ ...@@ -18,10 +17,8 @@
namespace Dapper.Contrib.Extensions namespace Dapper.Contrib.Extensions
{ {
public static partial class SqlMapperExtensions public static partial class SqlMapperExtensions
{ {
/// <summary> /// <summary>
/// Returns a single entity by a single id from table "Ts" asynchronously using .NET 4.5 Task. T must be of interface type. /// 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. /// Id must be marked with [Key] attribute.
...@@ -39,18 +36,9 @@ public static partial class SqlMapperExtensions ...@@ -39,18 +36,9 @@ public static partial class SqlMapperExtensions
string sql; string sql;
if (!GetQueries.TryGetValue(type.TypeHandle, out sql)) if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
{ {
var keys = KeyPropertiesCache(type); var key = GetSingleKey<T>(nameof(GetAsync));
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 name = GetTableName(type); 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"; sql = $"SELECT * FROM {name} WHERE {key.Name} = @id";
GetQueries[type.TypeHandle] = sql; GetQueries[type.TypeHandle] = sql;
} }
...@@ -58,7 +46,6 @@ public static partial class SqlMapperExtensions ...@@ -58,7 +46,6 @@ public static partial class SqlMapperExtensions
var dynParms = new DynamicParameters(); var dynParms = new DynamicParameters();
dynParms.Add("@id", id); dynParms.Add("@id", id);
if (!type.IsInterface()) if (!type.IsInterface())
return (await connection.QueryAsync<T>(sql, dynParms, transaction, commandTimeout).ConfigureAwait(false)).FirstOrDefault(); return (await connection.QueryAsync<T>(sql, dynParms, transaction, commandTimeout).ConfigureAwait(false)).FirstOrDefault();
...@@ -99,15 +86,9 @@ public static partial class SqlMapperExtensions ...@@ -99,15 +86,9 @@ public static partial class SqlMapperExtensions
string sql; string sql;
if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql)) if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql))
{ {
var keys = KeyPropertiesCache(type); GetSingleKey<T>(nameof(GetAll));
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");
var name = GetTableName(type); 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; sql = "SELECT * FROM " + name;
GetQueries[cacheType.TypeHandle] = sql; GetQueries[cacheType.TypeHandle] = sql;
} }
...@@ -301,8 +282,7 @@ public static partial class SqlMapperExtensions ...@@ -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 public static async Task<bool> DeleteAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
var type = typeof(T); var type = typeof(T);
var name = GetTableName(type); var statement = "DELETE FROM " + GetTableName(type);
var statement = $"DELETE FROM {name}";
var deleted = await connection.ExecuteAsync(statement, null, transaction, commandTimeout).ConfigureAwait(false); var deleted = await connection.ExecuteAsync(statement, null, transaction, commandTimeout).ConfigureAwait(false);
return deleted > 0; return deleted > 0;
} }
......
...@@ -127,6 +127,20 @@ private static bool IsWriteable(PropertyInfo pi) ...@@ -127,6 +127,20 @@ private static bool IsWriteable(PropertyInfo pi)
return writeAttribute.Write; 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> /// <summary>
/// Returns a single entity by a single id from table "Ts". /// Returns a single entity by a single id from table "Ts".
/// Id must be marked with [Key] attribute. /// Id must be marked with [Key] attribute.
...@@ -146,18 +160,9 @@ private static bool IsWriteable(PropertyInfo pi) ...@@ -146,18 +160,9 @@ private static bool IsWriteable(PropertyInfo pi)
string sql; string sql;
if (!GetQueries.TryGetValue(type.TypeHandle, out sql)) if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
{ {
var keys = KeyPropertiesCache(type); var key = GetSingleKey<T>(nameof(Get));
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 name = GetTableName(type); 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"; sql = $"select * from {name} where {key.Name} = @id";
GetQueries[type.TypeHandle] = sql; GetQueries[type.TypeHandle] = sql;
} }
...@@ -210,21 +215,13 @@ private static bool IsWriteable(PropertyInfo pi) ...@@ -210,21 +215,13 @@ private static bool IsWriteable(PropertyInfo pi)
string sql; string sql;
if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql)) if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql))
{ {
var keys = KeyPropertiesCache(type); GetSingleKey<T>(nameof(GetAll));
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 name = GetTableName(type); 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; sql = "select * from " + name;
GetQueries[cacheType.TypeHandle] = sql; GetQueries[cacheType.TypeHandle] = sql;
} }
if (!type.IsInterface()) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout); if (!type.IsInterface()) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);
var result = connection.Query(sql); 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