Search in sources :

Example 11 with Configuration

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

the class JavaGenerator method printConvenienceMethodProcedure.

protected void printConvenienceMethodProcedure(JavaWriter out, RoutineDefinition procedure, boolean instance) {
    // [#281] - Java can't handle more than 255 method parameters
    if (procedure.getInParameters().size() > 254) {
        log.warn("Too many parameters", "Procedure " + procedure + " has more than 254 in parameters. Skipping generation of convenience method.");
        return;
    }
    final String className = out.ref(getStrategy().getFullJavaClassName(procedure));
    final String configurationArgument = disambiguateJavaMemberName(procedure.getInParameters(), "configuration");
    final String localVar = disambiguateJavaMemberName(procedure.getInParameters(), "p");
    final List<ParameterDefinition> outParams = list(procedure.getReturnValue(), procedure.getOutParameters());
    out.tab(1).javadoc("Call <code>%s</code>", procedure.getQualifiedOutputName());
    if (scala) {
        out.tab(1).print("def ");
    } else {
        out.tab(1).print("public ");
        if (!instance) {
            out.print("static ");
        }
        if (outParams.size() == 0) {
            out.print("void ");
        } else if (outParams.size() == 1) {
            out.print(out.ref(getJavaType(outParams.get(0).getType())));
            out.print(" ");
        } else {
            out.print(className + " ");
        }
    }
    out.print(getStrategy().getJavaMethodName(procedure, Mode.DEFAULT));
    out.print("(");
    String glue = "";
    if (!instance) {
        if (scala)
            out.print("%s : %s", configurationArgument, Configuration.class);
        else
            out.print("%s %s", Configuration.class, configurationArgument);
        glue = ", ";
    }
    for (ParameterDefinition parameter : procedure.getInParameters()) {
        // Skip SELF parameter
        if (instance && parameter.equals(procedure.getInParameters().get(0))) {
            continue;
        }
        out.print(glue);
        if (scala)
            out.print("%s : %s", getStrategy().getJavaMemberName(parameter), refNumberType(out, parameter.getType()));
        else
            out.print("%s %s", refNumberType(out, parameter.getType()), getStrategy().getJavaMemberName(parameter));
        glue = ", ";
    }
    if (scala) {
        out.print(") : ");
        if (outParams.size() == 0) {
            out.print("Unit");
        } else if (outParams.size() == 1) {
            out.print(out.ref(getJavaType(outParams.get(0).getType())));
        } else {
            out.print(className);
        }
        out.println(" = {");
        out.tab(2).println("val %s = new %s", localVar, className);
    } else {
        out.println(") {");
        out.tab(2).println("%s %s = new %s();", className, localVar, className);
    }
    for (ParameterDefinition parameter : procedure.getInParameters()) {
        final String setter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
        final String arg = (instance && parameter.equals(procedure.getInParameters().get(0))) ? "this" : getStrategy().getJavaMemberName(parameter);
        if (scala)
            out.tab(2).println("%s.%s(%s)", localVar, setter, arg);
        else
            out.tab(2).println("%s.%s(%s);", localVar, setter, arg);
    }
    out.println();
    if (scala)
        out.tab(2).println("%s.execute(%s)", localVar, instance ? "configuration()" : configurationArgument);
    else
        out.tab(2).println("%s.execute(%s);", localVar, instance ? "configuration()" : configurationArgument);
    if (outParams.size() > 0) {
        final ParameterDefinition parameter = outParams.get(0);
        // Avoid disambiguation for RETURN_VALUE getter
        final String getter = parameter == procedure.getReturnValue() ? "getReturnValue" : getStrategy().getJavaGetterName(parameter, Mode.DEFAULT);
        final boolean isUDT = parameter.getType().isUDT();
        if (instance) {
            // [#3117] Avoid funny call-site ambiguity if this is a UDT that is implemented by an interface
            if (generateInterfaces() && isUDT) {
                final String columnTypeInterface = out.ref(getJavaType(parameter.getType(), Mode.INTERFACE));
                if (scala)
                    out.tab(2).println("from(%s.%s.asInstanceOf[%s])", localVar, getter, columnTypeInterface);
                else
                    out.tab(2).println("from((%s) %s.%s());", columnTypeInterface, localVar, getter);
            } else {
                if (scala)
                    out.tab(2).println("from(%s.%s)", localVar, getter);
                else
                    out.tab(2).println("from(%s.%s());", localVar, getter);
            }
        }
        if (outParams.size() == 1) {
            if (scala)
                out.tab(2).println("return %s.%s", localVar, getter);
            else
                out.tab(2).println("return %s.%s();", localVar, getter);
        } else if (outParams.size() > 1) {
            if (scala)
                out.tab(2).println("return %s", localVar);
            else
                out.tab(2).println("return %s;", localVar);
        }
    }
    out.tab(1).println("}");
}
Also used : Configuration(org.jooq.Configuration) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString)

