use of com.github.drinkjava2.jdialects.model.Column in project jDialects by drinkjava2.
the class DDLUtils method transferTableToObjectList.
/**
* Transfer table to a mixed DDL String or TableGenerator Object list
*/
private static void transferTableToObjectList(Dialect dialect, Table t, List<Object> objectResultList) {
DDLFeatures features = dialect.ddlFeatures;
StringBuilder buf = new StringBuilder();
boolean hasPkey = false;
String pkeys = "";
String tableName = t.getTableName();
Map<String, Column> columns = t.getColumns();
// Reserved words check
dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
for (Column col : columns.values()) {
dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
dialect.checkReservedWords(col.getPkeyName());
dialect.checkReservedWords(col.getUniqueConstraintName());
}
for (Column col : columns.values()) {
// autoGenerator, only support sequence or table for "Auto" type
if (col.getAutoGenerator()) {
if (features.supportsSequences || features.supportsPooledSequences) {
objectResultList.add(new Sequence(GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, GlobalIdGenerator.JDIALECTS_IDGEN_TABLE, 1, 1));
} else {
// GlobalIdGenerator
objectResultList.add(new GlobalIdGenerator());
}
}
// foreign keys
if (!StrUtils.isEmpty(col.getFkeyReferenceTable()))
objectResultList.add(new FKeyConstraint(tableName, col.getColumnName(), col.getFkeyReferenceTable(), col.getFkeyReferenceColumns()));
}
// sequence
for (Sequence seq : t.getSequences().values()) objectResultList.add(seq);
// tableGenerator
for (TableGenerator tg : t.getTableGenerators().values()) objectResultList.add(tg);
// check and cache prime keys
for (Column col : columns.values()) {
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 (Column c : columns.values()) {
if (c.getColumnType() == null)
DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
// column definition
buf.append(c.getColumnName()).append(" ");
// Identity or autoGenerator+supportIdentity
if (c.getIdentity() && !features.supportsIdentityColumns)
DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
if (c.getIdentity()) {
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.getNotNull())
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()));
}
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(")");
// type or engine for MariaDB & MySql
buf.append(dialect.engine());
objectResultList.add(buf.toString());
// unique constraint
for (Column column : columns.values()) addUniqueConstraintDDL(objectResultList, dialect, tableName, column);
// 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 (Column c : columns.values()) {
if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
}
}
use of com.github.drinkjava2.jdialects.model.Column in project jDialects by drinkjava2.
the class TableTest method checkModel.
private static Table checkModel() {
// column check
Table t = new Table("testTable");
t.column("s1").STRING(20).unique().notNull().check("s1>5");
t.column("s2").STRING(20).unique().check("s2>5");
t.column("s3").STRING(20).unique("uname1").notNull().check("s3>5");
t.column("s4").STRING(20).unique("uname2").check("s4>5");
return t;
}
use of com.github.drinkjava2.jdialects.model.Column in project jDialects by drinkjava2.
the class TableTest method testFKEY.
@Test
public void testFKEY() {
// FKEY
Table t = new Table("master");
t.column("id").INTEGER().pkey();
t.column("name").STRING(20);
t.column("address").INTEGER().pkey();
Table t2 = new Table("child");
t2.column("id").INTEGER().pkey();
t2.column("address2").VARCHAR(20).fkey("master", "name", "address");
t2.column("masterid3").VARCHAR(20).fkey("master", "id");
t2.column("aaaaa").VARCHAR(20).fkey("master", "name", "address");
printAllDialectsDDLs(t, t2);
}
Aggregations