-
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.
* Add generic DB Dialect * Use generic dialect as fallback --------- Co-authored-by: kaklakariada <[email protected]> Co-authored-by: kaklakariada <[email protected]>
- Loading branch information
1 parent
c9b7f8d
commit f00bce7
Showing
7 changed files
with
114 additions
and
3 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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/itsallcode/jdbc/dialect/GenericDialect.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,30 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import org.itsallcode.jdbc.resultset.generic.ColumnMetaData; | ||
|
||
/** | ||
* A generic {@link DbDialect} without any special handling. | ||
*/ | ||
public final class GenericDialect implements DbDialect { | ||
/** Singleton instance of the generic DB dialect. */ | ||
public static final DbDialect INSTANCE = new GenericDialect(); | ||
|
||
private GenericDialect() { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public boolean supportsUrl(final String jdbcUrl) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public ColumnValueExtractor createExtractor(final ColumnMetaData column) { | ||
return Extractors.generic(); | ||
} | ||
|
||
@Override | ||
public <T> ColumnValueSetter<T> createSetter(final Class<T> type) { | ||
return Setters.generic(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/org/itsallcode/jdbc/DbDialectFactoryTest.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; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.itsallcode.jdbc.dialect.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DbDialectFactoryTest { | ||
@Test | ||
void exasolDialect() { | ||
assertThat(new DbDialectFactory().createDialect("jdbc:exa:localhost:8563;schema=SCHEMA")) | ||
.isInstanceOf(ExasolDialect.class); | ||
} | ||
|
||
@Test | ||
void h2Dialect() { | ||
assertThat(new DbDialectFactory().createDialect("jdbc:h2:mem:")) | ||
.isInstanceOf(H2Dialect.class); | ||
} | ||
|
||
@Test | ||
void genericDialect() { | ||
assertThat(new DbDialectFactory().createDialect("jdbc:unknown:localhost:8563;schema=SCHEMA")) | ||
.isInstanceOf(GenericDialect.class); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/itsallcode/jdbc/dialect/GenericDialectITest.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,46 @@ | ||
package org.itsallcode.jdbc.dialect; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.h2.jdbcx.JdbcDataSource; | ||
import org.itsallcode.jdbc.*; | ||
import org.itsallcode.jdbc.resultset.ContextRowMapper; | ||
import org.itsallcode.jdbc.resultset.generic.Row; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class GenericDialectITest { | ||
|
||
static Stream<Arguments> types() { | ||
return Stream.of( | ||
Arguments.of(1), | ||
Arguments.of("a"), | ||
Arguments.of(1.0), | ||
Arguments.of(true), | ||
Arguments.of((Object) null)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("types") | ||
void setGet(final Object value) { | ||
try (SimpleConnection connection = genericDialectConnection()) { | ||
final List<Row> result = connection | ||
.query("select ?", asList(value), ContextRowMapper.generic(GenericDialect.INSTANCE)).toList(); | ||
assertThat(result) | ||
.hasSize(1) | ||
.first().extracting(row -> row.get(0).getValue()) | ||
.isEqualTo(value); | ||
} | ||
} | ||
|
||
private SimpleConnection genericDialectConnection() { | ||
final JdbcDataSource dataSource = new JdbcDataSource(); | ||
dataSource.setURL(H2TestFixture.H2_MEM_JDBC_URL); | ||
return DataSourceConnectionFactory.create(GenericDialect.INSTANCE, dataSource).getConnection(); | ||
} | ||
} |
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