use of org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement in project hono by eclipse.
the class QueryTest method testDoubleParams.
/**
* Test using the same named parameter multiple times.
*/
@Test
public void testDoubleParams() {
final Map<String, Object> params = new HashMap<>();
params.put("foo", 1);
params.put("bar", "baz");
final ExpandedStatement expanded = Statement.statement("select * from table where foo=:foo and foo2=:foo and bar=:bar and barfoo=:foo and 1=1").expand(params);
assertEquals("select * from table where foo=? and foo2=? and bar=? and barfoo=? and 1=1", expanded.getSql());
assertArrayEquals(new Object[] { 1, 1, "baz", 1 }, expanded.getParameters());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement in project hono by eclipse.
the class QueryTest method testSimpleParams.
/**
* Test a statement with some simple named parameters.
*/
@Test
public void testSimpleParams() {
final Map<String, Object> params = new HashMap<>();
params.put("foo", 1);
params.put("bar", "baz");
final ExpandedStatement expanded = Statement.statement("select * from table where foo=:foo and bar=:bar").expand(params);
assertEquals("select * from table where foo=? and bar=?", expanded.getSql());
assertArrayEquals(new Object[] { 1, "baz" }, expanded.getParameters());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement in project hono by eclipse.
the class QueryTest method testPostgresJson1.
/**
* Test that a simple postgres JSON statement works.
*/
@Test
public void testPostgresJson1() {
final ExpandedStatement expanded = Statement.statement("SELECT device_id, version, credentials FROM table WHERE tenant_id=:tenant_id AND credentials @> jsonb_build_object('type', :type, 'auth-id', :auth_id)").expand(map -> {
map.put("tenant_id", "tenant");
map.put("type", "hashed-password");
map.put("auth_id", "auth");
});
assertEquals("SELECT device_id, version, credentials FROM table WHERE tenant_id=? AND credentials @> jsonb_build_object('type', ?, 'auth-id', ?)", expanded.getSql());
assertArrayEquals(new Object[] { "tenant", "hashed-password", "auth" }, expanded.getParameters());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement in project hono by eclipse.
the class AbstractTenantStore method readTenantBy.
/**
* Read a tenant, using the provided statement.
*
* @param operations The operations to use.
* @param expanded The statement to use.
* @param spanContext The span to contribute to.
* @return A future, tracking the outcome of the operation.
*/
protected Future<Optional<TenantReadResult>> readTenantBy(final SQLOperations operations, final ExpandedStatement expanded, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "read tenant by", getClass().getSimpleName()).start();
return expanded.trace(this.tracer, span.context()).query(operations).<Optional<TenantReadResult>>flatMap(r -> {
final var entries = r.getRows(true);
span.log(Map.of("event", "read result", "rows", entries.size()));
switch(entries.size()) {
case 0:
return Future.succeededFuture(Optional.empty());
case 1:
return Future.succeededFuture(entries.get(0)).map(entry -> {
final var id = entry.getString("tenant_id");
final var tenant = Json.decodeValue(entry.getString("data"), Tenant.class);
final var version = Optional.ofNullable(entry.getString("version"));
return Optional.of(new TenantReadResult(id, tenant, version));
});
default:
return Future.failedFuture(new IllegalStateException("Found multiple entries for a single tenant"));
}
}).flatMap(result -> {
if (result.isPresent()) {
return fillTrustAnchors(operations, result.get(), span.context()).map(Optional::ofNullable);
} else {
return Future.succeededFuture(result);
}
}).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement in project hono by eclipse.
the class QueryTest method testPostgresCast.
/**
* Test that a postgres type case (double colon) works.
*/
@Test
public void testPostgresCast() {
final ExpandedStatement expanded = Statement.statement("INSERT INTO %s (tenant_id, device_id, version, data) VALUES (:tenant_id, :device_id, :version, to_jsonb(:data::jsonb))", "table").expand(map -> {
map.put("tenant_id", "tenant");
map.put("device_id", "device");
map.put("version", "version");
map.put("data", "{}");
});
assertEquals("INSERT INTO table (tenant_id, device_id, version, data) VALUES (?, ?, ?, to_jsonb(?::jsonb))", expanded.getSql());
assertArrayEquals(new Object[] { "tenant", "device", "version", "{}" }, expanded.getParameters());
}
Aggregations