use of com.github.drinkjava2.jdialects.model.FKeyModel in project jDialects by drinkjava2.
the class DDLCreateUtils method transferTableToObjectList.
/**
* Transfer table to a mixed DDL String or TableGen Object list
*/
/**
* @param dialect
* @param t
* @param objectResultList
*/
private static void transferTableToObjectList(Dialect dialect, TableModel t, List<Object> objectResultList) {
DDLFeatures features = dialect.ddlFeatures;
StringBuilder buf = new StringBuilder();
boolean hasPkey = false;
String pkeys = "";
String tableName = t.getTableName();
List<ColumnModel> columns = t.getColumns();
// Reserved words check
dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
// check index names
List<IndexModel> idexChks = t.getIndexConsts();
if (idexChks != null && !idexChks.isEmpty())
for (IndexModel index : idexChks) dialect.checkReservedWords(index.getName());
// check unique names
List<UniqueModel> ukChks = t.getUniqueConsts();
if (ukChks != null && !ukChks.isEmpty())
for (UniqueModel unique : ukChks) dialect.checkReservedWords(unique.getName());
// check Fkey names
List<FKeyModel> fkeyChks = t.getFkeyConstraints();
if (fkeyChks != null && !fkeyChks.isEmpty())
for (FKeyModel fkey : fkeyChks) dialect.checkReservedWords(fkey.getFkeyName());
for (// check column names
ColumnModel col : // check column names
columns) dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
// idGenerator
for (IdGenerator idGen : t.getIdGenerators()) objectResultList.add(idGen);
// Foreign key
for (FKeyModel fkey : t.getFkeyConstraints()) objectResultList.add(fkey);
// check and cache prime keys
for (ColumnModel col : columns) {
if (col.getTransientable())
continue;
if (col.getPkey()) {
hasPkey = true;
if (StrUtils.isEmpty(pkeys))
pkeys = col.getColumnName();
else
pkeys += "," + col.getColumnName();
}
}
// create table
buf.append(hasPkey ? dialect.ddlFeatures.createTableString : dialect.ddlFeatures.createMultisetTableString).append(" ").append(tableName).append(" ( ");
for (ColumnModel c : columns) {
if (c.getTransientable())
continue;
if (c.getColumnType() == null)
DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
// column definition
buf.append(c.getColumnName()).append(" ");
// Identity
if (GenerationType.IDENTITY.equals(c.getIdGenerationType()) && !features.supportsIdentityColumns)
DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
// Column type definition
if (GenerationType.IDENTITY.equals(c.getIdGenerationType())) {
if (features.hasDataTypeInIdentityColumn)
buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
buf.append(' ');
if (Type.BIGINT.equals(c.getColumnType()))
buf.append(features.identityColumnStringBigINT);
else
buf.append(features.identityColumnString);
} else {
buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
// Default
String defaultValue = c.getDefaultValue();
if (defaultValue != null) {
buf.append(" default ").append(defaultValue);
}
// Not null
if (!c.getNullable())
buf.append(" not null");
else
buf.append(features.nullColumnString);
}
// Check
if (!StrUtils.isEmpty(c.getCheck())) {
if (features.supportsColumnCheck)
buf.append(" check (").append(c.getCheck()).append(")");
else
logger.warn("Ignore unsupported check setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getCheck());
}
// Comments
if (c.getComment() != null) {
if (StrUtils.isEmpty(features.columnComment) && !features.supportsCommentOn)
logger.warn("Ignore unsupported comment setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getComment());
else
buf.append(StrUtils.replace(features.columnComment, "_COMMENT", c.getComment()));
}
// tail String
if (!StrUtils.isEmpty(c.getTail()))
buf.append(c.getTail());
buf.append(",");
}
// PKEY
if (!StrUtils.isEmpty(pkeys)) {
buf.append(" primary key (").append(pkeys).append("),");
}
// Table Check
if (!StrUtils.isEmpty(t.getCheck())) {
if (features.supportsTableCheck)
buf.append(" check (").append(t.getCheck()).append("),");
else
logger.warn("Ignore unsupported table check setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getCheck());
}
buf.setLength(buf.length() - 1);
buf.append(")");
// Engine for MariaDB & MySql only, for example "engine=innoDB"
String tableTypeString = features.tableTypeString;
if (!StrUtils.isEmpty(tableTypeString) && !DDLFeatures.NOT_SUPPORT.equals(tableTypeString)) {
buf.append(tableTypeString);
// EngineTail, for example:" DEFAULT CHARSET=utf8"
if (!StrUtils.isEmpty(t.getEngineTail()))
buf.append(t.getEngineTail());
}
objectResultList.add(buf.toString());
// table comment on
if (t.getComment() != null) {
if (features.supportsCommentOn)
objectResultList.add("comment on table " + t.getTableName() + " is '" + t.getComment() + "'");
else
logger.warn("Ignore unsupported table comment setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getComment());
}
// column comment on
for (ColumnModel c : columns) {
if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
}
// index
buildIndexDLL(dialect, objectResultList, t);
// unique
buildUniqueDLL(dialect, objectResultList, t);
}
use of com.github.drinkjava2.jdialects.model.FKeyModel in project jDialects by drinkjava2.
the class DebugUtils method getFkeyDebugInfo.
public static String getFkeyDebugInfo(TableModel t) {
StringBuilder sb = new StringBuilder();
sb.append("Fkeys:\r");
for (FKeyModel k : t.getFkeyConstraints()) {
sb.append("FkeyName=" + k.getFkeyName());
sb.append(", ColumnNames=" + k.getColumnNames());
sb.append(", RefTableAndColumns=" + Arrays.deepToString(k.getRefTableAndColumns()));
sb.append("\r");
}
return sb.toString();
}
use of com.github.drinkjava2.jdialects.model.FKeyModel in project jDialects by drinkjava2.
the class DDLCreateUtils method toCreateDDL.
/**
* Transfer tables to DDL by given dialect and without format it, if want get a
* formatted DDL, use DDLFormatter.format(DDLs) method to format it
*/
public static String[] toCreateDDL(Dialect dialect, TableModel... tables) {
// Store mixed DDL String, TableGen Object, SequenceGen Object ...
List<Object> objectResultList = new ArrayList<Object>();
for (TableModel table : tables) transferTableToObjectList(dialect, table, objectResultList);
boolean hasAutoIdGenerator = false;
for (TableModel table : tables) {
for (ColumnModel column : table.getColumns()) if (GenerationType.AUTO.equals(column.getIdGenerationType())) {
hasAutoIdGenerator = true;
break;
}
for (IdGenerator idGens : table.getIdGenerators()) if (hasAutoIdGenerator || idGens.dependOnAutoIdGenerator()) {
hasAutoIdGenerator = true;
break;
}
}
List<String> stringResultList = new ArrayList<String>();
List<TableIdGenerator> tbGeneratorList = new ArrayList<TableIdGenerator>();
List<SequenceIdGenerator> sequenceList = new ArrayList<SequenceIdGenerator>();
List<FKeyModel> fKeyConstraintList = new ArrayList<FKeyModel>();
for (Object strOrObj : objectResultList) {
if (!StrUtils.isEmpty(strOrObj)) {
if (strOrObj instanceof String)
stringResultList.add((String) strOrObj);
else if (strOrObj instanceof TableIdGenerator)
tbGeneratorList.add((TableIdGenerator) strOrObj);
else if (strOrObj instanceof SequenceIdGenerator)
sequenceList.add((SequenceIdGenerator) strOrObj);
else if (strOrObj instanceof FKeyModel)
fKeyConstraintList.add((FKeyModel) strOrObj);
}
}
if (hasAutoIdGenerator) {
IdGenerator realIdGen = AutoIdGenerator.INSTANCE.getSequenceOrTableIdGenerator(dialect);
if (realIdGen instanceof TableIdGenerator)
tbGeneratorList.add((TableIdGenerator) realIdGen);
else if (realIdGen instanceof SequenceIdGenerator)
sequenceList.add((SequenceIdGenerator) realIdGen);
else
throw new DialectException("Unknow exception happen for realIdGen, please report this bug");
}
buildSequenceDDL(dialect, stringResultList, sequenceList);
buildTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
outputFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
String[] result = stringResultList.toArray(new String[stringResultList.size()]);
if (Dialect.getGlobalAllowShowSql())
Dialect.logger.info("Create DDL:\r" + StrUtils.arrayToString(result, "\r"));
return result;
}
use of com.github.drinkjava2.jdialects.model.FKeyModel in project jDialects by drinkjava2.
the class DDLCreateUtils method outputFKeyConstraintDDL.
private static void outputFKeyConstraintDDL(Dialect dialect, List<String> stringList, List<FKeyModel> trueList) {
if (DDLFeatures.NOT_SUPPORT.equals(dialect.ddlFeatures.addForeignKeyConstraintString)) {
logger.warn("Dialect \"" + dialect + "\" does not support foreign key setting, settings be ignored");
return;
}
for (FKeyModel t : trueList) {
if (!t.getDdl())
// if ddl is false, skip
continue;
/*
* ADD CONSTRAINT _FKEYNAME FOREIGN KEY _FKEYNAME (_FK1, _FK2) REFERENCES
* _REFTABLE (_REF1, _REF2)
*/
String constName = t.getFkeyName();
if (StrUtils.isEmpty(constName))
constName = "fk_" + t.getTableName().toLowerCase() + "_" + StrUtils.replace(StrUtils.listToString(t.getColumnNames()), ",", "_");
String[] refTableAndColumns = t.getRefTableAndColumns();
DialectException.assureNotNull(refTableAndColumns);
String fkeyTemplate;
if (refTableAndColumns.length == 1)
fkeyTemplate = dialect.ddlFeatures.addFKeyRefPkeyString;
else
fkeyTemplate = dialect.ddlFeatures.addForeignKeyConstraintString;
fkeyTemplate = StrUtils.replace(fkeyTemplate, "_FK1, _FK2", StrUtils.listToString(t.getColumnNames()));
fkeyTemplate = StrUtils.replace(fkeyTemplate, "_REF1, _REF2", StrUtils.arrayToStringButSkipFirst(t.getRefTableAndColumns()));
fkeyTemplate = StrUtils.replace(fkeyTemplate, "_REFTABLE", t.getRefTableAndColumns()[0]);
fkeyTemplate = StrUtils.replace(fkeyTemplate, "_FKEYNAME", constName);
String tail = StrUtils.isEmpty(t.getFkeyTail()) ? "" : " " + t.getFkeyTail();
stringList.add("alter table " + t.getTableName() + " " + fkeyTemplate + tail);
}
}
use of com.github.drinkjava2.jdialects.model.FKeyModel in project jDialects by drinkjava2.
the class DDLDropUtils method outputDropFKeyConstraintDDL.
private static void outputDropFKeyConstraintDDL(Dialect dialect, List<String> stringResultList, List<FKeyModel> trueList) {
if (DDLFeatures.NOT_SUPPORT.equals(dialect.ddlFeatures.addForeignKeyConstraintString))
return;
for (FKeyModel t : trueList) {
if (!t.getDdl())
// if ddl is false, skip
continue;
String dropStr = dialect.ddlFeatures.dropForeignKeyString;
String constName = t.getFkeyName();
if (StrUtils.isEmpty(constName))
constName = "fk_" + t.getTableName().toLowerCase() + "_" + StrUtils.replace(StrUtils.listToString(t.getColumnNames()), ",", "_");
if (DDLFeatures.NOT_SUPPORT.equals(dropStr))
DialectException.throwEX("Dialect \"" + dialect + "\" does not support drop foreign key, for setting: \"" + "fk_" + constName + "\"");
stringResultList.add(0, "alter table " + t.getTableName() + " " + dropStr + " " + constName);
}
}
Aggregations