Skip to content

Commit a2bbcbc

Browse files
committed
PgSQL: Add support for DateConstruct/TimeConstruct functions
1 parent 3e825c1 commit a2bbcbc

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_0/Compiler.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal class Compiler : SqlCompiler
1818
private static readonly SqlNative OneYearInterval = SqlDml.Native("interval '1 year'");
1919
private static readonly SqlNative OneMonthInterval = SqlDml.Native("interval '1 month'");
2020
private static readonly SqlNative OneDayInterval = SqlDml.Native("interval '1 day'");
21+
22+
private static readonly SqlNative OneHourInterval = SqlDml.Native("interval '1 hour'");
2123
private static readonly SqlNative OneMinuteInterval = SqlDml.Native("interval '1 minute'");
2224
private static readonly SqlNative OneSecondInterval = SqlDml.Native("interval '1 second'");
2325

@@ -106,6 +108,21 @@ public override void Visit(SqlFunctionCall node)
106108
+ (OneDayInterval * (node.Arguments[2] - 1));
107109
newNode.AcceptVisitor(this);
108110
return;
111+
#if NET6_0_OR_GREATER //DO_DATEONLY
112+
case SqlFunctionType.DateConstruct:
113+
(SqlDml.Literal(new DateOnly(2001, 1, 1))
114+
+ (OneYearInterval * (node.Arguments[0] - 2001))
115+
+ (OneMonthInterval * (node.Arguments[1] - 1))
116+
+ (OneDayInterval * (node.Arguments[2] - 1))).AcceptVisitor(this);
117+
return;
118+
case SqlFunctionType.TimeConstruct: {
119+
((SqlDml.Literal(new TimeOnly(0, 0, 0))
120+
+ (OneHourInterval * (node.Arguments[0]))
121+
+ (OneMinuteInterval * (node.Arguments[1]))
122+
+ (OneSecondInterval * (node.Arguments[2] + (SqlDml.Cast(node.Arguments[3], SqlType.Double) / 1000))))).AcceptVisitor(this);
123+
return;
124+
}
125+
#endif
109126
case SqlFunctionType.DateTimeTruncate:
110127
(SqlDml.FunctionCall("date_trunc", "day", node.Arguments[0])).AcceptVisitor(this);
111128
return;

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_0/Translator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012-2022 Xtensive LLC.
1+
// Copyright (C) 2012-2023 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -53,6 +53,14 @@ public SqlFunctionTypeTranslations(int count)
5353

5454
/// <inheritdoc/>
5555
public override string DateTimeFormatString => @"\'yyyyMMdd HHmmss.ffffff\''::timestamp(6)'";
56+
#if NET6_0_OR_GREATER //DO_DATEONLY
57+
58+
/// <inheritdoc/>
59+
public override string DateOnlyFormatString => @"\'yyyyMMdd\''::date'";
60+
61+
/// <inheritdoc/>
62+
public override string TimeOnlyFormatString => @"\'HH:mm:ss.ffffff\''::time'";
63+
#endif
5664

5765
/// <inheritdoc/>
5866
public override string TimeSpanFormatString => "'{0}{1} days {0}{2}:{3}:{4}.{5:000}'::interval";

0 commit comments

Comments
 (0)