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);
}
}
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();
});
}
}
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;
}
Aggregations