Search in sources :

Example 1 with ExpandedStatement

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());
}
Also used : ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 2 with ExpandedStatement

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());
}
Also used : ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with ExpandedStatement

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());
}
Also used : ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) Test(org.junit.jupiter.api.Test)

Example 4 with ExpandedStatement

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());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) LoggerFactory(org.slf4j.LoggerFactory) AbstractStore(org.eclipse.hono.service.base.jdbc.store.AbstractStore) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) SpanContext(io.opentracing.SpanContext) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) Optional(java.util.Optional) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLOperations(io.vertx.ext.sql.SQLOperations) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Optional(java.util.Optional) Span(io.opentracing.Span)

Example 5 with ExpandedStatement

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());
}
Also used : ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) Test(org.junit.jupiter.api.Test)

Aggregations

ExpandedStatement (org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement)7 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)2 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 Future (io.vertx.core.Future)1 Json (io.vertx.core.json.Json)1 JsonObject (io.vertx.core.json.JsonObject)1 JDBCClient (io.vertx.ext.jdbc.JDBCClient)1 ResultSet (io.vertx.ext.sql.ResultSet)1 SQLOperations (io.vertx.ext.sql.SQLOperations)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 AbstractStore (org.eclipse.hono.service.base.jdbc.store.AbstractStore)1 SQL (org.eclipse.hono.service.base.jdbc.store.SQL)1 Statement (org.eclipse.hono.service.base.jdbc.store.Statement)1 StatementConfiguration (org.eclipse.hono.service.base.jdbc.store.StatementConfiguration)1