public String mysqlSelectDefault(String table,String column,String nullDefault) throws Exception {
return selectScalar("select column_default"
+ " from information_schema.columns where table_schema=schema() and table_name=? and column_name=?"
+ " and column_default is not null",nullDefault,table,column);
}
public boolean isAligned(Class<? extends Enum<?>> cls,String table,String column) throws Exception {
Set<String>values = EnumKit.names(cls);
Set<String>dbValues = selectMysqlEnumColumnValues(table, column);
return values.equals(dbValues);
}
public Set<String> selectMysqlEnumColumnValues(String table,String column) throws Exception {
String expression = selectScalar("SELECT SUBSTRING(COLUMN_TYPE,5) "
+ " FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=schema()"
+ " and column_type like 'ENUM%' AND TABLE_NAME=? and COLUMN_NAME=?", "", table, column);
if (expression.length() < 5) {
return Collections.emptySet();
}
return new TreeSet<>(Arrays.asList(StringKit.split(StringKit.replace(expression, new String[] { "(", ")", "'" }, new String[] { "", "", "" }))));
}
public void align(Class<? extends Enum<?>> cls,String table,String column,AlignEnumPolicy mismatchPolicy) throws Exception {
if (isAligned(cls, table, column)) {
return;
}
Enum<?>[] values = cls.getEnumConstants();
StringBuilder valueClause = new StringBuilder("(");
String comma = "";
for (Enum<?> t : values) {
valueClause.append(comma).append("'").append(t.name()).append("'");
comma = ",";
}
valueClause.append(")");
ColDef cd = getColDef(table,column);
if (! cd.isNullable()) {
valueClause.append(" not null ");
}
if (mismatchPolicy == AlignEnumPolicy.Delete) {
execute("delete from " + table + " where " + column + " not in " + valueClause);
}
execute("alter table " + table + " modify column " + column + " enum" + valueClause);
StringKit.println(cls.getName() + ".aligned for " + table + "." + column);
}
public enum AlignEnumPolicy{
Crash,Delete
}
align(CommissionFeeType.class,CommFeeRec.TABLE,CommFeeRec.TYPE);
align(CommissionStrategy.class, CompanyRec.TABLE,CompanyRec.COMMSTRATEGY);
align(CommissionStrategy.class, UserRec.TABLE,UserRec.COMMSTRATEGY);
align(DealerPrintMode.class,CustomerRec.TABLE,CustomerRec.DEALERPRINT);
align(DealerPrintMode.class,QuoteRec.TABLE,QuoteRec.DEALERPRINT);
align(DedCode.class,FamilyRec.TABLE,FamilyRec.DEDUCTIBLE);