Search in sources :

Example 1 with CallableStatementResultHandler

use of org.sagacity.sqltoy.callback.CallableStatementResultHandler in project sagacity-sqltoy by chenrenfei.

the class OracleDialectUtils method executeStore.

/**
 * @todo <b>oracle 存储过程调用,inParam需放在outParam前面(oracle存储过程返回结果必须用out
 *       参数返回,返回结果集则out 参数类型必须是OracleTypes.CURSOR,相对其他数据库比较特殊 )</b>
 * @param sqlToyConfig
 * @param sqlToyContext
 * @param storeSql
 * @param inParamValues
 * @param outParamTypes
 * @param conn
 * @param dbType
 * @param fetchSize
 * @return
 * @throws Exception
 */
public static StoreResult executeStore(final SqlToyConfig sqlToyConfig, final SqlToyContext sqlToyContext, final String storeSql, final Object[] inParamValues, final Integer[] outParamTypes, final Connection conn, final Integer dbType, final int fetchSize) throws Exception {
    CallableStatement callStat = null;
    ResultSet rs = null;
    return (StoreResult) SqlUtil.callableStatementProcess(null, callStat, rs, new CallableStatementResultHandler() {

        @Override
        public void execute(Object obj, CallableStatement callStat, ResultSet rs) throws Exception {
            callStat = conn.prepareCall(storeSql);
            if (fetchSize > 0) {
                callStat.setFetchSize(fetchSize);
            }
            SqlUtil.setParamsValue(sqlToyContext.getTypeHandler(), conn, dbType, callStat, inParamValues, null, 0);
            int cursorIndex = -1;
            int cursorCnt = 0;
            int inCount = (inParamValues == null) ? 0 : inParamValues.length;
            int outCount = (outParamTypes == null) ? 0 : outParamTypes.length;
            // 注册输出参数
            if (outCount != 0) {
                for (int i = 0; i < outCount; i++) {
                    callStat.registerOutParameter(i + inCount + 1, outParamTypes[i]);
                    if (OracleTypes.CURSOR == outParamTypes[i].intValue()) {
                        cursorCnt++;
                        cursorIndex = i;
                    }
                }
            }
            callStat.execute();
            StoreResult storeResult = new StoreResult();
            // 只返回最后一个CURSOR 类型的数据集
            if (cursorIndex != -1) {
                rs = (ResultSet) callStat.getObject(inCount + cursorIndex + 1);
                QueryResult tempResult = ResultUtils.processResultSet(sqlToyContext, sqlToyConfig, conn, rs, null, null, null, 0);
                storeResult.setLabelNames(tempResult.getLabelNames());
                storeResult.setLabelTypes(tempResult.getLabelTypes());
                storeResult.setRows(tempResult.getRows());
            }
            // 有返回参数(CURSOR 的类型不包含在内)
            if (outCount != 0) {
                Object[] outParams = new Object[outCount - cursorCnt];
                int index = 0;
                for (int i = 0; i < outCount; i++) {
                    if (OracleTypes.CURSOR != outParamTypes[i].intValue()) {
                        // 存储过程自动分页第一个返回参数是总记录数
                        outParams[index] = callStat.getObject(i + inCount + 1);
                        index++;
                    }
                }
                storeResult.setOutResult(outParams);
            }
            this.setResult(storeResult);
        }
    });
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) CallableStatementResultHandler(org.sagacity.sqltoy.callback.CallableStatementResultHandler) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) StoreResult(org.sagacity.sqltoy.model.StoreResult)

Example 2 with CallableStatementResultHandler

use of org.sagacity.sqltoy.callback.CallableStatementResultHandler in project sagacity-sqltoy by chenrenfei.

the class OracleDialectUtils method executeStore.

/**
 * @todo <b>oracle 存储过程调用,inParam需放在outParam前面(oracle存储过程返回结果必须用out
 *       参数返回,返回结果集则out 参数类型必须是OracleTypes.CURSOR,相对其他数据库比较特殊 )</b>
 * @param sqlToyConfig
 * @param sqlToyContext
 * @param storeSql
 * @param inParamValues
 * @param outParamTypes
 * @param conn
 * @return
 * @throws Exception
 */
