Search in sources :

Example 1 with MappedTable

use of org.jooq.conf.MappedTable in project jOOQ by jOOQ.

the class SchemaMapping method map.

/**
     * Apply mapping to a given table
     *
     * @param table The generated table to be mapped
     * @return The configured table
     */
@SuppressWarnings("unchecked")
public <R extends Record> Table<R> map(Table<R> table) {
    Table<R> result = table;
    // [#4652] Don't initialise table mapping if not necessary
    if (result != null && !mapping().getSchemata().isEmpty()) {
        Schema schema = result.getSchema();
        // [#1189] Schema can be null in SQLite
        // [#2089] DefaultSchema have empty schema names
        // [#1186] TODO: replace this by calling table.getQualifiedName()
        String schemaName = (schema == null) ? "" : schema.getName();
        String tableName = result.getName();
        String key = (schema == null || StringUtils.isEmpty(schemaName)) ? tableName : (schemaName + "." + tableName);
        // Lazy initialise table mapping
        if (!getTables().containsKey(key)) {
            // want to use Configuration and dependent objects in a "thread-safe" manner
            synchronized (this) {
                if (!getTables().containsKey(key)) {
                    schemaLoop: for (MappedSchema s : mapping().getSchemata()) {
                        if (matches(s, schemaName)) {
                            for (MappedTable t : s.getTables()) {
                                // A configured mapping was found, add a renamed table
                                if (matches(t, tableName)) {
                                    // Ignore self-mappings and void-mappings
                                    if (!isBlank(t.getOutput()))
                                        if (t.getInput() != null && !t.getOutput().equals(tableName))
                                            result = new RenamedTable<R>(result, t.getOutput());
                                        else if (t.getInputExpression() != null)
                                            result = new RenamedTable<R>(result, t.getInputExpression().matcher(tableName).replaceAll(t.getOutput()));
                                    break schemaLoop;
                                }
                            }
                        }
                    }
                    // Add mapped table or self if no mapping was found
                    getTables().put(key, result);
                }
            }
        }
        result = (Table<R>) getTables().get(key);
    }
    return result;
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedTable(org.jooq.conf.MappedTable) MappedSchema(org.jooq.conf.MappedSchema)

Example 2 with MappedTable

use of org.jooq.conf.MappedTable in project jOOQ by jOOQ.

the class Example_4_2_Settings method run.

@Test
public void run() {
    Select<?> select = DSL.select().from(AUTHOR).where(AUTHOR.ID.eq(3));
    Tools.title("A couple of settings at work - Formatting");
    out.println(using(H2, new Settings().withRenderFormatted(false)).render(select));
    out.println(using(H2, new Settings().withRenderFormatted(true)).render(select));
    Tools.title("A couple of settings at work - Schema");
    out.println(using(H2, new Settings().withRenderSchema(false)).render(select));
    out.println(using(H2, new Settings().withRenderSchema(true)).render(select));
    Tools.title("A couple of settings at work - Name style");
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.LOWER)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.QUOTED)).render(select));
    Tools.title("A couple of settings at work - Keyword style");
    out.println(using(H2, new Settings().withRenderKeywordStyle(RenderKeywordStyle.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordStyle(RenderKeywordStyle.LOWER)).render(select));
    Tools.title("A couple of settings at work - Mapping");
    out.println(using(H2, new Settings().withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput("PUBLIC").withOutput("test").withTables(new MappedTable().withInput("AUTHOR").withOutput("test-author"))))).render(select));
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedTable(org.jooq.conf.MappedTable) RenderMapping(org.jooq.conf.RenderMapping) Settings(org.jooq.conf.Settings) Test(org.junit.Test)

Example 3 with MappedTable

use of org.jooq.conf.MappedTable in project jOOQ by jOOQ.

the class Example_4_3_Settings method run.

@Test
public void run() {
    Select<?> select = DSL.select().from(AUTHOR).where(AUTHOR.ID.eq(3));
    Tools.title("A couple of settings at work - Formatting");
    out.println(using(H2, new Settings().withRenderFormatted(false)).render(select));
    out.println(using(H2, new Settings().withRenderFormatted(true)).render(select));
    Tools.title("A couple of settings at work - Schema");
    out.println(using(H2, new Settings().withRenderSchema(false)).render(select));
    out.println(using(H2, new Settings().withRenderSchema(true)).render(select));
    Tools.title("A couple of settings at work - Name style");
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.LOWER)).render(select));
    out.println(using(H2, new Settings().withRenderNameStyle(RenderNameStyle.QUOTED)).render(select));
    Tools.title("A couple of settings at work - Keyword style");
    out.println(using(H2, new Settings().withRenderKeywordStyle(RenderKeywordStyle.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordStyle(RenderKeywordStyle.LOWER)).render(select));
    Tools.title("A couple of settings at work - Mapping");
    out.println(using(H2, new Settings().withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput("PUBLIC").withOutput("test").withTables(new MappedTable().withInput("AUTHOR").withOutput("test-author"))))).render(select));
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedTable(org.jooq.conf.MappedTable) RenderMapping(org.jooq.conf.RenderMapping) Settings(org.jooq.conf.Settings) Test(org.junit.Test)

Example 4 with MappedTable

use of org.jooq.conf.MappedTable in project jOOQ by jOOQ.

the class SchemaMapping method add.

/**
     * Add tables to this mapping
     *
     * @param inputTable The table known at codegen time to be mapped
     * @param outputTable The table configured at run time to be mapped
     */
public void add(final Table<?> inputTable, final String outputTable) {
    logDeprecation();
    // Try to find a pre-existing schema mapping in the settings
    MappedSchema schema = null;
    MappedTable table = null;
    for (MappedSchema s : mapping().getSchemata()) {
        if (inputTable.getSchema().getName().equals(s.getInput())) {
            // Find existing mapped table
            tableLoop: for (MappedTable t : s.getTables()) {
                if (inputTable.getName().equals(t.getInput())) {
                    table = t;
                    break tableLoop;
                }
            }
            schema = s;
            break;
        }
    }
    if (schema == null) {
        schema = new MappedSchema().withInput(inputTable.getSchema().getName());
        mapping().getSchemata().add(schema);
    }
    if (table == null) {
        table = new MappedTable().withInput(inputTable.getName());
        schema.getTables().add(table);
    }
    // Add new mapping
    table.setOutput(outputTable);
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedTable(org.jooq.conf.MappedTable)

Aggregations

MappedSchema (org.jooq.conf.MappedSchema)4 MappedTable (org.jooq.conf.MappedTable)4 RenderMapping (org.jooq.conf.RenderMapping)2 Settings (org.jooq.conf.Settings)2 Test (org.junit.Test)2