From e946359bc244c186e652dcd6d114beb10fc1f675 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Fri, 12 Feb 2016 18:09:33 +0100 Subject: [PATCH] [#2995] INSERT INTO table SELECT ... renders bad SQL when column names are omitted and a plain SQL table is given --- .../java/org/jooq/impl/FieldMapForInsert.java | 34 ++++++++----------- .../java/org/jooq/impl/InsertQueryImpl.java | 1 - .../main/java/org/jooq/impl/MergeImpl.java | 2 +- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldMapForInsert.java b/jOOQ/src/main/java/org/jooq/impl/FieldMapForInsert.java index 77298cf0d3..6521bed66d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldMapForInsert.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldMapForInsert.java @@ -70,38 +70,39 @@ final class FieldMapForInsert extends AbstractQueryPartMap, Field> { ctx.sql('('); - if (indent) { + if (indent) ctx.formatIndentStart(); - } String separator = ""; for (Field field : values()) { ctx.sql(separator); - if (indent) { + if (indent) ctx.formatNewLine(); - } ctx.visit(field); separator = ", "; } - if (indent) { + if (indent) ctx.formatIndentEnd() .formatNewLine(); - } ctx.sql(')'); } final void toSQLReferenceKeys(Context ctx) { + + // [#2995] Do not generate empty column lists. + if (size() == 0) + return; + boolean indent = (size() > 1); - ctx.sql('('); + ctx.sql(" ("); - if (indent) { + if (indent) ctx.formatIndentStart(); - } // [#989] Avoid qualifying fields in INSERT field declaration boolean qualify = ctx.qualify(); @@ -111,9 +112,8 @@ final class FieldMapForInsert extends AbstractQueryPartMap, Field> { for (Field field : keySet()) { ctx.sql(separator); - if (indent) { + if (indent) ctx.formatNewLine(); - } ctx.visit(field); separator = ", "; @@ -121,10 +121,9 @@ final class FieldMapForInsert extends AbstractQueryPartMap, Field> { ctx.qualify(qualify); - if (indent) { + if (indent) ctx.formatIndentEnd() .formatNewLine(); - } ctx.sql(')'); } @@ -135,20 +134,17 @@ final class FieldMapForInsert extends AbstractQueryPartMap, Field> { } final void putFields(Collection> fields) { - for (Field field : fields) { + for (Field field : fields) put(field, null); - } } final void putValues(Collection> values) { - if (values.size() != size()) { + if (values.size() != size()) throw new IllegalArgumentException("The number of values must match the number of fields: " + this); - } Iterator> it = values.iterator(); - for (Entry, Field> entry : entrySet()) { + for (Entry, Field> entry : entrySet()) entry.setValue(it.next()); - } } final void set(Map, ?> map) { diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java index a17071eb22..c79c392654 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java @@ -345,7 +345,6 @@ final class InsertQueryImpl extends AbstractStoreQuery impl // [#1506] with DEFAULT VALUES, we might not have any columns to render if (insertMaps.isExecutable()) { - ctx.sql(' '); insertMaps.insertMaps.get(0).toSQLReferenceKeys(ctx); } diff --git a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java index 7f52ab3ec7..1a3688f39d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java @@ -1509,7 +1509,7 @@ implements // [#999] WHEN NOT MATCHED clause is optional if (notMatchedInsert != null) { ctx.formatSeparator() - .keyword("when not matched then insert").sql(' '); + .keyword("when not matched then insert"); notMatchedInsert.toSQLReferenceKeys(ctx); ctx.formatSeparator() .start(MERGE_VALUES)