Search in sources :

Example 1 with FieldMatcher

use of org.nutz.dao.FieldMatcher in project nutz by nutzam.

the class EntityOperator method addUpdateForIgnoreNull.

public List<Pojo> addUpdateForIgnoreNull(final Entity<?> en, final Object obj, final FieldMatcher fm) {
    if (null == en)
        return null;
    final FieldMatcher newFM;
    if (null == fm)
        newFM = FieldMatcher.make(null, null, true);
    else {
        newFM = fm;
        newFM.setIgnoreNull(true);
    }
    final List<Pojo> re = new ArrayList<Pojo>(Lang.length(obj));
    Lang.each(obj, new Each<Object>() {

        public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
            Pojo pojo = dao.pojoMaker.makeUpdate(en, ele).append(Pojos.Items.cndAuto(en, ele)).setOperatingObject(ele);
            pojo.getContext().setFieldMatcher(newFM);
            re.add(pojo);
        }
    });
    pojoList.addAll(re);
    return re;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ArrayList(java.util.ArrayList) FieldMatcher(org.nutz.dao.FieldMatcher)

Example 2 with FieldMatcher

use of org.nutz.dao.FieldMatcher in project nutz by nutzam.

the class QueryEntityFieldsPItem method joinSql.

public void joinSql(Entity<?> en, StringBuilder sb) {
    FieldMatcher fm = getFieldMatcher();
    if (null == fm) {
        sb.append("* ");
    } else {
        List<MappingField> efs = _en(en).getMappingFields();
        int old = sb.length();
        for (MappingField ef : efs) {
            if (fm.match(ef.getName()))
                sb.append(ef.getColumnNameInSql()).append(',');
        }
        if (sb.length() == old)
            throw Lang.makeThrow("No columns be queryed: '%s'", _en(en));
        sb.setCharAt(sb.length() - 1, ' ');
    }
}
Also used : MappingField(org.nutz.dao.entity.MappingField) FieldMatcher(org.nutz.dao.FieldMatcher)

Example 3 with FieldMatcher

use of org.nutz.dao.FieldMatcher in project nutz by nutzam.

the class DoUpdateLinkVisitor method visit.

public void visit(Object obj, final LinkField lnk) {
    Object value = lnk.getValue(obj);
    if (Lang.length(value) == 0)
        return;
    if (value instanceof Map<?, ?>)
        value = ((Map<?, ?>) value).values();
    FieldMatcher fm = FieldFilter.get(lnk.getLinkedEntity().getType());
    // 如果需要忽略 Null 字段,则为每个 POJO 都生成一条语句
    if (null != fm && fm.isIgnoreNull()) {
        opt.addUpdateForIgnoreNull(lnk.getLinkedEntity(), value, fm);
    } else // 否则生成一条批处理语句
    {
        opt.addUpdate(lnk.getLinkedEntity(), value);
    }
}
Also used : Map(java.util.Map) FieldMatcher(org.nutz.dao.FieldMatcher)

Example 4 with FieldMatcher

use of org.nutz.dao.FieldMatcher in project nutz by nutzam.

the class EntityCallback method invoke.

public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
    Entity<?> en = sql.getEntity();
    if (null == en)
        throw Lang.makeThrow("SQL without entity : %s", sql.toString());
    FieldMatcher fmh = sql.getContext().getFieldMatcher();
    if (null == fmh)
        sql.getContext().setFieldMatcher(FieldFilter.get(en.getType()));
    return process(rs, en, sql.getContext());
}
Also used : FieldMatcher(org.nutz.dao.FieldMatcher)

Aggregations

FieldMatcher (org.nutz.dao.FieldMatcher)4 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 MappingField (org.nutz.dao.entity.MappingField)1 Pojo (org.nutz.dao.sql.Pojo)1 ExitLoop (org.nutz.lang.ExitLoop)1 LoopException (org.nutz.lang.LoopException)1