Search in sources :

Example 31 with Configuration

use of org.jooq.Configuration in project spring-boot by spring-projects.

the class JooqExceptionTranslatorTests method exceptionTranslation.

@ParameterizedTest(name = "{0}")
@MethodSource
void exceptionTranslation(SQLDialect dialect, SQLException sqlException) {
    ExecuteContext context = mock(ExecuteContext.class);
    Configuration configuration = mock(Configuration.class);
    given(context.configuration()).willReturn(configuration);
    given(configuration.dialect()).willReturn(dialect);
    given(context.sqlException()).willReturn(sqlException);
    this.exceptionTranslator.exception(context);
    ArgumentCaptor<RuntimeException> captor = ArgumentCaptor.forClass(RuntimeException.class);
    then(context).should().exception(captor.capture());
    assertThat(captor.getValue()).isInstanceOf(BadSqlGrammarException.class);
}
Also used : Configuration(org.jooq.Configuration) ExecuteContext(org.jooq.ExecuteContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 32 with Configuration

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

the class JavaGenerator method printConvenienceMethodFunction.

protected void printConvenienceMethodFunction(JavaWriter out, RoutineDefinition function, boolean instance) {
    // [#281] - Java can't handle more than 255 method parameters
    if (function.getInParameters().size() > 254) {
        log.info("Too many parameters", "Function " + function + " has more than 254 in parameters. Skipping generation of convenience method.");
        return;
    }
    final String className = out.ref(getStrategy().getFullJavaClassName(function));
    final String functionName = function.getQualifiedOutputName();
    final String functionTypeFull = getJavaType(function.getReturnType(resolver(out)), out);
    final String functionType = out.ref(functionTypeFull);
    final String methodName = getStrategy().getJavaMethodName(function, Mode.DEFAULT);
    // [#3456] Local variables should not collide with actual function arguments
    final String configurationArgument = disambiguateJavaMemberName(function.getInParameters(), "configuration");
    final String localVar = disambiguateJavaMemberName(function.getInParameters(), "f");
    if (!printDeprecationIfUnknownType(out, functionTypeFull) && !printDeprecationIfUnknownTypes(out, function.getInParameters()))
        out.javadoc("Call <code>%s</code>", functionName);
    if (scala)
        out.println("%sdef %s(", visibility(), methodName);
    else if (kotlin)
        out.println("%sfun %s(", visibility(), methodName);
    else
        out.println("%s%s%s %s(", visibility(), !instance ? "static " : "", functionType, methodName);
    String separator = "  ";
    if (!instance) {
        if (scala)
            out.println("%s%s: %s", separator, scalaWhitespaceSuffix(configurationArgument), Configuration.class);
        else if (kotlin)
            out.println("%s%s: %s", separator, configurationArgument, Configuration.class);
        else
            out.println("%s%s %s", separator, Configuration.class, configurationArgument);
        separator = ", ";
    }
    for (ParameterDefinition parameter : function.getInParameters()) {
        // Skip SELF parameter
        if (instance && parameter.equals(function.getInParameters().get(0)))
            continue;
        final String paramType = refNumberType(out, parameter.getType(resolver(out)));
        final String paramMember = getStrategy().getJavaMemberName(parameter);
        if (scala)
            out.println("%s%s: %s", separator, scalaWhitespaceSuffix(paramMember), paramType);
        else if (kotlin)
            out.println("%s%s: %s%s", separator, paramMember, paramType, kotlinNullability(parameter));
        else
            out.println("%s%s %s", separator, paramType, paramMember);
        separator = ", ";
    }
    if (scala) {
        out.println("): %s = {", functionType);
        out.println("val %s = new %s()", localVar, className);
    } else if (kotlin) {
        out.println("): %s? {", functionType);
        out.println("val %s = %s()", localVar, className);
    } else {
        out.println(") {");
        out.println("%s %s = new %s();", className, localVar, className);
    }
    for (ParameterDefinition parameter : function.getInParameters()) {
        final String paramSetter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
        final String paramMember = (instance && parameter.equals(function.getInParameters().get(0))) ? "this" : getStrategy().getJavaMemberName(parameter);
        out.println("%s.%s(%s)%s", localVar, paramSetter, paramMember, semicolon);
    }
    out.println();
    out.println("%s.execute(%s)%s", localVar, instance ? "configuration()" : configurationArgument, semicolon);
    if (scala)
        out.println("%s.getReturnValue", localVar);
    else if (kotlin)
        out.println("return %s.returnValue", localVar);
    else
        out.println("return %s.getReturnValue();", localVar);
    out.println("}");
}
Also used : Configuration(org.jooq.Configuration) ParameterDefinition(org.jooq.meta.ParameterDefinition)

Example 33 with Configuration

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

the class JavaGenerator method printConvenienceMethodTableValuedFunction.

protected void printConvenienceMethodTableValuedFunction(JavaWriter out, TableDefinition function, String methodName) {
    // [#281] - Java can't handle more than 255 method parameters
    if (function.getParameters().size() > 254) {
        log.info("Too many parameters", "Function " + function + " has more than 254 in parameters. Skipping generation of convenience method.");
        return;
    }
    final String recordClassName = out.ref(getStrategy().getFullJavaClassName(function, Mode.RECORD));
    // [#3456] Local variables should not collide with actual function arguments
    final String configurationArgument = disambiguateJavaMemberName(function.getParameters(), "configuration");
    // [#5765] To prevent name clashes, this identifier is not imported
    final String functionName = getStrategy().getFullJavaIdentifier(function);
    if (!printDeprecationIfUnknownTypes(out, function.getParameters()))
        out.javadoc("Call <code>%s</code>.", function.getQualifiedOutputName());
    if (scala)
        out.println("%sdef %s(", visibility(), methodName);
    else if (kotlin)
        out.println("%sfun %s(", visibility(), methodName);
    else
        out.println("%sstatic %s<%s> %s(", visibility(), Result.class, recordClassName, methodName);
    String separator = "  ";
    if (scala)
        out.println("%s%s: %s", separator, scalaWhitespaceSuffix(configurationArgument), Configuration.class);
    else if (kotlin)
        out.println("%s%s: %s", separator, configurationArgument, Configuration.class);
    else
        out.println("%s%s %s", separator, Configuration.class, configurationArgument);
    printParameterDeclarations(out, function.getParameters(), false, ", ");
    if (scala) {
        out.println("): %s[%s] = %s.dsl().selectFrom(%s.call(", Result.class, recordClassName, configurationArgument, functionName);
    } else if (kotlin) {
        out.println("): %s<%s> = %s.dsl().selectFrom(%s.call(", Result.class, recordClassName, configurationArgument, functionName);
    } else {
        out.println(") {");
        out.println("return %s.dsl().selectFrom(%s.call(", configurationArgument, functionName);
    }
    separator = "  ";
    for (ParameterDefinition parameter : function.getParameters()) {
        out.println("%s%s", separator, getStrategy().getJavaMemberName(parameter));
        separator = ", ";
    }
    if (scala || kotlin)
        out.println(")).fetch()");
    else
        out.println(")).fetch();").println("}");
}
Also used : Configuration(org.jooq.Configuration) Result(org.jooq.Result) CloseResult(org.jooq.codegen.GeneratorWriter.CloseResult) ParameterDefinition(org.jooq.meta.ParameterDefinition)

Example 34 with Configuration

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

the class RecordDelegate method operate.

@SuppressWarnings("unchecked")
final <E extends Exception> R operate(ThrowingFunction<R, R, E> operation) throws E {
    R record = recordSupplier.get();
    // [#3300] Records that were fetched from the database
    if (fetched != null && record instanceof AbstractRecord)
        ((AbstractRecord) record).fetched = fetched;
    RecordListenerProvider[] providers = null;
    RecordListener[] listeners = null;
    DefaultRecordContext ctx = null;
    E exception = null;
    if (configuration != null) {
        providers = configuration.recordListenerProviders();
        if (!isEmpty(providers)) {
            listeners = map(providers, p -> p.provide(), RecordListener[]::new);
            ctx = new DefaultRecordContext(configuration, executeType(), record);
        }
    }
    if (listeners != null) {
        for (RecordListener listener : (ctx == null || ctx.settings().getRecordListenerStartInvocationOrder() != REVERSE ? Arrays.asList(listeners) : Tools.reverseIterable(listeners))) {
            switch(type) {
                case LOAD:
                    listener.loadStart(ctx);
                    break;
                case REFRESH:
                    listener.refreshStart(ctx);
                    break;
                case STORE:
                    listener.storeStart(ctx);
                    break;
                case INSERT:
                    listener.insertStart(ctx);
                    break;
                case UPDATE:
                    listener.updateStart(ctx);
                    break;
                case MERGE:
                    listener.mergeStart(ctx);
                    break;
                case DELETE:
                    listener.deleteStart(ctx);
                    break;
                default:
                    throw new IllegalStateException("Type not supported: " + type);
            }
        }
    }
    // [#1684] Do not attach configuration if settings say no
    if (attachRecords(configuration))
        record.attach(configuration);
    if (operation != null) {
        try {
            operation.apply(record);
        }// [#2770][#3036] Exceptions must not propagate before listeners receive "end" events
         catch (Exception e) {
            exception = (E) e;
            // Do not propagate these exception types to client code as they're not really "exceptions"
            if (!(e instanceof ControlFlowSignal)) {
                if (ctx != null)
                    ctx.exception = e;
                if (listeners != null)
                    for (RecordListener listener : listeners) listener.exception(ctx);
            }
        }
    }
    if (listeners != null) {
        for (RecordListener listener : (ctx == null || ctx.settings().getRecordListenerEndInvocationOrder() != REVERSE ? Arrays.asList(listeners) : Tools.reverseIterable(listeners))) {
            switch(type) {
                case LOAD:
                    listener.loadEnd(ctx);
                    break;
                case REFRESH:
                    listener.refreshEnd(ctx);
                    break;
                case STORE:
                    listener.storeEnd(ctx);
                    break;
                case INSERT:
                    listener.insertEnd(ctx);
                    break;
                case UPDATE:
                    listener.updateEnd(ctx);
                    break;
                case MERGE:
                    listener.mergeEnd(ctx);
                    break;
                case DELETE:
                    listener.deleteEnd(ctx);
                    break;
                default:
                    throw new IllegalStateException("Type not supported: " + type);
            }
        }
    }
    if (exception != null)
        throw exception;
    return record;
}
Also used : READ(org.jooq.ExecuteType.READ) Record(org.jooq.Record) Arrays(java.util.Arrays) Tools.map(org.jooq.impl.Tools.map) Tools.attachRecords(org.jooq.impl.Tools.attachRecords) WRITE(org.jooq.ExecuteType.WRITE) REVERSE(org.jooq.conf.InvocationOrder.REVERSE) Tools.isEmpty(org.jooq.impl.Tools.isEmpty) Supplier(java.util.function.Supplier) RecordListener(org.jooq.RecordListener) Configuration(org.jooq.Configuration) ExecuteType(org.jooq.ExecuteType) REFRESH(org.jooq.impl.RecordDelegate.RecordLifecycleType.REFRESH) LOAD(org.jooq.impl.RecordDelegate.RecordLifecycleType.LOAD) RecordListenerProvider(org.jooq.RecordListenerProvider) ControlFlowSignal(org.jooq.exception.ControlFlowSignal) WRITE(org.jooq.ExecuteType.WRITE) REVERSE(org.jooq.conf.InvocationOrder.REVERSE) RecordListenerProvider(org.jooq.RecordListenerProvider) RecordListener(org.jooq.RecordListener) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Example 35 with Configuration

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

the class QueriesImpl method fetchMany.

// ------------------------------------------------------------------------
// Execution API
// ------------------------------------------------------------------------
@Override
public final Results fetchMany() {
    Configuration c = configurationOrThrow();
    ResultsImpl results = new ResultsImpl(c);
    DSLContext ctx = c.dsl();
    for (Query query : this) if (query instanceof ResultQuery)
        results.resultsOrRows.addAll(ctx.fetchMany((ResultQuery<?>) query).resultsOrRows());
    else
        results.resultsOrRows.add(new ResultOrRowsImpl(ctx.execute(query)));
    return results;
}
Also used : Configuration(org.jooq.Configuration) IgnoreQuery(org.jooq.impl.DefaultParseContext.IgnoreQuery) ResultQuery(org.jooq.ResultQuery) Query(org.jooq.Query) ResultOrRowsImpl(org.jooq.impl.ResultsImpl.ResultOrRowsImpl) DSLContext(org.jooq.DSLContext) ResultQuery(org.jooq.ResultQuery)

Aggregations

Configuration (org.jooq.Configuration)46 DSLContext (org.jooq.DSLContext)13 List (java.util.List)9 ArrayList (java.util.ArrayList)8 ExecuteContext (org.jooq.ExecuteContext)8 Query (org.jooq.Query)8 Record (org.jooq.Record)6 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)6 Arrays (java.util.Arrays)5 SQLDialect (org.jooq.SQLDialect)5 BigInteger (java.math.BigInteger)4 OffsetDateTime (java.time.OffsetDateTime)4 Arrays.asList (java.util.Arrays.asList)4 Map (java.util.Map)4 Optional (java.util.Optional)4 DataAccessException (org.jooq.exception.DataAccessException)4 JooqLogger (org.jooq.tools.JooqLogger)4 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3