Skip to content

Commit

Permalink
[jOOQ#5360] [jOOQ#5373] Add <syntheticIdentities> regular expression …
Browse files Browse the repository at this point in the history
…to code generator configuration
  • Loading branch information
lukaseder committed Jul 4, 2016
1 parent ce1b91b commit 2cc5194
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 63 deletions.
31 changes: 14 additions & 17 deletions jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import static java.util.Collections.singletonList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -121,8 +120,8 @@ public abstract class AbstractDatabase implements Database {
private String[] recordVersionFields;
private String[] recordTimestampFields;
private String[] syntheticPrimaryKeys;
private String[] syntheticIdentities;
private String[] overridePrimaryKeys;
private String[] syntheticIdentities;
private boolean supportsUnsignedTypes;
private boolean ignoreProcedureReturnValues;
private boolean dateAsTimestamp;
Expand Down Expand Up @@ -690,21 +689,6 @@ public String[] getSyntheticPrimaryKeys() {
return syntheticPrimaryKeys;
}

@Override
public void setSyntheticIdentities(String[] syntheticIdentities) {
if (syntheticIdentities.length > 0 && !syntheticIdentities[0].isEmpty()) {
this.syntheticIdentities = syntheticIdentities;
}
}

@Override
public final boolean isSyntheticIdentity(ColumnDefinition columnDefinition) {
if (syntheticIdentities == null) {
return false;
}
return !filterExcludeInclude(singletonList(columnDefinition), null, syntheticIdentities, filters).isEmpty();
}

@Override
public void setOverridePrimaryKeys(String[] overridePrimaryKeys) {
this.overridePrimaryKeys = overridePrimaryKeys;
Expand All @@ -719,6 +703,19 @@ public String[] getOverridePrimaryKeys() {
return overridePrimaryKeys;
}

@Override
public void setSyntheticIdentities(String[] syntheticIdentities) {
this.syntheticIdentities = syntheticIdentities;
}

@Override
public final String[] getSyntheticIdentities() {
if (syntheticIdentities == null) {
syntheticIdentities = new String[0];
}
return syntheticIdentities;
}

@Override
public final void setConfiguredEnumTypes(List<EnumType> configuredEnumTypes) {
this.configuredEnumTypes = configuredEnumTypes;
Expand Down
17 changes: 0 additions & 17 deletions jOOQ-meta/src/main/java/org/jooq/util/AbstractTableDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

import org.jooq.Record;
import org.jooq.Table;
import org.jooq.tools.JooqLogger;

/**
* A base implementation for table definitions.
Expand All @@ -61,8 +60,6 @@ public abstract class AbstractTableDefinition
extends AbstractElementContainerDefinition<ColumnDefinition>
implements TableDefinition {

private static final JooqLogger log = JooqLogger.getLogger(AbstractTableDefinition.class);

private List<ParameterDefinition> parameters;
private TableDefinition parentTable;
private List<TableDefinition> childTables;
Expand Down Expand Up @@ -178,18 +175,4 @@ protected List<ColumnDefinition> getElements0() throws SQLException {
protected List<ParameterDefinition> getParameters0() {
return Collections.emptyList();
}

protected ColumnDefinition applySyntheticIdentities(ColumnDefinition columnDefinition) {
if (!columnDefinition.isIdentity() && getDatabase().isSyntheticIdentity(columnDefinition)) {
log.info("Synthetic Identity: " + columnDefinition.getQualifiedName());
return new DefaultColumnDefinition(
columnDefinition.getContainer(),
columnDefinition.getName(),
columnDefinition.getPosition(),
columnDefinition.getType(),
true,
columnDefinition.getComment());
}
return columnDefinition;
}
}
24 changes: 12 additions & 12 deletions jOOQ-meta/src/main/java/org/jooq/util/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,6 @@ public interface Database {
*/
String[] getSyntheticPrimaryKeys();

/**
* Columns matching these regular expressions will be considered as identity
* columns in generated code.
*/
void setSyntheticIdentities(String[] syntheticIdentityPattern);

/**
* Returns true if the given column is considered a synthetic identity column
* as defined by the pattern(s) passed to setSyntheticIdentities [#5360]
*/
boolean isSyntheticIdentity(ColumnDefinition columnDefinition);

/**
* Unique keys matching these regular expressions will be considered as
* primary keys in generated code.
Expand All @@ -448,6 +436,18 @@ public interface Database {
*/
String[] getOverridePrimaryKeys();

/**
* Columns matching these regular expressions will be considered as identity
* columns in generated code.
*/
void setSyntheticIdentities(String[] syntheticIdentities);

/**
* Columns matching these regular expressions will be considered as identity
* columns in generated code.
*/
String[] getSyntheticIdentities();

/**
* Database objects matching any of these field names will be generated as
* custom types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

package org.jooq.util;

import static java.util.Collections.singletonList;

import java.util.List;

/**
Expand All @@ -61,7 +63,13 @@ public DefaultColumnDefinition(TableDefinition table, String name, int position,
super(table, name, position, type, comment);

this.position = position;
this.isIdentity = isIdentity;
this.isIdentity = isIdentity || isSyntheticIdentity(this);
}

private static boolean isSyntheticIdentity(DefaultColumnDefinition column) {
AbstractDatabase db = (AbstractDatabase) column.getDatabase();
String[] syntheticIdentities = db.getSyntheticIdentities();
return !db.filterExcludeInclude(singletonList(column), null, syntheticIdentities, db.getFilters()).isEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
null
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
null
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
|| defaultString(record.get(Columns.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval"),
record.get(Columns.REMARKS));

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
null
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
record.get(Columns.COLUMN_COMMENT)
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
record.get(PG_DESCRIPTION.DESCRIPTION)
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
record.get(PG_DESCRIPTION.DESCRIPTION)
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public List<ColumnDefinition> getElements0() throws SQLException {
null
);

result.add(applySyntheticIdentities(column));
}
result.add(column);
}

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public List<ColumnDefinition> getElements0() throws SQLException {
null
);

result.add(applySyntheticIdentities(column));
result.add(column);
}

return result;
Expand Down
12 changes: 6 additions & 6 deletions jOOQ-meta/src/main/resources/xsd/jooq-codegen-3.9.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,6 @@
-->
<element name="recordTimestampFields" type="string" default="" minOccurs="0" maxOccurs="1" />

<!--
A regular expression matching all columns that represent identities
To be used if columns are not detected as automatically as identities
-->
<element name="syntheticIdentities" type="string" default="" minOccurs="0" maxOccurs="1"/>

<!--
A regular expression matching all columns that participate in "synthetic" primary keys,
which should be placed on generated UpdatableRecords, to be used with
Expand Down Expand Up @@ -472,6 +466,12 @@
-->
<element name="overridePrimaryKeys" type="string" default="" minOccurs="0" maxOccurs="1" />

<!--
A regular expression matching all columns that represent identities
To be used if columns are not detected as automatically as identities.
-->
<element name="syntheticIdentities" type="string" default="" minOccurs="0" maxOccurs="1"/>

<!--
Generate java.sql.Timestamp fields for DATE columns. This is
particularly useful for Oracle databases
Expand Down

0 comments on commit 2cc5194

Please sign in to comment.