Search in sources :

Example 1 with DatePart

use of org.jooq.DatePart in project jOOQ by jOOQ.

the class ParserImpl method parseFieldExtractIf.

private static final Field<?> parseFieldExtractIf(ParserContext ctx) {
    if (parseKeywordIf(ctx, "EXTRACT")) {
        parse(ctx, '(');
        DatePart part = parseDatePart(ctx);
        parseKeyword(ctx, "FROM");
        Field<?> field = parseField(ctx);
        parse(ctx, ')');
        return extract(field, part);
    }
    return null;
}
Also used : DatePart(org.jooq.DatePart)

Example 2 with DatePart

use of org.jooq.DatePart in project jOOQ by jOOQ.

the class DefaultParseContext method parseFieldExtractIf.

private final Field<?> parseFieldExtractIf() {
    if (parseFunctionNameIf("EXTRACT")) {
        parse('(');
        DatePart part = parseDatePart();
        parseKeyword("FROM");
        Field<?> field = parseField();
        parse(')');
        return extract(field, part);
    }
    return null;
}
Also used : DatePart(org.jooq.DatePart)

Example 3 with DatePart

use of org.jooq.DatePart in project jOOQ by jOOQ.

the class DefaultParseContext method parseFieldDateTruncIf.

private final Field<?> parseFieldDateTruncIf() {
    if (parseFunctionNameIf("DATE_TRUNC", "DATETIME_TRUNC")) {
        parse('(');
        Field<?> field;
        DatePart part;
        switch(parseFamily()) {
            default:
                part = parseDatePart();
                parse(',');
                field = parseField();
                break;
        }
        parse(')');
        return trunc(field, part);
    }
    return null;
}
Also used : DatePart(org.jooq.DatePart)

Example 4 with DatePart

use of org.jooq.DatePart in project jOOQ by jOOQ.

the class DefaultParseContext method parseFieldIntervalLiteralIf.

private final Field<?> parseFieldIntervalLiteralIf() {
    int p = position();
    if (parseKeywordIf("INTERVAL")) {
        if (peek('\'')) {
            return inline(parseIntervalLiteral());
        } else {
            Long interval = parseUnsignedIntegerLiteralIf();
            if (interval != null) {
                DatePart part = parseIntervalDatePart();
                long l = interval;
                int i = asInt(l);
                switch(part) {
                    case YEAR:
                        return inline(new YearToMonth(i));
                    case QUARTER:
                        return inline(new YearToMonth(0, 3 * i));
                    case MONTH:
                        return inline(new YearToMonth(0, i));
                    case WEEK:
                        return inline(new DayToSecond(7 * i));
                    case DAY:
                        return inline(new DayToSecond(i));
                    case HOUR:
                        return inline(new DayToSecond(0, i));
                    case MINUTE:
                        return inline(new DayToSecond(0, 0, i));
                    case SECOND:
                        return inline(new DayToSecond(0, 0, 0, i));
                    case MILLISECOND:
                        return inline(new DayToSecond(0, 0, 0, asInt(l / 1000), (int) (l % 1000 * 1000000)));
                    case MICROSECOND:
                        return inline(new DayToSecond(0, 0, 0, asInt(l / 1000000), (int) (l % 1000000 * 1000)));
                    case NANOSECOND:
                        return inline(new DayToSecond(0, 0, 0, asInt(l / 1000000000), (int) (l % 1000000000)));
                }
            } else {
                position(p);
                return field(parseIdentifier());
            }
        }
    }
    return null;
}
Also used : DatePart(org.jooq.DatePart) DayToSecond(org.jooq.types.DayToSecond) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint) YearToMonth(org.jooq.types.YearToMonth)

Example 5 with DatePart

use of org.jooq.DatePart in project jOOQ by jOOQ.

the class DefaultParseContext method parseFieldDateAddIf.

private final Field<?> parseFieldDateAddIf() {
    boolean sub = false;
    // SQL Server style
    if (parseFunctionNameIf("DATEADD")) {
        parse('(');
        DatePart part = parseDatePart();
        parse(',');
        Field<Number> interval = (Field<Number>) parseField();
        parse(',');
        Field<Date> date = (Field<Date>) parseField();
        parse(')');
        return DSL.dateAdd(date, interval, part);
    } else // MySQL style
    if (parseFunctionNameIf("DATE_ADD") || (sub = parseFunctionNameIf("DATE_SUB"))) {
        parse('(');
        Field<?> d = parseField();
        // [#12025] In the absence of meta data, assume TIMESTAMP
        Field<?> date = d.getDataType().isDateTime() ? d : d.coerce(TIMESTAMP);
        parse(',');
        // [#8792] TODO: Support parsing interval expressions
        Field<?> interval = parseFieldIntervalLiteralIf();
        parse(')');
        return sub ? DSL.dateSub((Field) date, (Field) interval) : DSL.dateAdd((Field) date, (Field) interval);
    }
    return null;
}
Also used : GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) DatePart(org.jooq.DatePart) DSL.rowNumber(org.jooq.impl.DSL.rowNumber) DSL.currentDate(org.jooq.impl.DSL.currentDate) DSL.toDate(org.jooq.impl.DSL.toDate) Date(java.sql.Date)

Aggregations

DatePart (org.jooq.DatePart)11 Field (org.jooq.Field)4 GroupField (org.jooq.GroupField)4 SelectField (org.jooq.SelectField)4 SortField (org.jooq.SortField)4 TableField (org.jooq.TableField)4 Date (java.sql.Date)2 Constraint (org.jooq.Constraint)2 DSL.constraint (org.jooq.impl.DSL.constraint)2 DSL.currentDate (org.jooq.impl.DSL.currentDate)2 DSL.toDate (org.jooq.impl.DSL.toDate)2 Name (org.jooq.Name)1 DSL.rowNumber (org.jooq.impl.DSL.rowNumber)1 DayToSecond (org.jooq.types.DayToSecond)1 YearToMonth (org.jooq.types.YearToMonth)1