use of org.jooq.Param in project keywhiz by square.
the class ClientDAO method sawClient.
public void sawClient(Client client) {
Instant now = Instant.now();
Instant lastSeen = Optional.ofNullable(client.getLastSeen()).map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())).orElse(null);
// this way we can have less granularity on lastSeen and save db writes
if (lastSeen == null || now.isAfter(lastSeen.plus(lastSeenThreshold, SECONDS))) {
dslContext.transaction(configuration -> {
Param<Long> val = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN);
DSL.using(configuration).update(CLIENTS).set(CLIENTS.LASTSEEN, DSL.when(CLIENTS.LASTSEEN.isNull(), val).otherwise(DSL.greatest(CLIENTS.LASTSEEN, val))).where(CLIENTS.ID.eq(client.getId())).execute();
});
}
}
use of org.jooq.Param in project jOOQ by jOOQ.
the class QuantifiedComparisonCondition method accept0.
@SuppressWarnings({ "unchecked" })
private final void accept0(Context<?> ctx) {
boolean quantifiedArray = query.array instanceof Param<?>;
boolean emulateOperator;
switch(comparator) {
case LIKE:
case NOT_LIKE:
case LIKE_IGNORE_CASE:
case NOT_LIKE_IGNORE_CASE:
emulateOperator = escape != null || NO_SUPPORT_QUANTIFIED_LIKE.contains(ctx.dialect());
break;
case SIMILAR_TO:
case NOT_SIMILAR_TO:
emulateOperator = escape != null || NO_SUPPORT_QUANTIFIED_SIMILAR_TO.contains(ctx.dialect());
break;
default:
emulateOperator = false;
break;
}
// arrays, such as x = any(?::int[]) in PostgreSQL
if (quantifiedArray && SUPPORTS_QUANTIFIED_ARRAYS.contains(ctx.dialect()) && !emulateOperator) {
accept1(ctx);
} else if (query.values != null || quantifiedArray) {
ctx.visit(DSL.condition(query.quantifier == Quantifier.ALL ? Operator.AND : Operator.OR, query.values != null ? map(query.values, v -> comparisonCondition(comparator, (Field<String>) v)) : map(((Param<? extends Object[]>) query.array).getValue(), v -> v instanceof Field ? comparisonCondition(comparator, (Field<String>) v) : comparisonCondition(comparator, v))));
} else if ((query.array != null || query.query != null) && emulateOperator) {
Field<String> pattern = DSL.field(name("pattern"), VARCHAR);
Condition cond;
Field<Boolean> lhs;
switch(comparator) {
case NOT_LIKE:
case NOT_SIMILAR_TO:
case NOT_LIKE_IGNORE_CASE:
cond = comparisonCondition(inverse(comparator), pattern);
lhs = inline(false);
break;
case LIKE:
case SIMILAR_TO:
case LIKE_IGNORE_CASE:
cond = comparisonCondition(comparator, pattern);
lhs = inline(true);
break;
default:
throw new IllegalStateException();
}
Table<?> t = query.array != null ? new ArrayTable(query.array).asTable("t", "pattern") : new AliasedSelect<>(query.query, true, true, name("pattern")).as("t");
Select<Record1<Boolean>> select = select(DSL.field(cond)).from(t);
ctx.visit(lhs.eq(query.quantifier.apply(select)));
} else {
accept1(ctx);
}
}
use of org.jooq.Param in project jOOQ by jOOQ.
the class ParsingConnection method translate.
static final Rendered translate(Configuration configuration, String sql, Param<?>... bindValues) {
log.debug("Translating from", sql);
Rendered result = null;
Supplier<CacheValue> miss = () -> {
log.debug("Translation cache miss", sql);
return new CacheValue(configuration, sql, bindValues);
};
Settings settings = configuration.settings();
if (CACHE_PARSING_CONNECTION.category.predicate.test(settings) && bindValues.length > 0) {
switch(getParamType(settings)) {
case INLINED:
case NAMED_OR_INLINED:
result = miss.get().rendered(bindValues);
break;
}
}
if (result == null)
result = Cache.run(configuration, miss, CACHE_PARSING_CONNECTION, () -> Cache.key(sql, map(nonNull(bindValues), f -> f.getDataType()))).rendered(bindValues);
log.debug("Translating to", result.sql);
return result;
}
use of org.jooq.Param in project jOOQ by jOOQ.
the class AbstractResult method format0.
/**
* @param value The value to be formatted
* @param visual Whether the formatted output is to be consumed visually
* (HTML, TEXT) or by a machine (CSV, JSON, XML)
*/
private static final String format0(Object value, boolean changed, boolean visual) {
// [#2741] TODO: This logic will be externalised in new SPI
String formatted = changed && visual ? "*" : "";
if (value == null) {
formatted += visual ? "{null}" : null;
} else if (value.getClass() == byte[].class) {
formatted += DatatypeConverter.printBase64Binary((byte[]) value);
} else if (value.getClass().isArray()) {
// [#6545] Nested arrays are handled recursively
formatted += Arrays.stream((Object[]) value).map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
} else if (value instanceof EnumType) {
EnumType e = (EnumType) value;
formatted += e.getLiteral();
} else if (value instanceof List) {
List<?> l = (List<?>) value;
formatted += l.stream().map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
} else if (value instanceof Record) {
Record r = (Record) value;
formatted += Arrays.stream(r.intoArray()).map(f -> format0(f, false, visual)).collect(joining(", ", "(", ")"));
} else // [#6080] Support formatting of nested ROWs
if (value instanceof Param) {
formatted += format0(((Param<?>) value).getValue(), false, visual);
} else // [#5238] Oracle DATE is really a TIMESTAMP(0)...
if (value instanceof Date) {
Date d = (Date) value;
String date = value.toString();
if (Date.valueOf(date).equals(value))
formatted += date;
else
formatted += new Timestamp(d.getTime());
} else {
formatted += value.toString();
}
return formatted;
}
use of org.jooq.Param in project jOOQ by jOOQ.
the class DefaultRenderContext method scopeEnd0.
@Override
void scopeEnd0() {
ScopeMarker[] markers = ScopeMarker.values();
ScopeStackElement[] beforeFirst = new ScopeStackElement[markers.length];
ScopeStackElement[] afterLast = new ScopeStackElement[markers.length];
ScopeContent[] content = new ScopeContent[markers.length];
for (ScopeMarker marker : markers) {
if (!marker.topLevelOnly || subqueryLevel() == 0) {
int i = marker.ordinal();
ScopeContent o = content[i] = (ScopeContent) data(marker.key);
if (o != null && !o.isEmpty()) {
beforeFirst[i] = scopeStack.get(marker.beforeFirst);
afterLast[i] = scopeStack.get(marker.afterLast);
}
}
}
outer: for (ScopeStackElement e1 : scopeStack.iterable(e -> e.scopeLevel == scopeStack.scopeLevel())) {
String replacedSQL = null;
QueryPartList<Param<?>> insertedBindValues = null;
if (e1.positions == null) {
continue outer;
} else // TODO: subqueryLevel() is lower than scopeLevel if we use implicit join in procedural logic
if (e1.joinNode != null && !e1.joinNode.children.isEmpty()) {
replacedSQL = configuration.dsl().renderContext().declareTables(true).sql('(').formatIndentStart(e1.indent).formatIndentStart().formatNewLine().visit(e1.joinNode.joinTree()).formatNewLine().sql(')').render();
} else {
elementLoop: for (int i = 0; i < beforeFirst.length; i++) {
ScopeStackElement e = beforeFirst[i];
ScopeContent c = content[i];
if (e1 == e && c != null) {
DefaultRenderContext ctx = new DefaultRenderContext(this, false);
markers[i].renderer.render((DefaultRenderContext) ctx.formatIndentStart(e.indent), e, afterLast[i], c);
replacedSQL = ctx.render();
insertedBindValues = ctx.bindValues();
break elementLoop;
}
}
}
if (replacedSQL != null) {
sql.replace(e1.positions[0], e1.positions[1], replacedSQL);
int shift = replacedSQL.length() - (e1.positions[1] - e1.positions[0]);
inner: for (ScopeStackElement e2 : scopeStack) {
if (e2.positions == null)
continue inner;
if (e2.positions[0] > e1.positions[0]) {
e2.positions[0] = e2.positions[0] + shift;
e2.positions[1] = e2.positions[1] + shift;
}
}
if (insertedBindValues != null) {
bindValues.addAll(e1.bindIndex - 1, insertedBindValues);
inner: for (ScopeStackElement e2 : scopeStack) {
if (e2.positions == null)
continue inner;
if (e2.bindIndex > e1.bindIndex)
e2.bindIndex = e2.bindIndex + insertedBindValues.size();
}
}
}
}
}
Aggregations