Some date and time updates

- Don't derive Timespan and DateTimeOffset type mapping from our JetDateTimeTypeMapping. We can derive from the normal base class for those types
- DateTimeOffset is now mapped to a string. This allows us to round-trip all the details, however any calculations or queries for any components do not work
pull/144/head
Christopher Jolly 2 years ago
parent 0d4f61e3fd
commit a6d9058c05

@ -467,8 +467,8 @@ namespace EntityFrameworkCore.Jet.Data
foreach (DbParameter parameter in parameters)
{
if (parameter.Value is TimeSpan ts)
parameter.Value = JetConfiguration.TimeSpanOffset + ts;
/*if (parameter.Value is TimeSpan ts)
parameter.Value = JetConfiguration.TimeSpanOffset + ts;*/
if (parameter.Value is DateTime dt)
{
// Hack: https://github.com/fsprojects/SQLProvider/issues/191

@ -1,7 +1,9 @@
using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Threading;
@ -198,7 +200,19 @@ namespace EntityFrameworkCore.Jet.Data
}
public virtual DateTimeOffset GetDateTimeOffset(int ordinal)
=> GetDateTime(ordinal);
{
var value = _wrappedDataReader.GetValue(ordinal);
if (value is String stringValue)
{
return DateTimeOffset.Parse(stringValue, null, DateTimeStyles.RoundtripKind);
}
else if (value is DateTime dateTime)
{
return new DateTimeOffset(dateTime);
}
return (DateTimeOffset)value;
}
public override decimal GetDecimal(int ordinal)
{

@ -60,7 +60,7 @@ public class JetDateOnlyMethodTranslator : IMethodCallTranslator
"DATEADD",
new[] { _sqlExpressionFactory.Constant(datePart), _sqlExpressionFactory.Convert(arguments[0], typeof(int)), instance },
nullable: true,
argumentsPropagateNullability: new[] { false, true, true },
argumentsPropagateNullability: new[] { false, false, true },
instance.Type,
instance.TypeMapping);
}

@ -27,8 +27,8 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal
/// </summary>
public SqlExpression? Translate(SqlExpression? instance, MemberInfo member, Type returnType, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (member.DeclaringType == typeof(DateTime) ||
member.DeclaringType == typeof(DateTimeOffset))
if (member.DeclaringType == typeof(DateTime) /*||
member.DeclaringType == typeof(DateTimeOffset)*/)
{
if (instance == null)
{
@ -48,7 +48,7 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal
"DATEVALUE",
new[]
{
_sqlExpressionFactory.Function("NOW", Array.Empty<SqlExpression>(), false, new[] { false },
_sqlExpressionFactory.Function("DATE", Array.Empty<SqlExpression>(), false, new[] { false },
returnType)
},
false,
@ -106,7 +106,7 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal
instance,
},
false,
new[] {false},
new[] { false },
returnType);
}
}

