Skip to content

Commit

Permalink
Use DateTime both DateOnly and TimeOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
unconverged committed Aug 20, 2022
1 parent 784a511 commit 7339249
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 313 deletions.
16 changes: 5 additions & 11 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public override string GetDataTypeName(int ordinal)
}

public override DateTime GetDateTime(int ordinal)
=> Types.DuckDBTimestamp.Load(queryResult, ordinal, currentRow).ToDateTime();

public DuckDBDateOnly GetDateOnly(int ordinal)
=> DuckDBDateOnly.Load(queryResult, ordinal, currentRow);

public DuckDBTimeOnly GetTimeOnly(int ordinal)
=> DuckDBTimeOnly.Load(queryResult, ordinal, currentRow);
=> Types.DuckDBTimestamp.Load(queryResult, ordinal, currentRow);

public override decimal GetDecimal(int ordinal)
{
Expand All @@ -92,8 +86,8 @@ public override Type GetFieldType(int ordinal)
DuckDBType.DuckdbTypeFloat => typeof(float),
DuckDBType.DuckdbTypeDouble => typeof(double),
DuckDBType.DuckdbTypeTimestamp => typeof(DateTime),
DuckDBType.DuckdbTypeDate => typeof(DuckDBDateOnly),
DuckDBType.DuckdbTypeTime => typeof(DuckDBTimeOnly),
DuckDBType.DuckdbTypeDate => typeof(DateTime),
DuckDBType.DuckdbTypeTime => typeof(DateTime),
DuckDBType.DuckdbTypeInterval => throw new NotImplementedException(),
DuckDBType.DuckdbTypeHugeInt => typeof(BigInteger),
DuckDBType.DuckdbTypeVarchar => typeof(string),
Expand Down Expand Up @@ -176,8 +170,8 @@ public override object GetValue(int ordinal)
DuckDBType.DuckdbTypeFloat => GetFloat(ordinal),
DuckDBType.DuckdbTypeDouble => GetDouble(ordinal),
DuckDBType.DuckdbTypeTimestamp => GetDateTime(ordinal),
DuckDBType.DuckdbTypeDate => GetDateOnly(ordinal),
DuckDBType.DuckdbTypeTime => GetTimeOnly(ordinal),
DuckDBType.DuckdbTypeDate => GetDateTime(ordinal),
DuckDBType.DuckdbTypeTime => GetDateTime(ordinal),
DuckDBType.DuckdbTypeInterval => throw new NotImplementedException(),
DuckDBType.DuckdbTypeHugeInt => GetBigInteger(ordinal),
DuckDBType.DuckdbTypeVarchar => GetString(ordinal),
Expand Down
22 changes: 1 addition & 21 deletions DuckDB.NET.Data/DuckDBParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,12 @@ public DuckDBParameter(string name, string value)
: this(name, DbType.String, value)
{
}

public DuckDBParameter(string name, DuckDBDateOnly value)
: this(name, DbType.Date, value)
{
}

public DuckDBParameter(string name, DuckDBTimeOnly value)
: this(name, DbType.Time, value)
{
}


public DuckDBParameter(string name, DateTime value)
: this(name, DbType.DateTime, Types.DuckDBTimestamp.FromDateTime(value))
{
}

public DuckDBParameter(DuckDBDateOnly value)
: this(DbType.Date, value)
{
}

public DuckDBParameter(DuckDBTimeOnly value)
: this(DbType.Time, value)
{
}

public DuckDBParameter(DateTime value)
: this(DbType.DateTime, Types.DuckDBTimestamp.FromDateTime(value))
{
Expand Down
48 changes: 0 additions & 48 deletions DuckDB.NET.Data/Types/DuckDBDateOnly.cs

This file was deleted.

52 changes: 0 additions & 52 deletions DuckDB.NET.Data/Types/DuckDBTimeOnly.cs

This file was deleted.

88 changes: 81 additions & 7 deletions DuckDB.NET.Data/Types/DuckDBTimestamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DuckDB.NET.Data.Types;

internal class DuckDBTimestamp : IDuckDBDateTimeType
internal class DuckDBTimestamp : IDuckDBParameterValue
{
private readonly DuckDBTimestampStruct nativeValue;

Expand All @@ -12,24 +12,83 @@ private DuckDBTimestamp(DuckDBTimestampStruct native)
}

public DuckDBState Bind(DuckDBPreparedStatement preparedStatement, long index)
{
return NativeMethods.PreparedStatements.DuckDBParamType(preparedStatement, index) switch
{
DuckDBType.DuckdbTypeDate => BindDate(preparedStatement, index),
DuckDBType.DuckdbTypeTime => BindTime(preparedStatement, index),
DuckDBType.DuckdbTypeTimestamp => BindTimestamp(preparedStatement, index),
DuckDBType.DuckdbTypeInvalid => BindTimestamp(preparedStatement, index),
_ => throw new ArgumentOutOfRangeException("Unexpected target data type.")
};
}

private DuckDBState BindDate(DuckDBPreparedStatement preparedStatement, long index)
{
var date = NativeMethods.DateTime.DuckDBToDate(nativeValue.Date);
return NativeMethods.PreparedStatements.DuckDBBindDate(preparedStatement, index, date);
}

private DuckDBState BindTime(DuckDBPreparedStatement preparedStatement, long index)
{
var time = NativeMethods.DateTime.DuckDBToTime(nativeValue.Time);
return NativeMethods.PreparedStatements.DuckDBBindTime(preparedStatement, index, time);
}

private DuckDBState BindTimestamp(DuckDBPreparedStatement preparedStatement, long index)
{
var timestamp = NativeMethods.DateTime.DuckDBToTimestamp(nativeValue);
return NativeMethods.PreparedStatements.DuckDBBindTimestamp(preparedStatement, index, timestamp);
}

public static DuckDBTimestamp Load(DuckDBResult result, long col, long row)
public static DateTime Load(DuckDBResult result, long col, long row)
{
return (NativeMethods.Query.DuckDBColumnType(result, col) switch
{
DuckDBType.DuckdbTypeTimestamp => LoadTimestamp(result, col, row),
DuckDBType.DuckdbTypeTime => LoadTime(result, col, row),
DuckDBType.DuckdbTypeDate => LoadDate(result, col, row),
_ => throw new ArgumentOutOfRangeException("Unexpected data type.")
}).ToDateTime();
}

private static DuckDBTimestamp LoadTimestamp(DuckDBResult result, long col, long row)
{
var timestamp = NativeMethods.Types.DuckDbValueTimestamp(result, col, row);
var timestampStruct = NativeMethods.DateTime.DuckDBFromTimestamp(timestamp);
return new DuckDBTimestamp(timestampStruct);
}

private static DuckDBTimestamp LoadTime(DuckDBResult result, long col, long row)
{
var time = NativeMethods.Types.DuckDbValueTime(result, col, row);
var timeStruct = NativeMethods.DateTime.DuckDBFromTime(time);
var timestamp = new DuckDBTimestampStruct
{
Time = timeStruct,
Date = new DuckDBDateStruct()
};
return new DuckDBTimestamp(timestamp);
}

private static DuckDBTimestamp LoadDate(DuckDBResult result, long col, long row)
{
var date = NativeMethods.Types.DuckDbValueDate(result, col, row);
var dateStruct = NativeMethods.DateTime.DuckDBFromDate(date);
var timestamp = new DuckDBTimestampStruct
{
Date = dateStruct,
Time = new DuckDBTimeStruct()
};
return new DuckDBTimestamp(timestamp);
}

public DateTime ToDateTime()
{
return new DateTime(
nativeValue.Date.Year,
nativeValue.Date.Month,
nativeValue.Date.Day,
Math.Max(nativeValue.Date.Year, DateTime.MinValue.Year),
Math.Max(nativeValue.Date.Month, DateTime.MinValue.Month),
Math.Max(nativeValue.Date.Day, DateTime.MinValue.Day),
nativeValue.Time.Hour,
nativeValue.Time.Min,
nativeValue.Time.Sec,
Expand All @@ -39,9 +98,24 @@ public DateTime ToDateTime()

public static DuckDBTimestamp FromDateTime(DateTime dateTime)
{
var nativeDate = new DuckDBDateStruct
{
Year = dateTime.Year,
Month = (byte)dateTime.Month,
Day = (byte)dateTime.Day
};

var nativeTime = new DuckDBTimeStruct
{
Hour = (byte)dateTime.Hour,
Min = (byte)dateTime.Minute,
Sec = (byte)dateTime.Second,
Msec = dateTime.Millisecond
};

var value = new DuckDBTimestampStruct {
Date = DuckDBDateOnly.FromDateTime(dateTime).NativeDate,
Time = DuckDBTimeOnly.FromDateTime(dateTime).NativeTime
Date = nativeDate,
Time = nativeTime
};
return new DuckDBTimestamp(value);
}
Expand Down
5 changes: 0 additions & 5 deletions DuckDB.NET.Data/Types/IDuckDBParameterValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@ namespace DuckDB.NET.Data.Types;
internal interface IDuckDBParameterValue
{
DuckDBState Bind(DuckDBPreparedStatement preparedStatement, long index);
}

internal interface IDuckDBDateTimeType : IDuckDBParameterValue
{
DateTime ToDateTime();
}
81 changes: 0 additions & 81 deletions DuckDB.NET.Test/DateTests.cs

This file was deleted.

Loading

0 comments on commit 7339249

Please sign in to comment.