Search in sources :

Example 1 with NutEntity

use of org.nutz.dao.impl.entity.NutEntity in project nutz by nutzam.

the class EntityHolder method makeEntity.

@SuppressWarnings({ "rawtypes", "unchecked" })
public <T extends Map<String, ?>> Entity<T> makeEntity(String tableName, T map) {
    final NutEntity<T> en = new NutEntity(map.getClass());
    en.setTableName(tableName);
    en.setViewName(tableName);
    boolean check = false;
    for (Entry<String, ?> entry : map.entrySet()) {
        String key = entry.getKey();
        // 是实体补充描述吗?
        if (key.startsWith("#")) {
            en.getMetas().put(key.substring(1), entry.getValue().toString());
            continue;
        } else // 以 "." 开头的字段,不是实体字段
        if (key.startsWith(".")) {
            continue;
        }
        // 是实体字段
        Object value = entry.getValue();
        Mirror<?> mirror = Mirror.me(value);
        NutMappingField ef = new NutMappingField(en);
        if (key.startsWith("+")) {
            ef.setAsAutoIncreasement();
            if (mirror != null && mirror.isIntLike())
                ef.setAsId();
            key = key.substring(1);
        }
        if (key.startsWith("!")) {
            ef.setAsNotNull();
            key = key.substring(1);
        }
        if (key.startsWith("*")) {
            key = key.substring(1);
            if (mirror != null && mirror.isIntLike())
                ef.setAsId();
            else
                ef.setAsName();
        }
        ef.setName(key);
        ef.setType(null == value ? Object.class : value.getClass());
        ef.setColumnName(key);
        // 猜测一下数据库类型
        Jdbcs.guessEntityFieldColumnType(ef);
        ef.setAdaptor(expert.getAdaptor(ef));
        if (mirror != null)
            ef.setType(mirror.getType());
        // 这里比较纠结,回设的时候应该用什么呢?
        ef.setInjecting(new InjectToMap(key));
        ef.setEjecting(new EjectFromMap(entry.getKey()));
        if (ef.isAutoIncreasement() && ef.isId() && expert.isSupportAutoIncrement() && !expert.isSupportGeneratedKeys()) {
            en.addAfterInsertMacro(expert.fetchPojoId(en, ef));
        }
        en.addMappingField(ef);
        if (mirror != null && !check)
            check = mirror.isEnum();
    }
    en.checkCompositeFields(null);
    // 最后在数据库中验证一下实体各个字段
    if (check)
        connCallback.invoke(new ConnCallback() {

            public void invoke(Connection conn) throws Exception {
                expert.setupEntityField(conn, en);
            }
        });
    // 搞定返回
    return en;
}
Also used : ConnCallback(org.nutz.dao.ConnCallback) NutEntity(org.nutz.dao.impl.entity.NutEntity) Connection(java.sql.Connection) NutMappingField(org.nutz.dao.impl.entity.field.NutMappingField) InjectToMap(org.nutz.lang.inject.InjectToMap) EjectFromMap(org.nutz.lang.eject.EjectFromMap)

Aggregations

Connection (java.sql.Connection)1 ConnCallback (org.nutz.dao.ConnCallback)1 NutEntity (org.nutz.dao.impl.entity.NutEntity)1 NutMappingField (org.nutz.dao.impl.entity.field.NutMappingField)1 EjectFromMap (org.nutz.lang.eject.EjectFromMap)1 InjectToMap (org.nutz.lang.inject.InjectToMap)1