Commit 12d7617f authored by Marc Gravell's avatar Marc Gravell
parent 9e5d28e4
...@@ -1152,7 +1152,12 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini ...@@ -1152,7 +1152,12 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
while (reader.Read()) while (reader.Read())
{ {
yield return (T)func(reader); object val = func(reader);
if (val == null || val is T) {
yield return (T)val;
} else {
yield return (T)Convert.ChangeType(val, typeof(T));
}
} }
// happy path; close the reader cleanly - no // happy path; close the reader cleanly - no
// need for "Cancel" etc // need for "Cancel" etc
......
...@@ -598,7 +598,7 @@ public void TestExecuteMultipleCommandStrongType() ...@@ -598,7 +598,7 @@ public void TestExecuteMultipleCommandStrongType()
connection.Execute("create table #t(Name nvarchar(max), Age int)"); connection.Execute("create table #t(Name nvarchar(max), Age int)");
try try
{ {
int tally = connection.Execute(@"insert #t (Name,Age) values(@Name, @Age)", new List<Student> int tally = connection.Execute(@"insert #t (Name,Age) values(@Name, @Age)", new List<Student>
{ {
new Student{Age = 1, Name = "sam"}, new Student{Age = 1, Name = "sam"},
new Student{Age = 2, Name = "bob"} new Student{Age = 2, Name = "bob"}
...@@ -980,7 +980,7 @@ public void TestFieldsAndPrivates() ...@@ -980,7 +980,7 @@ public void TestFieldsAndPrivates()
} }
public void ExecuteReader() public void ExecuteReader()
{ {
var dt = new DataTable(); var dt = new DataTable();
...@@ -1208,14 +1208,14 @@ public void TestDbString() ...@@ -1208,14 +1208,14 @@ public void TestDbString()
{ {
var obj = connection.Query("select datalength(@a) as a, datalength(@b) as b, datalength(@c) as c, datalength(@d) as d, datalength(@e) as e, datalength(@f) as f", var obj = connection.Query("select datalength(@a) as a, datalength(@b) as b, datalength(@c) as c, datalength(@d) as d, datalength(@e) as e, datalength(@f) as f",
new new
{ {
a = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }, a = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true },
b = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = false }, b = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = false },
c = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = true }, c = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = true },
d = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = false }, d = new DbString { Value = "abcde", IsFixedLength = false, Length = 10, IsAnsi = false },
e = new DbString { Value = "abcde", IsAnsi = true }, e = new DbString { Value = "abcde", IsAnsi = true },
f = new DbString { Value = "abcde", IsAnsi = false }, f = new DbString { Value = "abcde", IsAnsi = false },
}).First(); }).First();
((int)obj.a).IsEqualTo(10); ((int)obj.a).IsEqualTo(10);
((int)obj.b).IsEqualTo(20); ((int)obj.b).IsEqualTo(20);
((int)obj.c).IsEqualTo(5); ((int)obj.c).IsEqualTo(5);
...@@ -1518,15 +1518,15 @@ public void ParentChildIdentityAssociations() ...@@ -1518,15 +1518,15 @@ public void ParentChildIdentityAssociations()
var lookup = new Dictionary<int, Parent>(); var lookup = new Dictionary<int, Parent>();
var parents = connection.Query<Parent, Child, Parent>(@"select 1 as [Id], 1 as [Id] union all select 1,2 union all select 2,3 union all select 1,4 union all select 3,5", var parents = connection.Query<Parent, Child, Parent>(@"select 1 as [Id], 1 as [Id] union all select 1,2 union all select 2,3 union all select 1,4 union all select 3,5",
(parent, child) => (parent, child) =>
{
Parent found;
if (!lookup.TryGetValue(parent.Id, out found))
{ {
Parent found; lookup.Add(parent.Id, found = parent);
if (!lookup.TryGetValue(parent.Id, out found)) }
{ found.Children.Add(child);
lookup.Add(parent.Id, found = parent); return found;
} }).Distinct().ToDictionary(p => p.Id);
found.Children.Add(child);
return found;
}).Distinct().ToDictionary(p => p.Id);
parents.Count().IsEqualTo(3); parents.Count().IsEqualTo(3);
parents[1].Children.Select(c => c.Id).SequenceEqual(new[] { 1, 2, 4 }).IsTrue(); parents[1].Children.Select(c => c.Id).SequenceEqual(new[] { 1, 2, 4 }).IsTrue();
parents[2].Children.Select(c => c.Id).SequenceEqual(new[] { 3 }).IsTrue(); parents[2].Children.Select(c => c.Id).SequenceEqual(new[] { 3 }).IsTrue();
...@@ -2450,9 +2450,9 @@ public void TestIssue131() ...@@ -2450,9 +2450,9 @@ public void TestIssue131()
var results = connection.Query<dynamic, int, dynamic>( var results = connection.Query<dynamic, int, dynamic>(
"SELECT 1 Id, 'Mr' Title, 'John' Surname, 4 AddressCount", "SELECT 1 Id, 'Mr' Title, 'John' Surname, 4 AddressCount",
(person, addressCount) => (person, addressCount) =>
{ {
return person; return person;
}, },
splitOn: "AddressCount" splitOn: "AddressCount"
).FirstOrDefault(); ).FirstOrDefault();
...@@ -2671,7 +2671,7 @@ public void TestParameterInclusionNotSensitiveToCurrentCulture() ...@@ -2671,7 +2671,7 @@ public void TestParameterInclusionNotSensitiveToCurrentCulture()
public void LiteralReplacement() public void LiteralReplacement()
{ {
connection.Execute("create table #literal1 (id int not null, foo int not null)"); connection.Execute("create table #literal1 (id int not null, foo int not null)");
connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", new { id = 123, foo = 456}); connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", new { id = 123, foo = 456 });
var rows = new[] { new { id = 1, foo = 2 }, new { id = 3, foo = 4 } }; var rows = new[] { new { id = 1, foo = 2 }, new { id = 3, foo = 4 } };
connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", rows); connection.Execute("insert #literal1 (id,foo) values ({=id}, @foo)", rows);
var count = connection.Query<int>("select count(1) from #literal1 where id={=foo}", new { foo = 123 }).Single(); var count = connection.Query<int>("select count(1) from #literal1 where id={=foo}", new { foo = 123 }).Single();
...@@ -2799,6 +2799,21 @@ public void DbString() ...@@ -2799,6 +2799,21 @@ public void DbString()
b.IsEqualTo(6); b.IsEqualTo(6);
} }
class HasInt32
{
public int Value { get; set; }
}
// http://stackoverflow.com/q/23696254/23354
public void DownwardIntegerConversion()
{
const string sql = "select cast(42 as bigint) as Value";
int i = connection.Query<HasInt32>(sql).Single().Value;
Assert.IsEqualTo(42, i);
i = connection.Query<int>(sql).Single();
Assert.IsEqualTo(42, i);
}
class HasDoubleDecimal class HasDoubleDecimal
{ {
public double A { get; set; } public double A { get; set; }
......
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