[jOOQ/jOOQ#11338] Add convenience for common bracing / formatting / indenting SQL rendering
This commit is contained in:
parent
a0449b67a4
commit
cda7b1bc42
@ -373,12 +373,44 @@ public interface Context<C extends Context<C>> extends Scope {
|
||||
@NotNull
|
||||
C sql(String sql, boolean literal);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder},
|
||||
* followed by the usual calls to {@link #formatIndentStart()} and
|
||||
* {@link #formatNewLine()}.
|
||||
*/
|
||||
@NotNull
|
||||
C sqlIndentStart(String sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder} preceded
|
||||
* by the usual calls to {@link #formatIndentEnd()} and
|
||||
* {@link #formatNewLine()}.
|
||||
*/
|
||||
@NotNull
|
||||
C sqlIndentEnd(String sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder}.
|
||||
*/
|
||||
@NotNull
|
||||
C sql(char sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder},
|
||||
* followed by the usual calls to {@link #formatIndentStart()} and
|
||||
* {@link #formatNewLine()}.
|
||||
*/
|
||||
@NotNull
|
||||
C sqlIndentStart(char sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder} preceded
|
||||
* by the usual calls to {@link #formatIndentEnd()} and
|
||||
* {@link #formatNewLine()}.
|
||||
*/
|
||||
@NotNull
|
||||
C sqlIndentEnd(char sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder}.
|
||||
*/
|
||||
|
||||
@ -50,6 +50,8 @@ import org.jooq.QueryPart;
|
||||
import org.jooq.QueryPartInternal;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* A base class for {@link BindContext} implementations
|
||||
*
|
||||
@ -167,11 +169,31 @@ abstract class AbstractBindContext extends AbstractContext<BindContext> implemen
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sqlIndentStart(String sql) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sqlIndentEnd(String sql) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sql(char sql) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sqlIndentStart(char sql) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sqlIndentEnd(char sql) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BindContext sql(int sql) {
|
||||
return this;
|
||||
|
||||
@ -591,10 +591,6 @@ abstract class AbstractDMLQuery<R extends Record> extends AbstractRowCountQuery
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -872,7 +872,6 @@ public abstract class AbstractRoutine<T> extends AbstractNamed implements Routin
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -137,10 +137,6 @@ package org.jooq.impl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -442,10 +442,6 @@ final class BlockImpl extends AbstractRowCountQuery implements Block {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -199,9 +199,7 @@ final class CombinedCondition extends AbstractCondition {
|
||||
ctx.visit(conditions.get(0));
|
||||
}
|
||||
else {
|
||||
ctx.sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.sqlIndentStart('(');
|
||||
|
||||
Keyword op = operator == AND ? K_AND : K_OR;
|
||||
Keyword separator = null;
|
||||
@ -216,9 +214,7 @@ final class CombinedCondition extends AbstractCondition {
|
||||
separator = op;
|
||||
}
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -253,6 +253,8 @@ package org.jooq.impl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -447,10 +447,8 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
|
||||
|
||||
if (!columnFields.isEmpty()
|
||||
&& (select == null || !NO_SUPPORT_CTAS_COLUMN_NAMES.contains(ctx.dialect()))) {
|
||||
ctx.sql(" (")
|
||||
.start(CREATE_TABLE_COLUMNS)
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.sqlIndentStart(" (")
|
||||
.start(CREATE_TABLE_COLUMNS);
|
||||
|
||||
Field<?> identity = null;
|
||||
boolean qualify = ctx.qualify();
|
||||
@ -534,9 +532,7 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
|
||||
ctx.qualify(qualify);
|
||||
}
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
}
|
||||
|
||||
@ -601,9 +597,7 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
|
||||
.visit(K_AS);
|
||||
|
||||
if (WRAP_SELECT_IN_PARENS.contains(ctx.dialect()))
|
||||
ctx.sql(" (")
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.sqlIndentStart(" (");
|
||||
else
|
||||
ctx.formatSeparator();
|
||||
|
||||
@ -623,9 +617,7 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
|
||||
ctx.data().remove(DATA_SELECT_NO_DATA);
|
||||
|
||||
if (WRAP_SELECT_IN_PARENS.contains(ctx.dialect())) {
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
|
||||
if (FALSE.equals(withData) && !NO_SUPPORT_WITH_DATA.contains(ctx.dialect()))
|
||||
@ -667,11 +659,6 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -71,10 +71,6 @@ package org.jooq.impl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -86,6 +86,8 @@ import org.jooq.impl.Tools.DataKey;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.StringUtils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
@ -362,6 +364,16 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sqlIndentStart(String c) {
|
||||
return sql(c).formatIndentStart().formatNewLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sqlIndentEnd(String c) {
|
||||
return formatIndentEnd().formatNewLine().sql(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sql(char c) {
|
||||
applyNewLine();
|
||||
@ -374,6 +386,16 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sqlIndentStart(char c) {
|
||||
return sql(c).formatIndentStart().formatNewLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sqlIndentEnd(char c) {
|
||||
return formatIndentEnd().formatNewLine().sql(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sql(int i) {
|
||||
applyNewLine();
|
||||
|
||||
@ -144,11 +144,11 @@ final class Dual extends AbstractTable<Record> {
|
||||
break;
|
||||
|
||||
case HSQLDB:
|
||||
ctx.sql('(').formatIndentStart().formatNewLine()
|
||||
ctx.sqlIndentStart('(')
|
||||
.visit(K_SELECT).sql(" 1 ").visit(K_AS).sql(' ').visit(N_DUAL).formatSeparator()
|
||||
.visit(K_FROM).sql(' ').visit(DUAL_HSQLDB).formatSeparator()
|
||||
.visit(K_LIMIT).sql(" 1").formatIndentEnd().formatNewLine()
|
||||
.sql(") ").visit(K_AS).sql(' ').visit(N_DUAL);
|
||||
.visit(K_LIMIT).sql(" 1")
|
||||
.sqlIndentEnd(") ").visit(K_AS).sql(' ').visit(N_DUAL);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
@ -158,9 +158,7 @@ final class InCondition<T> extends AbstractCondition {
|
||||
|
||||
|
||||
case FIREBIRD: {
|
||||
ctx.sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.sqlIndentStart('(');
|
||||
|
||||
for (int i = 0; i < values.size(); i += IN_LIMIT) {
|
||||
if (i > 0) {
|
||||
@ -182,9 +180,7 @@ final class InCondition<T> extends AbstractCondition {
|
||||
toSQLSubValues(ctx, padded(ctx, values.subList(i, Math.min(i + IN_LIMIT, values.size()))));
|
||||
}
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -165,10 +165,6 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -280,9 +280,7 @@ implements
|
||||
|
||||
|
||||
private final void acceptStandard(Context<?> ctx) {
|
||||
ctx.visit(K_JSON_TABLE).sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.visit(K_JSON_TABLE).sqlIndentStart('(');
|
||||
|
||||
ctx.visit(json).sql(',').formatSeparator();
|
||||
acceptJSONPath(ctx);
|
||||
@ -294,9 +292,7 @@ implements
|
||||
|
||||
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
|
||||
private final void acceptJSONPath(Context<?> ctx) {
|
||||
|
||||
@ -390,16 +390,12 @@ implements
|
||||
);
|
||||
|
||||
if (wrap)
|
||||
ctx.sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
ctx.sqlIndentStart('(');
|
||||
|
||||
ctx.visit(table);
|
||||
|
||||
if (wrap)
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1319,9 +1319,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
|
||||
Boolean wrapDerivedTables = (Boolean) context.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
|
||||
if (TRUE.equals(wrapDerivedTables)) {
|
||||
context.sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine()
|
||||
context.sqlIndentStart('(')
|
||||
.data().remove(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
|
||||
}
|
||||
|
||||
@ -1511,9 +1509,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
.sql(option);
|
||||
|
||||
if (TRUE.equals(wrapDerivedTables))
|
||||
context.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')')
|
||||
context.sqlIndentEnd(')')
|
||||
.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true);
|
||||
|
||||
|
||||
@ -1639,17 +1635,13 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
ctx.visit(K_SELECT).separatorRequired(true)
|
||||
.declareFields(true, c -> c.visit(new SelectFieldList<>(unaliasedFields)))
|
||||
.formatSeparator()
|
||||
.visit(K_FROM).sql(" (")
|
||||
.formatIndentStart()
|
||||
.formatNewLine()
|
||||
.visit(K_FROM).sqlIndentStart(" (")
|
||||
.subquery(true);
|
||||
|
||||
toSQLReference0(ctx, originalFields, alternativeFields);
|
||||
|
||||
ctx.subquery(false)
|
||||
.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(") ")
|
||||
.sqlIndentEnd(") ")
|
||||
.visit(name("x"))
|
||||
.formatSeparator()
|
||||
.visit(K_WHERE).sql(' ')
|
||||
@ -1737,14 +1729,6 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -111,15 +111,9 @@ final class XMLExists extends AbstractCondition implements XMLExistsPassingStep
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
ctx.visit(K_XMLEXISTS).sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
|
||||
ctx.visit(K_XMLEXISTS).sqlIndentStart('(');
|
||||
acceptXPath(ctx, xpath);
|
||||
acceptPassing(ctx, passing, passingMechanism);
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
}
|
||||
|
||||
@ -137,10 +137,7 @@ final class XMLQuery extends AbstractField<XML> implements XMLQueryPassingStep {
|
||||
break;
|
||||
|
||||
default:
|
||||
ctx.visit(N_XMLQUERY).sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
|
||||
ctx.visit(N_XMLQUERY).sqlIndentStart('(');
|
||||
acceptXPath(ctx, xpath);
|
||||
acceptPassing(ctx, passing, passingMechanism);
|
||||
|
||||
@ -149,11 +146,8 @@ final class XMLQuery extends AbstractField<XML> implements XMLQueryPassingStep {
|
||||
|
||||
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
|
||||
break;
|
||||
ctx.sqlIndentEnd(')');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,10 +279,7 @@ implements
|
||||
|
||||
|
||||
private final void acceptStandard(Context<?> ctx) {
|
||||
ctx.visit(K_XMLTABLE).sql('(')
|
||||
.formatIndentStart()
|
||||
.formatNewLine();
|
||||
|
||||
ctx.visit(K_XMLTABLE).sqlIndentStart('(');
|
||||
acceptXPath(ctx, xpath);
|
||||
if (passing != null)
|
||||
acceptPassing(ctx, passing, passingMechanism);
|
||||
@ -290,9 +287,7 @@ implements
|
||||
ctx.formatSeparator()
|
||||
.visit(K_COLUMNS).separatorRequired(true).visit(columns);
|
||||
|
||||
ctx.formatIndentEnd()
|
||||
.formatNewLine()
|
||||
.sql(')');
|
||||
ctx.sqlIndentEnd(')');
|
||||
}
|
||||
|
||||
static final void acceptXPath(Context<?> ctx, Field<String> xpath) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user