Search in sources :

Example 1 with YearToMonth

use of org.jooq.types.YearToMonth 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 YearToMonth

use of org.jooq.types.YearToMonth 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)

Example 3 with YearToMonth

use of org.jooq.types.YearToMonth 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 4 with YearToMonth

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

the class PostgresUtils method toYearToMonth.

/**
 * Convert a Postgres interval to a jOOQ <code>YEAR TO MONTH</code> interval
 */
public static YearToMonth toYearToMonth(Object pgInterval) {
    boolean negative = pgInterval.toString().contains("-");
    if (pgIntervalAvailable() && pgInterval instanceof PGInterval) {
        PGInterval i = (PGInterval) pgInterval;
        if (negative)
            i.scale(-1);
        YearToMonth result = new YearToMonth(i.getYears(), i.getMonths());
        if (negative)
            result = result.neg();
        return result;
    } else
        throw new IllegalArgumentException("Unsupported interval type. Make sure you have the pgjdbc or redshift driver on your classpath: " + pgInterval);
}
Also used : PGInterval(org.postgresql.util.PGInterval) YearToMonth(org.jooq.types.YearToMonth)

Example 5 with YearToMonth

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

the class DefaultBinding method set.

@SuppressWarnings("unchecked")
@Override
public void set(BindingSetStatementContext<U> ctx) throws SQLException {
    Configuration configuration = ctx.configuration();
    SQLDialect dialect = ctx.dialect();
    T value = converter.to(ctx.value());
    if (log.isTraceEnabled())
        if (value != null && value.getClass().isArray() && value.getClass() != byte[].class)
            log.trace("Binding variable " + ctx.index(), Arrays.asList((Object[]) value) + " (" + type + ")");
        else
            log.trace("Binding variable " + ctx.index(), value + " (" + type + ")");
    // SQL dialect. See the following section for details
    if (value == null) {
        int sqlType = DefaultDataType.getDataType(dialect, type).getSQLType(configuration);
        // [#1126] Oracle's UDTs need to be bound with their type name
        if (UDTRecord.class.isAssignableFrom(type)) {
            ctx.statement().setNull(ctx.index(), sqlType, Tools.getMappedUDTName(configuration, (Class<UDTRecord<?>>) type));
        } else // Some dialects have trouble binding binary data as BLOB
        if (asList(POSTGRES).contains(configuration.family()) && sqlType == Types.BLOB) {
            ctx.statement().setNull(ctx.index(), Types.BINARY);
        } else // All other types can be set to null if the JDBC type is known
        if (sqlType != Types.OTHER) {
            ctx.statement().setNull(ctx.index(), sqlType);
        } else // [#729] In the absence of the correct JDBC type, try setObject
        {
            ctx.statement().setObject(ctx.index(), null);
        }
    } else {
        Class<?> actualType = type;
        // Try to infer the bind value type from the actual bind value if possible.
        if (actualType == Object.class) {
            actualType = value.getClass();
        }
        if (actualType == Blob.class) {
            ctx.statement().setBlob(ctx.index(), (Blob) value);
        } else if (actualType == Boolean.class) {
            ctx.statement().setBoolean(ctx.index(), (Boolean) value);
        } else if (actualType == BigDecimal.class) {
            if (asList(SQLITE).contains(dialect.family())) {
                ctx.statement().setString(ctx.index(), value.toString());
            } else {
                ctx.statement().setBigDecimal(ctx.index(), (BigDecimal) value);
            }
        } else if (actualType == BigInteger.class) {
            if (asList(SQLITE).contains(dialect.family())) {
                ctx.statement().setString(ctx.index(), value.toString());
            } else {
                ctx.statement().setBigDecimal(ctx.index(), new BigDecimal((BigInteger) value));
            }
        } else if (actualType == Byte.class) {
            ctx.statement().setByte(ctx.index(), (Byte) value);
        } else if (actualType == byte[].class) {
            ctx.statement().setBytes(ctx.index(), (byte[]) value);
        } else if (actualType == Clob.class) {
            ctx.statement().setClob(ctx.index(), (Clob) value);
        } else if (actualType == Double.class) {
            ctx.statement().setDouble(ctx.index(), (Double) value);
        } else if (actualType == Float.class) {
            ctx.statement().setFloat(ctx.index(), (Float) value);
        } else if (actualType == Integer.class) {
            ctx.statement().setInt(ctx.index(), (Integer) value);
        } else if (actualType == Long.class) {
            ctx.statement().setLong(ctx.index(), (Long) value);
        } else if (actualType == Short.class) {
            ctx.statement().setShort(ctx.index(), (Short) value);
        } else if (actualType == String.class) {
            ctx.statement().setString(ctx.index(), (String) value);
        } else // -------------------------------------------------------------
        if (Tools.isDate(actualType)) {
            Date date = getDate(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), date.toString());
            } else {
                ctx.statement().setDate(ctx.index(), date);
            }
        } else if (Tools.isTime(actualType)) {
            Time time = getTime(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), time.toString());
            } else {
                ctx.statement().setTime(ctx.index(), time);
            }
        } else if (Tools.isTimestamp(actualType)) {
            Timestamp timestamp = getTimestamp(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), timestamp.toString());
            } else {
                ctx.statement().setTimestamp(ctx.index(), timestamp);
            }
        } else if (actualType == OffsetTime.class) {
            String string = format((OffsetTime) value);
            ctx.statement().setString(ctx.index(), string);
        } else if (actualType == OffsetDateTime.class) {
            ctx.statement().setString(ctx.index(), format((OffsetDateTime) value));
        } else // [#566] Interval data types are best bound as Strings
        if (actualType == YearToMonth.class) {
            if (dialect.family() == POSTGRES) {
                ctx.statement().setObject(ctx.index(), toPGInterval((YearToMonth) value));
            } else {
                ctx.statement().setString(ctx.index(), value.toString());
            }
        } else if (actualType == DayToSecond.class) {
            if (dialect.family() == POSTGRES) {
                ctx.statement().setObject(ctx.index(), toPGInterval((DayToSecond) value));
            } else {
                ctx.statement().setString(ctx.index(), value.toString());
            }
        } else if (actualType == UByte.class) {
            ctx.statement().setShort(ctx.index(), ((UByte) value).shortValue());
        } else if (actualType == UShort.class) {
            ctx.statement().setInt(ctx.index(), ((UShort) value).intValue());
        } else if (actualType == UInteger.class) {
            ctx.statement().setLong(ctx.index(), ((UInteger) value).longValue());
        } else if (actualType == ULong.class) {
            ctx.statement().setBigDecimal(ctx.index(), new BigDecimal(value.toString()));
        } else if (actualType == UUID.class) {
            switch(dialect.family()) {
                // java.util.UUID data type
                case H2:
                case POSTGRES:
                    {
                        ctx.statement().setObject(ctx.index(), value);
                        break;
                    }
                // emulates the type
                default:
                    {
                        ctx.statement().setString(ctx.index(), value.toString());
                        break;
                    }
            }
        } else // The type byte[] is handled earlier. byte[][] can be handled here
        if (actualType.isArray()) {
            switch(dialect.family()) {
                case POSTGRES:
                    {
                        ctx.statement().setString(ctx.index(), toPGArrayString((Object[]) value));
                        break;
                    }
                case HSQLDB:
                    {
                        Object[] a = (Object[]) value;
                        Class<?> t = actualType;
                        // See also: https://sourceforge.net/p/hsqldb/bugs/1466
                        if (actualType == UUID[].class) {
                            a = Convert.convertArray(a, byte[][].class);
                            t = byte[][].class;
                        }
                        ctx.statement().setArray(ctx.index(), new MockArray(dialect, a, t));
                        break;
                    }
                case H2:
                    {
                        ctx.statement().setObject(ctx.index(), value);
                        break;
                    }
                default:
                    throw new SQLDialectNotSupportedException("Cannot bind ARRAY types in dialect " + dialect);
            }
        } else if (EnumType.class.isAssignableFrom(actualType)) {
            ctx.statement().setString(ctx.index(), ((EnumType) value).getLiteral());
        } else {
            ctx.statement().setObject(ctx.index(), value);
        }
    }
}
Also used : Configuration(org.jooq.Configuration) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) Timestamp(java.sql.Timestamp) UUID(java.util.UUID) UShort(org.jooq.types.UShort) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ULong(org.jooq.types.ULong) DayToSecond(org.jooq.types.DayToSecond) UShort(org.jooq.types.UShort) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) MockArray(org.jooq.tools.jdbc.MockArray) UInteger(org.jooq.types.UInteger) BigInteger(java.math.BigInteger) OffsetDateTime(java.time.OffsetDateTime) SQLDialect(org.jooq.SQLDialect) BigInteger(java.math.BigInteger) YearToMonth(org.jooq.types.YearToMonth)

Aggregations

YearToMonth (org.jooq.types.YearToMonth)5 DayToSecond (org.jooq.types.DayToSecond)4 Constraint (org.jooq.Constraint)2 DSL.constraint (org.jooq.impl.DSL.constraint)2 YearToSecond (org.jooq.types.YearToSecond)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Date (java.sql.Date)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1 OffsetTime (java.time.OffsetTime)1 UUID (java.util.UUID)1 Configuration (org.jooq.Configuration)1 DatePart (org.jooq.DatePart)1 SQLDialect (org.jooq.SQLDialect)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1