io.helidon.dbclient.DbStatementBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-dbclient Show documentation
Show all versions of helidon-dbclient Show documentation
Helidon Database Client API
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.dbclient;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import static io.helidon.dbclient.DbStatementParameters.UNDEFINED;
/**
* Base {@link DbStatement} implementation.
*
* @param type of subclass
*/
public abstract class DbStatementBase> implements DbStatement {
private final DbExecuteContext context;
private DbStatementParameters parameters;
/**
* Create a new instance.
*
* @param context context
*/
protected DbStatementBase(DbExecuteContext context) {
this.context = context;
}
/**
* Get the execution context.
*
* @return execution context
*/
public DbExecuteContext context() {
return context;
}
/**
* Returns execution context cast to it's extending class.
*
* @param cls {@link DbExecuteContext} extending class
* @return extended execution context
* @param execution context extending type
*/
protected C context(Class cls) {
return cls.cast(context);
}
/**
* Get the statement parameters.
*
* @return statement parameters
*/
public DbStatementParameters parameters() {
return parameters != null ? parameters : UNDEFINED;
}
/**
* Get the statement type.
*
* @return statement type
*/
public abstract DbStatementType statementType();
/**
* Execute the statement with interception.
*
* @param function function used to compute the query result
* @param query result type
* @return query result
*/
protected T doExecute(BiFunction, DbClientServiceContext, T> function) {
CompletableFuture stmtFuture = new CompletableFuture<>();
CompletableFuture queryFuture = new CompletableFuture<>();
queryFuture.whenComplete((r, ex) -> stmtFuture.complete(null));
DbClientServiceContext serviceContext =
new DbClientServiceContextImpl(context, statementType(), stmtFuture, queryFuture, parameters);
context().clientServices().forEach(service -> service.statement(serviceContext));
return function.apply(queryFuture, serviceContext);
}
/**
* Decorate the given stream to invoke {@link Stream#close()} on terminal operations.
*
* @param stream stream to decorate
* @param the type of the stream elements
* @return decorated stream
*/
protected static Stream autoClose(Stream stream) {
return AutoClosingStream.decorate(stream);
}
@Override
public S namedParam(Object parameters) {
@SuppressWarnings("unchecked")
Class