-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75701b2
commit 3f5d9ea
Showing
18 changed files
with
269 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData.ColumnMetaData; | ||
|
||
public interface DbDialect { | ||
Extractor createConverter(final ColumnMetaData column); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/itsallcode/jdbc/dialect/ExasolDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
|
||
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData.ColumnMetaData; | ||
|
||
public class ExasolDialect implements DbDialect { | ||
|
||
public Extractor createConverter(final ColumnMetaData column) { | ||
return switch (column.getType().getJdbcType()) { | ||
case TIMESTAMP -> Extractors.timestampToUTCInstant(); | ||
case CLOB -> Extractors.clobToString(); | ||
case BLOB -> Extractors.blobToBytes(); | ||
case TIME -> Extractors.forType(LocalTime.class); | ||
case DATE -> Extractors.forType(LocalDate.class); | ||
default -> Extractors.generic(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface Extractor { | ||
|
||
Object getObject(ResultSet resultSet, int columnIndex) throws SQLException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import java.sql.*; | ||
import java.util.Calendar; | ||
import java.util.TimeZone; | ||
|
||
public class Extractors { | ||
private static final Calendar UTC_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("UTC")); | ||
|
||
private Extractors() { | ||
} | ||
|
||
static Extractor timestampToUTCInstant() { | ||
return nonNull((resultSet, columnIndex) -> resultSet.getTimestamp(columnIndex, UTC_CALENDAR).toInstant()); | ||
} | ||
|
||
private static Extractor nonNull(final Extractor extractor) { | ||
return (resultSet, columnIndex) -> { | ||
resultSet.getObject(columnIndex); | ||
if (resultSet.wasNull()) { | ||
return null; | ||
} | ||
return extractor.getObject(resultSet, columnIndex); | ||
}; | ||
} | ||
|
||
static Extractor clobToString() { | ||
return nonNull((resultSet, columnIndex) -> { | ||
final Clob clob = resultSet.getClob(columnIndex); | ||
return clob.getSubString(1, (int) clob.length()); | ||
}); | ||
} | ||
|
||
static Extractor blobToBytes() { | ||
return nonNull((resultSet, columnIndex) -> { | ||
final Blob blob = resultSet.getBlob(columnIndex); | ||
return blob.getBytes(1, (int) blob.length()); | ||
}); | ||
} | ||
|
||
static Extractor forType(final Class<?> type) { | ||
return (resultSet, columnIndex) -> resultSet.getObject(columnIndex, type); | ||
} | ||
|
||
static Extractor generic() { | ||
return ResultSet::getObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
|
||
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData.ColumnMetaData; | ||
|
||
public class H2Dialect implements DbDialect { | ||
|
||
public Extractor createConverter(final ColumnMetaData column) { | ||
return switch (column.getType().getJdbcType()) { | ||
case TIMESTAMP -> Extractors.timestampToUTCInstant(); | ||
case CLOB -> Extractors.clobToString(); | ||
case BLOB -> Extractors.blobToBytes(); | ||
case TIME -> Extractors.forType(LocalTime.class); | ||
case DATE -> Extractors.forType(LocalDate.class); | ||
default -> Extractors.generic(); | ||
}; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/itsallcode/jdbc/resultset/ColumnValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.itsallcode.jdbc.resultset; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import org.itsallcode.jdbc.dialect.Extractor; | ||
|
||
@FunctionalInterface | ||
public interface ColumnValueConverter { | ||
|
||
<T> T getObject(ResultSet resultSet, int columnIndex, Class<T> type) throws SQLException; | ||
|
||
static ColumnValueConverter generic() { | ||
return simple(ResultSet::getObject); | ||
} | ||
|
||
static ColumnValueConverter simple(final Extractor extractor) { | ||
return new ColumnValueConverter() { | ||
@Override | ||
public <T> T getObject(final ResultSet resultSet, final int columnIndex, final Class<T> type) | ||
throws SQLException { | ||
return type.cast(extractor.getObject(resultSet, columnIndex)); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/itsallcode/jdbc/resultset/ResultSetValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.itsallcode.jdbc.resultset; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.*; | ||
|
||
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData; | ||
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData.ColumnMetaData; | ||
|
||
public class ResultSetValueConverter { | ||
|
||
private final Map<Integer, ColumnValueConverter> convertersByIndex; | ||
private final Map<String, Integer> columnIndexByLabel; | ||
|
||
private ResultSetValueConverter(final Map<Integer, ColumnValueConverter> convertersByIndex, | ||
final Map<String, Integer> columnIndexByLabel) { | ||
this.convertersByIndex = convertersByIndex; | ||
this.columnIndexByLabel = columnIndexByLabel; | ||
} | ||
|
||
public static ResultSetValueConverter create(final SimpleMetaData resultSetMetadata, | ||
final List<ColumnValueConverter> converters) { | ||
final Map<Integer, ColumnValueConverter> convertersByIndex = new HashMap<>(); | ||
final Map<String, Integer> columnIndexByLabel = new HashMap<>(); | ||
for (int i = 1; i <= converters.size(); i++) { | ||
final ColumnValueConverter converter = converters.get(i - 1); | ||
final ColumnMetaData metaData = resultSetMetadata.getColumnByIndex(i); | ||
convertersByIndex.put(metaData.getColumnIndex(), converter); | ||
columnIndexByLabel.put(metaData.getLabel(), i); | ||
} | ||
return new ResultSetValueConverter(convertersByIndex, columnIndexByLabel); | ||
} | ||
|
||
public <T> T getObject(final ResultSet delegate, final int columnIndex, final Class<T> type) throws SQLException { | ||
return getConverter(columnIndex).getObject(delegate, columnIndex, type); | ||
} | ||
|
||
public <T> T getObject(final ResultSet delegate, final String columnLabel, final Class<T> type) | ||
throws SQLException { | ||
final int columnIndex = getIndexForLabel(columnLabel); | ||
return getConverter(columnIndex).getObject(delegate, columnIndex, type); | ||
} | ||
|
||
private int getIndexForLabel(final String columnLabel) { | ||
final Integer index = columnIndexByLabel.get(columnLabel); | ||
if (index == null) { | ||
throw new IllegalStateException("No index found for column label '" + columnLabel + "'"); | ||
} | ||
return index.intValue(); | ||
} | ||
|
||
private ColumnValueConverter getConverter(final int columnIndex) { | ||
final ColumnValueConverter converter = convertersByIndex.get(columnIndex); | ||
if (converter == null) { | ||
throw new IllegalStateException("No converter found for column index " + columnIndex); | ||
} | ||
return converter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.