me.icymint.libra.jdbc.dialect.MySqlDialect 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;
/**
* MYSQL方言。
*
* @author Daniel Yu
* @since 2013-3-14
*
*/
public class MySqlDialect 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("AUTO_INCREMENT ");
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 "SHOW TABLES LIKE '" + tableName(t) + "'";
}
@Override
public String getDatabaseProductName() {
return "MySQL";
}
}