Commit 8c7a14cd authored by Marc Gravell's avatar Marc Gravell

Merge branch 'master' of github.com:StackExchange/dapper-dot-net

parents 0f218852 336cacbe
......@@ -127,10 +127,11 @@ public static partial class SqlMapperExtensions
public static async Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
{
var isList = false;
var type = typeof(T);
if (sqlAdapter == null)
sqlAdapter = GetFormatter(connection);
var isList = false;
if (type.IsArray || type.IsGenericType())
{
isList = true;
......@@ -147,7 +148,7 @@ public static partial class SqlMapperExtensions
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{
var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbColumnList.AppendFormat("[{0}]", property.Name);
sqlAdapter.AppendColumnName(sbColumnList, property.Name);
if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbColumnList.Append(", ");
}
......@@ -163,8 +164,6 @@ public static partial class SqlMapperExtensions
if (!isList) //single entity
{
if (sqlAdapter == null)
sqlAdapter = GetFormatter(connection);
return await sqlAdapter.InsertAsync(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
sbParameterList.ToString(), keyProperties, entityToInsert);
}
......@@ -211,10 +210,12 @@ public static partial class SqlMapperExtensions
var computedProperties = ComputedPropertiesCache(type);
var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList();
var adapter = GetFormatter(connection);
for (var i = 0; i < nonIdProps.Count; i++)
{
var property = nonIdProps.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
adapter.AppendColumnNameEqualsValue(sb, property.Name);
if (i < nonIdProps.Count - 1)
sb.AppendFormat(", ");
}
......@@ -222,7 +223,7 @@ public static partial class SqlMapperExtensions
for (var i = 0; i < keyProperties.Count; i++)
{
var property = keyProperties.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
adapter.AppendColumnNameEqualsValue(sb, property.Name);
if (i < keyProperties.Count - 1)
sb.AppendFormat(" and ");
}
......@@ -348,12 +349,12 @@ public partial class MySqlAdapter
var id = r.First().id;
if (id == null) return 0;
var pi = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
if (!pi.Any()) return id;
if (!pi.Any()) return Convert.ToInt32(id);
var idp = pi.First();
idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);
return (int)id;
return Convert.ToInt32(id);
}
}
......
......@@ -42,6 +42,7 @@ public interface ITableNameMapper
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly ISqlAdapter DefaultAdapter = new SqlServerAdapter();
private static readonly Dictionary<string, ISqlAdapter> AdapterDictionary
= new Dictionary<string, ISqlAdapter>
{
......@@ -465,16 +466,15 @@ private static string GetTableName(Type type)
/// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
/// </summary>
public static GetDatabaseTypeDelegate GetDatabaseType;
private static ISqlAdapter GetFormatter(IDbConnection connection)
{
var name = GetDatabaseType?.Invoke(connection).ToLower()
?? connection.GetType().Name.ToLower();
return !AdapterDictionary.ContainsKey(name) ?
new SqlServerAdapter() :
AdapterDictionary[name];
return !AdapterDictionary.ContainsKey(name)
? DefaultAdapter
: AdapterDictionary[name];
}
static class ProxyGenerator
......@@ -754,12 +754,12 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
var id = r.First().id;
if (id == null) return 0;
var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
if (!propertyInfos.Any()) return id;
if (!propertyInfos.Any()) return Convert.ToInt32(id);
var idp = propertyInfos.First();
idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);
return (int)id;
return Convert.ToInt32(id);
}
public void AppendColumnName(StringBuilder sb, string columnName)
......
......@@ -235,7 +235,6 @@ public async Task UpdateListAsync()
var name = connection.Query<User>("select * from users").First().Name;
name.Contains("updated").IsTrue();
}
}
[Fact]
......@@ -261,7 +260,6 @@ public async Task DeleteListAsync()
users = connection.Query<User>("select * from users").ToList();
users.Count.IsEqualTo(numberOfEntities - 10);
}
}
[Fact]
......
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