[#2203] Add Executor.map(Schema) and Executor.map(Table) as a
convenience to apply runtime schema mapping
This commit is contained in:
parent
ff12bbc730
commit
3e2778122c
@ -42,14 +42,14 @@ import org.jooq.impl.TableImpl;
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class RenamedTable extends TableImpl<Record> {
|
||||
class RenamedTable<R extends Record> extends TableImpl<R> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -309012919785933903L;
|
||||
|
||||
RenamedTable(Table<?> delegate, String rename) {
|
||||
RenamedTable(Table<R> delegate, String rename) {
|
||||
super(rename, delegate.getSchema());
|
||||
|
||||
for (Field<?> field : delegate.fields()) {
|
||||
|
||||
@ -362,9 +362,10 @@ public class SchemaMapping implements Serializable {
|
||||
* @param table The generated table to be mapped
|
||||
* @return The configured table
|
||||
*/
|
||||
public Table<?> map(Table<?> table) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <R extends Record> Table<R> map(Table<R> table) {
|
||||
if (ignoreMapping) return table;
|
||||
Table<?> result = null;
|
||||
Table<R> result = null;
|
||||
|
||||
if (table != null) {
|
||||
Schema schema = table.getSchema();
|
||||
@ -383,7 +384,7 @@ public class SchemaMapping implements Serializable {
|
||||
// want to use Factory and dependent objects in a "thread-safe" manner
|
||||
synchronized (this) {
|
||||
if (!getTables().containsKey(key)) {
|
||||
Table<?> mapped = table;
|
||||
Table<R> mapped = table;
|
||||
|
||||
schemaLoop:
|
||||
for (MappedSchema s : mapping.getSchemata()) {
|
||||
@ -395,7 +396,7 @@ public class SchemaMapping implements Serializable {
|
||||
|
||||
// Ignore self-mappings and void-mappings
|
||||
if (!isBlank(t.getOutput()) && !t.getOutput().equals(t.getInput())) {
|
||||
mapped = new RenamedTable(table, t.getOutput());
|
||||
mapped = new RenamedTable<R>(table, t.getOutput());
|
||||
}
|
||||
|
||||
break schemaLoop;
|
||||
@ -410,7 +411,7 @@ public class SchemaMapping implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
result = getTables().get(key);
|
||||
result = (Table<R>) getTables().get(key);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@ -437,6 +437,34 @@ public class Executor implements Configuration {
|
||||
configuration.setExecuteListeners(listeners);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a schema to another one.
|
||||
* <p>
|
||||
* This will map a schema onto another one, depending on configured schema
|
||||
* mapping in this <code>Executor</code>. If no applicable schema mapping
|
||||
* can be found, the schema itself is returned.
|
||||
*
|
||||
* @param schema A schema
|
||||
* @return The mapped schema
|
||||
*/
|
||||
public final Schema map(Schema schema) {
|
||||
return Utils.getMappedSchema(this, schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a table to another one.
|
||||
* <p>
|
||||
* This will map a table onto another one, depending on configured table
|
||||
* mapping in this <code>Executor</code>. If no applicable table mapping
|
||||
* can be found, the table itself is returned.
|
||||
*
|
||||
* @param table A table
|
||||
* @return The mapped table
|
||||
*/
|
||||
public final <R extends Record> Table<R> map(Table<R> table) {
|
||||
return Utils.getMappedTable(this, table);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Convenience methods accessing the underlying Connection
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -993,7 +993,7 @@ final class Utils {
|
||||
* Map a {@link Table} according to the configured {@link org.jooq.SchemaMapping}
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
static final Table<?> getMappedTable(Configuration configuration, Table<?> table) {
|
||||
static final <R extends Record> Table<R> getMappedTable(Configuration configuration, Table<R> table) {
|
||||
org.jooq.SchemaMapping mapping = configuration.getSchemaMapping();
|
||||
|
||||
if (mapping != null) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user