[#7947] Let code generation patterns match also partially qualified object names

This commit is contained in:
lukaseder 2018-10-17 11:31:49 +02:00
parent 12882d77be
commit 0d2351382e
5 changed files with 93 additions and 11 deletions

View File

@ -505,6 +505,7 @@ public class GenerationTool {
if (d.getRegexFlags() != null)
database.setRegexFlags(d.getRegexFlags());
database.setRegexMatchesPartialQualification(!FALSE.equals(d.isRegexMatchesPartialQualification()));
SchemaVersionProvider svp = null;
CatalogVersionProvider cvp = null;

View File

@ -47,6 +47,7 @@ import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@ -103,6 +104,7 @@ public abstract class AbstractDatabase implements Database {
private SQLDialect dialect;
private Connection connection;
private List<RegexFlag> regexFlags;
private boolean regexMatchesPartialQualification;
private List<Filter> filters;
private String[] excludes;
private String[] includes = { ".*" };
@ -353,6 +355,20 @@ public abstract class AbstractDatabase implements Database {
return true;
}
final boolean matches(Pattern pattern, Definition definition) {
if (!getRegexMatchesPartialQualification())
return pattern.matcher(definition.getName()).matches()
|| pattern.matcher(definition.getQualifiedName()).matches();
List<Name> parts = Arrays.asList(definition.getQualifiedNamePart().parts());
for (int i = parts.size() - 1; i >= 0; i--)
if (pattern.matcher(DSL.name(parts.subList(i, parts.size()).toArray(new Name[0])).unquotedName().toString()).matches())
return true;
return false;
}
final Pattern pattern(String regex) {
Pattern pattern = patterns.get(regex);
@ -883,6 +899,16 @@ public abstract class AbstractDatabase implements Database {
return regexFlags;
}
@Override
public final void setRegexMatchesPartialQualification(boolean regexMatchesPartialQualification) {
this.regexMatchesPartialQualification = regexMatchesPartialQualification;
}
@Override
public final boolean getRegexMatchesPartialQualification() {
return regexMatchesPartialQualification;
}
@Override
public void setRecordVersionFields(String[] recordVersionFields) {
this.recordVersionFields = recordVersionFields;
@ -1819,12 +1845,7 @@ public abstract class AbstractDatabase implements Database {
definitionsLoop: for (T definition : definitions) {
if (e != null) {
for (String exclude : e) {
Pattern p = pattern(exclude);
if (exclude != null &&
(p.matcher(definition.getName()).matches() ||
p.matcher(definition.getQualifiedName()).matches())) {
if (exclude != null && matches(pattern(exclude), definition)) {
if (log.isDebugEnabled())
log.debug("Exclude", "Excluding " + definition.getQualifiedName() + " because of pattern " + exclude);
@ -1835,11 +1856,7 @@ public abstract class AbstractDatabase implements Database {
if (i != null) {
for (String include : i) {
Pattern p = pattern(include);
if (include != null &&
(p.matcher(definition.getName()).matches() ||
p.matcher(definition.getQualifiedName()).matches())) {
if (include != null && matches(pattern(include), definition)) {
// [#3488] This allows for filtering out additional objects, in case the applicable
// code generation configuration might cause conflicts in resulting code

View File

@ -579,6 +579,20 @@ public interface Database extends AutoCloseable {
*/
List<RegexFlag> getRegexFlags();
/**
* Whether the regular expressions matching database objects should match
* partially qualified names as well as fully qualified and unqualified
* names.
*/
void setRegexMatchesPartialQualification(boolean regexMatchesPartialQualification);
/**
* Whether the regular expressions matching database objects should match
* partially qualified names as well as fully qualified and unqualified
* names.
*/
boolean getRegexMatchesPartialQualification();
/**
* Table columns matching these regular expressions will be considered as
* record version fields in generated code.

View File

@ -44,6 +44,8 @@ public class Database implements Serializable
@XmlList
@XmlElement(defaultValue = "COMMENTS CASE_INSENSITIVE")
protected List<RegexFlag> regexFlags;
@XmlElement(defaultValue = "true")
protected Boolean regexMatchesPartialQualification = true;
@XmlElement(defaultValue = ".*")
@XmlJavaTypeAdapter(StringAdapter.class)
protected String includes = ".*";
@ -245,6 +247,30 @@ public class Database implements Serializable
return this.regexFlags;
}
/**
* Whether regular expressions that match qualified object names also match partial qualifications (e.g. `table\.column` matches `schema.table.column`) or only full and/or no qualifications (e.g. `schema\.table\.column` and `column` match `schema.table.column`)
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isRegexMatchesPartialQualification() {
return regexMatchesPartialQualification;
}
/**
* Sets the value of the regexMatchesPartialQualification property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setRegexMatchesPartialQualification(Boolean value) {
this.regexMatchesPartialQualification = value;
}
/**
* All elements that are generated from your schema.
* <p>
@ -1327,6 +1353,11 @@ public class Database implements Serializable
return this;
}
public Database withRegexMatchesPartialQualification(Boolean value) {
setRegexMatchesPartialQualification(value);
return this;
}
public Database withIncludes(String value) {
setIncludes(value);
return this;
@ -1655,6 +1686,11 @@ public class Database implements Serializable
}
sb.append("</regexFlags>");
}
if (regexMatchesPartialQualification!= null) {
sb.append("<regexMatchesPartialQualification>");
sb.append(regexMatchesPartialQualification);
sb.append("</regexMatchesPartialQualification>");
}
if (includes!= null) {
sb.append("<includes>");
sb.append(includes);
@ -1927,6 +1963,15 @@ public class Database implements Serializable
return false;
}
}
if (regexMatchesPartialQualification == null) {
if (other.regexMatchesPartialQualification!= null) {
return false;
}
} else {
if (!regexMatchesPartialQualification.equals(other.regexMatchesPartialQualification)) {
return false;
}
}
if (includes == null) {
if (other.includes!= null) {
return false;
@ -2323,6 +2368,7 @@ public class Database implements Serializable
int result = 1;
result = ((prime*result)+((name == null)? 0 :name.hashCode()));
result = ((prime*result)+((regexFlags == null)? 0 :regexFlags.hashCode()));
result = ((prime*result)+((regexMatchesPartialQualification == null)? 0 :regexMatchesPartialQualification.hashCode()));
result = ((prime*result)+((includes == null)? 0 :includes.hashCode()));
result = ((prime*result)+((excludes == null)? 0 :excludes.hashCode()));
result = ((prime*result)+((includeExcludeColumns == null)? 0 :includeExcludeColumns.hashCode()));

View File

@ -439,6 +439,10 @@ encoding=UTF-8</pre></li>
The default value is "COMMENTS CASE_INSENSITIVE"]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="regexMatchesPartialQualification" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether regular expressions that match qualified object names also match partial qualifications (e.g. `table\.column` matches `schema.table.column`) or only full and/or no qualifications (e.g. `schema\.table\.column` and `column` match `schema.table.column`)]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="includes" type="string" default=".*" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[All elements that are generated from your schema.
<p>