Skip to content

Commit

Permalink
0002849: Create Table maps a column originally created as an
Browse files Browse the repository at this point in the history
ENUM('y','n') to a Enum(2)
  • Loading branch information
philipmarzullo committed Jan 11, 2019
1 parent aa8bbef commit dc1ea20
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Expand Up @@ -38,7 +38,6 @@
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.IndexColumn;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.AbstractDdlBuilder;
import org.jumpmind.db.platform.DatabaseNamesConstants;
Expand Down Expand Up @@ -335,7 +334,40 @@ protected String getSqlType(Column column) {
if (column.getMappedTypeCode() == Types.TIMESTAMP && column.getScale() > 0) {
sqlType = "DATETIME(" + column.getScale() + ")";
}
if("ENUM".equalsIgnoreCase(column.getJdbcTypeName())) {
if(column.getEnumValues() != null && column.getEnumValues().length > 0) {
// Redo the enum, specifying the values returned from the database in the enumValues field
// instead of the size of the column
StringBuilder tmpSqlType = new StringBuilder();
tmpSqlType.append(column.getJdbcTypeName());
tmpSqlType.append("(");
boolean appendComma = false;
for(String s : column.getEnumValues()) {
if(appendComma) {
tmpSqlType.append(",");
}
tmpSqlType.append("'").append(s).append("'");
appendComma = true;
}
tmpSqlType.append(")");
sqlType = tmpSqlType.toString();
}
}
return sqlType;
}


public static void main(String[] args) {
MySqlDdlBuilder ddlBuilder = new MySqlDdlBuilder();
String[] s = new String[3];
s[0]="a";
s[1]="b";
s[2]="c";
Column col = new Column("enumcol", true, 12, 3, 0);
col.setEnumValues(s);
col.setJdbcTypeName("ENUM");
Table currentTable = new Table("enumtest", col);
String ddl = ddlBuilder.createTable(currentTable);
System.out.println(ddl);
}

}
Expand Up @@ -31,20 +31,25 @@
import java.util.Map;

import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.model.Trigger.TriggerType;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.AbstractJdbcDdlReader;
import org.jumpmind.db.platform.DatabaseMetaDataWrapper;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.db.sql.JdbcSqlTemplate;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.SqlTemplateSettings;
import org.jumpmind.db.util.BasicDataSourceFactory;
import org.jumpmind.properties.TypedProperties;
import org.jumpmind.security.SecurityServiceFactory;

/*
* Reads a database model from a MySql database.
Expand Down Expand Up @@ -150,8 +155,34 @@ protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object

if (column.getJdbcTypeName().equalsIgnoreCase("enum")) {
ISqlTemplate template = platform.getSqlTemplate();
// Version 8 populates TABLE_CAT, all others populate TABLE_SCHEMA
// But historically, the metaData.getCatalog() was used to provide the value for the query

// Query for version 5.5, 5.6, and 5.7
String unParsedEnums = template.queryForString("SELECT SUBSTRING(COLUMN_TYPE,5) FROM information_schema.COLUMNS"
+ " WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?", metaData.getCatalog(), (String) values.get("TABLE_NAME"), column.getName());
+ " WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?",
//metaData.getCatalog(),
//(String) values.get("TABLE_CAT"),
(String) values.get("TABLE_SCHEMA"),
(String) values.get("TABLE_NAME"), column.getName());
if(unParsedEnums == null) {
// Query for version 8.0
unParsedEnums = template.queryForString("SELECT SUBSTRING(COLUMN_TYPE,5) FROM information_schema.COLUMNS"
+ " WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?",
//metaData.getCatalog(),
(String) values.get("TABLE_CAT"),
//(String) values.get("TABLE_SCHEMA"),
(String) values.get("TABLE_NAME"), column.getName());
if(unParsedEnums == null) {
// Query originally used
unParsedEnums = template.queryForString("SELECT SUBSTRING(COLUMN_TYPE,5) FROM information_schema.COLUMNS"
+ " WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?",
metaData.getCatalog(),
//(String) values.get("TABLE_CAT"),
//(String) values.get("TABLE_SCHEMA"),
(String) values.get("TABLE_NAME"), column.getName());
}
}
if (unParsedEnums != null) {
unParsedEnums = unParsedEnums.trim();
if (unParsedEnums.startsWith("(")) {
Expand Down Expand Up @@ -295,4 +326,30 @@ public Trigger mapRow(Row row) {

return triggers;
}

public static void main(String[] args) throws SQLException {
// mariadb.db.driver=org.mariadb.jdbc.Driver
// mariadb.db.user=root
// mariadb.db.password=admin
// mariadb.root.db.url=jdbc:mysql://localhost/SymmetricRoot?tinyInt1isBit=false
// mariadb.server.db.url=jdbc:mysql://localhost/SymmetricRoot?tinyInt1isBit=false
// mariadb.client.db.url=jdbc:mysql://localhost/SymmetricClient?tinyInt1isBit=false
TypedProperties properties = new TypedProperties();
properties.put("db.driver", "org.mariadb.jdbc.Driver");
// properties.put("db.driver", "com.mysql.jdbc.Driver");
properties.put("db.user", "root");
properties.put("db.password", "my-secret-pw");
properties.put("db.url", "jdbc:mysql://localhost:3306/phil?tinyInt1isBit=false");
Connection connection = null;
MySqlDdlReader reader = new MySqlDdlReader(
new MySqlDatabasePlatform(BasicDataSourceFactory.create(properties, SecurityServiceFactory.create()),
new SqlTemplateSettings()));
Database database = reader.getDatabase(connection);
Table[] tables = database.getTables();
Table table = tables[0];
MySqlDdlBuilder ddlBuilder = new MySqlDdlBuilder();
String ddl = ddlBuilder.createTable(table);
System.out.println(ddl);
}

}

0 comments on commit dc1ea20

Please sign in to comment.