Skip to content

Commit

Permalink
Merge pull request jOOQ#5373 from oflege/master
Browse files Browse the repository at this point in the history
Add <syntheticIdentities> regular expression to code generator configuration
  • Loading branch information
lukaseder authored Jul 4, 2016
2 parents 48ea22d + 453f3f0 commit 7f8c397
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ else if (schema.getOutputSchema() == null) {
database.setRecordTimestampFields(new String[] { defaultString(d.getRecordTimestampFields()) });
database.setSyntheticPrimaryKeys(new String[] { defaultString(d.getSyntheticPrimaryKeys()) });
database.setOverridePrimaryKeys(new String[] { defaultString(d.getOverridePrimaryKeys()) });
database.setSyntheticIdentities(new String[] { defaultString(d.getSyntheticIdentities()) });
database.setConfiguredCustomTypes(d.getCustomTypes());
database.setConfiguredEnumTypes(d.getEnumTypes());
database.setConfiguredForcedTypes(d.getForcedTypes());
Expand Down
17 changes: 17 additions & 0 deletions jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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 @@ -120,6 +121,7 @@ public abstract class AbstractDatabase implements Database {
private String[] recordVersionFields;
private String[] recordTimestampFields;
private String[] syntheticPrimaryKeys;
private String[] syntheticIdentities;
private String[] overridePrimaryKeys;
private boolean supportsUnsignedTypes;
private boolean ignoreProcedureReturnValues;
Expand Down Expand Up @@ -688,6 +690,21 @@ 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 Down
17 changes: 17 additions & 0 deletions jOOQ-meta/src/main/java/org/jooq/util/AbstractTableDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

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

/**
* A base implementation for table definitions.
Expand All @@ -60,6 +61,8 @@ 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 @@ -175,4 +178,18 @@ 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;
}
}
12 changes: 12 additions & 0 deletions jOOQ-meta/src/main/java/org/jooq/util/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ 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 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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(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(column);
}
result.add(applySyntheticIdentities(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(column);
result.add(applySyntheticIdentities(column));
}

return result;
Expand Down
6 changes: 6 additions & 0 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,6 +438,12 @@
-->
<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

0 comments on commit 7f8c397

Please sign in to comment.