Search in sources :

Example 1 with YearToSecond

use of org.jooq.types.YearToSecond in project jOOQ by jOOQ.

the class DefaultParseContext method parseIntervalLiteral.

private final Interval parseIntervalLiteral() {
    Interval result = parsePostgresIntervalLiteralIf();
    if (result != null)
        return result;
    String string = parseStringLiteral();
    String message = "Illegal interval literal";
    if (parseIntervalPrecisionKeywordIf("YEAR"))
        if (parseKeywordIf("TO") && parseIntervalPrecisionKeywordIf("MONTH"))
            return requireNotNull(YearToMonth.yearToMonth(string), message);
        else
            return requireNotNull(YearToMonth.year(string), message);
    else if (parseIntervalPrecisionKeywordIf("MONTH"))
        return requireNotNull(YearToMonth.month(string), message);
    else if (parseIntervalPrecisionKeywordIf("DAY"))
        if (parseKeywordIf("TO"))
            if (parseIntervalPrecisionKeywordIf("SECOND"))
                return requireNotNull(DayToSecond.dayToSecond(string), message);
            else if (parseIntervalPrecisionKeywordIf("MINUTE"))
                return requireNotNull(DayToSecond.dayToMinute(string), message);
            else if (parseIntervalPrecisionKeywordIf("HOUR"))
                return requireNotNull(DayToSecond.dayToHour(string), message);
            else
                throw expected("HOUR", "MINUTE", "SECOND");
        else
            return requireNotNull(DayToSecond.day(string), message);
    else if (parseIntervalPrecisionKeywordIf("HOUR"))
        if (parseKeywordIf("TO"))
            if (parseIntervalPrecisionKeywordIf("SECOND"))
                return requireNotNull(DayToSecond.hourToSecond(string), message);
            else if (parseIntervalPrecisionKeywordIf("MINUTE"))
                return requireNotNull(DayToSecond.hourToMinute(string), message);
            else
                throw expected("MINUTE", "SECOND");
        else
            return requireNotNull(DayToSecond.hour(string), message);
    else if (parseIntervalPrecisionKeywordIf("MINUTE"))
        if (parseKeywordIf("TO") && parseIntervalPrecisionKeywordIf("SECOND"))
            return requireNotNull(DayToSecond.minuteToSecond(string), message);
        else
            return requireNotNull(DayToSecond.minute(string), message);
    else if (parseIntervalPrecisionKeywordIf("SECOND"))
        return requireNotNull(DayToSecond.second(string), message);
    DayToSecond ds = DayToSecond.valueOf(string);
    if (ds != null)
        return ds;
    YearToMonth ym = YearToMonth.valueOf(string);
    if (ym != null)
        return ym;
    YearToSecond ys = YearToSecond.valueOf(string);
    if (ys != null)
        return ys;
    throw exception(message);
}
Also used : DayToSecond(org.jooq.types.DayToSecond) YearToSecond(org.jooq.types.YearToSecond) YearToMonth(org.jooq.types.YearToMonth) Interval(org.jooq.types.Interval)

Example 2 with YearToSecond

use of org.jooq.types.YearToSecond in project jOOQ by jOOQ.

the class DefaultParseContext method parsePostgresIntervalLiteralIf.

private final Interval parsePostgresIntervalLiteralIf() {
    int p = position();
    p: if (parseIf('\'')) {
        parseIf('@');
        Number year = null;
        Number month = null;
        Number day = null;
        Number hour = null;
        Number minute = null;
        Number second = null;
        do {
            boolean minus = parseIf('-');
            if (!minus)
                parseIf('+');
            Number n = parseUnsignedNumericLiteralIf(minus ? Sign.MINUS : Sign.NONE);
            if (n == null)
                break p;
            switch(characterUpper()) {
                case 'D':
                    if (parseKeywordIf("D") || parseKeywordIf("DAY") || parseKeywordIf("DAYS"))
                        if (day == null)
                            day = n;
                        else
                            throw exception("Day part already defined");
                    break;
                case 'H':
                    if (parseKeywordIf("H") || parseKeywordIf("HOUR") || parseKeywordIf("HOURS"))
                        if (hour == null)
                            hour = n;
                        else
                            throw exception("Hour part already defined");
                    break;
                case 'M':
                    if (parseKeywordIf("M") || parseKeywordIf("MIN") || parseKeywordIf("MINS") || parseKeywordIf("MINUTE") || parseKeywordIf("MINUTES"))
                        if (minute == null)
                            minute = n;
                        else
                            throw exception("Minute part already defined");
                    else if (parseKeywordIf("MON") || parseKeywordIf("MONS") || parseKeywordIf("MONTH") || parseKeywordIf("MONTHS"))
                        if (month == null)
                            month = n;
                        else
                            throw exception("Month part already defined");
                    break;
                case 'S':
                    if (parseKeywordIf("S") || parseKeywordIf("SEC") || parseKeywordIf("SECS") || parseKeywordIf("SECOND") || parseKeywordIf("SECONDS"))
                        if (second == null)
                            second = n;
                        else
                            throw exception("Second part already defined");
                    break;
                case 'Y':
                    if (parseKeywordIf("Y") || parseKeywordIf("YEAR") || parseKeywordIf("YEARS"))
                        if (year == null)
                            year = n;
                        else
                            throw exception("Year part already defined");
                    break;
                default:
                    break p;
            }
        } while (!parseIf('\''));
        int months = (month == null ? 0 : month.intValue()) + (year == null ? 0 : asInt((long) (year.doubleValue() * 12)));
        double seconds = (month == null ? 0.0 : ((month.doubleValue() % 1.0) * 30 * 86400)) + (day == null ? 0.0 : ((day.doubleValue() * 86400))) + (hour == null ? 0.0 : ((hour.doubleValue() * 3600))) + (minute == null ? 0.0 : ((minute.doubleValue() * 60))) + (second == null ? 0.0 : ((second.doubleValue())));
        return new YearToSecond(new YearToMonth(0, months), new DayToSecond(0, 0, 0, asInt((long) seconds), asInt((long) ((seconds % 1.0) * 1000000000))));
    }
    position(p);
    return null;
}
Also used : DSL.rowNumber(org.jooq.impl.DSL.rowNumber) DayToSecond(org.jooq.types.DayToSecond) YearToSecond(org.jooq.types.YearToSecond) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint) YearToMonth(org.jooq.types.YearToMonth)

Aggregations

DayToSecond (org.jooq.types.DayToSecond)2 YearToMonth (org.jooq.types.YearToMonth)2 YearToSecond (org.jooq.types.YearToSecond)2 Constraint (org.jooq.Constraint)1 DSL.constraint (org.jooq.impl.DSL.constraint)1 DSL.rowNumber (org.jooq.impl.DSL.rowNumber)1 Interval (org.jooq.types.Interval)1