private final void toSQLInsert(RenderContext context) { context .start(INSERT_INSERT_INTO) .keyword("insert") .sql(" ") // [#1295] MySQL natively supports the IGNORE keyword .keyword( (onDuplicateKeyIgnore && asList(MARIADB, MYSQL).contains(context.configuration().dialect())) ? "ignore " : "") .keyword("into") .sql(" ") .visit(getInto()); // [#1506] with DEFAULT VALUES, we might not have any columns to render if (insertMaps.isExecutable()) { context.sql(" "); insertMaps.insertMaps.get(0).toSQLReferenceKeys(context); } context.end(INSERT_INSERT_INTO); if (defaultValues) { switch (context.configuration().dialect().family()) { /* [pro] xx xxxx xxxxxxx xxxx xxxx xxxx xxxxxxx xx [/pro] */ case DERBY: case MARIADB: case MYSQL: context.sql(" ").keyword("values").sql("("); int count = getInto().fields().length; String separator = ""; for (int i = 0; i < count; i++) { context.sql(separator); context.keyword("default"); separator = ", "; } context.sql(")"); break; default: context.sql(" ").keyword("default values"); break; } } else { context.visit(insertMaps); } }
@SuppressWarnings("unchecked") private final Merge<R> toMerge(Configuration configuration) { Table<R> i = getInto(); if (i.getPrimaryKey() != null) { Condition condition = null; List<Field<?>> key = new ArrayList<Field<?>>(); for (Field<?> f : i.getPrimaryKey().getFields()) { Field<Object> field = (Field<Object>) f; Field<Object> value = (Field<Object>) insertMaps.getMap().get(field); key.add(value); Condition other = field.equal(value); if (condition == null) { condition = other; } else { condition = condition.and(other); } } MergeOnConditionStep<R> on = create(configuration).mergeInto(i).usingDual().on(condition); // [#1295] Use UPDATE clause only when with ON DUPLICATE KEY UPDATE, // not with ON DUPLICATE KEY IGNORE MergeNotMatchedStep<R> notMatched = on; if (onDuplicateKeyUpdate) { notMatched = on.whenMatchedThenUpdate().set(updateMap); } return notMatched .whenNotMatchedThenInsert(insertMaps.getMap().keySet()) .values(insertMaps.getMap().values()); } else { throw new IllegalStateException( "The ON DUPLICATE KEY IGNORE/UPDATE clause cannot be simulated when inserting into non-updatable tables : " + getInto()); } }
@Override protected final FieldMapForInsert getValues() { return insertMaps.getMap(); }
@Override public final void newRecord() { insertMaps.newRecord(); }
@Override public final boolean isExecutable() { return insertMaps.isExecutable() || defaultValues; }
@Override public final void addValues(Map<? extends Field<?>, ?> map) { insertMaps.getMap().set(map); }