Search in sources :

Example 41 with Configuration

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

the class AbstractRoutine method execute.

@Override
public final int execute() {
    Configuration config = configurationOrThrow(this);
    SQLDialect family = config.family();
    results.clear();
    outValues.clear();
    // [#8431] PG 11 procedures, however, must be called with CallableStatement
    if (isSQLUsable() && REQUIRE_SELECT_FROM.contains(config.dialect())) {
        return executeSelectFromPOSTGRES();
    } else // Procedures (no return value) are always executed as CallableStatement
    if (type == null) {
        return executeCallableStatement();
    } else {
        switch(family) {
            // syntax for functions. Select functions from DUAL instead
            case HSQLDB:
                // returns a cursor. Instead, SELECT * FROM table(f()) works
                if (SQLDataType.RESULT.equals(type.getSQLDataType())) {
                    return executeSelectFromHSQLDB();
                } else // Fall through
                {
                }
            case FIREBIRD:
            case H2:
                return executeSelect();
            // DML statements
            default:
                return executeCallableStatement();
        }
    }
}
Also used : Configuration(org.jooq.Configuration) SQLDialect(org.jooq.SQLDialect)

Example 42 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.info("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());
    final String methodName = getStrategy().getJavaMethodName(procedure, Mode.DEFAULT);
    final String firstOutParamType = outParams.size() == 1 ? out.ref(getJavaType(outParams.get(0).getType(resolver(out)), out)) : "";
    if (!printDeprecationIfUnknownTypes(out, procedure.getAllParameters()))
        out.javadoc("Call <code>%s</code>", procedure.getQualifiedOutputName());
    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 " : "", outParams.size() == 0 ? "void" : outParams.size() == 1 ? firstOutParamType : className, 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 : procedure.getInParameters()) {
        // Skip SELF parameter
        if (instance && parameter.equals(procedure.getInParameters().get(0)))
            continue;
        final String memberName = getStrategy().getJavaMemberName(parameter);
        final String typeName = refNumberType(out, parameter.getType(resolver(out)));
        if (scala)
            out.println("%s%s: %s", separator, scalaWhitespaceSuffix(memberName), typeName);
        else if (kotlin)
            out.println("%s%s: %s?", separator, memberName, typeName);
        else
            out.println("%s%s %s", separator, typeName, memberName);
        separator = ", ";
    }
    if (scala) {
        out.println("): %s = {", outParams.size() == 0 ? "Unit" : outParams.size() == 1 ? firstOutParamType : className);
        out.println("val %s = new %s", localVar, className);
    } else if (kotlin) {
        out.println("): %s%s {", outParams.size() == 0 ? "Unit" : outParams.size() == 1 ? firstOutParamType : className, outParams.size() == 1 ? "?" : "");
        out.println("val %s = %s()", localVar, className);
    } else {
        out.println(") {");
        out.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);
        out.println("%s.%s(%s)%s", localVar, setter, arg, semicolon);
    }
    out.println();
    out.println("%s.execute(%s)%s", localVar, instance ? "configuration()" : configurationArgument, semicolon);
    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(resolver(out)).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(resolver(out, Mode.INTERFACE)), out, Mode.INTERFACE));
                if (scala)
                    out.println("from(%s.%s.asInstanceOf[%s])", localVar, getter, columnTypeInterface);
                else if (kotlin)
                    out.println("from(%s.%s() as %s);", localVar, getter, columnTypeInterface);
                else
                    out.println("from((%s) %s.%s());", columnTypeInterface, localVar, getter);
            } else {
                out.println("from(%s.%s%s)%s", localVar, getter, emptyparens, semicolon);
            }
        }
        if (outParams.size() == 1)
            out.println("return %s.%s%s%s", localVar, getter, emptyparens, semicolon);
        else if (outParams.size() > 1)
            out.println("return %s%s", localVar, semicolon);
    }
    out.println("}");
}
Also used : Configuration(org.jooq.Configuration) ParameterDefinition(org.jooq.meta.ParameterDefinition)

Example 43 with Configuration

use of org.jooq.Configuration in project curiostack by curioswitch.

the class DatabaseModule method dbContext.

