[#1137] Exclude MySQL column-level enum types when that column is overridden by a <forcedType/>
[#1237] Don't generate enum classes for columns in MySQL tables that are excluded from code generation
This commit is contained in:
parent
342f435e7f
commit
4251ec133c
@ -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);
|
||||
|
||||
@ -97,29 +97,28 @@ abstract class AbstractTypedElementDefinition<T extends Definition>
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -253,6 +253,12 @@ public interface Database {
|
||||
*/
|
||||
List<ForcedType> getConfiguredForcedTypes();
|
||||
|
||||
/**
|
||||
* Get the configured forced type object for any given {@link Definition},
|
||||
* or <code>null</code> if no {@link ForcedType} matches the definition.
|
||||
*/
|
||||
ForcedType getConfiguredForcedType(Definition definition);
|
||||
|
||||
/**
|
||||
* Database objects matching any of these regular expressions will not be
|
||||
* generated.
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -4149,12 +4149,6 @@ public class Factory implements FactoryOperations {
|
||||
@Support
|
||||
public static <T> Field<T> val(T value) {
|
||||
|
||||
// null is intercepted immediately
|
||||
// TODO [#1128] Don't do this, create a NULL bind value instead!
|
||||
// if (value == null) {
|
||||
// return (Field<T>) NULL();
|
||||
// }
|
||||
|
||||
// Prevent errors due to type erasure and unchecked invocation
|
||||
if (value instanceof Field<?>) {
|
||||
return (Field<T>) value;
|
||||
|
||||
@ -124,6 +124,7 @@ public class MySQLDataType<T> extends AbstractDataType<T> {
|
||||
|
||||
public static final MySQLDataType<String> MEDIUMTEXT = new MySQLDataType<String>(SQLDataType.CLOB, "mediumtext", "char");
|
||||
public static final MySQLDataType<String> LONGTEXT = new MySQLDataType<String>(SQLDataType.CLOB, "longtext", "char");
|
||||
public static final MySQLDataType<String> ENUM = new MySQLDataType<String>(SQLDataType.VARCHAR, "enum", "char");
|
||||
public static final MySQLDataType<String> SET = new MySQLDataType<String>(SQLDataType.VARCHAR, "set", "char");
|
||||
public static final MySQLDataType<byte[]> TINYBLOB = new MySQLDataType<byte[]>(SQLDataType.BLOB, "tinyblob", "binary");
|
||||
public static final MySQLDataType<byte[]> MEDIUMBLOB = new MySQLDataType<byte[]>(SQLDataType.BLOB, "mediumblob", "binary");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user