Search in sources :

Example 1 with DbClientServiceContext

use of io.helidon.dbclient.DbClientServiceContext in project helidon by oracle.

the class AbstractStatement method execute.

@Override
public R execute() {
    CompletableFuture<Long> queryFuture = new CompletableFuture<>();
    CompletableFuture<Void> statementFuture = new CompletableFuture<>();
    DbClientServiceContext dbContext = DbClientServiceContext.create(dbType()).resultFuture(queryFuture).statementFuture(statementFuture);
    update(dbContext);
    Single<DbClientServiceContext> dbContextFuture = clientContext.invokeServices(dbContext);
    return doExecute(dbContextFuture, statementFuture, queryFuture);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext)

Example 2 with DbClientServiceContext

use of io.helidon.dbclient.DbClientServiceContext in project helidon by oracle.

the class DbClientTracing method apply.

@Override
protected Single<DbClientServiceContext> apply(DbClientServiceContext serviceContext) {
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig("dbclient", "statement");
    if (!spanConfig.enabled()) {
        return Single.just(serviceContext);
    }
    Context context = serviceContext.context();
    Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
    // now if span context is missing, we build a span without a parent
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(serviceContext.statementName());
    context.get(SpanContext.class).ifPresent(spanBuilder::asChildOf);
    Span span = spanBuilder.start();
    span.setTag("db.operation", serviceContext.statementType().toString());
    if (spanConfig.logEnabled("statement", true)) {
        Tags.DB_STATEMENT.set(span, serviceContext.statement());
    }
    Tags.COMPONENT.set(span, "dbclient");
    Tags.DB_TYPE.set(span, serviceContext.dbType());
    serviceContext.statementFuture().thenAccept(nothing -> {
        if (spanConfig.logEnabled("statement-finish", true)) {
            span.log(Map.of("type", "statement"));
        }
    });
    serviceContext.resultFuture().thenAccept(count -> {
        if (spanConfig.logEnabled("result-finish", true)) {
            span.log(Map.of("type", "result", "count", count));
        }
        span.finish();
    }).exceptionally(throwable -> {
        Tags.ERROR.set(span, Boolean.TRUE);
        span.log(Map.of("event", "error", "error.kind", "Exception", "error.object", throwable, "message", throwable.getMessage()));
        span.finish();
        return null;
    });
    return Single.just(serviceContext);
}
Also used : Context(io.helidon.common.context.Context) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) SpanContext(io.opentracing.SpanContext) DbClientServiceBase(io.helidon.dbclient.common.DbClientServiceBase) Tracer(io.opentracing.Tracer) Config(io.helidon.config.Config) TracingConfigUtil(io.helidon.tracing.config.TracingConfigUtil) Context(io.helidon.common.context.Context) GlobalTracer(io.opentracing.util.GlobalTracer) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) Tags(io.opentracing.tag.Tags) SpanContext(io.opentracing.SpanContext) Map(java.util.Map) Single(io.helidon.common.reactive.Single) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) GlobalTracer(io.opentracing.util.GlobalTracer) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 3 with DbClientServiceContext

use of io.helidon.dbclient.DbClientServiceContext in project helidon by oracle.

the class JdbcStatementTest method testMultipleNamedParameterUsage.

/**
 * Issue #2599: Named parameters should be usable more than once in a statement.
 * Verify parameters order returned by parser.
 * Verify how parameters were set in PreparedStatement.
 */
