From 8e09036e8e28abfc231734f8f5f37fde0b613a6a Mon Sep 17 00:00:00 2001 From: lukaseder Date: Fri, 4 Mar 2016 16:54:02 +0100 Subject: [PATCH] [#2928] Mapping tables based on regular expressions --- .../src/main/java/org/jooq/SchemaMapping.java | 32 +++++++--- .../main/java/org/jooq/conf/RegexAdapter.java | 61 +++++++++++++++++++ jOOQ/src/main/resources/xjb/binding.xjb | 4 ++ .../main/resources/xsd/jooq-runtime-3.8.0.xsd | 29 ++++++--- 4 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/conf/RegexAdapter.java diff --git a/jOOQ/src/main/java/org/jooq/SchemaMapping.java b/jOOQ/src/main/java/org/jooq/SchemaMapping.java index aa740a189e..d2852847af 100644 --- a/jOOQ/src/main/java/org/jooq/SchemaMapping.java +++ b/jOOQ/src/main/java/org/jooq/SchemaMapping.java @@ -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(result, t.getOutput()); - } + if (!isBlank(t.getOutput())) + if (t.getInput() != null && !t.getOutput().equals(tableName)) + result = new RenamedTable(result, t.getOutput()); + else if (t.getInputExpression() != null) + result = new RenamedTable(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 diff --git a/jOOQ/src/main/java/org/jooq/conf/RegexAdapter.java b/jOOQ/src/main/java/org/jooq/conf/RegexAdapter.java new file mode 100644 index 0000000000..4e25cd7532 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/conf/RegexAdapter.java @@ -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 { + + @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(); + } +} diff --git a/jOOQ/src/main/resources/xjb/binding.xjb b/jOOQ/src/main/resources/xjb/binding.xjb index 97c2169ba5..38decef32f 100644 --- a/jOOQ/src/main/resources/xjb/binding.xjb +++ b/jOOQ/src/main/resources/xjb/binding.xjb @@ -15,6 +15,10 @@ + + + + diff --git a/jOOQ/src/main/resources/xsd/jooq-runtime-3.8.0.xsd b/jOOQ/src/main/resources/xsd/jooq-runtime-3.8.0.xsd index d50af58e60..c33fc67786 100644 --- a/jOOQ/src/main/resources/xsd/jooq-runtime-3.8.0.xsd +++ b/jOOQ/src/main/resources/xsd/jooq-runtime-3.8.0.xsd @@ -122,11 +122,18 @@ - - + + - + + + + - - + + + + + + +