@ -29,17 +29,17 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal
{typeof(DateTime).GetRuntimeMethod(nameof(DateTime.AddMinutes), new[] {typeof(double)})!, "n"},
{typeof(DateTime).GetRuntimeMethod(nameof(DateTime.AddSeconds), new[] {typeof(double)})!, "s"},
// {typeof(DateTime).GetRuntimeMethod(nameof(DateTime.AddMilliseconds), new[] {typeof(double)})!, "millisecond"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddYears), new[] {typeof(int)})!, "yyyy"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddMonths), new[] {typeof(int)})!, "m"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddDays), new[] {typeof(double)})!, "d"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddHours), new[] {typeof(double)})!, "h"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddMinutes), new[] {typeof(double)})!, "n"},
{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddSeconds), new[] {typeof(double)})!, "s"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddYears), new[] {typeof(int)})!, "yyyy"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddMonths), new[] {typeof(int)})!, "m"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddDays), new[] {typeof(double)})!, "d"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddHours), new[] {typeof(double)})!, "h"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddMinutes), new[] {typeof(double)})!, "n"},
//{typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddSeconds), new[] {typeof(double)})!, "s"},
// {typeof(DateTimeOffset).GetRuntimeMethod(nameof(DateTimeOffset.AddMilliseconds), new[] {typeof(double)})!, "millisecond"}
};
public JetDateTimeMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
=> _sqlExpressionFactory = (JetSqlExpressionFactory) sqlExpressionFactory;
=> _sqlExpressionFactory = (JetSqlExpressionFactory)sqlExpressionFactory;
public SqlExpression? Translate(SqlExpression? instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{

@ -21,7 +21,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
[NotNull] string storeType,
[NotNull] IJetOptions options,
DbType? dbType = null)
: base(storeType, dbType ?? System.Data.DbType.DateTime)
: base(storeType)
{
_options = options;
}

@ -8,26 +8,23 @@ using Microsoft.EntityFrameworkCore.Storage;
namespace EntityFrameworkCore.Jet.Storage.Internal
{
public class JetDateTimeOffsetTypeMapping : JetDateTimeTypeMapping
public class JetDateTimeOffsetTypeMapping : DateTimeOffsetTypeMapping
{
private readonly IJetOptions _options;
private const string DateTimeOffsetFormatConst = @"'{0:yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz}'";
public JetDateTimeOffsetTypeMapping(
[NotNull] string storeType,
[NotNull] IJetOptions options)
: base(
storeType,
options,
System.Data.DbType.DateTime,
typeof(DateTimeOffset)) // delibrately use DbType.DateTime, because OleDb will throw a
// "No mapping exists from DbType DateTimeOffset to a known OleDbType."
// exception when using DbType.DateTimeOffset.
storeType, System.Data.DbType.String) // delibrately use DbType.DateTime, because OleDb will throw a
// "No mapping exists from DbType DateTimeOffset to a known OleDbType."
// exception when using DbType.DateTimeOffset.
{
_options = options;
}
protected JetDateTimeOffsetTypeMapping(RelationalTypeMappingParameters parameters, IJetOptions options)
: base(parameters, options)
: base(parameters)
{
_options = options;
}
@ -40,13 +37,14 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
// OLE DB can't handle the DateTimeOffset type.
if (parameter.Value is DateTimeOffset dateTimeOffset)
{
parameter.Value = dateTimeOffset.DateTime;
parameter.Value = dateTimeOffset.ToString("O");
parameter.DbType = System.Data.DbType.String;
}
base.ConfigureParameter(parameter);
}
protected override DateTime ConvertToDateTimeCompatibleValue(object value)
=> ((DateTimeOffset) value).DateTime;
protected override string SqlLiteralFormatString
=> DateTimeOffsetFormatConst;
}
}

@ -17,7 +17,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
public JetTimeOnlyTypeMapping(
[NotNull] string storeType,
[NotNull] IJetOptions options)
: base(storeType, System.Data.DbType.DateTime)
: base(storeType)
{
_options = options;
}
@ -34,7 +34,8 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
if (parameter.Value != null)
{
((TimeOnly)parameter.Value).Deconstruct(out int hour, out int min, out int sec);
parameter.Value = JetConfiguration.TimeSpanOffset.Add(new TimeSpan(hour, min, sec));
//parameter.Value = JetConfiguration.TimeSpanOffset.Add(new TimeSpan(hour, min, sec));
parameter.Value = new TimeSpan(hour, min, sec);
}
}

@ -8,28 +8,28 @@ using Microsoft.EntityFrameworkCore.Storage;
namespace EntityFrameworkCore.Jet.Storage.Internal
{
public class JetTimeSpanTypeMapping : JetDateTimeTypeMapping
public class JetTimeSpanTypeMapping : TimeSpanTypeMapping
{
[NotNull] private readonly IJetOptions _options;
public JetTimeSpanTypeMapping(
[NotNull] string storeType,
[NotNull] IJetOptions options)
: base(storeType, options, System.Data.DbType.DateTime, typeof(TimeSpan))
: base(storeType)
{
_options = options;
}
protected JetTimeSpanTypeMapping(RelationalTypeMappingParameters parameters, IJetOptions options)
: base(parameters, options)
: base(parameters)
{
_options = options;
}
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new JetTimeSpanTypeMapping(parameters, _options);
protected override DateTime ConvertToDateTimeCompatibleValue(object value)
=> JetConfiguration.TimeSpanOffset + (TimeSpan) value;
/*protected override DateTime ConvertToDateTimeCompatibleValue(object value)
=> JetConfiguration.TimeSpanOffset + (TimeSpan)value;*/
}
}

@ -87,9 +87,9 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
// https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa140015(v=office.10)
_datetime = new JetDateTimeTypeMapping("datetime", options, dbType: DbType.DateTime);
_datetimeoffset = new JetDateTimeOffsetTypeMapping("datetime", options);
_dateonly = new JetDateOnlyTypeMapping("datetime", options, dbType: DbType.Date);
_timeonly = new JetTimeOnlyTypeMapping("datetime", options);
_datetimeoffset = new JetDateTimeOffsetTypeMapping("varchar(50)", options);
_dateonly = new JetDateOnlyTypeMapping("date", options, dbType: DbType.Date);
_timeonly = new JetTimeOnlyTypeMapping("time", options);
_timespan = new JetTimeSpanTypeMapping("datetime", options);
_storeTypeMappings

Loading…
Cancel
Save