[#7794] Support DatePart.EPOCH
This commit is contained in:
parent
f0ac0ea00c
commit
08e2d8e695
@ -653,6 +653,7 @@ term =
|
||||
| 'DECODE' '(' field ',' field ',' field { ',' field } ')'
|
||||
| 'DENSE_RANK' ( '(' ')' over | '(' fields ')' withinGroup )
|
||||
| ( 'DEG' | 'DEGREE' | 'DEGREES' ) '(' sum ')'
|
||||
| 'EPOCH' '(' field ')'
|
||||
| 'EXTRACT' '(' datePart 'FROM' field ')'
|
||||
| 'EXP' '(' sum ')'
|
||||
| 'EVERY' '(' field ')' [ filter ] [ over ]
|
||||
@ -787,6 +788,7 @@ datePart =
|
||||
| ( 'MILLISECOND' | 'MS' )
|
||||
| ( 'MICROSECOND' | 'MCS' )
|
||||
| ( 'NANOSECOND' | 'NS' )
|
||||
| 'EPOCH'
|
||||
| ( 'QUARTER' | 'QQ' | 'Q' )
|
||||
| ( 'WEEK ' | 'WW' | 'WK' )
|
||||
| ( 'ISO_DAY_OF_WEEK' | 'ISODOW' )
|
||||
|
||||
@ -167,11 +167,8 @@ public enum DatePart {
|
||||
|
||||
/**
|
||||
* The epoch in seconds since 1970-01-01.
|
||||
*
|
||||
* @deprecated - 3.11 - [#2132] Support for this type is still experimental
|
||||
*/
|
||||
@Support({ POSTGRES })
|
||||
@Deprecated
|
||||
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
EPOCH("epoch"),
|
||||
|
||||
/**
|
||||
|
||||
@ -14703,6 +14703,43 @@ public class DSL {
|
||||
return new Extract(nullSafe(field), datePart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the epoch of a date.
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(Field, DatePart)}
|
||||
* with {@link DatePart#EPOCH}
|
||||
*/
|
||||
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
public static Field<Integer> epoch(java.util.Date value) {
|
||||
return extract(value, DatePart.EPOCH);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the epoch of a date.
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(Field, DatePart)}
|
||||
* with {@link DatePart#EPOCH}
|
||||
*/
|
||||
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
public static Field<Integer> epoch(Temporal value) {
|
||||
return extract(value, DatePart.EPOCH);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the epoch of a date.
|
||||
* <p>
|
||||
* This is the same as calling {@link #extract(Field, DatePart)}
|
||||
* with {@link DatePart#EPOCH}
|
||||
*/
|
||||
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
public static Field<Integer> epoch(Field<?> field) {
|
||||
return extract(field, DatePart.EPOCH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the year part of a date.
|
||||
* <p>
|
||||
|
||||
@ -44,6 +44,7 @@ import static org.jooq.impl.DSL.inline;
|
||||
import static org.jooq.impl.DSL.one;
|
||||
import static org.jooq.impl.SQLDataType.INTEGER;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
@ -87,6 +88,8 @@ final class Extract extends AbstractFunction<Integer> {
|
||||
return DSL.field("{strftime}('%S', {0})", INTEGER, field);
|
||||
|
||||
// See: https://www.sqlite.org/lang_datefunc.html
|
||||
case EPOCH:
|
||||
return DSL.field("{strftime}('%s', {0})", INTEGER, field);
|
||||
case ISO_DAY_OF_WEEK:
|
||||
return dowSun0ToISO(DSL.field("{strftime}('%w', {0})", INTEGER, field));
|
||||
case DAY_OF_WEEK:
|
||||
@ -222,6 +225,11 @@ final class Extract extends AbstractFunction<Integer> {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -231,6 +239,8 @@ final class Extract extends AbstractFunction<Integer> {
|
||||
case MARIADB:
|
||||
case MYSQL:
|
||||
switch (datePart) {
|
||||
case EPOCH:
|
||||
return DSL.field("{unix_timestamp}({0})", INTEGER, field);
|
||||
case ISO_DAY_OF_WEEK:
|
||||
return DSL.field("{weekday}({0})", INTEGER, field).add(one());
|
||||
case DAY_OF_WEEK:
|
||||
|
||||
@ -92,6 +92,7 @@ import static org.jooq.impl.DSL.dayOfYear;
|
||||
import static org.jooq.impl.DSL.defaultValue;
|
||||
import static org.jooq.impl.DSL.deg;
|
||||
import static org.jooq.impl.DSL.denseRank;
|
||||
import static org.jooq.impl.DSL.epoch;
|
||||
import static org.jooq.impl.DSL.every;
|
||||
import static org.jooq.impl.DSL.exists;
|
||||
import static org.jooq.impl.DSL.exp;
|
||||
@ -4568,6 +4569,10 @@ final class ParserImpl implements Parser {
|
||||
else if (parseFunctionNameIf(ctx, "EXP"))
|
||||
return exp((Field) parseFieldSumParenthesised(ctx));
|
||||
|
||||
if (D.is(type))
|
||||
if ((field = parseFieldEpochIf(ctx)) != null)
|
||||
return field;
|
||||
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
@ -5539,6 +5544,13 @@ final class ParserImpl implements Parser {
|
||||
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
case 'E':
|
||||
if (parseKeywordIf(ctx, "EPOCH"))
|
||||
return DatePart.EPOCH;
|
||||
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
case 'H':
|
||||
if (parseKeywordIf(ctx, "HOUR") ||
|
||||
@ -6130,6 +6142,17 @@ final class ParserImpl implements Parser {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final Field<?> parseFieldEpochIf(ParserContext ctx) {
|
||||
if (parseFunctionNameIf(ctx, "EPOCH")) {
|
||||
parse(ctx, '(');
|
||||
Field<Timestamp> f1 = (Field) parseField(ctx, D);
|
||||
parse(ctx, ')');
|
||||
return epoch(f1);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final Field<?> parseFieldHourIf(ParserContext ctx) {
|
||||
if (parseFunctionNameIf(ctx, "HOUR")) {
|
||||
parse(ctx, '(');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user