Search in sources :

Example 1 with RenderMapping

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

the class SchemaMapping method map.

/**
 * Apply mapping to a given schema
 *
 * @param schema The schema to be mapped
 * @return The configured schema
 */
@Nullable
public Schema map(Schema schema) {
    // The DefaultConfiguration's ignoreMapping flag!
    if (!renderSchema())
        return null;
    else // [#9708] Don't map an already mapped schema again
    if (schema instanceof RenamedSchema)
        return schema;
    Schema result = schema;
    RenderMapping m = mapping();
    // [#4642] Don't initialise schema mapping if not necessary
    if (!m.getSchemata().isEmpty() || !m.getCatalogs().isEmpty() || !isEmpty(m.getDefaultSchema()) || !isEmpty(m.getDefaultCatalog())) {
        if (result == null)
            result = schema(name(""));
        Catalog catalog = result.getCatalog();
        if (catalog == null)
            catalog = DSL.catalog(name(""));
        // [#2089] DefaultSchema has an empty schema name
        // [#7498] But we're mapping those names as well
        String catalogName = catalog.getName();
        String schemaName = result.getName();
        String key = StringUtils.isEmpty(catalogName) ? schemaName : catalogName + '.' + schemaName;
        // Lazy initialise schema mapping
        if (!getSchemata().containsKey(key)) {
            // want to use a Configuration and dependent objects in a "thread-safe" manner
            synchronized (this) {
                if (!getSchemata().containsKey(key)) {
                    catalogLoop: for (MappedCatalog c : m.getCatalogs()) {
                        if (matches(c, catalogName)) {
                            for (MappedSchema s : c.getSchemata()) {
                                if (matches(s, schemaName)) {
                                    // Ignore self-mappings and void-mappings
                                    if (!isBlank(s.getOutput()))
                                        if (s.getInput() != null && !s.getOutput().equals(schemaName))
                                            result = new RenamedSchema(map(catalog), result, s.getOutput());
                                        else if (s.getInputExpression() != null)
                                            result = new RenamedSchema(map(catalog), result, s.getInputExpression().matcher(schemaName).replaceAll(s.getOutput()));
                                    break catalogLoop;
                                }
                            }
                            // [#7498] Even without schema mapping configuration, we may still need to map the catalog
                            result = new RenamedSchema(map(catalog), result, schemaName);
                            break catalogLoop;
                        }
                    }
                    if (!(result instanceof RenamedSchema)) {
                        for (MappedSchema s : m.getSchemata()) {
                            // A configured mapping was found, add a renamed schema
                            if (matches(s, schemaName)) {
                                // Ignore self-mappings and void-mappings
                                if (!isBlank(s.getOutput()))
                                    if (s.getInput() != null && !s.getOutput().equals(schemaName))
                                        result = new RenamedSchema(map(catalog), result, s.getOutput());
                                    else if (s.getInputExpression() != null)
                                        result = new RenamedSchema(map(catalog), result, s.getInputExpression().matcher(schemaName).replaceAll(s.getOutput()));
                                break;
                            }
                        }
                    }
                    // [#13034] Apply defaultCatalog irrespective of defaultSchema and the above mappings
                    if (result.getCatalog() != null && map(result.getCatalog()) == null)
                        result = new RenamedSchema(null, result, result.getName());
                    // [#13035] Cache the application of the defaultSchema
                    if ("".equals(result.getName()))
                        result = null;
                    else if (result.getName().equals(m.getDefaultSchema()) && (result.getCatalog() == null || "".equals(result.getCatalog().getName()) || result.getCatalog().getName().equals(m.getDefaultCatalog())))
                        result = null;
                    // Add mapped schema or self if no mapping was found
                    getSchemata().put(key, result);
                }
            }
        }
        result = getSchemata().get(key);
    }
    return result;
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedCatalog(org.jooq.conf.MappedCatalog) RenderMapping(org.jooq.conf.RenderMapping) MappedSchema(org.jooq.conf.MappedSchema) MappedCatalog(org.jooq.conf.MappedCatalog) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with RenderMapping

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

the class SchemaMapping method map.

@Nullable
public Catalog map(Catalog catalog) {
    // precedence over the DefaultConfiguration's ignoreMapping flag!
    if (!renderCatalog())
        return null;
    else // Don't map an already mapped catalog again
    if (catalog instanceof RenamedCatalog)
        return catalog;
    Catalog result = catalog;
    if (result == null)
        result = DSL.catalog(name(""));
    // [#2089] DefaultCatalog has an empty schema name
    // But we're mapping those names as well
    String catalogName = result.getName();
    // [#4642] Don't initialise catalog mapping if not necessary
    RenderMapping m = mapping();
    if (!m.getCatalogs().isEmpty() || !isEmpty(m.getDefaultCatalog())) {
        // Lazy initialise catalog mapping
        if (!getCatalogs().containsKey(catalogName)) {
            // want to use a Configuration and dependent objects in a "thread-safe" manner
            synchronized (this) {
                if (!getCatalogs().containsKey(catalogName)) {
                    for (MappedCatalog c : m.getCatalogs()) {
                        // A configured mapping was found, add a renamed catalog
                        if (matches(c, catalogName)) {
                            // Ignore self-mappings and void-mappings
                            if (!isBlank(c.getOutput()))
                                if (c.getInput() != null && !c.getOutput().equals(catalogName))
                                    result = new RenamedCatalog(result, c.getOutput());
                                else if (c.getInputExpression() != null)
                                    result = new RenamedCatalog(result, c.getInputExpression().matcher(catalogName).replaceAll(c.getOutput()));
                            break;
                        }
                    }
                    // [#13035] Cache the application of the defaultCatalog
                    if ("".equals(result.getName()) || result.getName().equals(m.getDefaultCatalog()))
                        result = null;
                    // Add mapped catalog or self if no mapping was found
                    getCatalogs().put(catalogName, result);
                }
            }
        }
        result = getCatalogs().get(catalogName);
    }
    return result;
}
Also used : MappedCatalog(org.jooq.conf.MappedCatalog) RenderMapping(org.jooq.conf.RenderMapping) MappedCatalog(org.jooq.conf.MappedCatalog) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RenderMapping

use of org.jooq.conf.RenderMapping 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 case");
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.LOWER)).render(select));
    Tools.title("A couple of settings at work - Name quoting");
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.ALWAYS)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_QUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_UNQUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.NEVER)).render(select));
    Tools.title("A couple of settings at work - Keyword case");
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.LOWER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.PASCAL)).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 RenderMapping

use of org.jooq.conf.RenderMapping 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 case");
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.LOWER)).render(select));
    Tools.title("A couple of settings at work - Name quoting");
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.ALWAYS)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_QUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_UNQUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.NEVER)).render(select));
    Tools.title("A couple of settings at work - Keyword case");
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.LOWER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.PASCAL)).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)

Aggregations

RenderMapping (org.jooq.conf.RenderMapping)4 MappedSchema (org.jooq.conf.MappedSchema)3 Nullable (org.jetbrains.annotations.Nullable)2 MappedCatalog (org.jooq.conf.MappedCatalog)2 MappedTable (org.jooq.conf.MappedTable)2 Settings (org.jooq.conf.Settings)2 Test (org.junit.Test)2