Example 12 with Configuration

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

the class AbstractDatabase method create.

@SuppressWarnings("serial")
protected final DSLContext create(boolean muteExceptions) {
    // [#3800] Make sure that faulty queries are logged in a formatted
    //         way to help users provide us with bug reports
    final Configuration configuration = create0().configuration();
    if (muteExceptions) {
        return DSL.using(configuration);
    } else {
        final Settings newSettings = SettingsTools.clone(configuration.settings()).withRenderFormatted(true);
        final ExecuteListenerProvider[] oldProviders = configuration.executeListenerProviders();
        final ExecuteListenerProvider[] newProviders = new ExecuteListenerProvider[oldProviders.length + 1];
        System.arraycopy(oldProviders, 0, newProviders, 0, oldProviders.length);
        newProviders[oldProviders.length] = new DefaultExecuteListenerProvider(new DefaultExecuteListener() {

            class SQLPerformanceWarning extends Exception {
            }

            @Override
            public void start(ExecuteContext ctx) {
                //         SQLPerformanceWarning.
                if (!initialised) {
                    DSL.using(configuration).selectOne().fetch();
                    initialised = true;
                }
            }

            @Override
            public void executeStart(ExecuteContext ctx) {
                ctx.data("org.jooq.util.AbstractDatabase.watch", new StopWatch());
            }

            @Override
            public void executeEnd(ExecuteContext ctx) {
                StopWatch watch = (StopWatch) ctx.data("org.jooq.util.AbstractDatabase.watch");
                if (watch.split() > TimeUnit.SECONDS.toNanos(5L)) {
                    watch.splitWarn("Slow SQL");
                    log.warn("Slow SQL", "jOOQ Meta executed a slow query (slower than 5 seconds)" + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()), new SQLPerformanceWarning());
                }
            }

            @Override
            public void exception(ExecuteContext ctx) {
                log.warn("SQL exception", "Exception while executing meta query: " + (ctx.sqlException() != null ? ctx.sqlException().getMessage() : ctx.exception() != null ? ctx.exception().getMessage() : "No exception available") + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()));
            }

            private String formatted(Query query) {
                return DSL.using(configuration.derive(newSettings)).renderInlined(query);
            }
        });
        return DSL.using(configuration.derive(newProviders));
    }
}
Also used : DefaultExecuteListener(org.jooq.impl.DefaultExecuteListener) Configuration(org.jooq.Configuration) Query(org.jooq.Query) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) ExecuteListenerProvider(org.jooq.ExecuteListenerProvider) ExecuteContext(org.jooq.ExecuteContext) Settings(org.jooq.conf.Settings) StopWatch(org.jooq.tools.StopWatch)

Example 13 with Configuration

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

the class DefaultBinding method register.

@SuppressWarnings("unchecked")
@Override
public void register(BindingRegisterContext<U> ctx) throws SQLException {
    Configuration configuration = ctx.configuration();
    int sqlType = DefaultDataType.getDataType(ctx.dialect(), type).getSQLType(ctx.configuration());
    if (log.isTraceEnabled())
        log.trace("Registering variable " + ctx.index(), "" + type);
    switch(configuration.family()) {
        default:
            {
                ctx.statement().registerOutParameter(ctx.index(), sqlType);
                break;
            }
    }
}
Also used : Configuration(org.jooq.Configuration)

Example 14 with Configuration

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

Example 15 with Configuration

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

the class AbstractRoutine method registerOutParameters.

private final void registerOutParameters(ExecuteContext ctx) throws SQLException {
    Configuration c = ctx.configuration();
    Map<Object, Object> data = ctx.data();
    CallableStatement statement = (CallableStatement) ctx.statement();
    // Note that some RDBMS do not support binding by name very well
    for (Parameter<?> parameter : getParameters()) if (resultParameter(parameter))
        registerOutParameter(c, data, statement, parameter);
}
Also used : Configuration(org.jooq.Configuration) CallableStatement(java.sql.CallableStatement)

Aggregations

Configuration (org.jooq.Configuration)21 AttachableInternal (org.jooq.AttachableInternal)4 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)4 OffsetDateTime (java.time.OffsetDateTime)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecuteContext (org.jooq.ExecuteContext)3 Query (org.jooq.Query)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Date (java.sql.Date)2 Time (java.sql.Time)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 LocalTime (java.time.LocalTime)2 OffsetTime (java.time.OffsetTime)2 SQLDialect (org.jooq.SQLDialect)2 DefaultExecuteContext.localConfiguration (org.jooq.impl.DefaultExecuteContext.localConfiguration)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1