4949import org .jooq .SelectConditionStep ;
5050import org .jooq .SelectJoinStep ;
5151import org .jooq .Table ;
52+ import org .jooq .exception .DataAccessException ;
5253
5354@ Slf4j
5455@ Data
@@ -79,7 +80,10 @@ private static void assignStatsToPath(ConceptElementId<?> resolvedId, Map<Concep
7980 * collect unique fields used/defined in the expressions.
8081 */
8182 private static List <Field <?>> collectAllFields (List <CTCondition .Expression > expressions ) {
83+
84+
8285 List <Field <?>> fields = expressions .stream ()
86+ //TODO determine length of chars, for now we are relying on a fixed length because it's quite cumbersome
8387 .map (expression -> expression .conditions ().keySet ())
8488 .flatMap (Collection ::stream )
8589 .distinct ()
@@ -119,11 +123,13 @@ public void createConceptIdJoinTable(TreeConcept concept) {
119123 Name tableName = idsTableName (concept .getName ());
120124
121125 // allFields are the statements to extract values from the underlying tables, we use them to generate the field names
122- List <Field <?>> fieldNames = new ArrayList <>(allFields );
123- fieldNames .addFirst (field (CONCEPT_ID_FIELD .getName (), VARCHAR (findMaxIdLength (expressions ))));
126+ List <Field <?>> fields = new ArrayList <>();
127+
128+ fields .addAll (allFields );
129+ fields .addFirst (field (CONCEPT_ID_FIELD .getName (), VARCHAR (findMaxIdLength (expressions ))));
124130
125- createConceptIdsTable (tableName , fieldNames );
126- insertConceptIdMappings (tableName , fieldNames , rows , dslContext );
131+ createConceptIdsTable (tableName , fields );
132+ insertConceptIdMappings (tableName , fields , rows , dslContext );
127133 }
128134
129135 @ NotNull
@@ -211,11 +217,19 @@ private Name idsTableName(@NotBlank String name) {
211217 private void insertConceptIdMappings (Name tableName , List <Field <?>> fieldNames , List <RowN > rows , DSLContext dsl ) {
212218 log .info ("BEGIN inserting {} rows into {}" , rows .size (), tableName );
213219
214- InsertValuesStepN <Record > insertConceptTable = dsl .insertInto (table (tableName ))
215- .columns (fieldNames )
216- .valuesOfRows (rows );
220+ // We're using batching here because some DBMS don't allow mass inserts.
221+ // There's a chance, we rework this to use a prepared statement with lots of bindings under the hood. But that needs to rework the entire stream of rows.
222+ List <InsertValuesStepN <?>> inserts = new ArrayList <>(rows .size ());
223+
224+ for (RowN row : rows ) {
225+ inserts .add (dsl .insertInto (table (tableName ))
226+ .columns (fieldNames )
227+ .values (row ));
228+ }
229+
230+ dsl .batch (inserts )
231+ .execute ();
217232
218- insertConceptTable .execute ();
219233
220234 log .trace ("DONE inserting into {}" , tableName );
221235 }
@@ -224,17 +238,23 @@ private void insertConceptIdMappings(Name tableName, List<Field<?>> fieldNames,
224238 * Drop the table, then recreate it.
225239 * TODO add an index.
226240 */
227- private void createConceptIdsTable (Name tableName , List <Field <?>> fieldNames ) {
241+ private void createConceptIdsTable (Name tableName , List <Field <?>> fields ) {
228242
229- log .debug ("Creating table {} with fields {}" , tableName , fieldNames );
243+ log .debug ("Creating table {} with fields {}" , tableName , fields );
230244
231- dslContext .dropTableIfExists (tableName )
232- .cascade ()
233- .execute ();
245+ try {
246+ dslContext .dropTable (tableName )
247+ .cascade ()
248+ .execute ();
249+ }
250+ catch (DataAccessException exception ) {
251+ // Likely it doesn't exist. Some DBMS just don't support drop-IfExists so this is the next best thing :^)
252+ log .trace ("Failed to drop table {}" , tableName , exception );
253+ }
234254
235255 CreateTableElementListStep createTable =
236256 dslContext .createTable (tableName )
237- .columns (fieldNames );
257+ .columns (fields );
238258
239259
240260 createTable .execute ();
0 commit comments