use of com.github.drinkjava2.jdialects.Dialect 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.Dialect 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.Dialect in project jDialects by drinkjava2.
the class DDLCreateUtils method buildIndexDLL.
private static void buildIndexDLL(Dialect dialect, List<Object> objectResultList, TableModel t) {
List<IndexModel> l = t.getIndexConsts();
if (l == null || l.isEmpty())
return;
String template;
if (Dialect.Teradata14Dialect.equals(dialect))
template = "create $ifUnique index $indexName ($indexValues) on " + t.getTableName();
else
template = "create $ifUnique index $indexName on " + t.getTableName() + " ($indexValues)";
for (IndexModel index : l) {
String indexname = index.getName();
if (StrUtils.isEmpty(indexname))
indexname = "IX_" + t.getTableName() + "_" + StrUtils.arrayToString(index.getColumnList(), "_");
String ifUnique = index.getUnique() ? "unique" : "";
String result = StrUtils.replace(template, "$ifUnique", ifUnique);
result = StrUtils.replace(result, "$indexName", indexname);
result = StrUtils.replace(result, "$indexValues", StrUtils.arrayToString(index.getColumnList()));
objectResultList.add(result);
}
}
use of com.github.drinkjava2.jdialects.Dialect 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);
}
}
use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.
the class DDLDropUtils method buildDropTableGeneratorDDL.
private static void buildDropTableGeneratorDDL(Dialect dialect, List<String> stringResultList, List<TableIdGenerator> tbGeneratorList) {
for (TableIdGenerator tg : tbGeneratorList) {
// @formatter:off
DialectException.assureNotEmpty(tg.getName(), "TableGen name can not be empty");
DialectException.assureNotEmpty(tg.getTable(), "TableGen tableName can not be empty of \"" + tg.getName() + "\"");
DialectException.assureNotEmpty(tg.getPkColumnName(), "TableGen pkColumnName can not be empty of \"" + tg.getName() + "\"");
DialectException.assureNotEmpty(tg.getPkColumnValue(), "TableGen pkColumnValue can not be empty of \"" + tg.getName() + "\"");
DialectException.assureNotEmpty(tg.getValueColumnName(), "TableGen valueColumnName can not be empty of \"" + tg.getName() + "\"");
// @formatter:on
}
Set<String> tableExisted = new HashSet<String>();
for (TableIdGenerator tg : tbGeneratorList) {
String tableName = tg.getTable().toLowerCase();
if (!tableExisted.contains(tableName)) {
stringResultList.add(0, dialect.dropTableDDL(tableName));
tableExisted.add(tableName);
}
}
}
Aggregations