2424import org .jooq .CSVFormat ;
2525import org .jooq .Cursor ;
2626import org .jooq .DSLContext ;
27+ import org .jooq .LoaderError ;
2728import org .jooq .SQLDialect ;
2829import org .jooq .Table ;
2930import org .jooq .TableField ;
3031import org .jooq .impl .DSL ;
3132import org .springframework .stereotype .Repository ;
33+ import org .springframework .util .CollectionUtils ;
3234
3335import java .io .IOException ;
3436import java .io .InputStream ;
@@ -73,6 +75,23 @@ public void exportCsv(Writer writer, Table<?> table) throws IOException {
7375 }
7476 }
7577
78+ @ Override
79+ public void beforeImport () {
80+ SQLDialect dialectFamily = ctx .configuration ().dialect ().family ();
81+
82+ boolean isOk = dialectFamily == SQLDialect .MYSQL || dialectFamily == SQLDialect .MARIADB ;
83+ if (!isOk ) {
84+ throw new IllegalStateException ("Unsupported dialect " + dialectFamily );
85+ }
86+
87+ ctx .execute ("set foreign_key_checks=0" );
88+ }
89+
90+ @ Override
91+ public void afterImport () {
92+ ctx .execute ("set foreign_key_checks=1" );
93+ }
94+
7695 /**
7796 * https://www.jooq.org/doc/latest/manual/sql-execution/importing/importing-api/
7897 * https://www.jooq.org/doc/latest/manual/sql-execution/importing/importing-sources/importing-source-csv/
@@ -89,6 +108,13 @@ public void importCsv(InputStream in, Table<?> table) throws IOException {
89108 .fieldsCorresponding ()
90109 .execute ();
91110
111+ if (!CollectionUtils .isEmpty (loader .errors ())) {
112+ for (LoaderError error : loader .errors ()) {
113+ log .error ("Exception happened" , error .exception ());
114+ }
115+ throw new RuntimeException ("There were errors loading data into table " + table .getName ());
116+ }
117+
92118 int processed = loader .processed ();
93119
94120 log .info ("Imported '{}' with processedRows={}, storedRows={}, errorCount={}, errorMessages={}" ,
@@ -141,13 +167,7 @@ private void resetAutoIncrement(Table<?> table) {
141167
142168 long nextVal = ((Number ) maxId ).longValue () + 1 ;
143169
144- SQLDialect dialectFamily = ctx .configuration ().dialect ().family ();
145-
146- if (dialectFamily == SQLDialect .MYSQL || dialectFamily == SQLDialect .MARIADB ) {
147- ctx .execute ("ALTER TABLE " + table .getName () + " AUTO_INCREMENT = " + nextVal );
148- } else {
149- log .warn ("Auto increment not supported for dialect family {}" , dialectFamily );
150- }
170+ ctx .execute (DSL .sql ("ALTER TABLE {0} AUTO_INCREMENT = {1}" , table , DSL .val (nextVal )));
151171 }
152172
153173}
0 commit comments