[jOOQ/jOOQ#9011] Support CURRENT_TIMESTAMP(<precision>)

`DSL#currentTimestamp()` and `DSL#now()` have been overloaded with a
method declaring a `Field<Integer>` parameter, representing the
precision for the timestamp function. Many dialects support this.

Also the parser can now parse expressions like `NOW(6)`.
This commit is contained in:
Knut Wannheden 2019-08-08 10:42:02 +02:00
parent 1ec32db4bc
commit 6c15996371
3 changed files with 67 additions and 7 deletions

View File

@ -37,14 +37,30 @@
*/
package org.jooq.impl;
// ...
// ...
import static org.jooq.SQLDialect.CUBRID;
import static org.jooq.SQLDialect.DERBY;
// ...
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
import static org.jooq.impl.Keywords.F_CURRENT_BIGDATETIME;
import static org.jooq.impl.Keywords.F_CURRENT_TIMESTAMP;
import static org.jooq.impl.Keywords.F_NOW;
import static org.jooq.impl.Keywords.K_CURRENT;
import static org.jooq.impl.Keywords.K_TIMESTAMP;
import java.util.EnumSet;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
@ -54,10 +70,18 @@ final class CurrentTimestamp<T> extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private static final EnumSet<SQLDialect> NO_SUPPORT_PRECISION = EnumSet.of(CUBRID, DERBY, SQLITE);
private final Field<Integer> precision;
CurrentTimestamp(DataType<T> type) {
this(null, type);
}
CurrentTimestamp(Field<Integer> precision, DataType<T> type) {
super(DSL.name("current_timestamp"), type);
this.precision = precision;
}
@Override
@ -96,10 +120,16 @@ final class CurrentTimestamp<T> extends AbstractField<T> {
case HSQLDB:
case POSTGRES:
case SQLITE:
ctx.visit(F_CURRENT_TIMESTAMP);
if (precision != null && !NO_SUPPORT_PRECISION.contains(ctx.family()))
ctx.visit(F_CURRENT_TIMESTAMP).sql('(').visit(precision).sql(')');
else
ctx.visit(F_CURRENT_TIMESTAMP);
break;
default:
ctx.visit(F_CURRENT_TIMESTAMP).sql("()");
if (precision != null && !NO_SUPPORT_PRECISION.contains(ctx.family()))
ctx.visit(F_CURRENT_TIMESTAMP).sql('(').visit(precision).sql(')');
else
ctx.visit(F_CURRENT_TIMESTAMP).sql("()");
break;
}
}

View File

@ -14040,6 +14040,16 @@ public class DSL {
return new CurrentTimestamp<>(SQLDataType.TIMESTAMP);
}
/**
* Get the current_timestamp() function returning a SQL standard
* {@link SQLDataType#TIMESTAMP} type with the specified fractional
* seconds precision.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Timestamp> currentTimestamp(Field<Integer> precision) {
return new CurrentTimestamp<>(precision, SQLDataType.TIMESTAMP);
}
/**
* Synonym for {@link #currentTimestamp()}.
*/
@ -14048,6 +14058,14 @@ public class DSL {
return currentTimestamp();
}
/**
* Synonym for {@link #currentTimestamp(Field)}.
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Timestamp> now(Field<Integer> precision) {
return currentTimestamp(precision);
}
/**
* Get the current_date() function returning a SQL standard

View File

@ -5555,8 +5555,15 @@ final class ParserImpl implements Parser {
if (D.is(type))
if ((parseKeywordIf(ctx, "CURRENT_DATE") || parseKeywordIf(ctx, "CURRENT DATE")) && (parseIf(ctx, '(') && parse(ctx, ')') || true))
return currentDate();
else if ((parseKeywordIf(ctx, "CURRENT_TIMESTAMP") || parseKeywordIf(ctx, "CURRENT TIMESTAMP")) && (parseIf(ctx, '(') && parse(ctx, ')') || true))
return currentTimestamp();
else if (parseKeywordIf(ctx, "CURRENT_TIMESTAMP") || parseKeywordIf(ctx, "CURRENT TIMESTAMP")) {
Field<Integer> precision = null;
if (parseIf(ctx, '('))
if (!parseIf(ctx, ')')) {
precision = (Field<Integer>) parseField(ctx, N);
parse(ctx, ')');
}
return precision != null ? currentTimestamp(precision) : currentTimestamp();
}
else if ((parseKeywordIf(ctx, "CURRENT_TIME") || parseKeywordIf(ctx, "CURRENT TIME")) && (parseIf(ctx, '(') && parse(ctx, ')') || true))
return currentTime();
else if (parseFunctionNameIf(ctx, "CURDATE") && parse(ctx, '(') && parse(ctx, ')'))
@ -5780,8 +5787,13 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseNextvalCurrvalIf(ctx, SequenceMethod.NEXTVAL)) != null)
return field;
else if (parseFunctionNameIf(ctx, "NOW") && parse(ctx, '(') && parse(ctx, ')'))
return now();
else if (parseFunctionNameIf(ctx, "NOW") && parse(ctx, '(')) {
if (parseIf(ctx, ')'))
return now();
Field<Integer> precision = (Field<Integer>) parseField(ctx, N);
parse(ctx, ')');
return now(precision);
}
break;