[#8168] Add support for procedural blocks that do not generate BEGIN .. END

This commit is contained in:
lukaseder 2019-01-07 12:20:31 +01:00
parent 84c6f43630
commit c7d1366438
6 changed files with 110 additions and 22 deletions

View File

@ -753,6 +753,28 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, MARIADB, POSTGRES })
Block begin(Collection<? extends Statement> statements);
// -------------------------------------------------------------------------
// XXX Plain SQL API
// -------------------------------------------------------------------------

View File

@ -90,12 +90,15 @@ final class BlockImpl extends AbstractQuery implements Block {
private final Collection<? extends Statement> statements;
BlockImpl(Configuration configuration, Collection<? extends Statement> statements) {
private final Collection<? extends Statement> statements;
private final boolean alwaysWrapInBeginEnd;
BlockImpl(Configuration configuration, Collection<? extends Statement> statements, boolean alwaysWrapInBeginEnd) {
super(configuration);
this.statements = statements;
this.alwaysWrapInBeginEnd = alwaysWrapInBeginEnd;
}
@Override
@ -167,18 +170,27 @@ final class BlockImpl extends AbstractQuery implements Block {
default: {
increment(ctx.data(), DATA_BLOCK_NESTING);
accept0(ctx);
decrement(ctx.data(), DATA_BLOCK_NESTING);
break;
}
}
}
private final void accept0(Context<?> ctx) {
accept1(ctx, new ArrayList<Statement>(statements));
boolean wrapInBeginEnd =
alwaysWrapInBeginEnd
;
accept1(ctx, new ArrayList<Statement>(statements), wrapInBeginEnd);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static final void accept1(Context<?> ctx, List<Statement> statements) {
private static final void accept1(Context<?> ctx, List<Statement> statements, boolean wrapInBeginEnd) {
@ -221,7 +233,9 @@ final class BlockImpl extends AbstractQuery implements Block {
accept2(ctx, statements);
accept2(ctx, statements, wrapInBeginEnd);
}
private static final void semicolonAfterStatement(Context<?> ctx, Statement s) {
@ -241,13 +255,15 @@ final class BlockImpl extends AbstractQuery implements Block {
private static final void accept2(Context<?> ctx, List<Statement> statements) {
ctx.visit(K_BEGIN);
private static final void accept2(Context<?> ctx, List<Statement> statements, boolean wrapInBeginEnd) {
if (wrapInBeginEnd) {
ctx.visit(K_BEGIN);
if (ctx.family() == MARIADB)
ctx.sql(' ').visit(K_NOT).sql(' ').visit(K_ATOMIC);
if (ctx.family() == MARIADB)
ctx.sql(' ').visit(K_NOT).sql(' ').visit(K_ATOMIC);
ctx.formatIndentStart();
ctx.formatIndentStart();
}
if (statements.isEmpty()) {
switch (ctx.family()) {
@ -305,13 +321,14 @@ final class BlockImpl extends AbstractQuery implements Block {
}
}
ctx.formatIndentEnd()
.formatSeparator()
.visit(K_END);
if (wrapInBeginEnd) {
ctx.formatIndentEnd()
.formatSeparator()
.visit(K_END);
switch (ctx.family()) {
case FIREBIRD:
break;
switch (ctx.family()) {
case FIREBIRD:
break;
@ -319,8 +336,9 @@ final class BlockImpl extends AbstractQuery implements Block {
default:
ctx.sql(';');
default:
ctx.sql(';');
}
}
}

View File

@ -9761,6 +9761,28 @@ public class DSL {

View File

@ -788,9 +788,23 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
@Override
public Block begin(Collection<? extends Statement> statements) {
return new BlockImpl(configuration(), statements);
return new BlockImpl(configuration(), statements, true);
}
// -------------------------------------------------------------------------
// XXX Plain SQL API
// -------------------------------------------------------------------------

View File

@ -145,9 +145,6 @@ package org.jooq.impl;

View File

@ -4600,6 +4600,21 @@ final class Tools {
return null;
}
/**
* Whether a counter is currently at the top level or not.
*
* @see #increment(Map, DataKey)
* @see #decrement(Map, DataKey)
*/
static final boolean toplevel(Map<Object, Object> data, DataKey key) {
Integer updateCounts = (Integer) data.get(key);
if (updateCounts == null)
throw new IllegalStateException();
else
return updateCounts == 1;
}
/**
* Increment a counter and return true if the counter was zero prior to
* incrementing.