Skip to content

Commit 0adc93e

Browse files
committed
SQLite: Support for TimeOnly(ticks) ctor
1 parent edef8e4 commit 0adc93e

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

  • Orm/Xtensive.Orm.Sqlite/Sql.Drivers.Sqlite/v3

Orm/Xtensive.Orm.Sqlite/Sql.Drivers.Sqlite/v3/Compiler.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,7 @@ public override void Visit(SqlFunctionCall node)
216216
arguments[2] - 1).AcceptVisitor(this);
217217
return;
218218
case SqlFunctionType.TimeConstruct:
219-
TimeAddSeconds(TimeAddMinutes(TimeAddHours(SqlDml.Literal(new TimeOnly(0, 0, 0, 0)),
220-
arguments[0]),
221-
arguments[1]),
222-
arguments[2], arguments[3]).AcceptVisitor(this);
219+
TimeConstruct(arguments).AcceptVisitor(this);
223220
return;
224221
case SqlFunctionType.TimeAddHours:
225222
TimeAddHours(arguments[0], arguments[1]).AcceptVisitor(this);
@@ -539,6 +536,35 @@ private static SqlExpression DateAddMonth(SqlExpression date, SqlExpression mont
539536
private static SqlExpression DateAddDay(SqlExpression date, SqlExpression days) =>
540537
SqlDml.FunctionCall("STRFTIME", DateFormat, date, SqlDml.Concat(days, " ", "DAYS"));
541538

539+
private static SqlExpression TimeConstruct(IReadOnlyList<SqlExpression> arguments)
540+
{
541+
SqlExpression hour, minute, second, millisecond;
542+
if (arguments.Count == 4) {
543+
hour = arguments[0];
544+
minute = arguments[1];
545+
second = arguments[2];
546+
millisecond = arguments[3];
547+
}
548+
else if (arguments.Count == 1) {
549+
var ticks = arguments[0];
550+
hour = SqlDml.Cast(ticks / 36000000000, SqlType.Int32);
551+
minute = SqlDml.Cast((ticks / 600000000) % 60, SqlType.Int32);
552+
second = SqlDml.Cast((ticks / 10000000) % 60, SqlType.Int32);
553+
millisecond = SqlDml.Cast(ticks % 10000000, SqlType.Int32);
554+
}
555+
else {
556+
throw new InvalidOperationException("Unsupported count of parameters");
557+
}
558+
return TimeAddSeconds(
559+
TimeAddMinutes(
560+
TimeAddHours(
561+
SqlDml.Literal(new TimeOnly(0, 0, 0, 0)),
562+
hour),
563+
minute),
564+
second,
565+
millisecond);
566+
}
567+
542568
private static SqlExpression TimeAddHours(SqlExpression time, SqlExpression seconds) =>
543569
SqlDml.FunctionCall("STRFTIME", TimeFormat, time, SqlDml.Concat(seconds, " ", "HOURS"));
544570

0 commit comments

Comments
 (0)