From 4251ec133c539fa5dfdce34ea07562cb2b1b332e Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 16 Mar 2012 12:08:32 +0000 Subject: [PATCH] [#1137] Exclude MySQL column-level enum types when that column is overridden by a [#1237] Don't generate enum classes for columns in MySQL tables that are excluded from code generation --- .../java/org/jooq/util/AbstractDatabase.java | 11 ++++++ .../util/AbstractTypedElementDefinition.java | 37 +++++++++---------- .../src/main/java/org/jooq/util/Database.java | 6 +++ .../org/jooq/util/mysql/MySQLDatabase.java | 25 ++++++++++--- jOOQ-test/src/org/jooq/test/mysql/create.sql | 5 ++- jOOQ/src/main/java/org/jooq/impl/Factory.java | 6 --- .../org/jooq/util/mysql/MySQLDataType.java | 1 + 7 files changed, 59 insertions(+), 32 deletions(-) diff --git a/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java b/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java index b0dabe2d51..04d6fbf763 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java @@ -372,6 +372,17 @@ public abstract class AbstractDatabase implements Database { return result; } + @Override + public final ForcedType getConfiguredForcedType(Definition definition) { + for (ForcedType forcedType : getConfiguredForcedTypes()) { + if (definition.getQualifiedName().matches(forcedType.getExpressions())) { + return forcedType; + } + } + + return null; + } + @Override public final EnumDefinition getEnum(SchemaDefinition schema, String name) { return getEnum(schema, name, false); diff --git a/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java b/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java index 0a2b6dde06..05e538e99d 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java @@ -97,29 +97,28 @@ abstract class AbstractTypedElementDefinition Database db = container.getDatabase(); // [#677] Forced types for matching regular expressions - for (ForcedType forcedType : db.getConfiguredForcedTypes()) { - if (getQualifiedName().matches(forcedType.getExpressions())) { - log.debug("Forcing type", this + " into " + forcedType.getName()); - DataType forcedDataType = null; + ForcedType forcedType = db.getConfiguredForcedType(this); + if (forcedType != null) { + log.debug("Forcing type", this + " into " + forcedType.getName()); + DataType forcedDataType = null; - String t = definedType.getType(); - int l = definedType.getLength(); - int p = definedType.getPrecision(); - int s = definedType.getScale(); + String t = definedType.getType(); + int l = definedType.getLength(); + int p = definedType.getPrecision(); + int s = definedType.getScale(); - try { - forcedDataType = getDialectDataType(db.getDialect(), forcedType.getName(), p, s); - } catch (SQLDialectNotSupportedException ignore) {} + try { + forcedDataType = getDialectDataType(db.getDialect(), forcedType.getName(), p, s); + } catch (SQLDialectNotSupportedException ignore) {} - // [#677] SQLDataType matches are actual type-rewrites - if (forcedDataType != null) { - type = new DefaultDataTypeDefinition(db, getSchema(), forcedType.getName(), l, p, s); - } + // [#677] SQLDataType matches are actual type-rewrites + if (forcedDataType != null) { + type = new DefaultDataTypeDefinition(db, getSchema(), forcedType.getName(), l, p, s); + } - // Other forced types are UDT's, enums, etc. - else { - type = new DefaultDataTypeDefinition(db, getSchema(), t, l, p, s, forcedType.getName()); - } + // Other forced types are UDT's, enums, etc. + else { + type = new DefaultDataTypeDefinition(db, getSchema(), t, l, p, s, forcedType.getName()); } } diff --git a/jOOQ-meta/src/main/java/org/jooq/util/Database.java b/jOOQ-meta/src/main/java/org/jooq/util/Database.java index 58384078b9..39f0dddb54 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/Database.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/Database.java @@ -253,6 +253,12 @@ public interface Database { */ List getConfiguredForcedTypes(); + /** + * Get the configured forced type object for any given {@link Definition}, + * or null if no {@link ForcedType} matches the definition. + */ + ForcedType getConfiguredForcedType(Definition definition); + /** * Database objects matching any of these regular expressions will not be * generated. diff --git a/jOOQ-meta/src/main/java/org/jooq/util/mysql/MySQLDatabase.java b/jOOQ-meta/src/main/java/org/jooq/util/mysql/MySQLDatabase.java index eafeddf886..9a5c215f41 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/mysql/MySQLDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/mysql/MySQLDatabase.java @@ -229,18 +229,33 @@ public class MySQLDatabase extends AbstractDatabase { for (Record record : records) { SchemaDefinition schema = getSchema(record.getValue(Columns.TABLE_SCHEMA)); + String comment = record.getValue(Columns.COLUMN_COMMENT); String table = record.getValue(Columns.TABLE_NAME); String column = record.getValue(Columns.COLUMN_NAME); String name = table + "_" + column; String columnType = record.getValue(Columns.COLUMN_TYPE); - DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment); - for (String string : columnType.replaceAll("enum\\(|\\)", "").split(",")) { - definition.addLiteral(string.trim().replaceAll("'", "")); - } + // [#1237] Don't generate enum classes for columns in MySQL tables + // that are excluded from code generation + TableDefinition tableDefinition = getTable(schema, table); + if (tableDefinition != null) { + ColumnDefinition columnDefinition = tableDefinition.getColumn(column); - result.add(definition); + if (columnDefinition != null) { + + // [#1137] Avoid generating enum classes for enum types that + // are explicitly forced to another type + if (getConfiguredForcedType(columnDefinition) == null) { + DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment); + for (String string : columnType.replaceAll("enum\\(|\\)", "").split(",")) { + definition.addLiteral(string.trim().replaceAll("'", "")); + } + + result.add(definition); + } + } + } } return result; diff --git a/jOOQ-test/src/org/jooq/test/mysql/create.sql b/jOOQ-test/src/org/jooq/test/mysql/create.sql index 02dee6956d..14b690eb79 100644 --- a/jOOQ-test/src/org/jooq/test/mysql/create.sql +++ b/jOOQ-test/src/org/jooq/test/mysql/create.sql @@ -66,8 +66,8 @@ CREATE TABLE t_booleans ( one_zero int, true_false_lc varchar(5), true_false_uc varchar(5), - yes_no_lc varchar(3), - yes_no_uc varchar(3), + yes_no_lc enum('yes', 'no'), + yes_no_uc enum('YES', 'NO'), y_n_lc char(1), y_n_uc char(1), vc_boolean varchar(1), @@ -224,6 +224,7 @@ CREATE TABLE t_author ( CREATE TABLE t_book_details ( ID INT NOT NULL COMMENT 'The details ID', + E enum('A', 'B', 'C') COMMENT '#1237 Don''t generate an enum for this', CONSTRAINT pk_t_book_details PRIMARY KEY (ID) ) ENGINE = InnoDB diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index 6835a9460a..3542dc4110 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -4149,12 +4149,6 @@ public class Factory implements FactoryOperations { @Support public static Field val(T value) { - // null is intercepted immediately - // TODO [#1128] Don't do this, create a NULL bind value instead! -// if (value == null) { -// return (Field) NULL(); -// } - // Prevent errors due to type erasure and unchecked invocation if (value instanceof Field) { return (Field) value; diff --git a/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDataType.java b/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDataType.java index f5d27cd4d1..e3a4d6f49d 100644 --- a/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDataType.java +++ b/jOOQ/src/main/java/org/jooq/util/mysql/MySQLDataType.java @@ -124,6 +124,7 @@ public class MySQLDataType extends AbstractDataType { public static final MySQLDataType MEDIUMTEXT = new MySQLDataType(SQLDataType.CLOB, "mediumtext", "char"); public static final MySQLDataType LONGTEXT = new MySQLDataType(SQLDataType.CLOB, "longtext", "char"); + public static final MySQLDataType ENUM = new MySQLDataType(SQLDataType.VARCHAR, "enum", "char"); public static final MySQLDataType SET = new MySQLDataType(SQLDataType.VARCHAR, "set", "char"); public static final MySQLDataType TINYBLOB = new MySQLDataType(SQLDataType.BLOB, "tinyblob", "binary"); public static final MySQLDataType MEDIUMBLOB = new MySQLDataType(SQLDataType.BLOB, "mediumblob", "binary");