public static StoreResult executeStore(final SqlToyConfig sqlToyConfig, final SqlToyContext sqlToyContext, final String storeSql, final Object[] inParamValues, final Integer[] outParamTypes, final Connection conn) throws Exception {
    CallableStatement callStat = null;
    ResultSet rs = null;
    return (StoreResult) SqlUtil.callableStatementProcess(null, callStat, rs, new CallableStatementResultHandler() {

        public void execute(Object obj, CallableStatement callStat, ResultSet rs) throws Exception {
            callStat = conn.prepareCall(storeSql);
            SqlUtil.setParamsValue(conn, callStat, inParamValues, null, 0);
            int cursorIndex = -1;
            int cursorCnt = 0;
            int inCount = (inParamValues == null) ? 0 : inParamValues.length;
            int outCount = (outParamTypes == null) ? 0 : outParamTypes.length;
            // 注册输出参数
            if (outCount != 0) {
                for (int i = 0; i < outCount; i++) {
                    callStat.registerOutParameter(i + inCount + 1, outParamTypes[i]);
                    if (OracleTypes.CURSOR == outParamTypes[i].intValue()) {
                        cursorCnt++;
                        cursorIndex = i;
                    }
                }
            }
            callStat.execute();
            StoreResult storeResult = new StoreResult();
            // 只返回最后一个CURSOR 类型的数据集
            if (cursorIndex != -1) {
                rs = (ResultSet) callStat.getObject(inCount + cursorIndex + 1);
                QueryResult tempResult = ResultUtils.processResultSet(sqlToyContext, sqlToyConfig, conn, rs, null, null, 0);
                storeResult.setLabelNames(tempResult.getLabelNames());
                storeResult.setLabelTypes(tempResult.getLabelTypes());
                storeResult.setRows(tempResult.getRows());
            }
            // 有返回参数(CURSOR 的类型不包含在内)
            if (outCount != 0) {
                Object[] outParams = new Object[outCount - cursorCnt];
                int index = 0;
                for (int i = 0; i < outCount; i++) {
                    if (OracleTypes.CURSOR != outParamTypes[i].intValue()) {
                        // 存储过程自动分页第一个返回参数是总记录数
                        outParams[index] = callStat.getObject(i + inCount + 1);
                        index++;
                    }
                }
                storeResult.setOutResult(outParams);
            }
            this.setResult(storeResult);
        }
    });
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) CallableStatementResultHandler(org.sagacity.sqltoy.callback.CallableStatementResultHandler) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) StoreResult(org.sagacity.sqltoy.model.StoreResult)

Example 3 with CallableStatementResultHandler

use of org.sagacity.sqltoy.callback.CallableStatementResultHandler in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method executeStore.

/**
 * @todo <b>通用的存储过程调用,inParam需放在outParam前面</b>
 * @param sqlToyConfig
 * @param sqlToyContext
 * @param storeSql
 * @param inParamValues
 * @param outParamTypes
 * @param conn
 * @return
 * @throws Exception
 */
public static StoreResult executeStore(final SqlToyConfig sqlToyConfig, final SqlToyContext sqlToyContext, final String storeSql, final Object[] inParamValues, final Integer[] outParamTypes, final Connection conn) throws Exception {
    CallableStatement callStat = null;
    ResultSet rs = null;
    return (StoreResult) SqlUtil.callableStatementProcess(null, callStat, rs, new CallableStatementResultHandler() {

        public void execute(Object obj, CallableStatement callStat, ResultSet rs) throws Exception {
            callStat = conn.prepareCall(storeSql);
            boolean isFirstResult = StringUtil.matches(storeSql, STORE_PATTERN);
            int addIndex = isFirstResult ? 1 : 0;
            SqlUtil.setParamsValue(conn, callStat, inParamValues, null, addIndex);
            int inCount = (inParamValues == null) ? 0 : inParamValues.length;
            int outCount = (outParamTypes == null) ? 0 : outParamTypes.length;
            // 注册输出参数
            if (outCount != 0) {
                if (isFirstResult)
                    callStat.registerOutParameter(1, outParamTypes[0]);
                for (int i = addIndex; i < outCount; i++) {
                    callStat.registerOutParameter(i + inCount + 1, outParamTypes[i]);
                }
            }
            callStat.execute();
            rs = callStat.getResultSet();
            // 执行查询 解决存储过程返回多个结果集问题,取最后一个结果集
            while (callStat.getMoreResults()) {
                rs = callStat.getResultSet();
            }
            StoreResult storeResult = new StoreResult();
            if (rs != null) {
                QueryResult tempResult = ResultUtils.processResultSet(sqlToyContext, sqlToyConfig, conn, rs, null, null, 0);
                storeResult.setLabelNames(tempResult.getLabelNames());
                storeResult.setLabelTypes(tempResult.getLabelTypes());
                storeResult.setRows(tempResult.getRows());
            }
            // 有返回参数如:(?=call (? in,? out) )
            if (outCount != 0) {
                Object[] outParams = new Object[outCount];
                if (isFirstResult)
                    outParams[0] = callStat.getObject(1);
                for (int i = addIndex; i < outCount; i++) {
                    outParams[i] = callStat.getObject(i + inCount + 1);
                }
                storeResult.setOutResult(outParams);
            }
            this.setResult(storeResult);
        }
    });
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) CallableStatementResultHandler(org.sagacity.sqltoy.callback.CallableStatementResultHandler) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) StoreResult(org.sagacity.sqltoy.model.StoreResult)

