[#7498] Improve RenderMapping to allow for adding a schema to unqualified objects
This commit is contained in:
parent
3e6ea7ff79
commit
fafe20bbf7
@ -45,7 +45,7 @@ package org.jooq;
|
||||
public interface Named extends QueryPart {
|
||||
|
||||
/**
|
||||
* The name of this object.
|
||||
* The unqualified name of this object.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
|
||||
@ -51,11 +51,10 @@ class RenamedTable<R extends Record> extends TableImpl<R> {
|
||||
*/
|
||||
private static final long serialVersionUID = -309012919785933903L;
|
||||
|
||||
RenamedTable(Table<R> delegate, String rename) {
|
||||
super(rename, delegate.getSchema());
|
||||
RenamedTable(Schema schema, Table<R> delegate, String rename) {
|
||||
super(rename, schema);
|
||||
|
||||
for (Field<?> field : delegate.fields()) {
|
||||
for (Field<?> field : delegate.fields())
|
||||
createField(field.getName(), field.getDataType(), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,6 +37,8 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.impl.DSL.schema;
|
||||
import static org.jooq.tools.StringUtils.isBlank;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -294,11 +296,10 @@ public class SchemaMapping implements Serializable {
|
||||
|
||||
Schema result = schema;
|
||||
if (result != null) {
|
||||
String schemaName = result.getName();
|
||||
|
||||
// [#2089] DefaultSchema has an empty schema name
|
||||
if (StringUtils.isEmpty(schemaName))
|
||||
return null;
|
||||
// [#7498] But we're mapping those names as well
|
||||
String schemaName = result.getName();
|
||||
|
||||
// [#4642] Don't initialise schema mapping if not necessary
|
||||
if (!mapping().getSchemata().isEmpty()) {
|
||||
@ -337,9 +338,8 @@ public class SchemaMapping implements Serializable {
|
||||
|
||||
// The configured default schema is mapped to "null". This prevents
|
||||
// it from being rendered to SQL
|
||||
if (result.getName().equals(mapping().getDefaultSchema())) {
|
||||
if (result.getName().equals(mapping().getDefaultSchema()))
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -362,9 +362,12 @@ public class SchemaMapping implements Serializable {
|
||||
// [#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();
|
||||
if (schema == null)
|
||||
schema = schema(name(""));
|
||||
|
||||
String schemaName = schema.getName();
|
||||
String tableName = result.getName();
|
||||
String key = (schema == null || StringUtils.isEmpty(schemaName)) ? tableName : (schemaName + "." + tableName);
|
||||
String key = StringUtils.isEmpty(schemaName) ? tableName : (schemaName + "." + tableName);
|
||||
|
||||
// Lazy initialise table mapping
|
||||
if (!getTables().containsKey(key)) {
|
||||
@ -385,13 +388,17 @@ public class SchemaMapping implements Serializable {
|
||||
// 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());
|
||||
result = new RenamedTable<R>(map(schema), result, t.getOutput());
|
||||
else if (t.getInputExpression() != null)
|
||||
result = new RenamedTable<R>(result, t.getInputExpression().matcher(tableName).replaceAll(t.getOutput()));
|
||||
result = new RenamedTable<R>(map(schema), result, t.getInputExpression().matcher(tableName).replaceAll(t.getOutput()));
|
||||
|
||||
break schemaLoop;
|
||||
}
|
||||
}
|
||||
|
||||
// [#7498] Even without table mapping configuration, we may still need to map the schema
|
||||
result = new RenamedTable<R>(map(schema), result, tableName);
|
||||
break schemaLoop;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1121,16 +1121,13 @@ public abstract class AbstractRoutine<T> extends AbstractNamed implements Routin
|
||||
ctx.visit(value);
|
||||
}
|
||||
|
||||
private final void toSQLQualifiedName(RenderContext context) {
|
||||
Schema mappedSchema = Tools.getMappedSchema(context.configuration(), getSchema());
|
||||
|
||||
if (context.qualify()) {
|
||||
if (mappedSchema != null) {
|
||||
context.visit(mappedSchema);
|
||||
context.sql('.');
|
||||
}
|
||||
|
||||
private final void toSQLQualifiedName(RenderContext ctx) {
|
||||
if (ctx.qualify()) {
|
||||
Schema mapped = Tools.getMappedSchema(ctx.configuration(), getSchema());
|
||||
|
||||
if (mapped != null && !"".equals(mapped.getName()))
|
||||
ctx.visit(mapped)
|
||||
.sql('.');
|
||||
|
||||
|
||||
|
||||
@ -1144,7 +1141,7 @@ public abstract class AbstractRoutine<T> extends AbstractNamed implements Routin
|
||||
|
||||
}
|
||||
|
||||
context.literal(getName());
|
||||
ctx.literal(getName());
|
||||
}
|
||||
|
||||
private final void fetchOutParameters(ExecuteContext ctx) throws SQLException {
|
||||
|
||||
@ -52,6 +52,7 @@ import static org.jooq.JoinType.OUTER_APPLY;
|
||||
import static org.jooq.JoinType.RIGHT_OUTER_JOIN;
|
||||
import static org.jooq.JoinType.STRAIGHT_JOIN;
|
||||
// ...
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.impl.DSL.table;
|
||||
import static org.jooq.impl.DSL.val;
|
||||
import static org.jooq.impl.Tools.EMPTY_FIELD;
|
||||
@ -388,7 +389,7 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
|
||||
if (tableschema == null)
|
||||
tableschema = getQualifiedName().qualified()
|
||||
? DSL.schema(getQualifiedName().qualifier())
|
||||
: null;
|
||||
: DSL.schema(name(""));
|
||||
|
||||
return tableschema;
|
||||
}
|
||||
|
||||
@ -37,6 +37,8 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.impl.DSL.name;
|
||||
|
||||
import org.jooq.Catalog;
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Context;
|
||||
@ -79,7 +81,7 @@ public class PackageImpl extends AbstractNamed implements Package {
|
||||
if (schema == null)
|
||||
schema = getQualifiedName().qualified()
|
||||
? DSL.schema(getQualifiedName().qualifier())
|
||||
: null;
|
||||
: DSL.schema(name(""));
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
@ -40,6 +40,7 @@ package org.jooq.impl;
|
||||
|
||||
import static org.jooq.Clause.SCHEMA;
|
||||
import static org.jooq.Clause.SCHEMA_REFERENCE;
|
||||
import static org.jooq.impl.DSL.name;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -100,18 +101,20 @@ public class SchemaImpl extends AbstractNamed implements Schema {
|
||||
if (catalog == null)
|
||||
catalog = getQualifiedName().qualified()
|
||||
? DSL.catalog(getQualifiedName().qualifier())
|
||||
: null;
|
||||
: DSL.catalog(name(""));
|
||||
|
||||
return catalog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
Catalog mappedCatalog = Tools.getMappedCatalog(ctx.configuration(), getCatalog());
|
||||
if (ctx.qualifyCatalog()) {
|
||||
Catalog mappedCatalog = Tools.getMappedCatalog(ctx.configuration(), getCatalog());
|
||||
|
||||
if (ctx.qualifyCatalog() && mappedCatalog != null) {
|
||||
ctx.visit(mappedCatalog);
|
||||
ctx.sql('.');
|
||||
if (mappedCatalog != null && !"".equals(mappedCatalog.getName())) {
|
||||
ctx.visit(mappedCatalog);
|
||||
ctx.sql('.');
|
||||
}
|
||||
}
|
||||
|
||||
ctx.visit(getUnqualifiedName());
|
||||
|
||||
@ -232,16 +232,13 @@ public class SequenceImpl<T extends Number> extends AbstractNamed implements Seq
|
||||
private final void accept0(Context<?> ctx, boolean asStringLiterals) {
|
||||
Schema mappedSchema = Tools.getMappedSchema(ctx.configuration(), schema);
|
||||
|
||||
if (mappedSchema != null && ctx.family() != CUBRID) {
|
||||
if (asStringLiterals) {
|
||||
if (mappedSchema != null && !"".equals(mappedSchema.getName()) && ctx.family() != CUBRID)
|
||||
if (asStringLiterals)
|
||||
ctx.visit(inline(mappedSchema.getName()))
|
||||
.sql(", ");
|
||||
}
|
||||
else {
|
||||
else
|
||||
ctx.visit(mappedSchema)
|
||||
.sql('.');
|
||||
}
|
||||
}
|
||||
|
||||
if (asStringLiterals)
|
||||
ctx.visit(inline(name));
|
||||
|
||||
@ -241,7 +241,7 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
|
||||
(!NO_SUPPORT_QUALIFIED_TVF_CALLS.contains(ctx.family()) || parameters == null || ctx.declareTables())) {
|
||||
Schema mappedSchema = Tools.getMappedSchema(ctx.configuration(), getSchema());
|
||||
|
||||
if (mappedSchema != null) {
|
||||
if (mappedSchema != null && !"".equals(mappedSchema.getName())) {
|
||||
ctx.visit(mappedSchema);
|
||||
ctx.sql('.');
|
||||
}
|
||||
|
||||
@ -2523,7 +2523,7 @@ final class Tools {
|
||||
Schema mapped = getMappedSchema(configuration, udt.getSchema());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (mapped != null)
|
||||
if (mapped != null && !"".equals(mapped.getName()))
|
||||
sb.append(mapped.getName()).append('.');
|
||||
|
||||
|
||||
|
||||
@ -209,7 +209,7 @@ public class UDTImpl<R extends UDTRecord<R>> extends AbstractNamed implements UD
|
||||
public final void accept(Context<?> ctx) {
|
||||
Schema mappedSchema = Tools.getMappedSchema(ctx.configuration(), getSchema());
|
||||
|
||||
if (mappedSchema != null)
|
||||
if (mappedSchema != null && !"".equals(mappedSchema.getName()))
|
||||
ctx.visit(mappedSchema).sql('.');
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user