Skip to content

Commit 3b3abe5

Browse files
authored
Merge pull request #6 from ergon/bugfix/interval
Add support for postgres interval date type
2 parents 5c015b2 + 2690d63 commit 3b3abe5

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

core/src/main/java/ch/ergon/adam/core/db/schema/DataType.java

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public enum DataType {
2828
DATE,
2929
TIMESTAMP,
3030
TIME,
31+
INTERVALYEARTOSECOND,
3132
INTERVALYEARTOMONTH,
3233
INTERVALDAYTOSECOND,
3334
LOCALDATE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ch.ergon.adam.integrationtest.postgresql;
2+
3+
import ch.ergon.adam.integrationtest.AbstractDbTestBase;
4+
import ch.ergon.adam.integrationtest.DummySink;
5+
import ch.ergon.adam.core.db.schema.Schema;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
11+
public class PostgreSqlIntervalFieldTest extends AbstractDbTestBase {
12+
13+
public PostgreSqlIntervalFieldTest() {
14+
super(new PostgreSqlTestDbUrlProvider());
15+
}
16+
17+
private static final String YML = "---\n" +
18+
"name: \"test_table\"\n" +
19+
"fields:\n" +
20+
"- name: \"duration\"\n" +
21+
" dataType: \"INTERVALYEARTOSECOND\"\n" +
22+
"--- []\n" +
23+
"--- []\n";
24+
25+
private static final String CREATE_TABLE_SQL =
26+
"create table test_table (" +
27+
"duration interval not null" +
28+
")";
29+
30+
@Test
31+
public void testCreateTableToYml() throws Exception {
32+
getSourceDbConnection().createStatement().execute(CREATE_TABLE_SQL);
33+
sourceToTarget();
34+
DummySink dummySink = targetToDummy();
35+
Schema schema = dummySink.getTargetSchema();
36+
migrateTargetWithSchema(schema);
37+
String yml = targetToYml();
38+
assertThat(yml, is(YML));
39+
}
40+
41+
}

jooq/src/main/java/ch/ergon/adam/jooq/JooqSink.java

+3
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ protected DataType<?> mapRawType(ch.ergon.adam.core.db.schema.DataType type) {
410410
case TIME:
411411
jooqType = SQLDataType.TIME;
412412
break;
413+
case INTERVALYEARTOSECOND:
414+
jooqType = SQLDataType.INTERVAL;
415+
break;
413416
case INTERVALYEARTOMONTH:
414417
jooqType = SQLDataType.INTERVALYEARTOMONTH;
415418
break;

jooq/src/main/java/ch/ergon/adam/jooq/JooqSource.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import ch.ergon.adam.core.db.schema.Table;
1010
import org.jooq.*;
1111
import org.jooq.impl.DSL;
12+
import org.jooq.types.DayToSecond;
13+
import org.jooq.types.YearToMonth;
14+
import org.jooq.types.YearToSecond;
1215

1316
import java.sql.Connection;
1417
import java.sql.DriverManager;
@@ -193,7 +196,20 @@ protected boolean isSequence(org.jooq.Field<?> jooqField) {
193196
}
194197

195198
protected DataType mapDataTypeFromJooq(org.jooq.Field<?> jooqField) {
196-
String typeName = jooqField.getDataType().getSQLDataType().getTypeName();
199+
org.jooq.DataType<?> sqlDataType = jooqField.getDataType().getSQLDataType();
200+
if (sqlDataType.isInterval()) {
201+
Class<?> type = sqlDataType.getType();
202+
if (type.equals(YearToSecond.class)) {
203+
return DataType.INTERVALYEARTOSECOND;
204+
} else if (type.equals(YearToMonth.class)) {
205+
return DataType.INTERVALYEARTOMONTH;
206+
} else if (type.equals(DayToSecond.class)) {
207+
return DataType.INTERVALDAYTOSECOND;
208+
} else {
209+
throw new RuntimeException("Unsupported interval type [" + type.getName() + "]");
210+
}
211+
}
212+
String typeName = sqlDataType.getTypeName();
197213
try {
198214
return DataType.valueOf(typeName.toUpperCase().replace(" ", ""));
199215
} catch (IllegalArgumentException e) {

0 commit comments

Comments
 (0)