[jOOQ/jOOQ#11338] Add convenience for common bracing / formatting / indenting SQL rendering

This commit is contained in:
Lukas Eder 2021-01-29 14:52:48 +01:00
parent a0449b67a4
commit cda7b1bc42
20 changed files with 105 additions and 110 deletions

View File

@ -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}.
*/

View File

@ -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;

View File

@ -591,10 +591,6 @@ abstract class AbstractDMLQuery<R extends Record> extends AbstractRowCountQuery

View File

@ -872,7 +872,6 @@ public abstract class AbstractRoutine<T> extends AbstractNamed implements Routin
}

View File

@ -137,10 +137,6 @@ package org.jooq.impl;

View File

@ -442,10 +442,6 @@ final class BlockImpl extends AbstractRowCountQuery implements Block {

View File

@ -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(')');
}
}
}

View File

@ -253,6 +253,8 @@ package org.jooq.impl;

View File

@ -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

View File

@ -71,10 +71,6 @@ package org.jooq.impl;

View File

@ -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();

View File

@ -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;

View File

@ -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;
}

View File

@ -165,10 +165,6 @@ final class JSONObject<J> extends AbstractField<J> implements JSONObjectNullStep

View File

@ -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) {

View File

@ -390,16 +390,12 @@ implements
);
if (wrap)
ctx.sql('(')
.formatIndentStart()
.formatNewLine();
ctx.sqlIndentStart('(');
ctx.visit(table);
if (wrap)
ctx.formatIndentEnd()
.formatNewLine()
.sql(')');
ctx.sqlIndentEnd(')');
}
/**

View File

@ -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

View File

@ -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(')');
}
}

View File

@ -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;
}
}
}

View File

@ -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) {