[#7796] Support DatePart.QUARTER

This commit is contained in:
Lukas Eder 2018-08-22 15:46:56 +02:00
parent 08e2d8e695
commit e395520fee
5 changed files with 82 additions and 12 deletions

View File

@ -709,6 +709,7 @@ term =
| ( 'PERCENTILE_CONT' | 'PERCENTILE_DISC' ) '(' unsignedNumericLiteral ')' withinGroup [ over ]
| ( 'POW' | 'POWER' ) '(' field ',' field ')'
| 'PRIOR' concat
| 'QUARTER' '(' field ')'
| (
'REGR_SLOPE'
| 'REGR_INTERCEPT'

View File

@ -173,11 +173,8 @@ public enum DatePart {
/**
* The quarter. Jan-Mar = 1, Apr-Jun = 2, Jul-Sep = 3, Oct-Dec = 4.
*
* @deprecated - 3.11 - [#2132] Support for this type is still experimental
*/
@Support({ POSTGRES })
@Deprecated
@Support
QUARTER("quarter"),
/**

View File

@ -14740,6 +14740,43 @@ public class DSL {
return extract(field, DatePart.EPOCH);
}
/**
* Get the quarter of a date.
* <p>
* This is the same as calling {@link #extract(Field, DatePart)}
* with {@link DatePart#QUARTER}
*/
@Support
public static Field<Integer> quarter(java.util.Date value) {
return extract(value, DatePart.QUARTER);
}
/**
* Get the quarter of a date.
* <p>
* This is the same as calling {@link #extract(Field, DatePart)}
* with {@link DatePart#QUARTER}
*/
@Support
public static Field<Integer> quarter(Temporal value) {
return extract(value, DatePart.QUARTER);
}
/**
* Get the quarter of a date.
* <p>
* This is the same as calling {@link #extract(Field, DatePart)}
* with {@link DatePart#QUARTER}
*/
@Support
public static Field<Integer> quarter(Field<?> field) {
return extract(field, DatePart.QUARTER);
}
/**
* Get the year part of a date.
* <p>

View File

@ -50,7 +50,6 @@ import java.sql.Timestamp;
import org.jooq.Configuration;
import org.jooq.DatePart;
import org.jooq.Field;
import org.jooq.exception.SQLDialectNotSupportedException;
/**
* @author Lukas Eder
@ -97,7 +96,7 @@ final class Extract extends AbstractFunction<Integer> {
case DAY_OF_YEAR:
return DSL.field("{strftime}('%j', {0})", INTEGER, field);
default:
throw new SQLDialectNotSupportedException("DatePart not supported: " + datePart);
return getNativeFunction();
}
@ -152,7 +151,13 @@ final class Extract extends AbstractFunction<Integer> {
throw new SQLDialectNotSupportedException("DatePart not supported: " + datePart);
return getNativeFunction();
@ -241,6 +246,8 @@ final class Extract extends AbstractFunction<Integer> {
switch (datePart) {
case EPOCH:
return DSL.field("{unix_timestamp}({0})", INTEGER, field);
case QUARTER:
return DSL.field("{quarter}({0})", INTEGER, field);
case ISO_DAY_OF_WEEK:
return DSL.field("{weekday}({0})", INTEGER, field).add(one());
case DAY_OF_WEEK:
@ -255,6 +262,8 @@ final class Extract extends AbstractFunction<Integer> {
case POSTGRES:
switch (datePart) {
case QUARTER:
return DSL.field("{extract}({quarter from} {0})", INTEGER, field);
case DAY_OF_WEEK:
return DSL.field("({extract}({dow from} {0}) + 1)", INTEGER, field);
case ISO_DAY_OF_WEEK:
@ -267,16 +276,21 @@ final class Extract extends AbstractFunction<Integer> {
case HSQLDB:
switch (datePart) {
case QUARTER:
return DSL.field("{quarter}({0})", INTEGER, field);
case ISO_DAY_OF_WEEK:
return dowSun1ToISO(DSL.field("{extract}({day_of_week from} {0})", INTEGER, field));
default:
return getNativeFunction();
}
case H2:
switch (datePart) {
case QUARTER:
return DSL.field("{quarter}({0})", INTEGER, field);
default:
return getNativeFunction();
}
default:
return getNativeFunction();
}
@ -295,6 +309,11 @@ final class Extract extends AbstractFunction<Integer> {
}
private final Field<Integer> getNativeFunction() {
return DSL.field("{extract}({0} {from} {1})", INTEGER, datePart.toKeyword(), field);
switch (datePart) {
case QUARTER:
return DSL.month(field).add(inline(2)).div(inline(3));
default:
return DSL.field("{extract}({0} {from} {1})", INTEGER, datePart.toKeyword(), field);
}
}
}

View File

@ -167,6 +167,7 @@ import static org.jooq.impl.DSL.position;
import static org.jooq.impl.DSL.primaryKey;
import static org.jooq.impl.DSL.prior;
import static org.jooq.impl.DSL.privilege;
import static org.jooq.impl.DSL.quarter;
import static org.jooq.impl.DSL.rad;
import static org.jooq.impl.DSL.rangeBetweenCurrentRow;
import static org.jooq.impl.DSL.rangeBetweenFollowing;
@ -4740,6 +4741,10 @@ final class ParserImpl implements Parser {
if (ctx.characterNext() == '\'')
return inline(parseStringLiteral(ctx));
if (D.is(type))
if ((field = parseFieldQuarterIf(ctx)) != null)
return field;
case 'r':
case 'R':
if (S.is(type))
@ -6153,6 +6158,17 @@ final class ParserImpl implements Parser {
return null;
}
private static final Field<?> parseFieldQuarterIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "QUARTER")) {
parse(ctx, '(');
Field<Timestamp> f1 = (Field) parseField(ctx, D);
parse(ctx, ')');
return quarter(f1);
}
return null;
}
private static final Field<?> parseFieldHourIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "HOUR")) {
parse(ctx, '(');