[#8163] Deprecate synonym keywords and use suffix underscore instead

This commit is contained in:
lukaseder 2019-01-04 09:54:07 +01:00
parent 541dd6d245
commit ea5af2f9d6
8 changed files with 167 additions and 37 deletions

View File

@ -5525,10 +5525,10 @@ ORDER BY CASE TITLE
]]>&#160;</sql>
<java><![CDATA[create.select()
.from(BOOK)
.orderBy(choose(BOOK.TITLE)
.orderBy(case_(BOOK.TITLE)
.when("1984", 0)
.when("Animal Farm", 1)
.otherwise(2).asc())
.else_(2).asc())
.fetch();]]></java></code-pair><html>
<p>
@ -8661,18 +8661,18 @@ END]]></sql>
<java><![CDATA[DSL
.when(AUTHOR.FIRST_NAME.eq("Paulo"), "brazilian")
.when(AUTHOR.FIRST_NAME.eq("George"), "english")
.otherwise("unknown");
.else_("unknown");
// OR:
DSL.choose(AUTHOR.FIRST_NAME)
DSL.case_(AUTHOR.FIRST_NAME)
.when("Paulo", "brazilian")
.when("George", "english")
.otherwise("unknown");]]></java>
.else_("unknown");]]></java>
</code-pair><html>
<p>
In jOOQ, both syntaxes are supported (The second one is emulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle <code>DECODE</code> function, or choose(), and otherwise(), which means the same as else.
In jOOQ, both syntaxes are supported (The second one is emulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle <code>DECODE</code> function, or choose() / case_(), and otherwise() / else_().
</p>
<p>
@ -20753,18 +20753,18 @@ WHEN_MATCHED_THEN_UPDATE()</java>
</ul>
<p>
jOOQ replaces those keywords by "synonyms":
jOOQ uses a suffix on those keywords to prevent a collision:
</p>
</html><code-pair>
<sql>CASE .. ELSE
PIVOT .. FOR .. IN ..</sql>
<java>decode() .. otherwise()
pivot(..).on(..).in(..)</java>
<java>case_() .. else_()
pivot(..).for_(..).in(..)</java>
</code-pair><html>
<p>
There is more future collision potential with:
There is more future collision potential with, each resolved with a suffix:
</p>
<ul>
<li><code>BOOLEAN</code></li>
@ -20896,7 +20896,7 @@ for (Record record : create.select(
.and(PARAMETERS.SPECIFIC_NAME.eq(r1.SPECIFIC_NAME))
.and(upper(PARAMETERS.PARAMETER_MODE).ne("IN"))),
val("void"))
.otherwise(r1.DATA_TYPE).as("data_type"),
.else_(r1.DATA_TYPE).as("data_type"),
r1.CHARACTER_MAXIMUM_LENGTH,
r1.NUMERIC_PRECISION,
r1.NUMERIC_SCALE,

View File

@ -110,4 +110,31 @@ public interface CaseConditionStep<T> extends Field<T> {
*/
@Support
Field<T> otherwise(Select<? extends Record1<T>> result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> else_(T result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> else_(Field<T> result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> else_(Select<? extends Record1<T>> result);
}

View File

@ -135,4 +135,22 @@ public interface CaseWhenStep<V, T> extends Field<T> {
*/
@Support
Field<T> otherwise(Field<T> result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> else_(T result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> else_(Field<T> result);
}

View File

@ -77,5 +77,12 @@ public interface PivotForStep {
}

View File

@ -70,7 +70,7 @@ final class CaseConditionStepImpl<T> extends AbstractFunction<T> implements Case
private final List<Condition> conditions;
private final List<Field<T>> results;
private Field<T> otherwise;
private Field<T> else_;
CaseConditionStepImpl(Condition condition, Field<T> result) {
super("case", result.getDataType());
@ -101,19 +101,34 @@ final class CaseConditionStepImpl<T> extends AbstractFunction<T> implements Case
@Override
public final Field<T> otherwise(T result) {
return otherwise(Tools.field(result));
return else_(result);
}
@Override
public final Field<T> otherwise(Field<T> result) {
this.otherwise = result;
return else_(result);
}
@Override
public final Field<T> otherwise(Select<? extends Record1<T>> result) {
return else_(result);
}
@Override
public final Field<T> else_(T result) {
return else_(Tools.field(result));
}
@Override
public final Field<T> else_(Field<T> result) {
this.else_ = result;
return this;
}
@Override
public final Field<T> otherwise(Select<? extends Record1<T>> result) {
return otherwise(DSL.field(result));
public final Field<T> else_(Select<? extends Record1<T>> result) {
return else_(DSL.field(result));
}
@Override
@ -201,9 +216,9 @@ final class CaseConditionStepImpl<T> extends AbstractFunction<T> implements Case
.visit(K_THEN).sql(' ').visit(results.get(i));
}
if (otherwise != null)
if (else_ != null)
ctx.formatSeparator()
.visit(K_ELSE).sql(' ').visit(otherwise);
.visit(K_ELSE).sql(' ').visit(else_);
ctx.formatIndentEnd()
.formatSeparator()

View File

@ -68,7 +68,7 @@ final class CaseWhenStepImpl<V, T> extends AbstractFunction<T> implements CaseWh
private final Field<V> value;
private final List<Field<V>> compareValues;
private final List<Field<T>> results;
private Field<T> otherwise;
private Field<T> else_;
CaseWhenStepImpl(Field<V> value, Field<V> compareValue, Field<T> result) {
this(value, result.getDataType());
@ -102,12 +102,22 @@ final class CaseWhenStepImpl<V, T> extends AbstractFunction<T> implements CaseWh
@Override
public final Field<T> otherwise(T result) {
return otherwise(Tools.field(result));
return else_(result);
}
@Override
public final Field<T> otherwise(Field<T> result) {
this.otherwise = result;
return else_(result);
}
@Override
public final Field<T> else_(T result) {
return else_(Tools.field(result));
}
@Override
public final Field<T> else_(Field<T> result) {
this.else_ = result;
return this;
}
@ -263,14 +273,14 @@ final class CaseWhenStepImpl<V, T> extends AbstractFunction<T> implements CaseWh
}
}
if (otherwise != null)
if (else_ != null)
ctx.formatSeparator()
.visit(K_ELSE).sql(' ')
.visit(otherwise);
.visit(else_);
ctx.formatIndentEnd();
if (size > 1 || otherwise != null)
if (size > 1 || else_ != null)
ctx.formatSeparator();
else
ctx.sql(' ');

View File

@ -12028,28 +12028,84 @@ public class DSL {
return decode().value(value);
}
/**
* The T-SQL <code>CHOOSE()</code> function.
*/
@Support
public static <T> Field<T> choose(int index, T... values) {
return choose(val(index), (Field<T>[]) Tools.fields(values).toArray(EMPTY_FIELD));
}
/**
* The T-SQL <code>CHOOSE()</code> function.
*/
@Support
@SafeVarargs
public static <T> Field<T> choose(int index, Field<T>... values) {
return choose(val(index), values);
}
/**
* The T-SQL <code>CHOOSE()</code> function.
*/
@Support
public static <T> Field<T> choose(Field<Integer> index, T... values) {
return choose(index, (Field<T>[]) Tools.fields(values).toArray(EMPTY_FIELD));
}
/**
* The T-SQL <code>CHOOSE()</code> function.
*/
@Support
@SafeVarargs
public static <T> Field<T> choose(Field<Integer> index, Field<T>... values) {
return new Choose<T>(index, values);
}
/**
* Initialise a {@link Case} statement.
*
* @see Case
*/
@Support
public static Case case_() {
return decode();
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE value WHEN 1 THEN 'one'
* WHEN 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
*
* @see Case
*/
@Support
public static <V> CaseValueStep<V> case_(V value) {
return decode().value(value);
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE value WHEN 1 THEN 'one'
* WHEN 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
*
* @see Case
*/
@Support
public static <V> CaseValueStep<V> case_(Field<V> value) {
return decode().value(value);
}
/**
* Initialise a {@link Case} statement.
* <p>
@ -12059,9 +12115,6 @@ public class DSL {
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, T result) {
@ -12077,9 +12130,6 @@ public class DSL {
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, Field<T> result) {
@ -12095,9 +12145,6 @@ public class DSL {
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result) {

View File

@ -83,7 +83,7 @@ implements
private final Table<?> table;
private final SelectFieldList<Field<?>> aggregateFunctions;
private Field<T> on;
private Field<T> for_;
private SelectFieldList<Field<?>> in;
Pivot(Table<?> table, Field<?>... aggregateFunctions) {
@ -170,7 +170,7 @@ implements
// This loop finds all fields qualify for GROUP BY clauses
for (Field<?> field : table.fields()) {
if (!aggregatedFields.contains(field)) {
if (!on.equals(field)) {
if (!for_.equals(field)) {
aliasedGroupingFields.add(pivot.field(field));
groupingFields.add(field);
}
@ -190,7 +190,7 @@ implements
Select<?> aggregateSelect = using(configuration)
.select(aggregateFunction)
.from(table)
.where(on.equal((Field<T>) inField))
.where(for_.equal((Field<T>) inField))
.and(join);
aggregationSelects.add(aggregateSelect.asField(inField.getName() + "_" + aggregateFunction.getName()));
@ -203,7 +203,7 @@ implements
.select(aliasedGroupingFields)
.select(aggregationSelects)
.from(pivot)
.where(pivot.field(on).in(in.toArray(EMPTY_FIELD)))
.where(pivot.field(for_).in(in.toArray(EMPTY_FIELD)))
.groupBy(aliasedGroupingFields)
.asTable();
@ -340,6 +340,12 @@ implements