Skip to content

Change DATEDIFF to DATEDIFF_BIG for Sql Server v13+ #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2021
Merged
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
41 changes: 38 additions & 3 deletions Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v13/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
// Copyright (C) 2018 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Copyright (C) 2018-2021 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2018.09.21

using Xtensive.Sql.Dml;

namespace Xtensive.Sql.Drivers.SqlServer.v13
{
internal class Compiler : v12.Compiler
{
protected const string DayPart = "DAY";
protected const string MillisecondPart = "MS";
protected const string NanosecondPart = "NS";
protected const string ZeroTime = "'00:00:00.0000000'";

/// <inheritdoc/>
public override void Visit(SqlFunctionCall node)
{
if (node.FunctionType == SqlFunctionType.DateTimeOffsetTimeOfDay) {
DateTimeOffsetTimeOfDay(node.Arguments[0]).AcceptVisitor(this);
}
else {
base.Visit(node);
}
}

protected override SqlExpression DateTimeSubtractDateTime(SqlExpression date1, SqlExpression date2)
{
return DateDiffBigNanosecond(date2, date1);
}

#region Static Helpers

protected static SqlUserFunctionCall DateDiffBigNanosecond(SqlExpression date1, SqlExpression date2) =>
SqlDml.FunctionCall("DATEDIFF_BIG", SqlDml.Native(NanosecondPart), date1, date2);

private static SqlExpression DateTimeOffsetTimeOfDay(SqlExpression dateTimeOffset) =>
DateDiffBigNanosecond(
SqlDml.Native(ZeroTime),
SqlDml.Cast(dateTimeOffset, new SqlValueType("time")));

#endregion

public Compiler(SqlDriver driver)
: base(driver)
{
Expand Down