use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class PsqlJdbcExpert method createEntity.
public boolean createEntity(Dao dao, Entity<?> en) {
StringBuilder sb = new StringBuilder("CREATE TABLE " + en.getTableName() + "(");
// 创建字段
for (MappingField mf : en.getMappingFields()) {
if (mf.isReadonly())
continue;
sb.append('\n').append(mf.getColumnNameInSql());
// 自增主键特殊形式关键字
if (mf.isId() && mf.isAutoIncreasement()) {
sb.append(" SERIAL");
} else {
sb.append(' ').append(evalFieldType(mf));
// 非主键的 @Name,应该加入唯一性约束
if (mf.isName() && en.getPkType() != PkType.NAME) {
sb.append(" UNIQUE NOT NULL");
} else // 普通字段
{
if (mf.isUnsigned())
sb.append(" UNSIGNED");
if (mf.isNotNull())
sb.append(" NOT NULL");
if (mf.isAutoIncreasement())
throw Lang.noImplement();
if (mf.hasDefaultValue())
addDefaultValue(sb, mf);
}
}
sb.append(',');
}
// 创建主键
List<MappingField> pks = en.getPks();
if (!pks.isEmpty()) {
sb.append('\n');
sb.append(String.format("CONSTRAINT %s_pkey PRIMARY KEY (", en.getTableName().replace('.', '_').replace('"', '_')));
for (MappingField pk : pks) {
sb.append(pk.getColumnNameInSql()).append(',');
}
sb.setCharAt(sb.length() - 1, ')');
sb.append("\n ");
}
// 结束表字段设置
sb.setCharAt(sb.length() - 1, ')');
// 执行创建语句
dao.execute(Sqls.create(sb.toString()));
// 创建索引
dao.execute(createIndexs(en).toArray(new Sql[0]));
// 创建关联表
createRelation(dao, en);
// 添加注释(表注释与字段注释)
addComment(dao, en);
return true;
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class Sqlserver2005JdbcExpert method addComment.
private void addComment(Dao dao, Entity<?> en, String commentColumn) {
// 字段注释
if (en.hasColumnComment()) {
List<Sql> sqls = new ArrayList<Sql>();
for (MappingField mf : en.getMappingFields()) {
if (mf.hasColumnComment()) {
Sql columnCommentSQL = Sqls.create(commentColumn);
columnCommentSQL.vars().set("table", en.getTableName()).set("column", mf.getColumnName()).set("columnComment", mf.getColumnComment());
sqls.add(columnCommentSQL);
}
}
// 执行创建语句
dao.execute(sqls.toArray(new Sql[sqls.size()]));
}
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class AbstractJdbcExpert method makePksName.
public String makePksName(Entity<?> en) {
String name = en.getType().getAnnotation(PK.class).name();
if (Strings.isBlank(name)) {
StringBuilder sb = new StringBuilder();
for (MappingField mf : en.getPks()) {
sb.append("_").append(mf.getColumnName());
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
return name;
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class AbstractJdbcExpert method addComment.
public void addComment(Dao dao, Entity<?> en, String commentTable, String commentColumn) {
if (!en.hasTableComment() && !en.hasColumnComment()) {
return;
}
List<Sql> sqls = new ArrayList<Sql>();
// 表注释
if (en.hasTableComment()) {
Sql tableCommentSQL = Sqls.create(Strings.isBlank(commentTable) ? DEFAULT_COMMENT_TABLE : commentTable);
tableCommentSQL.vars().set("table", en.getTableName()).set("tableComment", en.getTableComment());
sqls.add(tableCommentSQL);
}
// 字段注释
if (en.hasColumnComment()) {
for (MappingField mf : en.getMappingFields()) {
if (mf.hasColumnComment()) {
Sql columnCommentSQL = Sqls.create(Strings.isBlank(commentColumn) ? DEFAULT_COMMENT_COLUMN : commentColumn);
columnCommentSQL.vars().set("table", en.getTableName()).set("column", mf.getColumnName()).set("columnComment", mf.getColumnComment());
sqls.add(columnCommentSQL);
}
}
}
// 执行创建语句
dao.execute(sqls.toArray(new Sql[sqls.size()]));
}
use of org.nutz.dao.entity.MappingField in project nutz by nutzam.
the class DerbyJdbcExpert method createEntity.
public boolean createEntity(Dao dao, Entity<?> en) {
StringBuilder sb = new StringBuilder("CREATE TABLE " + en.getTableName() + "(");
// 创建字段
for (MappingField mf : en.getMappingFields()) {
if (mf.isReadonly())
continue;
sb.append('\n').append(mf.getColumnNameInSql());
sb.append(' ').append(evalFieldType(mf));
// 非主键的 @Name,应该加入唯一性约束
if (mf.isName() && en.getPkType() != PkType.NAME) {
sb.append(" UNIQUE NOT NULL");
} else // 普通字段
{
// 下面的关于Timestamp处理,是因为MySql中第一出现Timestamp的话,如果没有设定default,数据库默认会设置为CURRENT_TIMESTAMP
if (mf.isUnsigned())
sb.append(" UNSIGNED");
if (mf.isNotNull()) {
sb.append(" NOT NULL");
}
if (mf.isAutoIncreasement())
sb.append(" generated by default as identity");
if (mf.getColumnType() == ColType.TIMESTAMP) {
if (mf.hasDefaultValue()) {
sb.append(" ").append(getDefaultValue(mf));
} else {
if (mf.isNotNull()) {
sb.append(" DEFAULT 0");
} else {
sb.append(" DEFAULT NULL");
}
}
} else {
if (mf.hasDefaultValue())
addDefaultValue(sb, mf);
}
}
if (mf.hasColumnComment()) {
sb.append(" COMMENT '").append(mf.getColumnComment()).append("'");
}
sb.append(',');
}
// 创建主键
List<MappingField> pks = en.getPks();
if (!pks.isEmpty()) {
sb.append('\n');
sb.append("PRIMARY KEY (");
for (MappingField pk : pks) {
sb.append(pk.getColumnNameInSql()).append(',');
}
sb.setCharAt(sb.length() - 1, ')');
sb.append("\n ");
}
// 结束表字段设置
sb.setCharAt(sb.length() - 1, ')');
// 表名注释
if (en.hasTableComment()) {
sb.append(" COMMENT='").append(en.getTableComment()).append("'");
}
// 执行创建语句
dao.execute(Sqls.create(sb.toString()));
// 创建索引
dao.execute(createIndexs(en).toArray(new Sql[0]));
// 创建关联表
createRelation(dao, en);
return true;
}
Aggregations