Skip to content

Commit 747d863

Browse files
committed
add custom logic for temporal values during import
1 parent b68af2e commit 747d863

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/main/java/de/rwth/idsg/steve/repository/impl/DataImportExportRepositoryImpl.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@
2121
import de.rwth.idsg.steve.repository.DataImportExportRepository;
2222
import lombok.RequiredArgsConstructor;
2323
import lombok.extern.slf4j.Slf4j;
24+
import org.apache.commons.lang3.StringUtils;
2425
import org.jooq.CSVFormat;
26+
import org.jooq.Converter;
2527
import org.jooq.Cursor;
2628
import org.jooq.DSLContext;
29+
import org.jooq.Field;
2730
import org.jooq.LoaderError;
2831
import org.jooq.SQLDialect;
2932
import org.jooq.Table;
3033
import org.jooq.TableField;
34+
import org.jooq.impl.AbstractConverter;
3135
import org.jooq.impl.DSL;
36+
import org.jooq.impl.SQLDataType;
3237
import org.springframework.stereotype.Repository;
3338
import org.springframework.util.CollectionUtils;
3439

3540
import java.io.IOException;
3641
import java.io.InputStream;
3742
import java.io.Writer;
3843
import java.nio.charset.StandardCharsets;
44+
import java.sql.Timestamp;
45+
import java.time.Instant;
46+
import java.util.Arrays;
47+
import java.util.List;
3948

4049
/**
4150
* @author Sevket Goekay <sevketgokay@gmail.com>
@@ -52,7 +61,12 @@ public class DataImportExportRepositoryImpl implements DataImportExportRepositor
5261

5362
private final CSVFormat csvFormatWithHeader = new CSVFormat();
5463
private final CSVFormat csvFormatNoHeader = csvFormatWithHeader.header(false);
64+
private final Converter<String, Timestamp> isoTimestampConverter = new IsoTimestampConverter();
5565

66+
/**
67+
* DateTime will be exported via its toString method in the else-block of {@link org.jooq.impl.AbstractResult#format0(Object, boolean, boolean)}
68+
* because nothing else matches. The serialized values will be ISO8601 format.
69+
*/
5670
@Override
5771
public void exportCsv(Writer writer, Table<?> table) throws IOException {
5872
// write header line
@@ -105,7 +119,7 @@ public void importCsv(InputStream in, Table<?> table) throws IOException {
105119
.bulkAfter(BATCH_SIZE) // Put up to X rows in a single bulk statement.
106120
.batchAfter(BATCH_SIZE) // Put up to X statements (bulk or not) in a single statement batch.
107121
.loadCSV(in, StandardCharsets.UTF_8)
108-
.fieldsCorresponding()
122+
.fields(getTableFields(table))
109123
.execute();
110124

111125
if (!CollectionUtils.isEmpty(loader.errors())) {
@@ -170,4 +184,43 @@ private void resetAutoIncrement(Table<?> table) {
170184
ctx.execute(DSL.sql("ALTER TABLE {0} AUTO_INCREMENT = {1}", table, DSL.val(nextVal)));
171185
}
172186

187+
// -------------------------------------------------------------------------
188+
// Loader API cannot import temporal values in ISO8601 UTC format into a
189+
// Timestamp. More context: https://groups.google.com/g/jooq-user/c/VzZdIT7Xdnc
190+
//
191+
// Because of this, we are overriding the default converter of TIMESTAMP
192+
// table fields during the import.
193+
// -------------------------------------------------------------------------
194+
195+
private List<Field<?>> getTableFields(Table<?> table) {
196+
return Arrays.stream(table.fields())
197+
.map(it -> {
198+
if (it.getDataType().isTimestamp()) {
199+
return DSL.field(it.getName(), SQLDataType.VARCHAR(50)).convert(isoTimestampConverter);
200+
} else {
201+
return it;
202+
}
203+
}).toList();
204+
}
205+
206+
private static class IsoTimestampConverter extends AbstractConverter<String, Timestamp> {
207+
208+
private IsoTimestampConverter() {
209+
super(String.class, Timestamp.class);
210+
}
211+
212+
@Override
213+
public Timestamp from(String str) {
214+
if (StringUtils.isEmpty(str)) {
215+
return null;
216+
}
217+
return Timestamp.from(Instant.parse(str));
218+
}
219+
220+
@Override
221+
public String to(Timestamp ts) {
222+
return ts == null ? null : ts.toString();
223+
}
224+
}
225+
173226
}

0 commit comments

Comments
 (0)