Search in sources :

Example 1 with MappingField

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

the class AbstractJdbcExpert method createIndexSql.

public Sql createIndexSql(Entity<?> en, EntityIndex index) {
    StringBuilder sb = new StringBuilder();
    if (index.isUnique())
        sb.append("Create UNIQUE Index ");
    else
        sb.append("Create Index ");
    if (index.getName().contains("$"))
        sb.append(TableName.render(new CharSegment(index.getName())));
    else
        sb.append(index.getName());
    sb.append(" ON ").append(en.getTableName()).append("(");
    for (EntityField field : index.getFields()) {
        if (field instanceof MappingField) {
            MappingField mf = (MappingField) field;
            sb.append(mf.getColumnNameInSql()).append(',');
        } else {
            throw Lang.makeThrow(DaoException.class, "%s %s is NOT a mapping field, can't use as index field!!", en.getClass(), field.getName());
        }
    }
    sb.setCharAt(sb.length() - 1, ')');
    return Sqls.create(sb.toString());
}
Also used : CharSegment(org.nutz.lang.segment.CharSegment) MappingField(org.nutz.dao.entity.MappingField) EntityField(org.nutz.dao.entity.EntityField)

Example 2 with MappingField

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

the class AbstractJdbcExpert method setupEntityField.

// ====================================================================
// 下面为子类默认实现几个接口函数
public void setupEntityField(Connection conn, Entity<?> en) {
    List<MappingField> mfs = new ArrayList<MappingField>();
    for (MappingField mf : en.getMappingFields()) {
        if (mf.getTypeMirror().isEnum()) {
            mfs.add(mf);
        }
    }
    if (mfs.isEmpty())
        return;
    Statement stat = null;
    ResultSet rs = null;
    ResultSetMetaData rsmd = null;
    try {
        // 获取数据库元信息
        stat = conn.createStatement();
        rs = stat.executeQuery(createResultSetMetaSql(en));
        rsmd = rs.getMetaData();
        // 循环字段检查
        List<String> columnNames = new ArrayList<String>();
        List<String> columnLabels = new ArrayList<String>();
        int columnCount = rsmd.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            columnNames.add(rsmd.getColumnName(i));
            columnLabels.add(rsmd.getColumnLabel(i));
        }
        for (MappingField mf : mfs) {
            try {
                int ci = columnNames.indexOf(mf.getColumnName()) + 1;
                if (ci == 0) {
                    log.debugf("Can not find @Column(%s) in table/view (%s), skip checking", mf.getColumnName(), rsmd.getTableName(1));
                    continue;
                }
                // 枚举类型在数据库中的值
                if (Daos.isIntLikeColumn(rsmd, ci)) {
                    mf.setColumnType(ColType.INT);
                } else {
                    mf.setColumnType(ColType.VARCHAR);
                }
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debugf("Table '%s' doesn't exist!", en.getViewName());
    } finally // Close ResultSet and Statement
    {
        Daos.safeClose(stat, rs);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) DaoStatement(org.nutz.dao.sql.DaoStatement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) MappingField(org.nutz.dao.entity.MappingField) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Example 3 with MappingField

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

the class NutDao method insert.

public <T> T insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) {
    Object obj = Lang.first(t);
    Entity<?> en = getEntity(obj.getClass());
    List<String> names = new ArrayList<String>();
    for (MappingField mf : en.getMappingFields()) {
        Object tmp = mf.getValue(obj);
        if (ignoreNull && tmp == null) {
            continue;
        }
        if (ignoreZero && (tmp == null || (tmp instanceof Number && ((Number) tmp).intValue() == 0))) {
            continue;
        }
        if (ignoreBlankStr && (tmp instanceof CharSequence && Strings.isBlank((CharSequence) tmp)))
            continue;
        names.add(mf.getName());
    }
    FieldFilter ff = FieldFilter.create(obj.getClass(), "^(" + Strings.join("|", names.toArray()) + ")$");
    Molecule<T> m = new Molecule<T>() {

        public void run() {
            insert(t);
            setObj(t);
        }
    };
    return ff.run(m);
}
Also used : FieldFilter(org.nutz.dao.FieldFilter) Molecule(org.nutz.trans.Molecule) ArrayList(java.util.ArrayList) MappingField(org.nutz.dao.entity.MappingField)

Example 4 with MappingField

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

the class EntityObjectContext method keys.

public Set<String> keys() {
    Set<String> names = new HashSet<String>(en.getMappingFields().size());
    names.add(ME);
    for (MappingField mf : en.getMappingFields()) names.add(mf.getName());
    names.addAll(ext.keySet());
    return names;
}
Also used : MappingField(org.nutz.dao.entity.MappingField) HashSet(java.util.HashSet)

Example 5 with MappingField

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

the class ExtDaoInvocationHandler method dataDict.

/** 根据Pojo生成数据字典,zdoc格式 */
public static StringBuilder dataDict(Dao dao, String... packages) {
    StringBuilder sb = new StringBuilder();
    List<Class<?>> ks = new ArrayList<Class<?>>();
    for (String packageName : packages) {
        ks.addAll(Scans.me().scanPackage(packageName));
    }
    Iterator<Class<?>> it = ks.iterator();
    while (it.hasNext()) {
        Class<?> klass = it.next();
        if (klass.getAnnotation(Table.class) == null)
            it.remove();
    }
    // log.infof("Found %d table class", ks.size());
    JdbcExpert exp = dao.getJdbcExpert();
    Entity<?> entity = null;
    String line = "-------------------------------------------------------------------\n";
    sb.append("#title:数据字典\n");
    sb.append("#author:wendal\n");
    sb.append("#index:0,1\n").append(line);
    for (Class<?> klass : ks) {
        sb.append(line);
        entity = dao.getEntity(klass);
        sb.append("表名 ").append(entity.getTableName()).append("\n\n");
        if (!Strings.isBlank(entity.getTableComment()))
            sb.append("表注释: ").append(entity.getTableComment());
        sb.append("\t").append("Java类名 ").append(klass.getName()).append("\n\n");
        sb.append("\t||序号||列名||数据类型||主键||非空||默认值||java属性名||java类型||注释||\n");
        int index = 1;
        for (MappingField field : entity.getMappingFields()) {
            String dataType = exp.evalFieldType(field);
            sb.append("\t||").append(index++).append("||").append(field.getColumnName()).append("||").append(dataType).append("||").append(field.isPk()).append("||").append(field.isNotNull()).append("||").append(field.getDefaultValue(null) == null ? " " : field.getDefaultValue(null)).append("||").append(field.getName()).append("||").append(field.getTypeClass().getName()).append("||").append(field.getColumnComment() == null ? " " : field.getColumnComment()).append("||\n");
        }
    }
    return sb;
}
Also used : Table(org.nutz.dao.entity.annotation.Table) JdbcExpert(org.nutz.dao.jdbc.JdbcExpert) ArrayList(java.util.ArrayList) MappingField(org.nutz.dao.entity.MappingField)

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