Search in sources :

Example 6 with Param

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

the class BatchSingle method executePrepared.

private final int[] executePrepared() {
    ExecuteContext ctx = new DefaultExecuteContext(configuration, new Query[] { query });
    ExecuteListener listener = ExecuteListeners.get(ctx);
    Connection connection = ctx.connection();
    Param<?>[] params = extractParams();
    try {
        // [#8968] Keep start() event inside of lifecycle management
        listener.start(ctx);
        listener.renderStart(ctx);
        // [#1520] TODO: Should the number of bind values be checked, here?
        ctx.sql(dsl.render(query));
        listener.renderEnd(ctx);
        listener.prepareStart(ctx);
        if (ctx.statement() == null)
            ctx.statement(connection.prepareStatement(ctx.sql()));
        listener.prepareEnd(ctx);
        // [#9295] use query timeout from settings
        int t = SettingsTools.getQueryTimeout(0, ctx.settings());
        if (t != 0)
            ctx.statement().setQueryTimeout(t);
        for (Object[] bindValues : allBindValues) {
            listener.bindStart(ctx);
            // [#1371] [#2139] Don't bind variables directly onto statement, bind them through the collected params
            // list to preserve type information
            // [#3547]         The original query may have no Params specified - e.g. when it was constructed with
            // plain SQL. In that case, infer the bind value type directly from the bind value
            visitAll(new DefaultBindContext(configuration, ctx.statement()), (params.length > 0) ? fields(bindValues, params) : fields(bindValues));
            listener.bindEnd(ctx);
            ctx.statement().addBatch();
        }
        listener.executeStart(ctx);
        int[] result = ctx.statement().executeBatch();
        int[] batchRows = ctx.batchRows();
        for (int i = 0; i < batchRows.length && i < result.length; i++) batchRows[i] = result[i];
        listener.executeEnd(ctx);
        return result;
    }// [#3427] ControlFlowSignals must not be passed on to ExecuteListners
     catch (ControlFlowSignal e) {
        throw e;
    } catch (RuntimeException e) {
        ctx.exception(e);
        listener.exception(ctx);
        throw ctx.exception();
    } catch (SQLException e) {
        ctx.sqlException(e);
        listener.exception(ctx);
        throw ctx.exception();
    } finally {
        Tools.safeClose(listener, ctx);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecuteContext(org.jooq.ExecuteContext) ExecuteListener(org.jooq.ExecuteListener) Param(org.jooq.Param) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Example 7 with Param

use of org.jooq.Param 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 8 with Param

use of org.jooq.Param in project killbill-analytics-plugin by killbill.

the class Metadata method getTemplateVariables.

public List<Field<?>> getTemplateVariables(@Nullable final String sourceQuery) {
    if (sourceQuery == null) {
        return null;
    }
    // The context has likely the wrong SQL Engine here (e.g. sourceQuery might be for Trino)
    // but it shouldn't matter to find these params
    final Query query;
    try {
        query = context.parser().parseQuery(sourceQuery);
    } catch (final ParserException e) {
        logger.debug("Unable to extract template variables from query {}", sourceQuery, e);
        return null;
    }
    final Map<String, Param<?>> params = query.getParams();
    final List<Field<?>> templateVariables = new LinkedList<Field<?>>();
    for (final String paramCandidate : params.keySet()) {
        final Integer index = Ints.tryParse(paramCandidate);
        if (index != null) {
            // Positional param, likely not user specified (could be funciton arguments, limits, etc.)
            continue;
        }
        // TODO Allow the user to configure the data type for each parameter (would likely require a DDL change on the analytics_reports table)
        final DataType<String> dataType = DefaultDataType.getDataType(SQLDialect.DEFAULT, String.class);
        templateVariables.add(DSL.field(paramCandidate, dataType));
    }
    return templateVariables;
}
Also used : ParserException(org.jooq.impl.ParserException) Field(org.jooq.Field) Query(org.jooq.Query) Param(org.jooq.Param) LinkedList(java.util.LinkedList)

Aggregations

Param (org.jooq.Param)8 List (java.util.List)5 Configuration (org.jooq.Configuration)5 DSLContext (org.jooq.DSLContext)4 Field (org.jooq.Field)4 ArrayList (java.util.ArrayList)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 SQLException (java.sql.SQLException)2 Instant (java.time.Instant)2 OffsetDateTime (java.time.OffsetDateTime)2 ArrayDeque (java.util.ArrayDeque)2 Arrays (java.util.Arrays)2 Deque (java.util.Deque)2 Constants (org.jooq.Constants)2 Table (org.jooq.Table)2 Settings (org.jooq.conf.Settings)2 SettingsTools.renderLocale (org.jooq.conf.SettingsTools.renderLocale)2 DataAccessException (org.jooq.exception.DataAccessException)2 JooqLogger (org.jooq.tools.JooqLogger)2