Skip to content

Commit

Permalink
Add javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Oct 20, 2024
1 parent 1ce68ad commit 7c84b69
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/itsallcode/jdbc/dialect/ColumnValueSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> object type
*/
public interface ColumnValueSetter<T> {

/**
* 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;
}
8 changes: 8 additions & 0 deletions src/main/java/org/itsallcode/jdbc/dialect/DbDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> object type
* @param type object type
* @return column value setter
*/
<T> ColumnValueSetter<T> createSetter(Class<T> type);
}
8 changes: 4 additions & 4 deletions src/main/java/org/itsallcode/jdbc/dialect/Setters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import java.time.*;
import java.time.format.DateTimeFormatter;

public final class Setters {
final class Setters {
private Setters() {
}

public static <T> ColumnValueSetter<T> generic() {
static <T> ColumnValueSetter<T> generic() {
return PreparedStatement::setObject;
}

Expand All @@ -17,13 +17,13 @@ static ColumnValueSetter<LocalDate> localDateToString() {
.setString(parameterIndex, date.toString());
}

public static ColumnValueSetter<Instant> instantToString(final DateTimeFormatter dateTimeFormatter,
static ColumnValueSetter<Instant> 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<LocalDateTime> localDateTimeToString(final DateTimeFormatter dateTimeFormatter) {
static ColumnValueSetter<LocalDateTime> localDateTimeToString(final DateTimeFormatter dateTimeFormatter) {
return (final PreparedStatement stmt, final int parameterIndex, final LocalDateTime localDateTime) -> stmt
.setString(parameterIndex, dateTimeFormatter.format(localDateTime));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> GENERIC_SETTER = Setters.generic();
private static final ColumnValueSetter<Object> GENERIC_SETTER = PreparedStatement::setObject;
private final DbDialect dialect;
private final Map<Class<?>, ColumnValueSetter<Object>> setters = new HashMap<>();

/**
* Create a new instance.
*
* @param dialect database dialect
*/
public ParamSetterProvider(final DbDialect dialect) {
this.dialect = dialect;
}
Expand Down

0 comments on commit 7c84b69

Please sign in to comment.