me.icymint.libra.jdbc.dialect.Db2NtDialect Maven / Gradle / Ivy
package me.icymint.libra.jdbc.dialect;
import me.icymint.libra.jdbc.model.Column;
import me.icymint.libra.jdbc.model.Table;
import me.icymint.libra.jdbc.query.TypeTransfer;
/**
* DB2NT方言。
*
* @author Daniel Yu
* @since 2013-3-14
*
*/
public class Db2NtDialect extends AbstractDialect {
@Override
public String createTable(Table t) {
StringBuilder sb = new StringBuilder("CREATE TABLE ").append(
tableName(t)).append(" (");
for (Column col : t.getColumns()) {
sb.append(col.toString()).append(" ")
.append(TypeTransfer.getInstance().query(col.getTypes()));
if (col.getLength() > 0) {
sb.append("(").append(col.getLength()).append(") ");
} else
sb.append(" ");
if (col.isNotNull())
sb.append("NOT NULL ");
if (col.isAutoIncrement())
sb.append("GENERATED ALWAYS AS IDENTITY (START WITH 0 INCREMENT BY 1) ");
Object dev = col.getDefaultValue();
if (dev != null) {
sb.append("DEFAULT '").append(dev).append("' ");
}
sb.append(",\n");
}
// **约束生成。
genKeys(sb, t.getKeys(), t);
genFKeys(sb, t.getForeignKeys(), t);
genUniques(sb, t.getUniques(), t);
sb.deleteCharAt(sb.lastIndexOf(",\n")).append(")");
// **索引。
genIndexes(sb, t.getIndexes(), t);
return sb.toString();
}
@Override
public String existsTable(Table t) {
String schema = t.getParent().getSchema();
if (schema == null)
schema = t.getInfo().getTemplate().getUsername();
return "SELECT NAME FROM SYSIBM.SYSTABLES WHERE NAME='"
+ t.toString().toUpperCase() + "' AND TYPE='T' AND CREATOR='"
+ schema.toUpperCase() + "'";
}
@Override
public String getDatabaseProductName() {
return "DB2/NT";
}
@Override
protected String tableName(Table t) {
String schm = t.getParent().getSchema();
if (schm != null)
return schm + "." + t.toString();
else
return super.tableName(t);
}
@Override
public String truncTable(Table t) {
return this.deleteTable(t);
}
}