Navigation Menu

Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Fix tuple mapping issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed May 8, 2018
1 parent 1d03700 commit c90537e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs
Expand Up @@ -229,7 +229,8 @@ public string SelectInto<TModel>()

if (typeof(TModel) != typeof(List<object>) &&
typeof(TModel) != typeof(Dictionary<string, object>) &&
typeof(TModel) != typeof(object)) //dynamic
typeof(TModel) != typeof(object) && //dynamic
!typeof(TModel).IsValueTuple())
{
selectDef = typeof(TModel).GetModelDefinition();
if (selectDef != modelDef && tableDefs.Contains(selectDef))
Expand Down
10 changes: 4 additions & 6 deletions src/ServiceStack.OrmLite/OrmLiteUtils.cs
Expand Up @@ -168,21 +168,19 @@ public static T ConvertToValueTuple<T>(this IDataReader reader, object[] values,
var field = typeFields.GetAccessor(itemName);
if (field == null) break;

var dbValue = values != null
? values[i]
: reader.GetValue(i);

var fieldType = field.FieldInfo.FieldType;
var converter = dialectProvider.GetConverter(fieldType);
var dbValue = converter.GetValue(reader, i, values);
if (dbValue == null)
continue;

var fieldType = field.FieldInfo.FieldType;
if (dbValue.GetType() == fieldType)
{
field.PublicSetterRef(ref row, dbValue);
}
else
{
var converter = dialectProvider.GetConverter(fieldType);
var fieldValue = converter.FromDbValue(fieldType, dbValue);
field.PublicSetterRef(ref row, fieldValue);
}
Expand Down
87 changes: 87 additions & 0 deletions tests/ServiceStack.OrmLite.Tests/Issues/DynamicDataIssue.cs
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Data;
using NUnit.Framework;
using ServiceStack.DataAnnotations;
using ServiceStack.Text;

namespace ServiceStack.OrmLite.Tests.Issues
{
public class ParentTbl
{
[AutoIncrement]
public long Id { get; set; }

public DateTime? DateMarried { get; set; }

[Reference]
public List<ChildTbl> Childs { get; set; } = new List<ChildTbl>();

public DateTime? DateOfBirth { get; set; }
}

public class ChildTbl
{
[AutoIncrement]
public long Id { get; set; }

[References(typeof(ParentTbl))]
public long ParentId { get; set; }

public DateTime? DateOfDeath { get; set; }
}


public class DynamicDataIssue : OrmLiteTestBase
{
private static void InitTables(IDbConnection db)
{
db.DropTable<ChildTbl>();
db.DropTable<ParentTbl>();
db.CreateTable<ParentTbl>();
db.CreateTable<ChildTbl>();
}

[Test]
public void Can_select_null_DateTime_in_nullable_Tuple()
{
var date = new DateTime(2000,1,1);

using (var db = OpenDbConnection())
{
InitTables(db);

db.Insert(new ParentTbl { DateOfBirth = date });
db.Insert(new ParentTbl { DateOfBirth = null });

db.Select<ParentTbl>();
db.Select<(int, DateTime?)>(db.From<ParentTbl>());
db.Select<(int, DateTime?)>(db.From<ParentTbl>().Select(x => new { x.Id, x.DateOfBirth }));
db.Select<(int, DateTime)>(db.From<ParentTbl>().Select(x => new { x.Id, x.DateOfBirth }));
}
}

[Test]
public void Complex_example()
{
using (var db = OpenDbConnection())
{
InitTables(db);

var parentTbl = new ParentTbl { DateMarried = DateTime.Today };
parentTbl.Id = db.Insert(parentTbl, selectIdentity:true);
db.Insert(new ChildTbl { ParentId = parentTbl.Id, DateOfDeath = null });

var q = db.From<ChildTbl>()
.RightJoin<ChildTbl, ParentTbl>((c, p) => c.ParentId == p.Id || c.Id == null)
.GroupBy<ParentTbl>((p) => new { p.Id })
.Select<ChildTbl, ParentTbl>((c, p) => new { p.Id, MaxKeyValuePeriodEnd = Sql.Max(c.DateOfDeath) });

var theSqlStatement = q.ToSelectStatement();

theSqlStatement.Print();
}
}

}
}

1 comment on commit c90537e

@daleholborow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex example test case won't actually trigger the db call will it? But thanks as always for prompt fixes!

Please sign in to comment.