Search in sources :

Example 1 with VarIndex

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

the class NutDaoExecutor method _runExec.

// 执行存储过程,简单实现
protected void _runExec(Connection conn, DaoStatement st) throws SQLException {
    if (st.getContext().getPager() != null) {
        throw Lang.makeThrow(DaoException.class, "NOT Pageable : " + st);
    }
    // 打印调试信息
    String sql = st.toPreparedStatement();
    if (log.isDebugEnabled())
        log.debug(sql);
    Object[][] paramMatrix = st.getParamMatrix();
    CallableStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareCall(sql);
        ValueAdaptor[] adaptors = st.getAdaptors();
        HashMap<Integer, OutParam> outParams = new HashMap<Integer, OutParam>();
        if (st instanceof Sql) {
            VarIndex varIndex = ((Sql) st).paramIndex();
            VarSet varSet = ((Sql) st).params();
            for (int i = 0; i < varIndex.size(); i++) {
                String name = varIndex.getOrderName(i);
                if (name.startsWith("OUT") && varSet.get(name).getClass() == Integer.class) {
                    Integer t = (Integer) varSet.get(name);
                    outParams.put(i, new OutParam(name, t));
                }
            }
        }
        // 创建语句并设置参数
        if (paramMatrix != null && paramMatrix.length > 0) {
            PreparedStatement pst = (PreparedStatement) stmt;
            Object[] pm = paramMatrix[0];
            for (int i = 0; i < pm.length; i++) {
                OutParam outParam = outParams.get(i);
                if (outParam == null)
                    adaptors[i].set(pst, pm[i], i + 1);
                else
                    stmt.registerOutParameter(i + 1, outParam.jdbcType);
            }
        }
        stmt.execute();
        if (outParams.size() > 0) {
            Record r = Record.create();
            for (Entry<Integer, OutParam> en : outParams.entrySet()) {
                OutParam outParam = en.getValue();
                int jdbcIndex = en.getKey() + 1;
                Object value;
                switch(outParam.jdbcType) {
                    case Types.INTEGER:
                        value = stmt.getInt(jdbcIndex);
                        break;
                    case Types.TIMESTAMP:
                        value = stmt.getTimestamp(jdbcIndex);
                        break;
                    case Types.CLOB:
                        value = stmt.getString(jdbcIndex);
                        break;
                    case Types.DATE:
                        value = stmt.getDate(jdbcIndex);
                        break;
                    default:
                        value = stmt.getObject(jdbcIndex);
                        break;
                }
                r.set(outParam.name.substring(3), value);
            }
            st.getContext().attr("OUT", r);
        }
        //先尝试读取第一个,并调用一次回调
        rs = stmt.getResultSet();
        try {
            st.onAfter(conn, rs, null);
        } finally {
            if (rs != null)
                rs.close();
        }
        while (true) {
            if (stmt.getMoreResults()) {
                rs = stmt.getResultSet();
                try {
                    if (rs != null)
                        st.onAfter(conn, rs, null);
                } finally {
                    if (rs != null)
                        rs.close();
                }
            }
            break;
        }
    } finally {
        if (stmt != null)
            stmt.close();
    }
}
Also used : HashMap(java.util.HashMap) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) VarIndex(org.nutz.dao.sql.VarIndex) PreparedStatement(java.sql.PreparedStatement) Sql(org.nutz.dao.sql.Sql) CallableStatement(java.sql.CallableStatement) VarSet(org.nutz.dao.sql.VarSet) ResultSet(java.sql.ResultSet) Record(org.nutz.dao.entity.Record)

Aggregations

CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 Record (org.nutz.dao.entity.Record)1 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)1 Sql (org.nutz.dao.sql.Sql)1 VarIndex (org.nutz.dao.sql.VarIndex)1 VarSet (org.nutz.dao.sql.VarSet)1