Skip to content

Commit

Permalink
[#5487] Add Table.as(String, BiFunction<? super Field<?>, ? super Int…
Browse files Browse the repository at this point in the history
…eger, ? extends String>) to allow for "functional aliasing" (with column index)
  • Loading branch information
lukaseder committed Aug 9, 2016
1 parent b7e29ba commit 4555a6d
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 57 deletions.
123 changes: 119 additions & 4 deletions jOOQ/src/main/java/org/jooq/DSLContext.java
Expand Up @@ -77,6 +77,7 @@
import java.util.Properties;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -3397,6 +3398,25 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, HSQLDB, POSTGRES })
WithAsStep with(String alias, Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Create a <code>WITH</code> clause to supply subsequent
* <code>SELECT</code>, <code>UPDATE</code>, <code>INSERT</code>,
* <code>DELETE</code>, and <code>MERGE</code> statements with
* {@link CommonTableExpression}s.
* <p>
* The <code>RECURSIVE</code> keyword may be optional or unsupported in some
* databases, in case of which it will not be rendered. For optimal database
* interoperability and readability, however, it is suggested that you use
* {@link #with(String, String...)} for strictly non-recursive CTE and
* {@link #withRecursive(String, String...)} for strictly recursive CTE.
* <p>
* This works in a similar way as {@link #with(String, String...)}, except
* that all column names are produced by a function that receives the CTE's
* {@link Select} columns and their column indexes as input.
*/
@Support({ FIREBIRD, HSQLDB, POSTGRES })
WithAsStep with(String alias, BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);


// [jooq-tools] START [with]

Expand Down Expand Up @@ -3862,6 +3882,29 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, HSQLDB, POSTGRES })
WithAsStep withRecursive(String alias, Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Create a <code>WITH</code> clause to supply subsequent
* <code>SELECT</code>, <code>UPDATE</code>, <code>INSERT</code>,
* <code>DELETE</code>, and <code>MERGE</code> statements with
* {@link CommonTableExpression}s.
* <p>
* The <code>RECURSIVE</code> keyword may be optional or unsupported in some
* databases, in case of which it will not be rendered. For optimal database
* interoperability and readability, however, it is suggested that you use
* {@link #with(String, String...)} for strictly non-recursive CTE
* and {@link #withRecursive(String, String...)} for strictly
* recursive CTE.
* <p>
* Note that the {@link SQLDialect#H2} database only supports single-table,
* <code>RECURSIVE</code> common table expression lists.
* <p>
* This works in a similar way as {@link #with(String, String...)}, except
* that all column names are produced by a function that receives the CTE's
* {@link Select} columns and their column indexes as input.
*/
@Support({ FIREBIRD, HSQLDB, POSTGRES })
WithAsStep withRecursive(String alias, BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);


// [jooq-tools] START [with-recursive]

Expand Down Expand Up @@ -7310,6 +7353,30 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support
CreateViewAsStep<Record> createView(String view, Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createView(String, String...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createView(String, String...)
*/
@Support
CreateViewAsStep<Record> createView(String view, BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createView(Name, Name...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createView(String, String...)
*/
@Support
CreateViewAsStep<Record> createView(Name view, Function<? super Field<?>, ? extends Name> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
Expand All @@ -7320,7 +7387,19 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#createView(String, String...)
*/
@Support
CreateViewAsStep<Record> createView(Name view, Function<? super Field<?>, ? extends String> fieldNameFunction);
CreateViewAsStep<Record> createView(Name view, BiFunction<? super Field<?>, ? super Integer, ? extends Name> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createView(Table, Field...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createView(String, String...)
*/
@Support
CreateViewAsStep<Record> createView(Table<?> view, Function<? super Field<?>, ? extends Field<?>> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
Expand All @@ -7332,7 +7411,7 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#createView(String, String...)
*/
@Support
CreateViewAsStep<Record> createView(Table<?> view, Function<? super Field<?>, ? extends String> fieldNameFunction);
CreateViewAsStep<Record> createView(Table<?> view, BiFunction<? super Field<?>, ? super Integer, ? extends Field<?>> fieldNameFunction);


/**
Expand Down Expand Up @@ -7372,6 +7451,30 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(String view, Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createViewIfNotExists(String, String...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createViewIfNotExists(String, String...)
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(String view, BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createViewIfNotExists(Name, Name...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createViewIfNotExists(String, String...)
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(Name view, Function<? super Field<?>, ? extends Name> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
Expand All @@ -7382,7 +7485,19 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#createViewIfNotExists(String, String...)
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(Name view, Function<? super Field<?>, ? extends String> fieldNameFunction);
CreateViewAsStep<Record> createViewIfNotExists(Name view, BiFunction<? super Field<?>, ? super Integer, ? extends Name> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
* <p>
* This works like {@link #createViewIfNotExists(Table, Field...)} except that the
* view's field names are derived from the view's {@link Select} statement
* using a function.
*
* @see DSL#createViewIfNotExists(String, String...)
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(Table<?> view, Function<? super Field<?>, ? extends Field<?>> fieldNameFunction);

/**
* Create a new DSL <code>CREATE VIEW</code> statement.
Expand All @@ -7394,7 +7509,7 @@ public interface DSLContext extends Scope , AutoCloseable {
* @see DSL#createViewIfNotExists(String, String...)
*/
@Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
CreateViewAsStep<Record> createViewIfNotExists(Table<?> view, Function<? super Field<?>, ? extends String> fieldNameFunction);
CreateViewAsStep<Record> createViewIfNotExists(Table<?> view, BiFunction<? super Field<?>, ? super Integer, ? extends Field<?>> fieldNameFunction);


/**
Expand Down
18 changes: 17 additions & 1 deletion jOOQ/src/main/java/org/jooq/Name.java
Expand Up @@ -54,6 +54,7 @@
// ...
// ...

import java.util.function.BiFunction;
import java.util.function.Function;

import javax.annotation.Generated;
Expand Down Expand Up @@ -121,11 +122,26 @@ public interface Name extends QueryPart {
* <p>
* This works in a similar way as {@link #fields(String...)}, except
* that all column names are produced by a function that receives the CTE's
* {@link Select} columns as input.
* {@link Select} columns and their column indexes as input.
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
DerivedColumnList fields(Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Add a list of fields to this name to make this name a
* {@link DerivedColumnList}.
* <p>
* The <code>DerivedColumnList</code> can then be used along with a
* subselect to form a {@link CommonTableExpression} to be used with
* <code>WITH</code> clauses.
* <p>
* This works in a similar way as {@link #fields(String...)}, except
* that all column names are produced by a function that receives the CTE's
* {@link Select} columns as input.
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
DerivedColumnList fields(BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);


// [jooq-tools] START [fields]

Expand Down
37 changes: 37 additions & 0 deletions jOOQ/src/main/java/org/jooq/Table.java
Expand Up @@ -67,6 +67,7 @@
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.jooq.conf.Settings;
Expand Down Expand Up @@ -365,6 +366,24 @@ public interface Table<R extends Record> extends TableLike<R> {
@Support
Table<R> as(String alias, Function<? super Field<?>, ? extends String> aliasFunction);

/**
* 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, i) -> "column" + i);
* </pre></code>
*
* @param alias The alias name
* @param aliasFunction The function providing field aliases.
* @return The table alias
*/
@Support
Table<R> as(String alias, BiFunction<? super Field<?>, ? super Integer, ? extends String> aliasFunction);


/**
* Create an alias for this table based on another table's name.
Expand Down Expand Up @@ -413,6 +432,24 @@ public interface Table<R extends Record> extends TableLike<R> {
@Support
Table<R> as(Table<?> otherTable, Function<? super Field<?>, ? extends Field<?>> aliasFunction);

/**
* 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, i) -> "column" + i);
* </pre></code>
*
* @param alias The alias name
* @param aliasFunction The function providing field aliases.
* @return The table alias
*/
@Support
Table<R> as(Table<?> otherTable, BiFunction<? super Field<?>, ? super Integer, ? extends Field<?>> aliasFunction);


// -------------------------------------------------------------------------
// XXX: JOIN clauses on tables
Expand Down
20 changes: 14 additions & 6 deletions jOOQ/src/main/java/org/jooq/TableLike.java
Expand Up @@ -40,6 +40,7 @@
*/
package org.jooq;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
Expand All @@ -51,7 +52,7 @@
public interface TableLike<R extends Record> extends QueryPart {

/**
* Get this table's fields as a {@link Row}
* Get this table's fields as a {@link Row}.
*/
Row fieldsRow();

Expand Down Expand Up @@ -171,7 +172,7 @@ public interface TableLike<R extends Record> extends QueryPart {
Field<?>[] fields(int... fieldIndexes);

/**
* The underlying table representation of this object
* The underlying table representation of this object.
* <p>
* This method is useful for things like
* <code>SELECT * FROM (SELECT * FROM x WHERE x.a = '1') WHERE ... </code>
Expand All @@ -180,29 +181,36 @@ public interface TableLike<R extends Record> extends QueryPart {
Table<R> asTable();

/**
* The underlying aliased table representation of this object
* The underlying aliased table representation of this object.
*
* @see Table#as(String)
*/
@Support
Table<R> asTable(String alias);

/**
* The underlying aliased table representation of this object
* The underlying aliased table representation of this object.
*
* @see Table#as(String, String...)
*/
@Support
Table<R> asTable(String alias, String... fieldAliases);



/**
* The underlying aliased table representation of this object
* 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);

/**
* The underlying aliased table representation of this object.
*
* @see Table#as(String, BiFunction)
*/
@Support
Table<R> asTable(String alias, BiFunction<? super Field<?>, ? super Integer, ? extends String> aliasFunction);

}
11 changes: 11 additions & 0 deletions jOOQ/src/main/java/org/jooq/WithStep.java
Expand Up @@ -51,6 +51,7 @@
// ...

import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;

import javax.annotation.Generated;
Expand Down Expand Up @@ -104,6 +105,16 @@ public interface WithStep extends QueryPart {
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
WithAsStep with(String alias, Function<? super Field<?>, ? extends String> fieldNameFunction);

/**
* Add another common table expression to the <code>WITH</code> clause.
* <p>
* This works in a similar way as {@link #with(String, String...)}, except
* that all column names are produced by a function that receives the CTE's
* {@link Select} columns and their column indexes as input.
*/
@Support({ FIREBIRD, H2, HSQLDB, POSTGRES })
WithAsStep with(String alias, BiFunction<? super Field<?>, ? super Integer, ? extends String> fieldNameFunction);


// [jooq-tools] START [with]

Expand Down

0 comments on commit 4555a6d

Please sign in to comment.