[#2995] INSERT INTO table SELECT ... renders bad SQL when column names are omitted and a plain SQL table is given

This commit is contained in:
lukaseder 2016-03-01 13:20:05 +01:00
parent ef631e5290
commit a3119571c7
3 changed files with 25 additions and 2 deletions

View File

@ -56,6 +56,7 @@ import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Tools.aliasedFields;
import static org.jooq.impl.Tools.fieldNames;
import static org.jooq.impl.Tools.DataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST;
import java.util.Arrays;
import java.util.Map;
@ -349,17 +350,25 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
.declareTables(declareTables);
// [#1506] with DEFAULT VALUES, we might not have any columns to render
if (insertMaps.isExecutable()) {
if (insertMaps.isExecutable())
insertMaps.insertMaps.get(0).toSQLReferenceKeys(ctx);
}
ctx.end(INSERT_INSERT_INTO);
if (select != null) {
// [#2995] Prevent the generation of wrapping parentheses around the
// INSERT .. SELECT statement's SELECT because they would be
// interpreted as the (missing) INSERT column list's parens.
if (insertMaps.insertMaps.get(0).size() == 0)
ctx.data(DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST, true);
ctx.formatSeparator()
.start(INSERT_SELECT)
.visit(select)
.end(INSERT_SELECT);
ctx.data().remove(DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST);
}
else if (defaultValues) {
switch (ctx.family()) {

View File

@ -99,6 +99,7 @@ import static org.jooq.impl.DSL.row;
import static org.jooq.impl.Tools.fieldArray;
import static org.jooq.impl.Tools.DataKey.DATA_COLLECTED_SEMI_ANTI_JOIN;
import static org.jooq.impl.Tools.DataKey.DATA_COLLECT_SEMI_ANTI_JOIN;
import static org.jooq.impl.Tools.DataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST;
import static org.jooq.impl.Tools.DataKey.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Tools.DataKey.DATA_OMIT_INTO_CLAUSE;
import static org.jooq.impl.Tools.DataKey.DATA_OVERRIDE_ALIASES_IN_ORDER_BY;
@ -926,6 +927,11 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp

View File

@ -340,6 +340,14 @@ final class Tools {
* [#1206] The collected Semi / Anti JOIN predicates.
*/
DATA_COLLECTED_SEMI_ANTI_JOIN,
/**
* [#2995] An <code>INSERT INTO t SELECT</code> statement. Without any
* explicit column list, the <code>SELECT</code> statement must not be
* wrapped in parentheses (which would be interpreted as the column
* list's parentheses).
*/
DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST,
}
/**