Example 4 with CallableStatementResultHandler

use of org.sagacity.sqltoy.callback.CallableStatementResultHandler in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method executeStore.

/**
 * @todo <b>通用的存储过程调用,inParam需放在outParam前面</b>
 * @param sqlToyConfig
 * @param sqlToyContext
 * @param storeSql
 * @param inParamValues
 * @param outParamTypes
 * @param conn
 * @param dbType
 * @param fetchSize
 * @return
 * @throws Exception
 */
public static StoreResult executeStore(final SqlToyConfig sqlToyConfig, final SqlToyContext sqlToyContext, final String storeSql, final Object[] inParamValues, final Integer[] outParamTypes, final Connection conn, final Integer dbType, final int fetchSize) throws Exception {
    CallableStatement callStat = null;
    ResultSet rs = null;
    return (StoreResult) SqlUtil.callableStatementProcess(null, callStat, rs, new CallableStatementResultHandler() {

        @Override
        public void execute(Object obj, CallableStatement callStat, ResultSet rs) throws Exception {
            callStat = conn.prepareCall(storeSql);
            if (fetchSize > 0) {
                callStat.setFetchSize(fetchSize);
            }
            boolean isFirstResult = StringUtil.matches(storeSql, STORE_PATTERN);
            int addIndex = isFirstResult ? 1 : 0;
            SqlUtil.setParamsValue(sqlToyContext.getTypeHandler(), conn, dbType, callStat, inParamValues, null, addIndex);
            int inCount = (inParamValues == null) ? 0 : inParamValues.length;
            int outCount = (outParamTypes == null) ? 0 : outParamTypes.length;
            // 注册输出参数
            if (outCount != 0) {
                if (isFirstResult) {
                    callStat.registerOutParameter(1, outParamTypes[0]);
                }
                for (int i = addIndex; i < outCount; i++) {
                    callStat.registerOutParameter(i + inCount + 1, outParamTypes[i]);
                }
            }
            callStat.execute();
            rs = callStat.getResultSet();
            // 执行查询 解决存储过程返回多个结果集问题,取最后一个结果集
            // while (callStat.getMoreResults()) {
            // rs = callStat.getResultSet();
            // }
            StoreResult storeResult = new StoreResult();
            if (rs != null) {
                QueryResult tempResult = ResultUtils.processResultSet(sqlToyContext, sqlToyConfig, conn, rs, null, null, null, 0);
                storeResult.setLabelNames(tempResult.getLabelNames());
                storeResult.setLabelTypes(tempResult.getLabelTypes());
                storeResult.setRows(tempResult.getRows());
            }
            // 有返回参数如:(?=call (? in,? out) )
            if (outCount != 0) {
                Object[] outParams = new Object[outCount];
                if (isFirstResult) {
                    outParams[0] = callStat.getObject(1);
                }
                for (int i = addIndex; i < outCount; i++) {
                    outParams[i] = callStat.getObject(i + inCount + 1);
                }
                storeResult.setOutResult(outParams);
            }
            this.setResult(storeResult);
        }
    });
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) CallableStatementResultHandler(org.sagacity.sqltoy.callback.CallableStatementResultHandler) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) StoreResult(org.sagacity.sqltoy.model.StoreResult)

Aggregations

CallableStatement (java.sql.CallableStatement)4 ResultSet (java.sql.ResultSet)4 CallableStatementResultHandler (org.sagacity.sqltoy.callback.CallableStatementResultHandler)4 QueryResult (org.sagacity.sqltoy.model.QueryResult)4 StoreResult (org.sagacity.sqltoy.model.StoreResult)4