me.icymint.libra.jdbc.dialect.SqlServerDialect 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;
/**
* SQLSERVER方言。
*
* @author Daniel Yu
* @since 2013-3-14
*
*/
public class SqlServerDialect 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("IDENTITY ");
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) {
return "SELECT NAME FROM SYSOBJECTS WHERE NAME='" + t.toString()
+ "' AND TYPE='U'";
}
@Override
public String getDatabaseProductName() {
return "Microsoft SQL Server";
}
@Override
protected String tableName(Table t) {
return "dbo." + t.toString();
}
}