use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.
the class DDLTest method tableCheckModel.
private static TableModel tableCheckModel() {
// table check
TableModel t = new TableModel("testTable");
t.check("s2>10");
t.column("s1").STRING(20).notNull();
t.column("s2").STRING(20);
return t;
}
use of com.github.drinkjava2.jdialects.model.Table 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.Table 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.Table 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.model.Table in project jDialects by drinkjava2.
the class DDLDropUtils method toDropDDL.
/**
* Transfer tables to drop DDL and without format it
*/
public static String[] toDropDDL(Dialect dialect, TableModel... tables) {
// resultList store mixed drop DDL + drop Ojbects
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");
}
buildDropSequenceDDL(dialect, stringResultList, sequenceList);
buildDropTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
outputDropFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
String[] result = stringResultList.toArray(new String[stringResultList.size()]);
if (Dialect.getGlobalAllowShowSql())
Dialect.logger.info("Drop DDL:\r" + StrUtils.arrayToString(result, "\r"));
return result;
}
Aggregations