[jOOQ/jOOQ#14065] Add internal utilities to help avoid repeating

expressions
This commit is contained in:
Lukas Eder 2022-10-10 17:55:56 +02:00
parent fd5d035301
commit 2449f09b9d
3 changed files with 108 additions and 0 deletions

View File

@ -103,6 +103,8 @@ public class Settings
protected Boolean renderGroupConcatMaxLenSessionVariable = true;
@XmlElement(defaultValue = "false")
protected Boolean renderParenthesisAroundSetOperationQueries = false;
@XmlElement(defaultValue = "true")
protected Boolean renderVariablesInDerivedTablesForEmulations = true;
@XmlElement(defaultValue = ".")
protected String namePathSeparator = ".";
@XmlElement(defaultValue = "false")
@ -1036,6 +1038,32 @@ public class Settings
this.renderParenthesisAroundSetOperationQueries = value;
}
/**
* Whether emulations that require repeating expressions should render variables for those expressions in derived tables.
* <p>
* For details, see <a href="https://github.com/jOOQ/jOOQ/issues/14065">https://github.com/jOOQ/jOOQ/issues/14065</a>.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isRenderVariablesInDerivedTablesForEmulations() {
return renderVariablesInDerivedTablesForEmulations;
}
/**
* Sets the value of the renderVariablesInDerivedTablesForEmulations property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setRenderVariablesInDerivedTablesForEmulations(Boolean value) {
this.renderVariablesInDerivedTablesForEmulations = value;
}
/**
* The character(s) to be used as a separator in paths encoded in a {@link Name}
* <p>
@ -4463,6 +4491,11 @@ public class Settings
return this;
}
public Settings withRenderVariablesInDerivedTablesForEmulations(Boolean value) {
setRenderVariablesInDerivedTablesForEmulations(value);
return this;
}
/**
* The character(s) to be used as a separator in paths encoded in a {@link Name}
* <p>
@ -5525,6 +5558,7 @@ public class Settings
builder.append("renderOutputForSQLServerReturningClause", renderOutputForSQLServerReturningClause);
builder.append("renderGroupConcatMaxLenSessionVariable", renderGroupConcatMaxLenSessionVariable);
builder.append("renderParenthesisAroundSetOperationQueries", renderParenthesisAroundSetOperationQueries);
builder.append("renderVariablesInDerivedTablesForEmulations", renderVariablesInDerivedTablesForEmulations);
builder.append("namePathSeparator", namePathSeparator);
builder.append("bindOffsetDateTimeType", bindOffsetDateTimeType);
builder.append("bindOffsetTimeType", bindOffsetTimeType);
@ -5925,6 +5959,15 @@ public class Settings
return false;
}
}
if (renderVariablesInDerivedTablesForEmulations == null) {
if (other.renderVariablesInDerivedTablesForEmulations!= null) {
return false;
}
} else {
if (!renderVariablesInDerivedTablesForEmulations.equals(other.renderVariablesInDerivedTablesForEmulations)) {
return false;
}
}
if (namePathSeparator == null) {
if (other.namePathSeparator!= null) {
return false;
@ -7183,6 +7226,7 @@ public class Settings
result = ((prime*result)+((renderOutputForSQLServerReturningClause == null)? 0 :renderOutputForSQLServerReturningClause.hashCode()));
result = ((prime*result)+((renderGroupConcatMaxLenSessionVariable == null)? 0 :renderGroupConcatMaxLenSessionVariable.hashCode()));
result = ((prime*result)+((renderParenthesisAroundSetOperationQueries == null)? 0 :renderParenthesisAroundSetOperationQueries.hashCode()));
result = ((prime*result)+((renderVariablesInDerivedTablesForEmulations == null)? 0 :renderVariablesInDerivedTablesForEmulations.hashCode()));
result = ((prime*result)+((namePathSeparator == null)? 0 :namePathSeparator.hashCode()));
result = ((prime*result)+((bindOffsetDateTimeType == null)? 0 :bindOffsetDateTimeType.hashCode()));
result = ((prime*result)+((bindOffsetTimeType == null)? 0 :bindOffsetTimeType.hashCode()));

View File

@ -123,6 +123,7 @@ import static org.jooq.impl.DSL.unquotedName;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.DefaultExecuteContext.localConnection;
import static org.jooq.impl.DefaultParseContext.SUPPORTS_HASH_COMMENT_SYNTAX;
import static org.jooq.impl.DerivedTable.NO_SUPPORT_CORRELATED_DERIVED_TABLE;
import static org.jooq.impl.Identifiers.QUOTES;
import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER;
import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER_ESCAPED;
@ -279,6 +280,9 @@ import org.jooq.FieldOrRow;
import org.jooq.FieldOrRowOrSelect;
import org.jooq.Fields;
import org.jooq.ForeignKey;
import org.jooq.Function1;
import org.jooq.Function2;
import org.jooq.Function3;
import org.jooq.Generator;
import org.jooq.JSON;
import org.jooq.JSONB;
@ -308,6 +312,7 @@ import org.jooq.Scope;
import org.jooq.ContextConverter;
import org.jooq.Select;
import org.jooq.SelectFieldOrAsterisk;
import org.jooq.SelectJoinStep;
import org.jooq.SortField;
import org.jooq.Source;
import org.jooq.Table;
@ -7161,4 +7166,57 @@ final class Tools {
static final ConverterContext converterContext(Configuration configuration) {
return new DefaultConverterContext(configuration(configuration));
}
/**
* Wrap an expression in a derived table to allow for simplifying
* referencing it.
*/
static final <T1, R> Field<R> derivedTable(
Context<?> ctx,
Field<T1> f1,
Function1<? super Field<T1>, ? extends Field<R>> f
) {
if (derivedTableEnabled(ctx) && !isSimple(ctx, f1))
return DSL.field(select(f.apply(f1.as("f1"))).from(select(f1.as("f1")).asTable("t")));
else
return f.apply(f1);
}
/**
* Wrap expressions in a derived table to allow for simplifying referencing
* them.
*/
static final <T1, T2, R> Field<R> derivedTable(
Context<?> ctx,
Field<T1> f1,
Field<T2> f2,
Function2<? super Field<T1>, ? super Field<T2>, ? extends Field<R>> f
) {
if (derivedTableEnabled(ctx) && !isSimple(ctx, f1) && !isSimple(ctx, f2))
return DSL.field(select(f.apply(f1.as("f1"), f2.as("f2"))).from(select(f1.as("f1"), f2.as("f2")).asTable("t")));
else
return f.apply(f1, f2);
}
/**
* Wrap expressions in a derived table to allow for simplifying referencing
* them.
*/
static final <T1, T2, T3, R> Field<R> derivedTable(
Context<?> ctx,
Field<T1> f1,
Field<T2> f2,
Field<T3> f3,
Function3<? super Field<T1>, ? super Field<T2>, ? super Field<T3>, ? extends Field<R>> f
) {
if (derivedTableEnabled(ctx) && !isSimple(ctx, f1) && !isSimple(ctx, f2) && !isSimple(ctx, f3))
return DSL.field(select(f.apply(f1.as("f1"), f2.as("f2"), f3.as("f3"))).from(select(f1.as("f1"), f2.as("f2"), f3.as("f3")).asTable("t")));
else
return f.apply(f1, f2, f3);
}
private static boolean derivedTableEnabled(Context<?> ctx) {
return !FALSE.equals(ctx.settings().isRenderVariablesInDerivedTablesForEmulations())
&& !NO_SUPPORT_CORRELATED_DERIVED_TABLE.contains(ctx.dialect());
}
}

View File

@ -214,6 +214,12 @@ When this setting is set to <code>true</code> the queries combined with set oper
For details, see <a href="https://github.com/jOOQ/jOOQ/issues/3676">https://github.com/jOOQ/jOOQ/issues/3676</a> and <a href="https://github.com/jOOQ/jOOQ/issues/9751">https://github.com/jOOQ/jOOQ/issues/9751</a>.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderVariablesInDerivedTablesForEmulations" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether emulations that require repeating expressions should render variables for those expressions in derived tables.
<p>
For details, see <a href="https://github.com/jOOQ/jOOQ/issues/14065">https://github.com/jOOQ/jOOQ/issues/14065</a>.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="namePathSeparator" type="string" minOccurs="0" maxOccurs="1" default=".">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The character(s) to be used as a separator in paths encoded in a {@link Name}
<p>