Skip to content

Commit 03fe0c0

Browse files
authored
Merge pull request #198 from DataObjects-NET/7.0-mssql-datediffbig
Change DATEDIFF to DATEDIFF_BIG for Sql Server v13+
2 parents b22423a + 17b0099 commit 03fe0c0

File tree

1 file changed

+38
-3
lines changed
  • Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v13

1 file changed

+38
-3
lines changed

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v13/Compiler.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1-
// Copyright (C) 2018 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2018-2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alexey Kulakov
55
// Created: 2018.09.21
66

7+
using Xtensive.Sql.Dml;
8+
79
namespace Xtensive.Sql.Drivers.SqlServer.v13
810
{
911
internal class Compiler : v12.Compiler
1012
{
13+
protected const string DayPart = "DAY";
14+
protected const string MillisecondPart = "MS";
15+
protected const string NanosecondPart = "NS";
16+
protected const string ZeroTime = "'00:00:00.0000000'";
17+
18+
/// <inheritdoc/>
19+
public override void Visit(SqlFunctionCall node)
20+
{
21+
if (node.FunctionType == SqlFunctionType.DateTimeOffsetTimeOfDay) {
22+
DateTimeOffsetTimeOfDay(node.Arguments[0]).AcceptVisitor(this);
23+
}
24+
else {
25+
base.Visit(node);
26+
}
27+
}
28+
29+
protected override SqlExpression DateTimeSubtractDateTime(SqlExpression date1, SqlExpression date2)
30+
{
31+
return DateDiffBigNanosecond(date2, date1);
32+
}
33+
34+
#region Static Helpers
35+
36+
protected static SqlUserFunctionCall DateDiffBigNanosecond(SqlExpression date1, SqlExpression date2) =>
37+
SqlDml.FunctionCall("DATEDIFF_BIG", SqlDml.Native(NanosecondPart), date1, date2);
38+
39+
private static SqlExpression DateTimeOffsetTimeOfDay(SqlExpression dateTimeOffset) =>
40+
DateDiffBigNanosecond(
41+
SqlDml.Native(ZeroTime),
42+
SqlDml.Cast(dateTimeOffset, new SqlValueType("time")));
43+
44+
#endregion
45+
1146
public Compiler(SqlDriver driver)
1247
: base(driver)
1348
{

0 commit comments

Comments
 (0)