Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #7230 - JDBC type specific setters based on EclipseLink #7246

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public class DbClientContext implements DbContext {
private final DbStatements statements;
private final String dbType;

private DbClientContext(Builder builder) {
/**
* Create an instance of client context.
*
* @param builder Builder for {@link DbClientContext}
*/
protected DbClientContext(
BuilderBase<? extends BuilderBase<?, ? extends DbClientContext>, ? extends DbClientContext> builder) {
this.dbMapperManager = builder.dbMapperManager;
this.mapperManager = builder.mapperManager;
this.clientServices = builder.clientServices;
Expand Down Expand Up @@ -81,20 +87,33 @@ public static Builder builder() {
/**
* Builder for {@link DbClientContext}.
*/
public static class Builder implements io.helidon.common.Builder<Builder, DbClientContext> {
public static final class Builder extends BuilderBase<Builder, DbClientContext> {

@Override
public DbClientContext build() {
return new DbClientContext(this);
}

}

/**
* Base builder for {@link DbClientContext}.
*
* @param <B> type of the builder
* @param <T> type of the built instance
*/
public abstract static class BuilderBase<B extends BuilderBase<B, T>, T extends DbClientContext> implements io.helidon.common.Builder<B, T> {

private DbMapperManager dbMapperManager;
private MapperManager mapperManager;
private List<DbClientService> clientServices = List.of();
private DbStatements statements;
private String dbType;

private Builder() {
}

@Override
public DbClientContext build() {
return new DbClientContext(this);
/**
* Creates an instance of base builder for {@link DbClientContext}.
*/
protected BuilderBase() {
}

/**
Expand All @@ -103,9 +122,9 @@ public DbClientContext build() {
* @param dbMapperManager DB mapper manager
* @return updated builder instance
*/
public Builder dbMapperManager(DbMapperManager dbMapperManager) {
public B dbMapperManager(DbMapperManager dbMapperManager) {
this.dbMapperManager = dbMapperManager;
return this;
return identity();
}

/**
Expand All @@ -114,9 +133,9 @@ public Builder dbMapperManager(DbMapperManager dbMapperManager) {
* @param mapperManager mapper manager
* @return updated builder instance
*/
public Builder mapperManager(MapperManager mapperManager) {
public B mapperManager(MapperManager mapperManager) {
this.mapperManager = mapperManager;
return this;
return identity();
}

/**
Expand All @@ -125,9 +144,9 @@ public Builder mapperManager(MapperManager mapperManager) {
* @param clientServices client service list
* @return updated builder instance
*/
public Builder clientServices(List<DbClientService> clientServices) {
public B clientServices(List<DbClientService> clientServices) {
this.clientServices = clientServices;
return this;
return identity();
}

/**
Expand All @@ -136,9 +155,9 @@ public Builder clientServices(List<DbClientService> clientServices) {
* @param statements statements
* @return updated builder instance
*/
public Builder statements(DbStatements statements) {
public B statements(DbStatements statements) {
this.statements = statements;
return this;
return identity();
}

/**
Expand All @@ -147,9 +166,9 @@ public Builder statements(DbStatements statements) {
* @param dbType database provider type
* @return updated builder instance
*/
public Builder dbType(String dbType) {
public B dbType(String dbType) {
this.dbType = dbType;
return this;
return identity();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ protected DbClientContext context() {
return context;
}

/**
* Return database client context cast to it's extending class.
*
* @param cls {@link DbClientContext} extending class
* @return extended client context
* @param <C> client context extending type
*/
protected <C extends DbClientContext> C context(Class<C> cls) {
return cls.cast(context);
}

@Override
public DbStatementQuery createNamedQuery(String statementName) {
return createNamedQuery(statementName, statementText(statementName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public class DbExecuteContext implements DbContext {
private final String statement;
private final DbClientContext clientContext;

private DbExecuteContext(Builder builder) {
/**
* Creates an instance of execution context.
*
* @param builder Helidon database client context builder
*/
protected DbExecuteContext(
BuilderBase<? extends BuilderBase<?, ? extends DbExecuteContext>, ? extends DbExecuteContext> builder) {
this.statementName = builder.statementName;
this.statement = builder.statement;
this.clientContext = builder.clientContext;
Expand Down Expand Up @@ -77,6 +83,17 @@ public String dbType() {
return clientContext.dbType();
}

/**
* Returns client context cast to it's extending class.
*
* @param cls {@link DbClientContext} extending class
* @return extended client context
* @param <C> client context extending type
*/
protected <C extends DbClientContext> C clientContext(Class<C> cls) {
return cls.cast(clientContext);
}

/**
* Create a new execution context.
*
Expand All @@ -102,10 +119,27 @@ public static Builder builder() {
return new Builder();
}


/**
* Builder for {@link DbExecuteContext}.
*/
public static class Builder implements io.helidon.common.Builder<Builder, DbExecuteContext> {
public static final class Builder extends BuilderBase<Builder, DbExecuteContext> {

@Override
public DbExecuteContext build() {
return new DbExecuteContext(this);
}

}

/**
* Base builder for {@link DbExecuteContext}.
*
* @param <B> type of the builder
* @param <T> type of the built instance
*/
public abstract static class BuilderBase<B extends BuilderBase<B, T>, T extends DbExecuteContext>
implements io.helidon.common.Builder<B, T> {

private String statementName;
private String statement;
Expand All @@ -117,9 +151,9 @@ public static class Builder implements io.helidon.common.Builder<Builder, DbExec
* @param statement statement
* @return updated builder instance
*/
public Builder statement(String statement) {
public B statement(String statement) {
this.statement = statement;
return this;
return identity();
}

/**
Expand All @@ -128,9 +162,9 @@ public Builder statement(String statement) {
* @param statementName statement name
* @return updated builder instance
*/
public Builder statementName(String statementName) {
public B statementName(String statementName) {
this.statementName = statementName;
return this;
return identity();
}

/**
Expand All @@ -139,14 +173,10 @@ public Builder statementName(String statementName) {
* @param clientContext client context
* @return updated builder instance
*/
public Builder clientContext(DbClientContext clientContext) {
public B clientContext(DbClientContext clientContext) {
this.clientContext = clientContext;
return this;
return identity();
}

@Override
public DbExecuteContext build() {
return new DbExecuteContext(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ 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 <C> execution context extending type
*/
protected <C extends DbExecuteContext> C context(Class<C> cls) {
return cls.cast(context);
}

/**
* Get the statement parameters.
*
Expand Down Expand Up @@ -262,4 +273,5 @@ public S addParam(String name, byte[] parameter) {
protected S identity() {
return (S) this;
}

}
45 changes: 45 additions & 0 deletions dbclient/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,28 @@
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -75,8 +93,35 @@
<artifactId>helidon-common-features-processor</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata-processor</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<dependencies>
<dependency>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-processor</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata-processor</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.inject.configdriven</groupId>
<artifactId>helidon-inject-configdriven-processor</artifactId>
<version>${helidon.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import io.helidon.dbclient.DbClient;
import io.helidon.dbclient.DbClientBase;
import io.helidon.dbclient.DbClientContext;

/**
* Helidon DB implementation for JDBC drivers.
Expand All @@ -34,12 +33,13 @@ class JdbcClient extends DbClientBase implements DbClient {
* @param builder builder
*/
JdbcClient(JdbcClientBuilder builder) {
super(DbClientContext.builder()
super(JdbcClientContext.jdbcBuilder()
.statements(builder.statements())
.dbMapperManager(builder.dbMapperManager())
.mapperManager(builder.mapperManager())
.clientServices(builder.clientServices())
.dbType(builder.connectionPool().dbType())
.parametersSetter(builder.parametersConfig())
.build());
connectionPool = builder.connectionPool();
}
Expand All @@ -59,6 +59,11 @@ public String dbType() {
return connectionPool.dbType();
}

@Override
public JdbcClientContext context() {
return (JdbcClientContext) super.context();
}

@Override
public <C> C unwrap(Class<C> cls) {
if (Connection.class.isAssignableFrom(cls)) {
Expand All @@ -70,4 +75,5 @@ public <C> C unwrap(Class<C> cls) {
throw new UnsupportedOperationException(String.format("Class %s is not supported for unwrap", cls.getName()));
}
}

}
Loading
Loading