Search in sources :

Example 11 with MappingField

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

the class UpdateFieldsPItem method joinSql.

public void joinSql(Entity<?> en, StringBuilder sb) {
    List<MappingField> mfs = _mfs(en);
    sb.append(" SET ");
    for (MappingField mf : mfs) sb.append(mf.getColumnNameInSql()).append("=?,");
    sb.setCharAt(sb.length() - 1, ' ');
}
Also used : MappingField(org.nutz.dao.entity.MappingField)

Example 12 with MappingField

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

the class BetweenExpression method joinAdaptor.

public int joinAdaptor(Entity<?> en, ValueAdaptor[] adaptors, int off) {
    MappingField mf = _field(en);
    if (null != mf) {
        adaptors[off++] = mf.getAdaptor();
        adaptors[off++] = mf.getAdaptor();
    } else {
        adaptors[off++] = Jdbcs.getAdaptorBy(min);
        adaptors[off++] = Jdbcs.getAdaptorBy(max);
    }
    return off;
}
Also used : MappingField(org.nutz.dao.entity.MappingField)

Example 13 with MappingField

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

the class OracleJdbcExpert 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(" NOT NULL UNIQUE");
        } else // 普通字段
        {
            if (mf.isPk() && en.getPks().size() == 1)
                sb.append(" primary key ");
            if (mf.isNotNull())
                sb.append(" NOT NULL");
            if (mf.hasDefaultValue() && mf.getColumnType() != ColType.BOOLEAN)
                addDefaultValue(sb, mf);
            if (// 有点暴力
            mf.isUnsigned() && mf.getColumnType() != ColType.BOOLEAN)
                sb.append(" Check ( ").append(mf.getColumnNameInSql()).append(" >= 0)");
        }
        sb.append(',');
    }
    // 结束表字段设置
    sb.setCharAt(sb.length() - 1, ')');
    List<Sql> sqls = new ArrayList<Sql>();
    sqls.add(Sqls.create(sb.toString()));
    // 创建复合主键
    List<MappingField> pks = en.getPks();
    if (pks.size() > 1) {
        StringBuilder pkNames = new StringBuilder();
        for (MappingField pk : pks) {
            pkNames.append(pk.getColumnName()).append(',');
        }
        pkNames.setLength(pkNames.length() - 1);
        String pkNames2 = makePksName(en);
        String sql = String.format("alter table %s add constraint primary_key_%s primary key (%s)", en.getTableName(), pkNames2, pkNames);
        sqls.add(Sqls.create(sql));
    }
    // 处理AutoIncreasement
    for (MappingField mf : en.getMappingFields()) {
        if (!mf.isAutoIncreasement())
            continue;
        // 序列
        sqls.add(Sqls.create(gSQL(CSEQ, en.getTableName(), mf.getColumnName())));
        // 触发器
        sqls.add(Sqls.create(gSQL(CTRI, en.getTableName(), mf.getColumnName())));
    }
    // 创建索引
    sqls.addAll(createIndexs(en));
    // TODO 详细处理Clob
    // TODO 详细处理Blob
    // 执行创建语句
    dao.execute(sqls.toArray(new Sql[sqls.size()]));
    // 创建关联表
    createRelation(dao, en);
    // 添加注释(表注释与字段注释)
    addComment(dao, en);
    return true;
}
Also used : ArrayList(java.util.ArrayList) MappingField(org.nutz.dao.entity.MappingField) Sql(org.nutz.dao.sql.Sql)

Example 14 with MappingField

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

the class OracleJdbcExpert method dropEntity.

@Override
public boolean dropEntity(Dao dao, Entity<?> en) {
    if (super.dropEntity(dao, en)) {
        if (en.getPks().isEmpty())
            return true;
        List<Sql> sqls = new ArrayList<Sql>();
        for (MappingField pk : en.getPks()) {
            if (pk.isAutoIncreasement()) {
                String sql = gSQL(DSEQ, en.getTableName(), pk.getColumnName());
                sqls.add(Sqls.create(sql));
            }
        }
        try {
            dao.execute(sqls.toArray(new Sql[sqls.size()]));
            return true;
        } catch (Exception e) {
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) MappingField(org.nutz.dao.entity.MappingField) SQLException(java.sql.SQLException) Sql(org.nutz.dao.sql.Sql)

Example 15 with MappingField

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

the class SQLiteJdbcExpert method createEntity.

@Override
public boolean createEntity(Dao dao, Entity<?> en) {
    StringBuilder sb = new StringBuilder("CREATE TABLE " + en.getTableName() + "(");
    if (en.getPks().size() > 1 && en.getPkType() == PkType.ID) {
        return false;
    }
    // 创建字段
    boolean mPks = en.getPks().size() > 1;
    for (MappingField mf : en.getMappingFields()) {
        if (mf.isReadonly())
            continue;
        sb.append('\n').append(mf.getColumnNameInSql());
        // AUTOINCREMENT),但这样就无法定义多主键!!
        if (mf.isId() && en.getPkType() == PkType.ID) {
            sb.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
            continue;
        } 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.isPk() && !mPks) {
                // 复合主键需要另外定义
                sb.append(" PRIMARY KEY");
            }
            if (mf.hasDefaultValue())
                addDefaultValue(sb, mf);
        }
        sb.append(',');
    }
    // 创建主键
    List<MappingField> pks = en.getPks();
    if (mPks) {
        sb.append('\n');
        sb.append("constraint pk_").append(en.getTableName()).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);
    return true;
}
Also used : MappingField(org.nutz.dao.entity.MappingField) Sql(org.nutz.dao.sql.Sql)

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