io.helidon.dbclient.jdbc.JdbcParametersConfig Maven / Gradle / Ivy
Show all versions of helidon-dbclient-jdbc Show documentation
/*
* Copyright (c) 2024 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.jdbc;
import java.util.Objects;
import java.util.Optional;
import io.helidon.builder.api.Prototype;
import io.helidon.common.Generated;
import io.helidon.common.config.Config;
/**
* JDBC parameters setter configuration.
*
* @see #builder()
* @see #create()
*/
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.dbclient.jdbc.JdbcParametersConfigBlueprint")
public interface JdbcParametersConfig extends JdbcParametersConfigBlueprint, Prototype.Api {
/**
* Create a new fluent API builder to customize configuration.
*
* @return a new builder
*/
static JdbcParametersConfig.Builder builder() {
return new JdbcParametersConfig.Builder();
}
/**
* Create a new fluent API builder from an existing instance.
*
* @param instance an existing instance used as a base for the builder
* @return a builder based on an instance
*/
static JdbcParametersConfig.Builder builder(JdbcParametersConfig instance) {
return JdbcParametersConfig.builder().from(instance);
}
/**
* Create a new instance from configuration.
*
* @param config used to configure the new instance
* @return a new instance configured from configuration
*/
static JdbcParametersConfig create(Config config) {
return JdbcParametersConfig.builder().config(config).buildPrototype();
}
/**
* Create a new instance with default values.
*
* @return a new instance
*/
static JdbcParametersConfig create() {
return JdbcParametersConfig.builder().buildPrototype();
}
/**
* Fluent API builder base for {@link JdbcParametersConfig}.
*
* @param type of the builder extending this abstract builder
* @param type of the prototype interface that would be built by {@link #buildPrototype()}
*/
abstract class BuilderBase, PROTOTYPE extends JdbcParametersConfig> implements Prototype.ConfiguredBuilder {
private boolean setObjectForJavaTime = true;
private boolean timestampForLocalTime = true;
private boolean useByteArrayBinding = true;
private boolean useNString = false;
private boolean useStringBinding = true;
private Config config;
private int stringBindingSize = 1024;
/**
* Protected to support extensibility.
*/
protected BuilderBase() {
}
/**
* Update this builder from an existing prototype instance. This method disables automatic service discovery.
*
* @param prototype existing prototype to update this builder from
* @return updated builder instance
*/
public BUILDER from(JdbcParametersConfig prototype) {
useNString(prototype.useNString());
useStringBinding(prototype.useStringBinding());
stringBindingSize(prototype.stringBindingSize());
useByteArrayBinding(prototype.useByteArrayBinding());
timestampForLocalTime(prototype.timestampForLocalTime());
setObjectForJavaTime(prototype.setObjectForJavaTime());
return self();
}
/**
* Update this builder from an existing prototype builder instance.
*
* @param builder existing builder prototype to update this builder from
* @return updated builder instance
*/
public BUILDER from(JdbcParametersConfig.BuilderBase, ?> builder) {
useNString(builder.useNString());
useStringBinding(builder.useStringBinding());
stringBindingSize(builder.stringBindingSize());
useByteArrayBinding(builder.useByteArrayBinding());
timestampForLocalTime(builder.timestampForLocalTime());
setObjectForJavaTime(builder.setObjectForJavaTime());
return self();
}
/**
* Update builder from configuration (node of this type).
* If a value is present in configuration, it would override currently configured values.
*
* @param config configuration instance used to obtain values to update this builder
* @return updated builder instance
*/
@Override
public BUILDER config(Config config) {
Objects.requireNonNull(config);
this.config = config;
config.get("use-n-string").as(Boolean.class).ifPresent(this::useNString);
config.get("use-string-binding").as(Boolean.class).ifPresent(this::useStringBinding);
config.get("string-binding-size").as(Integer.class).ifPresent(this::stringBindingSize);
config.get("use-byte-array-binding").as(Boolean.class).ifPresent(this::useByteArrayBinding);
config.get("timestamp-for-local-time").as(Boolean.class).ifPresent(this::timestampForLocalTime);
config.get("set-object-for-java-time").as(Boolean.class).ifPresent(this::setObjectForJavaTime);
return self();
}
/**
* Use SQL {@code NCHAR}, {@code NVARCHAR} or {@code LONGNVARCHAR} value conversion
* for {@link String} values.
* Default value is {@code false}.
*
* @param useNString whether N{@link String} conversion is used
* @return updated builder instance
* @see #useNString()
*/
public BUILDER useNString(boolean useNString) {
this.useNString = useNString;
return self();
}
/**
* Use {@link java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)} binding
* for {@link String} values with length above {@link #stringBindingSize()} limit.
* Default value is {@code true}.
*
* @param useStringBinding whether to use {@link java.io.CharArrayReader} binding
* @return updated builder instance
* @see #useStringBinding()
*/
public BUILDER useStringBinding(boolean useStringBinding) {
this.useStringBinding = useStringBinding;
return self();
}
/**
* {@link String} values with length above this limit will be bound
* using {@link java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)}
* if {@link #useStringBinding()} is set to {@code true}.
* Default value is {@code 1024}.
*
* @param stringBindingSize {@link String} values length limit for {@link java.io.CharArrayReader} binding
* @return updated builder instance
* @see #stringBindingSize()
*/
public BUILDER stringBindingSize(int stringBindingSize) {
this.stringBindingSize = stringBindingSize;
return self();
}
/**
* Use {@link java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)} binding
* for {@code byte[]} values.
* Default value is {@code true}.
*
* @param useByteArrayBinding whether to use {@link java.io.ByteArrayInputStream} binding
* @return updated builder instance
* @see #useByteArrayBinding()
*/
public BUILDER useByteArrayBinding(boolean useByteArrayBinding) {
this.useByteArrayBinding = useByteArrayBinding;
return self();
}
/**
* Use {@link java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp)}
* to set {@link java.time.LocalTime} values when {@code true}
* or use {@link java.sql.PreparedStatement#setTime(int, java.sql.Time)} when {@code false}.
* Default value is {@code true}.
* This option is vendor specific. Most of the databases are fine with {@link java.sql.Timestamp},
* but for example SQL Server requires {@link java.sql.Time}.
* This option does not apply when {@link #setObjectForJavaTime()} is set to {@code true}.
*
* @param timestampForLocalTime whether to use {@link java.sql.Timestamp} instead of {@link java.sql.Time}
* for {@link java.time.LocalTime} values
* @return updated builder instance
* @see #timestampForLocalTime()
*/
public BUILDER timestampForLocalTime(boolean timestampForLocalTime) {
this.timestampForLocalTime = timestampForLocalTime;
return self();
}
/**
* Set all {@code java.time} Date/Time values directly using {@link java.sql.PreparedStatement#setObject(int, Object)}.
* This option shall work fine for recent JDBC drivers.
* Default value is {@code true}.
*
* @param setObjectForJavaTime whether to use {@link java.sql.PreparedStatement#setObject(int, Object)} for {@code java.time} Date/Time values
* @return updated builder instance
* @see #setObjectForJavaTime()
*/
public BUILDER setObjectForJavaTime(boolean setObjectForJavaTime) {
this.setObjectForJavaTime = setObjectForJavaTime;
return self();
}
/**
* Use SQL {@code NCHAR}, {@code NVARCHAR} or {@code LONGNVARCHAR} value conversion
* for {@link String} values.
* Default value is {@code false}.
*
* @return the use n string
*/
public boolean useNString() {
return useNString;
}
/**
* Use {@link java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)} binding
* for {@link String} values with length above {@link #stringBindingSize()} limit.
* Default value is {@code true}.
*
* @return the use string binding
*/
public boolean useStringBinding() {
return useStringBinding;
}
/**
* {@link String} values with length above this limit will be bound
* using {@link java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)}
* if {@link #useStringBinding()} is set to {@code true}.
* Default value is {@code 1024}.
*
* @return the string binding size
*/
public int stringBindingSize() {
return stringBindingSize;
}
/**
* Use {@link java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)} binding
* for {@code byte[]} values.
* Default value is {@code true}.
*
* @return the use byte array binding
*/
public boolean useByteArrayBinding() {
return useByteArrayBinding;
}
/**
* Use {@link java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp)}
* to set {@link java.time.LocalTime} values when {@code true}
* or use {@link java.sql.PreparedStatement#setTime(int, java.sql.Time)} when {@code false}.
* Default value is {@code true}.
*
This option is vendor specific. Most of the databases are fine with {@link java.sql.Timestamp},
* but for example SQL Server requires {@link java.sql.Time}.
* This option does not apply when {@link #setObjectForJavaTime()} is set to {@code true}.
*
* @return the timestamp for local time
*/
public boolean timestampForLocalTime() {
return timestampForLocalTime;
}
/**
* Set all {@code java.time} Date/Time values directly using {@link java.sql.PreparedStatement#setObject(int, Object)}.
* This option shall work fine for recent JDBC drivers.
* Default value is {@code true}.
*
* @return the set object for java time
*/
public boolean setObjectForJavaTime() {
return setObjectForJavaTime;
}
/**
* If this instance was configured, this would be the config instance used.
*
* @return config node used to configure this builder, or empty if not configured
*/
public Optional config() {
return Optional.ofNullable(config);
}
@Override
public String toString() {
return "JdbcParametersConfigBuilder{"
+ "useNString=" + useNString + ","
+ "useStringBinding=" + useStringBinding + ","
+ "stringBindingSize=" + stringBindingSize + ","
+ "useByteArrayBinding=" + useByteArrayBinding + ","
+ "timestampForLocalTime=" + timestampForLocalTime + ","
+ "setObjectForJavaTime=" + setObjectForJavaTime
+ "}";
}
/**
* Handles providers and decorators.
*/
protected void preBuildPrototype() {
}
/**
* Validates required properties.
*/
protected void validatePrototype() {
}
/**
* Generated implementation of the prototype, can be extended by descendant prototype implementations.
*/
protected static class JdbcParametersConfigImpl implements JdbcParametersConfig {
private final boolean setObjectForJavaTime;
private final boolean timestampForLocalTime;
private final boolean useByteArrayBinding;
private final boolean useNString;
private final boolean useStringBinding;
private final int stringBindingSize;
/**
* Create an instance providing a builder.
*
* @param builder extending builder base of this prototype
*/
protected JdbcParametersConfigImpl(JdbcParametersConfig.BuilderBase, ?> builder) {
this.useNString = builder.useNString();
this.useStringBinding = builder.useStringBinding();
this.stringBindingSize = builder.stringBindingSize();
this.useByteArrayBinding = builder.useByteArrayBinding();
this.timestampForLocalTime = builder.timestampForLocalTime();
this.setObjectForJavaTime = builder.setObjectForJavaTime();
}
@Override
public boolean useNString() {
return useNString;
}
@Override
public boolean useStringBinding() {
return useStringBinding;
}
@Override
public int stringBindingSize() {
return stringBindingSize;
}
@Override
public boolean useByteArrayBinding() {
return useByteArrayBinding;
}
@Override
public boolean timestampForLocalTime() {
return timestampForLocalTime;
}
@Override
public boolean setObjectForJavaTime() {
return setObjectForJavaTime;
}
@Override
public String toString() {
return "JdbcParametersConfig{"
+ "useNString=" + useNString + ","
+ "useStringBinding=" + useStringBinding + ","
+ "stringBindingSize=" + stringBindingSize + ","
+ "useByteArrayBinding=" + useByteArrayBinding + ","
+ "timestampForLocalTime=" + timestampForLocalTime + ","
+ "setObjectForJavaTime=" + setObjectForJavaTime
+ "}";
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof JdbcParametersConfig other)) {
return false;
}
return useNString == other.useNString()
&& useStringBinding == other.useStringBinding()
&& stringBindingSize == other.stringBindingSize()
&& useByteArrayBinding == other.useByteArrayBinding()
&& timestampForLocalTime == other.timestampForLocalTime()
&& setObjectForJavaTime == other.setObjectForJavaTime();
}
@Override
public int hashCode() {
return Objects.hash(useNString, useStringBinding, stringBindingSize, useByteArrayBinding, timestampForLocalTime, setObjectForJavaTime);
}
}
}
/**
* Fluent API builder for {@link JdbcParametersConfig}.
*/
class Builder extends JdbcParametersConfig.BuilderBase implements io.helidon.common.Builder {
private Builder() {
}
@Override
public JdbcParametersConfig buildPrototype() {
preBuildPrototype();
validatePrototype();
return new JdbcParametersConfigImpl(this);
}
@Override
public JdbcParametersConfig build() {
return buildPrototype();
}
}
}