@Test
void testMultipleNamedParameterUsage() {
    // Data mockup
    Connection conn = new SqlConnectionMock();
    CompletableFuture<Connection> connFuture = new CompletableFuture<>();
    connFuture.complete(conn);
    Map<String, Object> params = new HashMap<>(3);
    params.put("name", "Name");
    // Let's try other tham Integer type
    params.put("count", (short) 5);
    params.put("id", 12);
    JdbcExecuteContext execCtx = JdbcExecuteContext.jdbcBuilder().dbType("Test").connection(connFuture).build();
    DbStatementContext stmtCtx = DbStatementContext.builder().statementName("test").statementType(DbStatementType.UPDATE).statementText("UPDATE TestTable SET name1=:name, name2=:name, count1=:count, count2=:count, count3=:count WHERE id=:id").build();
    JdbcStatementDml dml = new JdbcStatementDml(execCtx, stmtCtx);
    DbClientServiceContext dbContext = DbClientServiceContext.create(dml.dbType());
    dbContext.statement(stmtCtx.statement(), params);
    // Contains statement params setting info.
    JdbcStatement.Parser parser = new JdbcStatement.Parser(stmtCtx.statement());
    String jdbcStatement = parser.convert();
    // Parsed order of params.
    List<String> namesOrder = parser.namesOrder();
    // Verify that parsed names order matches DML statement
    assertThat(namesOrder.get(0), equalTo("name"));
    assertThat(namesOrder.get(1), equalTo("name"));
    assertThat(namesOrder.get(2), equalTo("count"));
    assertThat(namesOrder.get(3), equalTo("count"));
    assertThat(namesOrder.get(4), equalTo("count"));
    assertThat(namesOrder.get(5), equalTo("id"));
    // Build statement mockup from statement context with multiple parameters
    // It shall not fail when Issue #2599 is fixed
    SqlPreparedStatementMock stmt = (SqlPreparedStatementMock) dml.build(conn, dbContext);
    // Verify parameters set in statement mockup
    Map<Integer, SqlPreparedStatementMock.ParInfo> stmtParams = stmt.params();
    // Parameters count shall be 6
    assertThat(stmtParams.size(), equalTo(6));
    // 1st assignment name1=:name
    final ParInfo info1 = stmtParams.get(1);
    assertThat(info1.value(), equalTo("Name"));
    assertThat(info1.cls(), equalTo(String.class));
    // 2nd assignment name2=:name
    final ParInfo info2 = stmtParams.get(2);
    assertThat(info2.value(), equalTo("Name"));
    assertThat(info2.cls(), equalTo(String.class));
    // 3rd assignment count1=:count
    final ParInfo info3 = stmtParams.get(3);
    assertThat(info3.value(), equalTo((short) 5));
    assertThat(info3.cls(), equalTo(Short.class));
    // 4th assignment count2=:count
    final ParInfo info4 = stmtParams.get(4);
    assertThat(info4.value(), equalTo((short) 5));
    assertThat(info4.cls(), equalTo(Short.class));
    // 5th assignment count3=:count
    final ParInfo info5 = stmtParams.get(5);
    assertThat(info5.value(), equalTo((short) 5));
    assertThat(info5.cls(), equalTo(Short.class));
    // 6th assignment id=:id
    final ParInfo info6 = stmtParams.get(6);
    assertThat(info6.value(), equalTo(12));
    assertThat(info6.cls(), equalTo(Integer.class));
}
Also used : DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) HashMap(java.util.HashMap) Connection(java.sql.Connection) DbStatementContext(io.helidon.dbclient.common.DbStatementContext) ParInfo(io.helidon.dbclient.jdbc.SqlPreparedStatementMock.ParInfo) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.jupiter.api.Test)

Example 4 with DbClientServiceContext

use of io.helidon.dbclient.DbClientServiceContext in project helidon by oracle.

the class DbClientContext method invokeServices.

/**
 * Invoke all configured client services and return a single that completes once all the
 * client services complete.
 *
 * @param dbContext context for client services
 * @return a single with the same or modified client service context
 */
public Single<DbClientServiceContext> invokeServices(DbClientServiceContext dbContext) {
    CompletableFuture<DbClientServiceContext> result = CompletableFuture.completedFuture(dbContext);
    dbContext.context(Contexts.context().orElseGet(Context::create));
    for (DbClientService service : clientServices) {
        result = result.thenCompose(service::statement);
    }
    return Single.create(result);
}
Also used : DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) DbClientService(io.helidon.dbclient.DbClientService)

Aggregations

DbClientServiceContext (io.helidon.dbclient.DbClientServiceContext)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 Context (io.helidon.common.context.Context)1 Single (io.helidon.common.reactive.Single)1 Config (io.helidon.config.Config)1 DbClientService (io.helidon.dbclient.DbClientService)1 DbClientServiceBase (io.helidon.dbclient.common.DbClientServiceBase)1 DbStatementContext (io.helidon.dbclient.common.DbStatementContext)1 ParInfo (io.helidon.dbclient.jdbc.SqlPreparedStatementMock.ParInfo)1 SpanTracingConfig (io.helidon.tracing.config.SpanTracingConfig)1 TracingConfigUtil (io.helidon.tracing.config.TracingConfigUtil)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 Tags (io.opentracing.tag.Tags)1 GlobalTracer (io.opentracing.util.GlobalTracer)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1