Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions source/Sylvan.Data.Tests/DataBinderTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Sylvan.Data.Csv;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Xunit;

#if NET6_0_OR_GREATER
using DateType = System.DateOnly;
#else
using DateType = System.DateTime;
#endif

namespace Sylvan.Data
{
public class DataBinderTests
Expand All @@ -18,7 +23,7 @@ static ReadOnlyCollection<DbColumn> BuildSchema()
new Schema.Builder()
.Add<int>("Id")
.Add<string>("Name")
.Add<DateTime?>("Date")
.Add<DateType?>("Date")
.Build();
return schema.GetColumnSchema();
}
Expand Down Expand Up @@ -100,7 +105,7 @@ public void RecordClass()
{
Assert.Equal(1, record.Id);
Assert.Equal("Test", record.Name);
Assert.Equal(new DateTime(2020, 8, 12), record.Date);
Assert.Equal(new DateType(2020, 8, 12), record.Date);
}
}

Expand Down Expand Up @@ -332,7 +337,7 @@ class PopulationRecord
{
public string State { get; set; }
public string County { get; set; }
public Series<DateTime, int> Values { get; set; }
public Series<DateType, int> Values { get; set; }
}

[Fact]
Expand Down Expand Up @@ -404,7 +409,7 @@ public void Manual()

sealed class ManualBinder : IDataBinder<SeriesDateRecord>
{
readonly DataSeriesAccessor<DateTime, int> series0;
readonly DataSeriesAccessor<DateType, int> series0;
readonly int idIdx;
readonly int nameIdx;

Expand All @@ -414,16 +419,16 @@ public ManualBinder(ReadOnlyCollection<DbColumn> schema)
nameIdx = schema.Single(c => c.ColumnName == "Name").ColumnOrdinal.Value;
var seriesCols =
schema
.Where(c => DateTime.TryParse(c.ColumnName, out _))
.Select(c => new DataSeriesColumn<DateTime>(c.ColumnName, DateTime.Parse(c.ColumnName), c.ColumnOrdinal.Value));
this.series0 = new DataSeriesAccessor<DateTime, int>(seriesCols);
.Where(c => DateType.TryParse(c.ColumnName, out _))
.Select(c => new DataSeriesColumn<DateType>(c.ColumnName, DateType.Parse(c.ColumnName), c.ColumnOrdinal.Value));
this.series0 = new DataSeriesAccessor<DateType, int>(seriesCols);
}

public void Bind(DbDataReader record, SeriesDateRecord item)
{
item.Id = record.GetInt32(idIdx);
item.Name = record.GetString(nameIdx);
item.Values = new Series<DateTime, int>(this.series0, record);
item.Values = new Series<DateType, int>(this.series0, record);
}

public void Bind(DbDataReader record, object item)
Expand Down Expand Up @@ -480,7 +485,7 @@ public void BindErrorTest()
new Schema.Builder()
.Add<int>()
.Add<string>()
.Add<DateTime>()
.Add<DateType>()
.Build();

var data = CsvDataReader.Create(new StringReader(dataStr), new CsvDataReaderOptions { Schema = new CsvSchema(schema) });
Expand All @@ -501,7 +506,7 @@ public void DupeHeaderSuccess()
.Add<int>("Id")
.Add<string>("Blorp")
.Add<string>("Name")
.Add<DateTime>("Date")
.Add<DateType>("Date")
.Add<string>("Blorp")
.Build();

Expand All @@ -524,7 +529,7 @@ public void DupeHeaderFailure()
new Schema.Builder()
.Add<int>("Id")
.Add<string>("Name")
.Add<DateTime>("Date")
.Add<DateType>("Date")
.Add<string>("Name")
.Build();

Expand Down Expand Up @@ -571,7 +576,7 @@ class MyDataRecord
{
public int Id { get; private set; }
public string Name { get; private set; }
public DateTime? Date { get; private set; }
public DateType? Date { get; private set; }
}

class NumericNullRecord
Expand Down Expand Up @@ -600,7 +605,7 @@ class SeriesDateRecord
{
public int Id { get; set; }
public string Name { get; set; }
public Series<DateTime, int> Values { get; set; }
public Series<DateType, int> Values { get; set; }
}

class Record
Expand Down Expand Up @@ -637,7 +642,7 @@ record class MyRecordClass
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public DateType Date { get; set; }
}

#if NET6_0_OR_GREATER
Expand Down
9 changes: 9 additions & 0 deletions source/Sylvan.Data/DataBinderAccessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ partial class DataBinder
return name;
}

private static readonly bool MapDbDateToClrDateTime;

//internal static readonly Type IDataRecordType = typeof(IDataRecord);
internal static readonly Type DbDataRecordType = typeof(DbDataRecord);

Expand All @@ -52,6 +54,8 @@ partial class DataBinder
#endif
static DataBinder()
{
AppContext.TryGetSwitch("Sylvan.Data.MapDbDateToClrDateTime", out MapDbDateToClrDateTime);

//IDataRecordType = typeof(IDataRecord);
DbDataRecordType = typeof(DbDataReader);
IsDbNullMethod = DbDataRecordType.GetMethod("IsDBNull")!;
Expand Down Expand Up @@ -306,8 +310,13 @@ internal static Type GetDataType(DbType type)
return typeof(Guid);
case DbType.DateTime:
case DbType.DateTime2:
return typeof(DateTime);
case DbType.Date:
#if NET6_0_OR_GREATER
return MapDbDateToClrDateTime ? typeof(DateTime) : typeof(DateOnly);
#else
return typeof(DateTime);
#endif
case DbType.DateTimeOffset:
return typeof(DateTimeOffset);
}
Expand Down
Loading