[#2928] Mapping tables based on regular expressions

This commit is contained in:
lukaseder 2016-03-04 16:54:02 +01:00
parent 2570c1fb62
commit 8e09036e8e
4 changed files with 110 additions and 16 deletions

View File

@ -319,12 +319,14 @@ public class SchemaMapping implements Serializable {
for (MappedSchema s : mapping().getSchemata()) {
// A configured mapping was found, add a renamed schema
if (schemaName.equals(s.getInput())) {
if (matches(s, schemaName)) {
// Ignore self-mappings and void-mappings
if (!isBlank(s.getOutput()) && !s.getOutput().equals(s.getInput())) {
result = new RenamedSchema(result, s.getOutput());
}
if (!isBlank(s.getOutput()))
if (s.getInput() != null && !s.getOutput().equals(schemaName))
result = new RenamedSchema(result, s.getOutput());
else if (s.getInputExpression() != null)
result = new RenamedSchema(result, s.getInputExpression().matcher(schemaName).replaceAll(s.getOutput()));
break;
}
@ -380,16 +382,18 @@ public class SchemaMapping implements Serializable {
schemaLoop:
for (MappedSchema s : mapping().getSchemata()) {
if (schemaName.equals(s.getInput())) {
if (matches(s, schemaName)) {
for (MappedTable t : s.getTables()) {
// A configured mapping was found, add a renamed table
if (tableName.equals(t.getInput())) {
if (matches(t, tableName)) {
// Ignore self-mappings and void-mappings
if (!isBlank(t.getOutput()) && !t.getOutput().equals(t.getInput())) {
result = new RenamedTable<R>(result, t.getOutput());
}
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;
}
@ -409,6 +413,16 @@ public class SchemaMapping implements Serializable {
return result;
}
private final boolean matches(MappedSchema s, String schemaName) {
return (s.getInput() != null && schemaName.equals(s.getInput()))
|| (s.getInputExpression() != null && s.getInputExpression().matcher(schemaName).matches());
}
private final boolean matches(MappedTable t, String tableName) {
return (t.getInput() != null && tableName.equals(t.getInput()))
|| (t.getInputExpression() != null && t.getInputExpression().matcher(tableName).matches());
}
/**
* Synonym for {@link #use(String)}. Added for better interoperability with
* Spring

View File

@ -0,0 +1,61 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.conf;
import java.util.regex.Pattern;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
* @author Lukas Eder
*/
public class RegexAdapter extends XmlAdapter<String, Pattern> {
@Override
public Pattern unmarshal(String v) throws Exception {
return v == null ? null : Pattern.compile(v);
}
@Override
public String marshal(Pattern v) throws Exception {
return v == null ? null : v.pattern();
}
}

View File

@ -15,6 +15,10 @@
<xjc:serializable uid="380" />
</jaxb:globalBindings>
<!-- [#2928] Regular expressions -->
<jaxb:bindings schemaLocation="../xsd/jooq-runtime-3.8.0.xsd" multiple="true" node="//xs:element[@name='inputExpression']">
<xjc:javaType name="java.util.regex.Pattern" adapter="org.jooq.conf.RegexAdapter"/>
</jaxb:bindings>
<!-- Annotate the following classes with @SuppressWarnings -->
<jaxb:bindings schemaLocation="../xsd/jooq-runtime-3.8.0.xsd" multiple="true" node="//xs:complexType">

View File

@ -122,11 +122,18 @@
<complexType name="MappedSchema">
<all>
<!-- The input schema as defined in org.jooq.Schema.getName() -->
<element name="input" type="string" minOccurs="1" maxOccurs="1"/>
<!-- The input schema name as defined in org.jooq.Schema.getName()
Either <input/> or <inputExpression/> must be provided -->
<element name="input" type="string" minOccurs="0" maxOccurs="1"/>
<!-- The output schema as it will be rendered in SQL
When this is omitted, you can still apply table mapping -->
<!-- A regular expression matching the input schema name as defined in org.jooq.Schema.getName()
Either <input/> or <inputExpression/> must be provided -->
<element name="inputExpression" type="string" minOccurs="0" maxOccurs="1"/>
<!-- The output schema as it will be rendered in SQL.
* When this is omitted, you can still apply table mapping.
* When <input/> is provided, <output/> is a constant value
* When <inputExpression/> is provided, <output/> is a replacement expression -->
<element name="output" type="string" minOccurs="0" maxOccurs="1"/>
<!-- Configure table mapping for runtime table rewriting in
@ -143,10 +150,18 @@
<complexType name="MappedTable">
<all>
<!-- The input schema as defined in org.jooq.Table.getName() -->
<element name="input" type="string" minOccurs="1" maxOccurs="1"/>
<!-- The output schema as it will be rendered in SQL -->
<!-- The input table as defined in org.jooq.Table.getName()
Either <input/> or <inputExpression/> must be provided -->
<element name="input" type="string" minOccurs="0" maxOccurs="1"/>
<!-- A regular expression matching the input table name as defined in org.jooq.Table.getName()
Either <input/> or <inputExpression/> must be provided -->
<element name="inputExpression" type="string" minOccurs="0" maxOccurs="1"/>
<!-- The output table as it will be rendered in SQL
* When <input/> is provided, <output/> is a constant value
* When <inputExpression/> is provided, <output/> is a replacement expression -->
<element name="output" type="string" minOccurs="1" maxOccurs="1"/>
</all>
</complexType>