Commit 37b45caf authored by Sam Saffron's avatar Sam Saffron

more flexible multi mapping ...

parent 3ddbd667
...@@ -270,13 +270,21 @@ class DontMap {} ...@@ -270,13 +270,21 @@ class DontMap {}
{ {
int current = 0; int current = 0;
var splits = splitOn.Split(',').ToArray();
var splitIndex = 0;
Func<int> nextSplit = () => Func<int> nextSplit = () =>
{ {
var currentSplit = splits[splitIndex];
if (splits.Length > splitIndex + 1)
{
splitIndex++;
}
int pos; int pos;
for (pos = current + 1; pos < reader.FieldCount; pos++) for (pos = current + 1; pos < reader.FieldCount; pos++)
{ {
// some people like ID some id ... assuming case insensitive splits for now // some people like ID some id ... assuming case insensitive splits for now
if (string.Equals(reader.GetName(pos), splitOn, StringComparison.InvariantCultureIgnoreCase)) if (string.Equals(reader.GetName(pos), currentSplit, StringComparison.InvariantCultureIgnoreCase))
{ {
break; break;
} }
......
...@@ -39,7 +39,7 @@ public static void IsFalse(this bool b) ...@@ -39,7 +39,7 @@ public static void IsFalse(this bool b)
public static void IsTrue(this bool b) public static void IsTrue(this bool b)
{ {
if (b) if (!b)
{ {
throw new ApplicationException("Expected true"); throw new ApplicationException("Expected true");
} }
...@@ -203,18 +203,22 @@ public void TestSetPrivate() ...@@ -203,18 +203,22 @@ public void TestSetPrivate()
public void TestEnumeration() public void TestEnumeration()
{ {
var en = connection.Query<int>("select 1 as one", buffered: false); var en = connection.Query<int>("select 1 as one union all select 2 as one", buffered: false);
var i = en.GetEnumerator();
i.MoveNext();
bool gotException = false; bool gotException = false;
try try
{ {
connection.Query<int>("select 1 as one", buffered: false); var x = connection.Query<int>("select 1 as one", buffered: false).First();
} }
catch (Exception) catch (Exception)
{ {
gotException = true; gotException = true;
} }
var realItems = en.ToList(); while (i.MoveNext())
{ }
// should not exception, since enumertated // should not exception, since enumertated
en = connection.Query<int>("select 1 as one", buffered: false); en = connection.Query<int>("select 1 as one", buffered: false);
...@@ -224,22 +228,25 @@ public void TestEnumeration() ...@@ -224,22 +228,25 @@ public void TestEnumeration()
public void TestEnumerationDynamic() public void TestEnumerationDynamic()
{ {
var en = connection.Query("select 1 as one", buffered: false); var en = connection.Query("select 1 as one union all select 2 as one", buffered: false);
var i = en.GetEnumerator();
i.MoveNext();
bool gotException = false; bool gotException = false;
try try
{ {
connection.Query("select 1 as one", buffered: false); var x = connection.Query("select 1 as one", buffered: false).First();
} }
catch (Exception) catch (Exception)
{ {
gotException = true; gotException = true;
} }
int i = en.First().one; while (i.MoveNext())
i.IsEqualTo(1); { }
// should not exception, since enumertated // should not exception, since enumertated
en = connection.Query("select 1 as one",buffered:false); en = connection.Query("select 1 as one", buffered: false);
gotException.IsTrue(); gotException.IsTrue();
} }
...@@ -531,6 +538,46 @@ select 1111 ...@@ -531,6 +538,46 @@ select 1111
} }
class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}
class Address
{
public int AddressId { get; set; }
public string Name { get; set; }
public int PersonId { get; set; }
}
class Extra
{
public int Id { get; set; }
public string Name { get; set; }
}
public void TestFlexibleMultiMapping()
{
var sql =
@"select
1 as PersonId, 'bob' as Name,
2 as AddressId, 'abc street' as Name, 1 as PersonId,
3 as Id, 'fred' as Name
";
var personWithAddress = connection.Query<Person, Address, Extra, Tuple<Person, Address,Extra>>
(sql, (p,a,e) => Tuple.Create(p, a, e), splitOn: "AddressId,Id").First();
personWithAddress.Item1.PersonId.IsEqualTo(1);
personWithAddress.Item1.Name.IsEqualTo("bob");
personWithAddress.Item2.AddressId.IsEqualTo(2);
personWithAddress.Item2.Name.IsEqualTo("abc street");
personWithAddress.Item2.PersonId.IsEqualTo(1);
personWithAddress.Item3.Id.IsEqualTo(3);
personWithAddress.Item3.Name.IsEqualTo("fred");
}
/* TODO: /* TODO:
* *
public void TestMagicParam() public void TestMagicParam()
......
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