mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-18 14:27:47 +08:00
changed multi mapper sig, it makes more sense to define a return type that way you can return an aggregate object more naturally
This commit is contained in:
+31
-54
@@ -232,36 +232,36 @@ namespace Dapper
|
||||
/// <param name="splitOn">The Field we should split and read the second object from (default: id)</param>
|
||||
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Query<T, U>(this IDbConnection cnn, string sql, Action<T, U> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
public static IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
return MultiMap<T,U,DontMap, DontMap, DontMap>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
return MultiMap<TFirst, TSecond, DontMap, DontMap, DontMap, TReturn>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Query<T, U, V>(this IDbConnection cnn, string sql, Action<T, U, V> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
return MultiMap<T, U, V, DontMap, DontMap>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
return MultiMap<TFirst, TSecond, TThird, DontMap, DontMap, TReturn>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Query<T, U, V, Z>(this IDbConnection cnn, string sql, Action<T, U, V, Z> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
return MultiMap<T, U, V, Z, DontMap>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
return MultiMap<TFirst, TSecond, TThird, TFourth, DontMap, TReturn>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Query<T, U, V, Z, X>(this IDbConnection cnn, string sql, Action<T, U, V, Z, X> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
return MultiMap<T, U, V, Z, X>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
return MultiMap<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(cnn, sql, map, param as object, transaction, buffered, splitOn, commandTimeout);
|
||||
}
|
||||
|
||||
class DontMap {}
|
||||
static IEnumerable<T> MultiMap<T, U, V, Z, X>(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
static IEnumerable<TReturn> MultiMap<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
var results = MultiMapImpl<T, U, V, Z, X>(cnn, sql, map, param, transaction, splitOn, commandTimeout);
|
||||
var results = MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(cnn, sql, map, param, transaction, splitOn, commandTimeout);
|
||||
return buffered ? results.ToList() : results;
|
||||
}
|
||||
|
||||
static IEnumerable<T> MultiMapImpl<T, U, V, Z, X>(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, string splitOn = "Id", int? commandTimeout = null)
|
||||
static IEnumerable<TReturn> MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, string splitOn = "Id", int? commandTimeout = null)
|
||||
{
|
||||
var identity = new Identity(sql, cnn, typeof(T), param == null ? null : param.GetType(), otherTypes: new[] { typeof(T), typeof(U), typeof(V), typeof(Z), typeof(X) });
|
||||
var identity = new Identity(sql, cnn, typeof(TFirst), param == null ? null : param.GetType(), otherTypes: new[] { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth) });
|
||||
var info = GetCacheInfo(param, identity);
|
||||
|
||||
using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout))
|
||||
@@ -291,30 +291,30 @@ namespace Dapper
|
||||
var otherDeserializer = new List<object>();
|
||||
|
||||
split = nextSplit();
|
||||
info.Deserializer = GetDeserializer<T>(identity, reader, 0, split);
|
||||
info.Deserializer = GetDeserializer<TFirst>(identity, reader, 0, split);
|
||||
|
||||
if (typeof(U) != typeof(DontMap))
|
||||
if (typeof(TSecond) != typeof(DontMap))
|
||||
{
|
||||
var next = nextSplit();
|
||||
otherDeserializer.Add(GetDeserializer<U>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
otherDeserializer.Add(GetDeserializer<TSecond>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
split = next;
|
||||
}
|
||||
if (typeof(V) != typeof(DontMap))
|
||||
if (typeof(TThird) != typeof(DontMap))
|
||||
{
|
||||
var next = nextSplit();
|
||||
otherDeserializer.Add(GetDeserializer<V>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
otherDeserializer.Add(GetDeserializer<TThird>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
split = next;
|
||||
}
|
||||
if (typeof(Z) != typeof(DontMap))
|
||||
if (typeof(TFourth) != typeof(DontMap))
|
||||
{
|
||||
var next = nextSplit();
|
||||
otherDeserializer.Add(GetDeserializer<Z>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
otherDeserializer.Add(GetDeserializer<TFourth>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
split = next;
|
||||
}
|
||||
if (typeof(X) != typeof(DontMap))
|
||||
if (typeof(TFifth) != typeof(DontMap))
|
||||
{
|
||||
var next = nextSplit();
|
||||
otherDeserializer.Add(GetDeserializer<X>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
otherDeserializer.Add(GetDeserializer<TFifth>(identity, reader, split, next - split, returnNullIfFirstMissing: true));
|
||||
}
|
||||
|
||||
info.OtherDeserializers = otherDeserializer.ToArray();
|
||||
@@ -322,57 +322,37 @@ namespace Dapper
|
||||
queryCache[identity] = info;
|
||||
}
|
||||
|
||||
var deserializer = (Func<IDataReader, T>)info.Deserializer;
|
||||
var deserializer2 = (Func<IDataReader, U>)info.OtherDeserializers[0];
|
||||
var deserializer = (Func<IDataReader, TFirst>)info.Deserializer;
|
||||
var deserializer2 = (Func<IDataReader, TSecond>)info.OtherDeserializers[0];
|
||||
|
||||
|
||||
Func<IDataReader, T> mapIt = null;
|
||||
Func<IDataReader, TReturn> mapIt = null;
|
||||
|
||||
if (info.OtherDeserializers.Length == 1)
|
||||
{
|
||||
mapIt = r =>
|
||||
{
|
||||
var tmp = deserializer(r);
|
||||
((Action<T, U>)map)(tmp, deserializer2(r));
|
||||
return tmp;
|
||||
};
|
||||
mapIt = r => ((Func<TFirst, TSecond,TReturn>)map)(deserializer(r), deserializer2(r));
|
||||
}
|
||||
|
||||
if (info.OtherDeserializers.Length > 1)
|
||||
{
|
||||
var deserializer3 = (Func<IDataReader, V>)info.OtherDeserializers[1];
|
||||
var deserializer3 = (Func<IDataReader, TThird>)info.OtherDeserializers[1];
|
||||
|
||||
if (info.OtherDeserializers.Length == 2)
|
||||
{
|
||||
mapIt = r =>
|
||||
{
|
||||
var tmp = deserializer(r);
|
||||
((Action<T, U, V>)map)(tmp, deserializer2(r), deserializer3(r));
|
||||
return tmp;
|
||||
};
|
||||
mapIt = r => ((Func<TFirst, TSecond, TThird, TReturn>)map)(deserializer(r), deserializer2(r), deserializer3(r));
|
||||
}
|
||||
if (info.OtherDeserializers.Length > 2)
|
||||
{
|
||||
var deserializer4 = (Func<IDataReader, Z>)info.OtherDeserializers[2];
|
||||
var deserializer4 = (Func<IDataReader, TFourth>)info.OtherDeserializers[2];
|
||||
if (info.OtherDeserializers.Length == 3)
|
||||
{
|
||||
mapIt = r =>
|
||||
{
|
||||
var tmp = deserializer(r);
|
||||
((Action<T, U, V, Z>)map)(tmp, deserializer2(r), deserializer3(r), deserializer4(r));
|
||||
return tmp;
|
||||
};
|
||||
mapIt = r => ((Func<TFirst, TSecond, TThird, TFourth, TReturn>)map)(deserializer(r), deserializer2(r), deserializer3(r),deserializer4(r));
|
||||
}
|
||||
|
||||
if (info.OtherDeserializers.Length > 3)
|
||||
{
|
||||
var deserializer5 = (Func<IDataReader, X>)info.OtherDeserializers[3];
|
||||
mapIt = r =>
|
||||
{
|
||||
var tmp = deserializer(r);
|
||||
((Action<T, U, V, Z, X>)map)(tmp, deserializer2(r), deserializer3(r), deserializer4(r), deserializer5(r));
|
||||
return tmp;
|
||||
};
|
||||
var deserializer5 = (Func<IDataReader, TFifth>)info.OtherDeserializers[3];
|
||||
mapIt = r => ((Func<TFirst, TSecond, TThird, TFourth,TFifth,TReturn>)map)(deserializer(r), deserializer2(r), deserializer3(r), deserializer4(r),deserializer5(r));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -386,7 +366,6 @@ namespace Dapper
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static CacheInfo GetCacheInfo(object param, Identity identity)
|
||||
{
|
||||
CacheInfo info;
|
||||
@@ -856,6 +835,4 @@ namespace Dapper
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -126,10 +126,10 @@ namespace SqlMapper
|
||||
petapocoFast.ForceDateTimesToUtc = false;
|
||||
tests.Add(id => petapocoFast.Fetch<Post>("SELECT * from Posts where Id=@0", id), "PetaPoco (Fast)");
|
||||
|
||||
// Subsonic ActiveRecord
|
||||
// Subsonic ActiveRecord
|
||||
tests.Add(id => SubSonic.Post.SingleOrDefault(x => x.Id == id), "SubSonic ActiveRecord.SingleOrDefault");
|
||||
|
||||
// Subsonic ActiveRecord
|
||||
// Subsonic coding horror
|
||||
SubSonic.tempdbDB db=new SubSonic.tempdbDB();
|
||||
tests.Add(id => new SubSonic.Query.CodingHorror(db.Provider, "select * from Posts where Id = @0", id).ExecuteTypedList<Post>(), "SubSonic Coding Horror");
|
||||
|
||||
|
||||
+8
-7
@@ -274,7 +274,7 @@ namespace SqlMapper
|
||||
left join #Users u on u.Id = p.OwnerId
|
||||
Order by p.Id";
|
||||
|
||||
var data = connection.Query<Post, User>(sql, (post, user) => { post.Owner = user; }).ToList();
|
||||
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post; }).ToList();
|
||||
var p = data.First();
|
||||
|
||||
p.Content.IsEqualTo("Sams Post1");
|
||||
@@ -308,7 +308,7 @@ Order by p.Id";
|
||||
left join #Users u on u.Id = p.OwnerId
|
||||
Order by p.Id";
|
||||
|
||||
var data = connection.Query<dynamic, dynamic>(sql, (post, user) => { post.Owner = user; }).ToList();
|
||||
var data = connection.Query<dynamic, dynamic, dynamic>(sql, (post, user) => { post.Owner = user; return post; }).ToList();
|
||||
var p = data.First();
|
||||
|
||||
// hairy extension method support for dynamics
|
||||
@@ -341,8 +341,8 @@ Order by p.Id";
|
||||
public void TestMultiMappingVariations()
|
||||
{
|
||||
var sql = @"select 1 as Id, 'a' as Content, 2 as Id, 'b' as Content, 3 as Id, 'c' as Content, 4 as Id, 'd' as Content, 5 as Id, 'e' as Content";
|
||||
|
||||
var order = connection.Query<dynamic, dynamic, dynamic>(sql, (o, owner, creator) => { o.Owner = owner; o.Creator = creator; }).First();
|
||||
|
||||
var order = connection.Query<dynamic, dynamic, dynamic, dynamic>(sql, (o, owner, creator) => { o.Owner = owner; o.Creator = creator; return o; }).First();
|
||||
|
||||
Assert.IsEqualTo(order.Id, 1);
|
||||
Assert.IsEqualTo(order.Content, "a");
|
||||
@@ -351,11 +351,12 @@ Order by p.Id";
|
||||
Assert.IsEqualTo(order.Creator.Id, 3);
|
||||
Assert.IsEqualTo(order.Creator.Content, "c");
|
||||
|
||||
order = connection.Query<dynamic, dynamic, dynamic, dynamic>(sql, (o, owner, creator, address) =>
|
||||
order = connection.Query<dynamic, dynamic, dynamic, dynamic, dynamic>(sql, (o, owner, creator, address) =>
|
||||
{
|
||||
o.Owner = owner;
|
||||
o.Creator = creator;
|
||||
o.Owner.Address = address;
|
||||
return o;
|
||||
}).First();
|
||||
|
||||
Assert.IsEqualTo(order.Id, 1);
|
||||
@@ -367,7 +368,7 @@ Order by p.Id";
|
||||
Assert.IsEqualTo(order.Owner.Address.Id, 4);
|
||||
Assert.IsEqualTo(order.Owner.Address.Content, "d");
|
||||
|
||||
order = connection.Query<dynamic, dynamic, dynamic, dynamic, dynamic>(sql, (a, b, c, d, e) => { a.B = b; a.C = c; a.C.D = d; a.E = e; }).First();
|
||||
order = connection.Query<dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>(sql, (a, b, c, d, e) => { a.B = b; a.C = c; a.C.D = d; a.E = e; return a; }).First();
|
||||
|
||||
Assert.IsEqualTo(order.Id, 1);
|
||||
Assert.IsEqualTo(order.Content, "a");
|
||||
@@ -440,7 +441,7 @@ Order by p.Id";
|
||||
cnn.Execute("insert Posts values(2,'title2','body2',null)");
|
||||
cnn.Execute("insert Authors values(1,'sam')");
|
||||
|
||||
var data = cnn.Query<PostCE, AuthorCE>(@"select * from Posts p left join Authors a on a.ID = p.AuthorID", (post, author) => { post.Author = author; }).ToList();
|
||||
var data = cnn.Query<PostCE, AuthorCE, PostCE>(@"select * from Posts p left join Authors a on a.ID = p.AuthorID", (post, author) => { post.Author = author; return post; }).ToList();
|
||||
var firstPost = data.First();
|
||||
firstPost.Title.IsEqualTo("title");
|
||||
firstPost.Author.Name.IsEqualTo("sam");
|
||||
|
||||
Reference in New Issue
Block a user