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