@Provides
@Singleton
static DSLContext dbContext(DataSource dataSource, @ForDatabase ListeningExecutorService dbExecutor) {
    Configuration configuration = new DefaultConfiguration().set(dbExecutor).set(SQLDialect.MYSQL).set(new Settings().withRenderSchema(false)).set(new DataSourceConnectionProvider(dataSource)).set(DatabaseUtil.sfmRecordMapperProvider());
    DSLContext ctx = DSL.using(configuration);
    // Eagerly trigger JOOQ classinit for better startup performance.
    ctx.select().from("curio_server_framework_init").getSQL();
    return ctx;
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Settings(org.jooq.conf.Settings) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 44 with Configuration

use of org.jooq.Configuration in project keywhiz by square.

the class ClientDAO method sawClient.

public void sawClient(Client client, @Nullable Principal principal) {
    Instant now = Instant.now();
    Instant lastSeen = Optional.ofNullable(client.getLastSeen()).map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())).orElse(EPOCH);
    final Instant expiration;
    if (principal instanceof CertificatePrincipal) {
        expiration = ((CertificatePrincipal) principal).getCertificateExpiration();
    } else {
        expiration = EPOCH;
    }
    // this way we can have less granularity on lastSeen and save DB writes
    if (now.isAfter(lastSeen.plus(LAST_SEEN_THRESHOLD))) {
        dslContext.transaction(configuration -> {
            Param<Long> lastSeenValue = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN);
            Param<Long> expirationValue = DSL.val(expiration.getEpochSecond(), CLIENTS.EXPIRATION);
            DSL.using(configuration).update(CLIENTS).set(CLIENTS.LASTSEEN, when(CLIENTS.LASTSEEN.isNull(), lastSeenValue).otherwise(greatest(CLIENTS.LASTSEEN, lastSeenValue))).set(CLIENTS.EXPIRATION, expirationValue).where(CLIENTS.ID.eq(client.getId())).execute();
        });
    }
}
Also used : MEMBERSHIPS(keywhiz.jooq.tables.Memberships.MEMBERSHIPS) DSL(org.jooq.impl.DSL) DSL.when(org.jooq.impl.DSL.when) Inject(javax.inject.Inject) Duration(java.time.Duration) DSLContext(org.jooq.DSLContext) URI(java.net.URI) Client(keywhiz.api.model.Client) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) ClientsRecord(keywhiz.jooq.tables.records.ClientsRecord) Readonly(keywhiz.service.config.Readonly) CertificatePrincipal(keywhiz.auth.mutualssl.CertificatePrincipal) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Instant(java.time.Instant) Param(org.jooq.Param) Configuration(org.jooq.Configuration) List(java.util.List) CLIENTS(keywhiz.jooq.tables.Clients.CLIENTS) Principal(java.security.Principal) OffsetDateTime(java.time.OffsetDateTime) DSL.greatest(org.jooq.impl.DSL.greatest) Optional(java.util.Optional) RowHmacGenerator(keywhiz.service.crypto.RowHmacGenerator) EPOCH(java.time.Instant.EPOCH) CertificatePrincipal(keywhiz.auth.mutualssl.CertificatePrincipal) Instant(java.time.Instant)

Example 45 with Configuration

use of org.jooq.Configuration in project SimpleFlatMapper by arnaudroger.

the class JooqUnmapperTest method testUnmapping.

@Test
public void testUnmapping() throws Exception {
    Connection conn = DbHelper.objectDb();
    Configuration cfg = new DefaultConfiguration().set(conn).set(SQLDialect.HSQLDB);
    cfg.set(JooqMapperFactory.newInstance().newRecordUnmapperProvider(new DSLContextProvider() {

        @Override
        public DSLContext provide() {
            return DSL.using(cfg);
        }
    }));
    DSLContext dsl = DSL.using(cfg);
    Label label = new Label(1, UUID.randomUUID(), "label", false);
    LabelsRecord labelsRecord = dsl.newRecord(Labels.LABELS, label);
    assertEquals(label.getId(), labelsRecord.getId());
    assertEquals(label.getName(), labelsRecord.getName());
    assertEquals(label.getUuid(), labelsRecord.getUuid());
    assertEquals(label.getObsolete(), labelsRecord.getObsolete());
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) Connection(java.sql.Connection) Label(org.simpleflatmapper.jooq.test.books.Label) LabelsRecord(org.simpleflatmapper.jooq.test.books.LabelsRecord) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) DSLContextProvider(org.simpleflatmapper.jooq.DSLContextProvider) Test(org.junit.Test)

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