Skip to content

Commit e26210e

Browse files
committed
SqlDml: Add DateConstruct/TimeConstruct methods
1 parent 86b26be commit e26210e

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

Orm/Xtensive.Orm/Sql/SqlDml.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,19 @@ public static SqlFunctionCall DateTimeConstruct(SqlExpression year, SqlExpressio
565565
}
566566

567567
#if NET6_0_OR_GREATER //DO_DATEONLY
568+
569+
// SQL Server - DATEFROMPARTS(y, m, d, fractions, precision) https://learn.microsoft.com/en-us/sql/t-sql/functions/datefromparts-transact-sql?view=sql-server-2016
570+
// Mysql - MAKEDATE(year, dayofyear), propably. https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
571+
// Oracle - if x is the number of seconds after midnight on January 1, 1970: TO_DATE( '01/01/1970', 'MM/DD/YYYY') + (x / (24 * 60 * 60))
572+
// In the examples above, x can be any number, not necessarily an integer.
573+
//https://community.oracle.com/tech/developers/discussion/958492/how-to-convert-integer-to-date
574+
// PgSql - some magic with string concatination or by using to_timestampt(ticks-in-some-form::numeric)::time https://stackoverflow.com/questions/26198358/postgresql-cast-numeric-to-date-and-format
575+
// https://stackoverflow.com/questions/22835874/postgresql-convert-timestamp-to-time-or-retrieve-only-time-from-timestamp-col
576+
// from 9.4 make_date(year, month, day)
577+
578+
// firebird - dateadd(second, AmountOfSeconds, cast('00:00:00' as time))
579+
// sqlite - probably date(seconds-form-unixepoch, 'unixepoch')
580+
568581
public static SqlFunctionCall DateConstruct(SqlExpression year, SqlExpression month, SqlExpression day)
569582
{
570583
ArgumentNullException.ThrowIfNull(year);
@@ -576,14 +589,31 @@ public static SqlFunctionCall DateConstruct(SqlExpression year, SqlExpression mo
576589
return new SqlFunctionCall(SqlFunctionType.DateConstruct, year, month, day);
577590
}
578591

579-
public static SqlFunctionCall TimeConstruct(SqlExpression hours,
580-
SqlExpression minutes,
581-
SqlExpression seconds,
582-
SqlExpression milliseconds)
583-
{
584-
var m = milliseconds + 1000L * (seconds + 60L * (minutes + 60L * hours));
585-
var ticks = 10_000 * m;
586-
return new SqlFunctionCall(SqlFunctionType.TimeConstruct, ticks);
592+
// SQL Server - TIMEFROMPARTS(h, m, s, fractions, precision) https://learn.microsoft.com/en-us/sql/t-sql/functions/timefromparts-transact-sql?view=sql-server-2016
593+
// Mysql - addtime (MAKETIME(h, m, s), '00:00:00.<milliseconds>), propably. https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
594+
// Oracle - numtodsinterval(value-in-seconds.milliseconds, 'second') //https://docs.oracle.com/database/121/SQLRF/functions129.htm#SQLRF00682
595+
// PgSql - see current impl of DateConstruct for inspiration :)
596+
// from 9.4 there is make_time(h, m,s)
597+
// firebird - dateadd(second, ColAmountOfSeconds, cast('00:00:00' as time))
598+
// sqlite - time('00:00:00', '+ NNN.NNNN seconds' //https://codernotes.ru/articles/bazy-dannyh-t-sql/funkcii-daty-i-vremeni-v-sqlite.html
599+
600+
public static SqlFunctionCall TimeConstruct(SqlExpression hour,
601+
SqlExpression minute,
602+
SqlExpression second,
603+
SqlExpression millisecond)
604+
{
605+
ArgumentNullException.ThrowIfNull(hour);
606+
ArgumentNullException.ThrowIfNull(minute);
607+
ArgumentNullException.ThrowIfNull(second);
608+
ArgumentNullException.ThrowIfNull(millisecond);
609+
SqlValidator.EnsureIsArithmeticExpression(hour);
610+
SqlValidator.EnsureIsArithmeticExpression(minute);
611+
SqlValidator.EnsureIsArithmeticExpression(second);
612+
SqlValidator.EnsureIsArithmeticExpression(millisecond);
613+
614+
//var m = milliseconds + 1000L * (seconds + 60L * (minutes + 60L * hours));
615+
//var ticks = 10_000 * m;
616+
return new SqlFunctionCall(SqlFunctionType.TimeConstruct, hour, minute, second, millisecond);
587617
}
588618
#endif
589619

0 commit comments

Comments
 (0)