Search in sources :

Example 1 with PojoCallback

use of org.nutz.dao.sql.PojoCallback in project nutz by nutzam.

the class DoFetchLinkVisitor method visit.

public void visit(final Object obj, final LinkField lnk) {
    Pojo pojo = opt.maker().makeQuery(lnk.getLinkedEntity());
    pojo.setOperatingObject(obj);
    pojo.append(Pojos.Items.cnd(lnk.createCondition(obj)));
    pojo.setAfter(new PojoCallback() {

        public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
            Object value = lnk.getCallback().invoke(conn, rs, pojo, stmt);
            lnk.setValue(obj, value);
            return value;
        }
    });
    opt.add(pojo);
}
Also used : PojoCallback(org.nutz.dao.sql.PojoCallback) Pojo(org.nutz.dao.sql.Pojo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 2 with PojoCallback

use of org.nutz.dao.sql.PojoCallback in project nutz by nutzam.

the class DoInsertLinkVisitor method visit.

public void visit(final Object obj, final LinkField lnk) {
    final Object value = lnk.getValue(obj);
    if (Lang.length(value) == 0)
        return;
    // 从宿主对象更新关联对象
    opt.add(Pojos.createRun(new PojoCallback() {

        public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
            lnk.updateLinkedField(obj, value);
            return pojo.getOperatingObject();
        }
    }).setOperatingObject(obj));
    // 为其循环生成插入语句 : holder.getEntityBy 会考虑到集合和数组的情况的
    final Entity<?> en = lnk.getLinkedEntity();
    Lang.each(value, new Each<Object>() {

        public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
            if (ele == null)
                throw new NullPointerException("null ele in linked field!!");
            // 执行插入
            opt.addInsert(en, ele);
            // 更新字段
            opt.add(Pojos.createRun(new PojoCallback() {

                public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
                    lnk.saveLinkedField(obj, pojo.getOperatingObject());
                    return pojo.getOperatingObject();
                }
            }).setOperatingObject(ele));
        }
    });
}
Also used : Pojo(org.nutz.dao.sql.Pojo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) PojoCallback(org.nutz.dao.sql.PojoCallback) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ResultSet(java.sql.ResultSet)

Aggregations

Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Pojo (org.nutz.dao.sql.Pojo)2 PojoCallback (org.nutz.dao.sql.PojoCallback)2 ExitLoop (org.nutz.lang.ExitLoop)1 LoopException (org.nutz.lang.LoopException)1