[#4512] Allow for renaming tables and columns using Function
This commit is contained in:
parent
7dc077af3d
commit
a86a5f0c12
@ -66,6 +66,7 @@ import static org.jooq.SQLDialect.SQLITE;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.conf.Settings;
|
||||
import org.jooq.impl.DSL;
|
||||
@ -172,6 +173,32 @@ public interface Field<T> extends SelectField<T>, GroupField, FieldOrRow {
|
||||
@Support
|
||||
Field<T> as(Field<?> otherField);
|
||||
|
||||
|
||||
/**
|
||||
* Create an alias for this field.
|
||||
* <p>
|
||||
* Note that the case-sensitivity of the returned field depends on
|
||||
* {@link Settings#getRenderNameStyle()}. By default, field aliases are
|
||||
* quoted, and thus case-sensitive!
|
||||
* <p>
|
||||
* This works like {@link #as(String)}, except that field aliases are
|
||||
* provided by a function. This is useful, for instance, to prefix all
|
||||
* columns with a common prefix (on {@link Table#as(String, Function)}):
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.as("t1", f -> "prefix_" + f.getName());
|
||||
* </pre></code>
|
||||
* <p>
|
||||
* And then to use the same function also for individual fields:
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.MY_COLUMN.as(f -> "prefix_" + f.getName());
|
||||
* </pre></code>
|
||||
*/
|
||||
@Support
|
||||
Field<T> as(Function<? super Field<T>, ? extends String> aliasFunction);
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
|
||||
@ -40,6 +40,8 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.conf.Settings;
|
||||
|
||||
/**
|
||||
@ -50,7 +52,7 @@ import org.jooq.conf.Settings;
|
||||
public interface FieldLike {
|
||||
|
||||
/**
|
||||
* The underlying field representation of this object
|
||||
* The underlying field representation of this object.
|
||||
* <p>
|
||||
* This method is useful for things like
|
||||
* <code>SELECT y.*, (SELECT a FROM x) FROM y</code>
|
||||
@ -60,7 +62,7 @@ public interface FieldLike {
|
||||
<T> Field<T> asField();
|
||||
|
||||
/**
|
||||
* The underlying field representation of this object
|
||||
* The underlying field representation of this object.
|
||||
* <p>
|
||||
* This method is useful for things like
|
||||
* <code>SELECT y.*, (SELECT a FROM x) [alias] FROM y</code>
|
||||
@ -73,4 +75,33 @@ public interface FieldLike {
|
||||
*/
|
||||
<T> Field<T> asField(String alias);
|
||||
|
||||
|
||||
/**
|
||||
* The underlying field representation of this object.
|
||||
* <p>
|
||||
* This method is useful for things like
|
||||
* <code>SELECT y.*, (SELECT a FROM x) [alias] FROM y</code>
|
||||
* <p>
|
||||
* Note that the case-sensitivity of the returned field depends on
|
||||
* {@link Settings#getRenderNameStyle()}. By default, field aliases are
|
||||
* quoted, and thus case-sensitive!
|
||||
* <p>
|
||||
* This works like {@link #asField(String)}, except that field aliases are
|
||||
* provided by a function. This is useful, for instance, to prefix all
|
||||
* columns with a common prefix (on {@link Table#as(String, Function)}):
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.as("t1", f -> "prefix_" + f.getName());
|
||||
* </pre></code>
|
||||
* <p>
|
||||
* And then to use the same function also for individual fields:
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.MY_COLUMN.as(f -> "prefix_" + f.getName());
|
||||
* </pre></code>
|
||||
*/
|
||||
@Support
|
||||
<T> Field<T> asField(Function<? super Field<T>, ? extends String> aliasFunction);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -67,6 +67,7 @@ import static org.jooq.SQLDialect.SQLITE;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.conf.Settings;
|
||||
import org.jooq.impl.DSL;
|
||||
@ -292,7 +293,7 @@ public interface Table<R extends Record> extends TableLike<R> {
|
||||
Table<R> as(String alias);
|
||||
|
||||
/**
|
||||
* Create an alias for this table and its fields
|
||||
* Create an alias for this table and its fields.
|
||||
* <p>
|
||||
* Note that the case-sensitivity of the returned table and columns depends
|
||||
* on {@link Settings#getRenderNameStyle()}. By default, table aliases are
|
||||
@ -345,6 +346,26 @@ public interface Table<R extends Record> extends TableLike<R> {
|
||||
@Support
|
||||
Table<R> as(String alias, String... fieldAliases);
|
||||
|
||||
|
||||
/**
|
||||
* Create an alias for this table and its fields.
|
||||
* <p>
|
||||
* This works like {@link #as(String, String...)}, except that field aliases
|
||||
* are provided by a function. This is useful, for instance, to prefix all
|
||||
* columns with a common prefix:
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.as("t1", f -> "prefix_" + f.getName());
|
||||
* </pre></code>
|
||||
*
|
||||
* @param alias The alias name
|
||||
* @param aliasFunction The function providing field aliases.
|
||||
* @return The table alias
|
||||
*/
|
||||
@Support
|
||||
Table<R> as(String alias, Function<? super Field<?>, ? extends String> aliasFunction);
|
||||
|
||||
|
||||
/**
|
||||
* Create an alias for this table based on another table's name.
|
||||
* <p>
|
||||
@ -373,6 +394,26 @@ public interface Table<R extends Record> extends TableLike<R> {
|
||||
@Support
|
||||
Table<R> as(Table<?> otherTable, Field<?>... otherFields);
|
||||
|
||||
|
||||
/**
|
||||
* Create an alias for this table and its fields.
|
||||
* <p>
|
||||
* This works like {@link #as(String, String...)}, except that field aliases
|
||||
* are provided by a function. This is useful, for instance, to prefix all
|
||||
* columns with a common prefix:
|
||||
* <p>
|
||||
* <code><pre>
|
||||
* MY_TABLE.as(MY_OTHER_TABLE, f -> MY_OTHER_TABLE.field(f));
|
||||
* </pre></code>
|
||||
*
|
||||
* @param alias The alias name
|
||||
* @param aliasFunction The function providing field aliases.
|
||||
* @return The table alias
|
||||
*/
|
||||
@Support
|
||||
Table<R> as(Table<?> otherTable, Function<? super Field<?>, ? extends Field<?>> aliasFunction);
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX: JOIN clauses on tables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -40,6 +40,8 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* An object that can behave like a table (a table-like object)
|
||||
*
|
||||
@ -192,4 +194,15 @@ public interface TableLike<R extends Record> extends QueryPart {
|
||||
*/
|
||||
@Support
|
||||
Table<R> asTable(String alias, String... fieldAliases);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The underlying aliased table representation of this object
|
||||
*
|
||||
* @see Table#as(String, Function)
|
||||
*/
|
||||
@Support
|
||||
Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction);
|
||||
|
||||
}
|
||||
|
||||
@ -77,6 +77,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.BetweenAndStep;
|
||||
import org.jooq.Binding;
|
||||
@ -155,6 +156,11 @@ abstract class AbstractField<T> extends AbstractQueryPart implements Field<T> {
|
||||
return as(otherField.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Field<T> as(Function<? super Field<T>, ? extends String> aliasFunction) {
|
||||
return as(aliasFunction.apply(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getName() {
|
||||
return name;
|
||||
|
||||
@ -64,6 +64,8 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.jooq.Binding;
|
||||
import org.jooq.Catalog;
|
||||
@ -266,6 +268,18 @@ abstract class AbstractTable<R extends Record> extends AbstractQueryPart impleme
|
||||
return as(alias, fieldAliases);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
|
||||
return as(alias, aliasFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Table<R> as(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
|
||||
return as(alias, Stream.of(fields()).map(aliasFunction).toArray(String[]::new));
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// XXX: Table API
|
||||
// ------------------------------------------------------------------------
|
||||
@ -621,6 +635,13 @@ abstract class AbstractTable<R extends Record> extends AbstractQueryPart impleme
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Table<R> as(Table<?> otherTable, Function<? super Field<?>, ? extends Field<?>> aliasFunction) {
|
||||
return as(otherTable.getName(), f -> aliasFunction.apply(f).getName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -57,6 +57,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
@ -3640,6 +3641,13 @@ final class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10
|
||||
return getDelegate().asTable(alias, fieldAliases);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
|
||||
return getDelegate().asTable(alias, aliasFunction);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final <T> Field<T> asField() {
|
||||
return getDelegate().asField();
|
||||
@ -3650,6 +3658,13 @@ final class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10
|
||||
return getDelegate().asField(alias);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final <T> Field<T> asField(Function<? super Field<T>, ? extends String> aliasFunction) {
|
||||
return getDelegate().asField(aliasFunction);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Row fieldsRow() {
|
||||
return getDelegate().fieldsRow();
|
||||
|
||||
@ -117,6 +117,7 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Condition;
|
||||
@ -266,6 +267,13 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
return this.<T> asField().as(alias);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <T> Field<T> asField(Function<? super Field<T>, ? extends String> aliasFunction) {
|
||||
return this.<T> asField().as(aliasFunction);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Row fieldsRow() {
|
||||
return asTable().fieldsRow();
|
||||
@ -363,6 +371,13 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
return new DerivedTable<R>(this).as(alias, fieldAliases);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
|
||||
return new DerivedTable<R>(this).as(alias, aliasFunction);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected final Field<?>[] getFields(ResultSetMetaData meta) {
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user