Search in sources :

Example 6 with DatePart

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

the class DefaultParseContext method parseFieldTruncIf.

private final Field<?> parseFieldTruncIf() {
    boolean forceNumericPrecision = false;
    if (parseFunctionNameIf("TRUNC") || (forceNumericPrecision |= parseFunctionNameIf("TRUNCATE", "TRUNCNUM"))) {
        parse('(');
        Field<?> arg1 = parseField();
        if (forceNumericPrecision && parse(',') || parseIf(',')) {
            String part;
            if (!forceNumericPrecision && (part = parseStringLiteralIf()) != null) {
                part = part.toUpperCase();
                DatePart p;
                if ("YY".equals(part) || "YYYY".equals(part) || "YEAR".equals(part))
                    p = DatePart.YEAR;
                else if ("MM".equals(part) || "MONTH".equals(part))
                    p = DatePart.MONTH;
                else if ("DD".equals(part))
                    p = DatePart.DAY;
                else if ("HH".equals(part))
                    p = DatePart.HOUR;
                else if ("MI".equals(part))
                    p = DatePart.MINUTE;
                else if ("SS".equals(part))
                    p = DatePart.SECOND;
                else
                    throw exception("Unsupported date part");
                parse(')');
                return DSL.trunc((Field) arg1, p);
            } else {
                Field<?> arg2 = toField(parseNumericOp());
                parse(')');
                return DSL.trunc((Field) arg1, (Field) arg2);
            }
        }
        parse(')');
        // [#10668] Ignore TRUNC() when calling TRUNC(CURRENT_DATE) or TRUNC(SYSDATE) in Oracle
        if (arg1 instanceof CurrentDate)
            return arg1;
        else if (arg1.getDataType().isDateTime())
            return DSL.trunc((Field) arg1, DatePart.DAY);
        else if (arg1.getDataType().isNumeric())
            return DSL.trunc((Field) arg1, inline(0));
        else
            return DSL.trunc((Field) arg1);
    }
    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)

Example 7 with DatePart

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

the class DefaultParseContext method parseSumRightOperand.

private final Field parseSumRightOperand(FieldOrRow r, boolean add) {
    Field rhs = (Field) parseFactor();
    DatePart part;
    if (!ignoreProEdition() && (parseKeywordIf("YEAR") || parseKeywordIf("YEARS")) && requireProEdition())
        part = DatePart.YEAR;
    else if (!ignoreProEdition() && (parseKeywordIf("MONTH") || parseKeywordIf("MONTHS")) && requireProEdition())
        part = DatePart.MONTH;
    else if (!ignoreProEdition() && (parseKeywordIf("DAY") || parseKeywordIf("DAYS")) && requireProEdition())
        part = DatePart.DAY;
    else if (!ignoreProEdition() && (parseKeywordIf("HOUR") || parseKeywordIf("HOURS")) && requireProEdition())
        part = DatePart.HOUR;
    else if (!ignoreProEdition() && (parseKeywordIf("MINUTE") || parseKeywordIf("MINUTES")) && requireProEdition())
        part = DatePart.MINUTE;
    else if (!ignoreProEdition() && (parseKeywordIf("SECOND") || parseKeywordIf("SECONDS")) && requireProEdition())
        part = DatePart.SECOND;
    else
        part = null;
    Field lhs = (Field) r;
    if (add)
        return lhs.add(rhs);
    else if (lhs.getDataType().isDate() && rhs.getDataType().isDate())
        return DSL.dateDiff(lhs, rhs);
    else if (lhs.getDataType().isTimestamp() && rhs.getDataType().isTimestamp())
        return DSL.timestampDiff(lhs, rhs);
    else
        return lhs.sub(rhs);
}
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)

Example 8 with DatePart

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

the class DefaultParseContext method parseFieldDateDiffIf.

private final Field<?> parseFieldDateDiffIf() {
    if (parseFunctionNameIf("DATEDIFF")) {
        parse('(');
        DatePart datePart = parseDatePartIf();
        if (datePart != null)
            parse(',');
        Field<Date> d1 = (Field<Date>) parseField();
        if (parseIf(',')) {
            Field<Date> d2 = (Field<Date>) parseField();
            parse(')');
            if (datePart != null)
                return DSL.dateDiff(datePart, d1, d2);
            else
                return DSL.dateDiff(d1, d2);
        }
        parse(')');
        if (datePart != null)
            return DSL.dateDiff((Field) field(datePart.toName()), d1);
        else
            throw unsupportedClause();
    }
    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.currentDate(org.jooq.impl.DSL.currentDate) DSL.toDate(org.jooq.impl.DSL.toDate) Date(java.sql.Date)

Example 9 with DatePart

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

the class DefaultParseContext method parseFieldDatePartIf.

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

Example 10 with DatePart

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

the class DefaultParseContext method parseDatePartIf.

private final DatePart parseDatePartIf() {
    int p = position();
    boolean string = parseIf('\'');
    DatePart result = parseDatePartIf0();
    if (result == null)
        position(p);
    else if (string)
        parse('\'');
    // date part to a type explicitly
    if (parseIf("::"))
        parseDataType();
    return result;
}
Also used : DatePart(org.jooq.DatePart) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint)

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