diff --git a/src/main/java/org/itsallcode/jdbc/dialect/ColumnValueSetter.java b/src/main/java/org/itsallcode/jdbc/dialect/ColumnValueSetter.java index a124cbd..98765fe 100644 --- a/src/main/java/org/itsallcode/jdbc/dialect/ColumnValueSetter.java +++ b/src/main/java/org/itsallcode/jdbc/dialect/ColumnValueSetter.java @@ -3,7 +3,21 @@ import java.sql.PreparedStatement; import java.sql.SQLException; +/** + * Implementors of this interface optionally convert an object and set it on a + * {@link PreparedStatement}. + * + * @param object type + */ public interface ColumnValueSetter { + /** + * Optionally convert an object and set it on a {@link PreparedStatement}. + * + * @param stmt prepared statement + * @param parameterIndex parameter index + * @param object object to convert + * @throws SQLException if setting fails + */ void setObject(PreparedStatement stmt, int parameterIndex, T object) throws SQLException; } diff --git a/src/main/java/org/itsallcode/jdbc/dialect/DbDialect.java b/src/main/java/org/itsallcode/jdbc/dialect/DbDialect.java index dfc0b7b..3a5be6e 100644 --- a/src/main/java/org/itsallcode/jdbc/dialect/DbDialect.java +++ b/src/main/java/org/itsallcode/jdbc/dialect/DbDialect.java @@ -23,5 +23,13 @@ public interface DbDialect { */ ColumnValueExtractor createExtractor(final ColumnMetaData column); + /** + * Create a new column value setter for setting values on a + * {@link java.sql.PreparedStatement} specific to the dialect. + * + * @param object type + * @param type object type + * @return column value setter + */ ColumnValueSetter createSetter(Class type); } diff --git a/src/main/java/org/itsallcode/jdbc/dialect/Setters.java b/src/main/java/org/itsallcode/jdbc/dialect/Setters.java index 406217b..7592258 100644 --- a/src/main/java/org/itsallcode/jdbc/dialect/Setters.java +++ b/src/main/java/org/itsallcode/jdbc/dialect/Setters.java @@ -4,11 +4,11 @@ import java.time.*; import java.time.format.DateTimeFormatter; -public final class Setters { +final class Setters { private Setters() { } - public static ColumnValueSetter generic() { + static ColumnValueSetter generic() { return PreparedStatement::setObject; } @@ -17,13 +17,13 @@ static ColumnValueSetter localDateToString() { .setString(parameterIndex, date.toString()); } - public static ColumnValueSetter instantToString(final DateTimeFormatter dateTimeFormatter, + static ColumnValueSetter instantToString(final DateTimeFormatter dateTimeFormatter, final ZoneId timeZone) { return (final PreparedStatement stmt, final int parameterIndex, final Instant instant) -> stmt .setString(parameterIndex, dateTimeFormatter.format(LocalDateTime.ofInstant(instant, timeZone))); } - public static ColumnValueSetter localDateTimeToString(final DateTimeFormatter dateTimeFormatter) { + static ColumnValueSetter localDateTimeToString(final DateTimeFormatter dateTimeFormatter) { return (final PreparedStatement stmt, final int parameterIndex, final LocalDateTime localDateTime) -> stmt .setString(parameterIndex, dateTimeFormatter.format(localDateTime)); } diff --git a/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java b/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java index ca3a177..d4f3dea 100644 --- a/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java +++ b/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java @@ -3,10 +3,10 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - /** - * A {@link PreparedStatement} that delegates all methods and converts parameter - * value of {@link #convert(Object)} using a {@link ParameterMapper}. + * A {@link PreparedStatement} that delegates calls to + * {@link #setObject(int, Object)} to a setter provided by + * {@link ParamSetterProvider#findSetter(Object)}. */ public class ConvertingPreparedStatement extends DelegatingPreparedStatement { private final PreparedStatement originalDelegate; diff --git a/src/main/java/org/itsallcode/jdbc/statement/ParamSetterProvider.java b/src/main/java/org/itsallcode/jdbc/statement/ParamSetterProvider.java index 3edafd6..a8ea4ea 100644 --- a/src/main/java/org/itsallcode/jdbc/statement/ParamSetterProvider.java +++ b/src/main/java/org/itsallcode/jdbc/statement/ParamSetterProvider.java @@ -1,15 +1,26 @@ package org.itsallcode.jdbc.statement; +import java.sql.PreparedStatement; import java.util.HashMap; import java.util.Map; -import org.itsallcode.jdbc.dialect.*; +import org.itsallcode.jdbc.dialect.ColumnValueSetter; +import org.itsallcode.jdbc.dialect.DbDialect; +/** + * This class creates and caches {@link ColumnValueSetter} based on an object's + * type. + */ public class ParamSetterProvider { - private static final ColumnValueSetter GENERIC_SETTER = Setters.generic(); + private static final ColumnValueSetter GENERIC_SETTER = PreparedStatement::setObject; private final DbDialect dialect; private final Map, ColumnValueSetter> setters = new HashMap<>(); + /** + * Create a new instance. + * + * @param dialect database dialect + */ public ParamSetterProvider(final DbDialect dialect) { this.dialect = dialect; }