Skip to content

Commit

Permalink
Do not require casts for DateOnly and TimeOnly parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Aug 29, 2024
1 parent 0189c93 commit a25793d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DuckDB.NET.Data/Internal/DbTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal static class DbTypeMap
{typeof(DateTime), DbType.DateTime},
{typeof(DuckDBDateOnly), DbType.Date},
{typeof(DuckDBTimeOnly), DbType.Time},
#if NET6_0_OR_GREATER
{typeof(DateOnly), DbType.Date},
{typeof(TimeOnly), DbType.Time},
#endif
};


Expand Down
8 changes: 8 additions & 0 deletions DuckDB.NET.Data/Internal/PreparedStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,21 @@ private static DuckDBState BindBlob(DuckDBPreparedStatement preparedStatement, l

private static DuckDBState BindDateOnly(DuckDBPreparedStatement preparedStatement, long index, object value)
{
#if NET6_0_OR_GREATER
var date = NativeMethods.DateTimeHelpers.DuckDBToDate(value is DateOnly dateOnly ? (DuckDBDateOnly)dateOnly : (DuckDBDateOnly)value);
#else
var date = NativeMethods.DateTimeHelpers.DuckDBToDate((DuckDBDateOnly)value);
#endif
return NativeMethods.PreparedStatements.DuckDBBindDate(preparedStatement, index, date);
}

private static DuckDBState BindTimeOnly(DuckDBPreparedStatement preparedStatement, long index, object value)
{
#if NET6_0_OR_GREATER
var time = NativeMethods.DateTimeHelpers.DuckDBToTime(value is TimeOnly dateOnly ? (DuckDBTimeOnly)dateOnly : (DuckDBTimeOnly)value);
#else
var time = NativeMethods.DateTimeHelpers.DuckDBToTime((DuckDBTimeOnly)value);
#endif
return NativeMethods.PreparedStatements.DuckDBBindTime(preparedStatement, index, time);
}

Expand Down
22 changes: 22 additions & 0 deletions DuckDB.NET.Test/Parameters/DateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,26 @@ public void InsertAndQueryTest(int year, byte mon, byte day)
Command.CommandText = "DROP TABLE DateOnlyTestTable;";
Command.ExecuteNonQuery();
}

[Theory]
[InlineData(1992, 09, 20)]
[InlineData(2022, 05, 04)]
[InlineData(2022, 04, 05)]
public void BindDateOnly(int year, int mon, int day)
{
var expectedValue = new DateOnly(year, mon, day);

Command.CommandText = "SELECT ?;";
Command.Parameters.Add(new DuckDBParameter(expectedValue));

var scalar = Command.ExecuteScalar();

scalar.Should().BeOfType<DateOnly>();

var dateOnly = (DateOnly)scalar;

dateOnly.Year.Should().Be(year);
dateOnly.Month.Should().Be((byte)mon);
dateOnly.Day.Should().Be((byte)day);
}
}
26 changes: 26 additions & 0 deletions DuckDB.NET.Test/Parameters/TimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,30 @@ public void QueryTimeTzReaderTest(int hour, int minute, int second, int microsec

dateTimeOffset.Offset.Should().Be(new TimeSpan(offsetHours, offsetHours >= 0 ? offsetMinutes : -offsetMinutes, 0));
}

[Theory]
[InlineData(12, 15, 17, 350_000)]
[InlineData(12, 17, 15, 450_000)]
[InlineData(18, 15, 17, 125_000)]
[InlineData(12, 15, 17, 350_300)]
[InlineData(12, 17, 15, 450_500)]
[InlineData(18, 15, 17, 125_700)]
public void BindTimeOnly(int hour, int minute, int second, int microsecond)
{
var expectedValue = new TimeOnly(hour, minute, second,0).Add(TimeSpan.FromMicroseconds(microsecond));

Command.CommandText = "SELECT ?;";
Command.Parameters.Add(new DuckDBParameter(expectedValue));

var scalar = Command.ExecuteScalar();

scalar.Should().BeOfType<TimeOnly>();

var timeOnly = (TimeOnly)scalar;

timeOnly.Hour.Should().Be((byte)hour);
timeOnly.Minute.Should().Be((byte)minute);
timeOnly.Second.Should().Be((byte)second);
timeOnly.Ticks.Should().Be(expectedValue.Ticks);
}
}

0 comments on commit a25793d

Please sign in to comment.