Search in sources :

Example 26 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class GBaseJdbcExpert 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");
            } else if (mf.getColumnType() == ColType.TIMESTAMP) {
                sb.append(" NULL");
            }
            if (mf.isAutoIncreasement())
                sb.append(" AUTO_INCREMENT");
            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.hasMeta(META_ENGINE)) {
        sb.append(" ENGINE=" + en.getMeta(META_ENGINE));
    }
    // 默认采用 UTF-8 编码
    if (en.hasMeta(META_CHARSET)) {
        sb.append(" CHARSET=" + en.getMeta(META_CHARSET));
    } else {
        sb.append(" CHARSET=utf8");
    }
    // 表名注释
    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;
}
Also used : MappingField(org.nutz.dao.entity.MappingField) Sql(org.nutz.dao.sql.Sql)

Example 27 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class HsqldbJdbcExpert 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 // 普通字段
        {
            if (mf.isUnsigned())
                sb.append(" UNSIGNED");
            if (// 自增与非空,不允许同时使用!
            mf.isAutoIncreasement())
                sb.append(" GENERATED BY DEFAULT AS IDENTITY(START WITH 1)");
            else if (mf.isNotNull())
                sb.append(" NOT NULL");
            if (mf.hasDefaultValue())
                addDefaultValue(sb, mf);
        }
        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, ')');
    // 执行创建语句
    dao.execute(Sqls.create(sb.toString()));
    // 创建索引
    dao.execute(createIndexs(en).toArray(new Sql[0]));
    // 创建关联表
    createRelation(dao, en);
    // 添加注释(表注释与字段注释)
    addComment(dao, en);
    return true;
}
Also used : MappingField(org.nutz.dao.entity.MappingField) Sql(org.nutz.dao.sql.Sql)

Example 28 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class MysqlJdbcExpert 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");
            } else if (mf.getColumnType() == ColType.TIMESTAMP) {
                sb.append(" NULL");
            }
            if (mf.isAutoIncreasement())
                sb.append(" AUTO_INCREMENT");
            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.hasMeta(META_ENGINE)) {
        sb.append(" ENGINE=" + en.getMeta(META_ENGINE));
    }
    // 默认采用 UTF-8 编码
    if (en.hasMeta(META_CHARSET)) {
        sb.append(" CHARSET=" + en.getMeta(META_CHARSET));
    } else {
        sb.append(" CHARSET=utf8");
    }
    // 表名注释
    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;
}
Also used : MappingField(org.nutz.dao.entity.MappingField) Sql(org.nutz.dao.sql.Sql)

Example 29 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class NutPojoMaker method makeInsert.

public Pojo makeInsert(final Entity<?> en) {
    Pojo pojo = Pojos.pojo(expert, en, SqlType.INSERT);
    pojo.setEntity(en);
    pojo.append(Pojos.Items.entityTableName());
    pojo.append(Pojos.Items.insertFields());
    pojo.append(Pojos.Items.insertValues());
    if (expert.isSupportAutoIncrement()) {
        MappingField mf = en.getIdField();
        if (mf != null && mf.isAutoIncreasement()) {
            if (expert.isSupportGeneratedKeys()) {
                pojo.setAfter(new GeneratedKeys());
                pojo.getContext().attr("RETURN_GENERATED_KEYS", true);
            }
        }
    }
    return pojo;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) MappingField(org.nutz.dao.entity.MappingField)

Example 30 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class EntityParsingTest method test_override_field.

@Test
public void test_override_field() {
    Entity<?> en = en(Pet2.class);
    MappingField ef = en.getField("nickName");
    assertEquals("alias", ef.getColumnName());
    assertEquals(1, en.cloneBeforeInsertMacroes().size());
}
Also used : MappingField(org.nutz.dao.entity.MappingField) Test(org.junit.Test)

Aggregations

MappingField (org.nutz.dao.entity.MappingField)33 Sql (org.nutz.dao.sql.Sql)14 ArrayList (java.util.ArrayList)12 SQLException (java.sql.SQLException)5 PreparedStatement (java.sql.PreparedStatement)4 DaoException (org.nutz.dao.DaoException)4 Connection (java.sql.Connection)3 Chain (org.nutz.dao.Chain)3 ConnCallback (org.nutz.dao.ConnCallback)3 JdbcExpert (org.nutz.dao.jdbc.JdbcExpert)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)2 Date (java.util.Date)1 Set (java.util.Set)1 FieldFilter (org.nutz.dao.FieldFilter)1 FieldMatcher (org.nutz.dao.FieldMatcher)1