[#2603] Add <includeExcludeColumns/> flag to code generation

configuration to indicate that <includes/> and <excludes/> shall also
match column names
This commit is contained in:
Lukas Eder 2013-08-16 14:55:49 +02:00
parent 66301d6574
commit 70fd391aed
15 changed files with 370 additions and 6 deletions

View File

@ -36,6 +36,7 @@
package org.jooq.util;
import static java.lang.Boolean.TRUE;
import static org.jooq.tools.StringUtils.defaultIfNull;
import static org.jooq.tools.StringUtils.defaultString;
import static org.jooq.tools.StringUtils.isBlank;
@ -233,6 +234,7 @@ public class GenerationTool {
database.setConfiguredSchemata(schemata);
database.setIncludes(new String[] { defaultString(d.getIncludes()) });
database.setExcludes(new String[] { defaultString(d.getExcludes()) });
database.setIncludeExcludeColumns(TRUE.equals(d.isIncludeExcludeColumns()));
database.setRecordVersionFields(new String[] { defaultString(d.getRecordVersionFields()) });
database.setRecordTimestampFields(new String[] { defaultString(d.getRecordTimestampFields()) });
database.setConfiguredCustomTypes(d.getCustomTypes());

View File

@ -133,6 +133,9 @@ public class JavaGenerator extends AbstractGenerator {
log.info(" dialect", database.getDialect());
log.info(" target dir", getTargetDirectory());
log.info(" target package", getTargetPackage());
log.info(" includes", Arrays.asList(database.getIncludes()));
log.info(" excludes", Arrays.asList(database.getExcludes()));
log.info(" includeExcludeColumns", database.getIncludeExcludeColumns());
log.info("----------------------------------------------------------");
log.info("");
log.info("DefaultGenerator parameters");

View File

@ -77,6 +77,7 @@ public abstract class AbstractDatabase implements Database {
private DSLContext create;
private String[] excludes;
private String[] includes;
private boolean includeExcludeColumns;
private String[] recordVersionFields;
private String[] recordTimestampFields;
private boolean supportsUnsignedTypes;
@ -257,6 +258,16 @@ public abstract class AbstractDatabase implements Database {
return includes;
}
@Override
public final void setIncludeExcludeColumns(boolean includeExcludeColumns) {
this.includeExcludeColumns = includeExcludeColumns;
}
@Override
public final boolean getIncludeExcludeColumns() {
return includeExcludeColumns;
}
@Override
public void setRecordVersionFields(String[] recordVersionFields) {
this.recordVersionFields = recordVersionFields;
@ -705,6 +716,10 @@ public abstract class AbstractDatabase implements Database {
}
private final <T extends Definition> List<T> filterExcludeInclude(List<T> definitions) {
return filterExcludeInclude(definitions, excludes, includes);
}
static final <T extends Definition> List<T> filterExcludeInclude(List<T> definitions, String[] excludes, String[] includes) {
List<T> result = new ArrayList<T>();
definitionsLoop: for (T definition : definitions) {
@ -758,7 +773,7 @@ public abstract class AbstractDatabase implements Database {
return false;
}
private final String fetchedSize(List<?> fetched, List<?> included) {
static final String fetchedSize(List<?> fetched, List<?> included) {
return fetched.size() + " (" + included.size() + " included, " + (fetched.size() - included.size()) + " excluded)";
}

View File

@ -36,6 +36,10 @@
package org.jooq.util;
import static org.jooq.util.AbstractDatabase.fetchedSize;
import static org.jooq.util.AbstractDatabase.filterExcludeInclude;
import static org.jooq.util.AbstractDatabase.getDefinition;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
@ -77,7 +81,17 @@ extends AbstractDefinition {
elements = new ArrayList<E>();
try {
elements = getElements0();
Database db = getDatabase();
List<E> e = getElements0();
// [#2603] Filter exclude / include also for table columns
if (this instanceof TableDefinition && db.getIncludeExcludeColumns()) {
elements = filterExcludeInclude(e, db.getExcludes(), db.getIncludes());
log.info("Columns fetched", fetchedSize(e, elements));
}
else {
elements = e;
}
}
catch (SQLException e) {
log.error("Error while initialising type", e);
@ -92,7 +106,7 @@ extends AbstractDefinition {
}
protected final E getElement(String name, boolean ignoreCase) {
return AbstractDatabase.getDefinition(getElements(), name, ignoreCase);
return getDefinition(getElements(), name, ignoreCase);
}
protected final E getElement(int index) {

View File

@ -217,6 +217,18 @@ public interface Database {
*/
String[] getIncludes();
/**
* Indicate whether include / exclude regular expression shall also match
* database columns.
*/
void setIncludeExcludeColumns(boolean includeExcludeColumns);
/**
* Indicate whether include / exclude regular expression shall also match
* database columns.
*/
boolean getIncludeExcludeColumns();
/**
* Table columns matching these regular expressions will be considered as
* record version fields in generated code.

View File

@ -141,6 +141,12 @@
-->
<element name="excludes" type="string" default="" minOccurs="0" maxOccurs="1" />
<!--
This flag indicates whether include / exclude patterns should also match
columns within tables.
-->
<element name="includeExcludeColumns" type="boolean" default="false" minOccurs="0" maxOccurs="1" />
<!--
All table and view columns that are used as "version" fields for
optimistic locking (A Java regular expression. Use the pipe to separate several expressions).

View File

@ -11,7 +11,8 @@
<database>
<name>org.jooq.util.h2.H2Database</name>
<includes>.*</includes>
<excludes>T_BOOK_DETAILS|SYSTEM_SEQUENCE.*</excludes>
<excludes>T_BOOK_DETAILS|SYSTEM_SEQUENCE.*|.*\.COL2|COL3</excludes>
<includeExcludeColumns>true</includeExcludeColumns>
<recordVersionFields>REC_VERSION</recordVersionFields>
<recordTimestampFields>REC_TIMESTAMP</recordTimestampFields>
<dateAsTimestamp>false</dateAsTimestamp>

View File

@ -39,6 +39,7 @@ package org.jooq.test;
import static java.util.Arrays.asList;
import static org.jooq.impl.DSL.count;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.table;
import static org.jooq.test.h2.generatedclasses.Tables.T_BOOK_TO_BOOK_STORE;
import static org.jooq.test.h2.generatedclasses.Tables.T_BOOLEANS;
import static org.jooq.test.h2.generatedclasses.Tables.T_DATES;
@ -46,6 +47,7 @@ import static org.jooq.test.h2.generatedclasses.Tables.T_EXOTIC_TYPES;
import static org.jooq.test.h2.generatedclasses.Tables.T_IDENTITY;
import static org.jooq.test.h2.generatedclasses.Tables.T_IDENTITY_PK;
import static org.jooq.test.h2.generatedclasses.Tables.T_UNSIGNED;
import static org.jooq.test.h2.generatedclasses.Tables.V_2603;
import static org.jooq.test.h2.generatedclasses.Tables.V_AUTHOR;
import static org.jooq.test.h2.generatedclasses.Tables.V_BOOK;
import static org.jooq.test.h2.generatedclasses.tables.TAuthor.FIRST_NAME;
@ -118,6 +120,7 @@ import org.jooq.test.h2.generatedclasses.tables.records.T_639NumbersTableRecord;
import org.jooq.test.h2.generatedclasses.tables.records.T_725LobTestRecord;
import org.jooq.test.h2.generatedclasses.tables.records.T_785Record;
import org.jooq.test.h2.generatedclasses.tables.records.VLibraryRecord;
import org.jooq.test.h2.generatedclasses.tables.records.V_2603Record;
import org.jooq.test.h2.generatedclasses.tables.records.XUnusedRecord;
import org.jooq.types.UByte;
import org.jooq.types.UInteger;
@ -875,4 +878,25 @@ public class H2Test extends jOOQAbstractTest<
create().execute("DROP VIEW my_view");
}
}
@Test
public void testH2V2603WithExcludedColumns() throws Exception {
// The generated table only has two columns
V_2603Record record =
create().selectFrom(V_2603)
.fetchOne();
assertEquals(2, record.size());
assertEquals(1, (int) record.getCol1());
assertEquals(4, (int) record.getCol4());
// The actual table has four columns
Record r =
create().selectFrom(table(V_2603.getName()))
.fetchOne();
assertEquals(4, r.size());
assertEquals(asList(1, 2, 3, 4), asList(r.intoArray()));
}
}

View File

@ -1,3 +1,4 @@
DROP VIEW IF EXISTS v_2603/
DROP VIEW IF EXISTS "Ää"/
DROP VIEW IF EXISTS v_author/
DROP VIEW IF EXISTS v_book/
@ -366,6 +367,10 @@ CREATE VIEW "Ää" AS
SELECT 1 AS "Öö"
/
CREATE VIEW v_2603 AS
SELECT 1 AS col1, 2 AS col2, 3 AS col3, 4 as col4
/
CREATE ALIAS f_one FOR "org.jooq.test.h2.F.fOne";/
CREATE ALIAS f_number FOR "org.jooq.test.h2.F.fNumber";/
CREATE ALIAS f317 FOR "org.jooq.test.h2.F.f317";/

View File

@ -9,7 +9,7 @@ package org.jooq.test.h2.generatedclasses;
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Public extends org.jooq.impl.SchemaImpl {
private static final long serialVersionUID = -1624130452;
private static final long serialVersionUID = -2051987060;
/**
* The singleton instance of <code>PUBLIC</code>
@ -71,6 +71,7 @@ public class Public extends org.jooq.impl.SchemaImpl {
org.jooq.test.h2.generatedclasses.tables.VLibrary.V_LIBRARY,
org.jooq.test.h2.generatedclasses.tables.VAuthor.V_AUTHOR,
org.jooq.test.h2.generatedclasses.tables.VBook.V_BOOK,
org.jooq.test.h2.generatedclasses.tables.Ää.ÄÄ);
org.jooq.test.h2.generatedclasses.tables.Ää.ÄÄ,
org.jooq.test.h2.generatedclasses.tables.V_2603.V_2603);
}
}

View File

@ -145,4 +145,9 @@ public class Tables {
* The table PUBLIC.Ää
*/
public static final org.jooq.test.h2.generatedclasses.tables.Ää ÄÄ = org.jooq.test.h2.generatedclasses.tables.Ää.ÄÄ;
/**
* The table PUBLIC.V_2603
*/
public static final org.jooq.test.h2.generatedclasses.tables.V_2603 V_2603 = org.jooq.test.h2.generatedclasses.tables.V_2603.V_2603;
}

View File

@ -0,0 +1,43 @@
/**
* This class is generated by jOOQ
*/
package org.jooq.test.h2.generatedclasses.tables;
/**
* This class is generated by jOOQ.
*/
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class V_2603 extends org.jooq.impl.TableImpl<org.jooq.test.h2.generatedclasses.tables.records.V_2603Record> {
private static final long serialVersionUID = -575713597;
/**
* The singleton instance of <code>PUBLIC.V_2603</code>
*/
public static final org.jooq.test.h2.generatedclasses.tables.V_2603 V_2603 = new org.jooq.test.h2.generatedclasses.tables.V_2603();
/**
* The class holding records for this type
*/
@Override
public java.lang.Class<org.jooq.test.h2.generatedclasses.tables.records.V_2603Record> getRecordType() {
return org.jooq.test.h2.generatedclasses.tables.records.V_2603Record.class;
}
/**
* The column <code>PUBLIC.V_2603.COL1</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.V_2603Record, java.lang.Integer> COL1 = createField("COL1", org.jooq.impl.SQLDataType.INTEGER, V_2603);
/**
* The column <code>PUBLIC.V_2603.COL4</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.V_2603Record, java.lang.Integer> COL4 = createField("COL4", org.jooq.impl.SQLDataType.INTEGER, V_2603);
/**
* No further instances allowed
*/
private V_2603() {
super("V_2603", org.jooq.test.h2.generatedclasses.Public.PUBLIC);
}
}

View File

@ -0,0 +1,45 @@
/**
* This class is generated by jOOQ
*/
package org.jooq.test.h2.generatedclasses.tables.interfaces;
/**
* This class is generated by jOOQ.
*/
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public interface IV_2603 extends java.io.Serializable {
/**
* Setter for <code>PUBLIC.V_2603.COL1</code>.
*/
public void setCol1(java.lang.Integer value);
/**
* Getter for <code>PUBLIC.V_2603.COL1</code>.
*/
public java.lang.Integer getCol1();
/**
* Setter for <code>PUBLIC.V_2603.COL4</code>.
*/
public void setCol4(java.lang.Integer value);
/**
* Getter for <code>PUBLIC.V_2603.COL4</code>.
*/
public java.lang.Integer getCol4();
// -------------------------------------------------------------------------
// FROM and INTO
// -------------------------------------------------------------------------
/**
* Load data from another generated Record/POJO implementing the common interface IV_2603
*/
public void from(org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603 from);
/**
* Copy data into another generated Record/POJO implementing the common interface IV_2603
*/
public <E extends org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603> E into(E into);
}

View File

@ -0,0 +1,58 @@
/**
* This class is generated by jOOQ
*/
package org.jooq.test.h2.generatedclasses.tables.pojos;
/**
* This class is generated by jOOQ.
*/
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class V_2603 implements org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603 {
private static final long serialVersionUID = 2079543933;
private java.lang.Integer col1;
private java.lang.Integer col4;
@Override
public java.lang.Integer getCol1() {
return this.col1;
}
@Override
public void setCol1(java.lang.Integer col1) {
this.col1 = col1;
}
@Override
public java.lang.Integer getCol4() {
return this.col4;
}
@Override
public void setCol4(java.lang.Integer col4) {
this.col4 = col4;
}
// -------------------------------------------------------------------------
// FROM and INTO
// -------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public void from(org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603 from) {
setCol1(from.getCol1());
setCol4(from.getCol4());
}
/**
* {@inheritDoc}
*/
@Override
public <E extends org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603> E into(E into) {
into.from(this);
return into;
}
}

View File

@ -0,0 +1,130 @@
/**
* This class is generated by jOOQ
*/
package org.jooq.test.h2.generatedclasses.tables.records;
/**
* This class is generated by jOOQ.
*/
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class V_2603Record extends org.jooq.impl.TableRecordImpl<org.jooq.test.h2.generatedclasses.tables.records.V_2603Record> implements org.jooq.Record2<java.lang.Integer, java.lang.Integer>, org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603 {
private static final long serialVersionUID = -764503475;
/**
* Setter for <code>PUBLIC.V_2603.COL1</code>.
*/
@Override
public void setCol1(java.lang.Integer value) {
setValue(0, value);
}
/**
* Getter for <code>PUBLIC.V_2603.COL1</code>.
*/
@Override
public java.lang.Integer getCol1() {
return (java.lang.Integer) getValue(0);
}
/**
* Setter for <code>PUBLIC.V_2603.COL4</code>.
*/
@Override
public void setCol4(java.lang.Integer value) {
setValue(1, value);
}
/**
* Getter for <code>PUBLIC.V_2603.COL4</code>.
*/
@Override
public java.lang.Integer getCol4() {
return (java.lang.Integer) getValue(1);
}
// -------------------------------------------------------------------------
// Record2 type implementation
// -------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public org.jooq.Row2<java.lang.Integer, java.lang.Integer> fieldsRow() {
return (org.jooq.Row2) super.fieldsRow();
}
/**
* {@inheritDoc}
*/
@Override
public org.jooq.Row2<java.lang.Integer, java.lang.Integer> valuesRow() {
return (org.jooq.Row2) super.valuesRow();
}
/**
* {@inheritDoc}
*/
@Override
public org.jooq.Field<java.lang.Integer> field1() {
return org.jooq.test.h2.generatedclasses.tables.V_2603.COL1;
}
/**
* {@inheritDoc}
*/
@Override
public org.jooq.Field<java.lang.Integer> field2() {
return org.jooq.test.h2.generatedclasses.tables.V_2603.COL4;
}
/**
* {@inheritDoc}
*/
@Override
public java.lang.Integer value1() {
return getCol1();
}
/**
* {@inheritDoc}
*/
@Override
public java.lang.Integer value2() {
return getCol4();
}
// -------------------------------------------------------------------------
// FROM and INTO
// -------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public void from(org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603 from) {
setCol1(from.getCol1());
setCol4(from.getCol4());
}
/**
* {@inheritDoc}
*/
@Override
public <E extends org.jooq.test.h2.generatedclasses.tables.interfaces.IV_2603> E into(E into) {
into.from(this);
return into;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached V_2603Record
*/
public V_2603Record() {
super(org.jooq.test.h2.generatedclasses.tables.V_2603.V_2603);
}
}