Search in sources :

Example 1 with InsertRowCallbackHandler

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

the class SqlUtil method processNextLevel.

/**
 * @todo TreeTableRoute中处理下一层级的递归方法,逐层计算下一级节点的节点层次和路径
 * @param updateLevelAndRoute
 * @param nextNodeQueryStr
 * @param treeTableModel
 * @param pidsMap
 * @param ids
 * @param nodeLevel
 * @param conn
 * @throws Exception
 */
private static void processNextLevel(final String updateLevelAndRoute, final String nextNodeQueryStr, final TreeTableModel treeTableModel, final HashMap pidsMap, List ids, final int nodeLevel, Connection conn) throws Exception {
    // 修改节点level和节点路径
    batchUpdateByJdbc(updateLevelAndRoute, ids, 500, new InsertRowCallbackHandler() {

        public void process(PreparedStatement pst, int index, Object rowData) throws SQLException {
            String id = ((List) rowData).get(0).toString();
            // 获得父节点id和父节点路径
            String pid = ((List) rowData).get(2).toString();
            String nodeRoute = (String) pidsMap.get(pid);
            int size = treeTableModel.getIdLength();
            if (nodeRoute == null || nodeRoute.trim().equals("")) {
                nodeRoute = "";
                if (!treeTableModel.isChar() || treeTableModel.isAppendZero()) {
                    // 负数
                    if (CommonUtils.isInteger(pid) && pid.indexOf("-") == 0)
                        nodeRoute = nodeRoute.concat("-").concat(StringUtil.addLeftZero2Len(pid.substring(1), size - 1));
                    else
                        nodeRoute = nodeRoute.concat(StringUtil.addLeftZero2Len(pid, size));
                } else {
                    nodeRoute = nodeRoute.concat(StringUtil.addRightBlank2Len(pid, size));
                }
            } else
                nodeRoute = nodeRoute.trim();
            // update 2018-1-9 增加判断是否以逗号结尾,解决修改过程中出现双逗号问题
            if (!nodeRoute.endsWith(treeTableModel.getSplitSign()))
                nodeRoute = nodeRoute.concat(treeTableModel.getSplitSign());
            // 回置节点的nodeRoute值
            if (!treeTableModel.isChar() || treeTableModel.isAppendZero())
                nodeRoute = nodeRoute.concat(StringUtil.addLeftZero2Len(id, size));
            else
                nodeRoute = nodeRoute.concat(StringUtil.addRightBlank2Len(id, size));
            ((List) rowData).set(1, nodeRoute);
            // 节点等级
            pst.setInt(1, nodeLevel);
            // 节点路径(当节点路径长度不做补充统一长度操作,则末尾自动加上一个分割符)
            pst.setString(2, nodeRoute + ((size < 2) ? treeTableModel.getSplitSign() : ""));
            if (treeTableModel.isChar()) {
                pst.setString(3, id);
            } else
                pst.setLong(3, Long.parseLong(id));
        }
    }, null, false, conn);
    // 处理节点的下一层次
    int size = ids.size();
    int fromIndex = 0;
    int toIndex = -1;
    // 避免in()中的参数过多,每次500个
    String inStrs;
    List subIds = null;
    List nextIds = null;
    boolean exist = false;
    while (toIndex < size) {
        fromIndex = toIndex + 1;
        toIndex += 500;
        if (toIndex >= size - 1) {
            toIndex = size - 1;
            exist = true;
        }
        if (fromIndex >= toIndex) {
            subIds = new ArrayList();
            subIds.add(ids.get(toIndex));
        } else
            subIds = ids.subList(fromIndex, toIndex + 1);
        inStrs = combineQueryInStr(subIds, 0, null, treeTableModel.isChar());
        // 获取下一层节点
        nextIds = findByJdbcQuery(nextNodeQueryStr.replaceFirst("\\$\\{inStr\\}", inStrs), null, null, null, conn);
        // 递归处理下一层
        if (nextIds != null && !nextIds.isEmpty()) {
            processNextLevel(updateLevelAndRoute, nextNodeQueryStr, treeTableModel, CollectionUtil.hashList(subIds, 0, 1, true), nextIds, nodeLevel + 1, conn);
        }
        if (exist)
            break;
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) InsertRowCallbackHandler(org.sagacity.sqltoy.callback.InsertRowCallbackHandler)

Example 2 with InsertRowCallbackHandler

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

the class SqlUtil method processNextLevel.

/**
 * @todo TreeTableRoute中处理下一层级的递归方法,逐层计算下一级节点的节点层次和路径
 * @param typeHandler
 * @param updateLevelAndRoute
 * @param nextNodeQueryStr
 * @param treeTableModel
 * @param pidsMap
 * @param ids
 * @param nodeLevel
 * @param conn
 * @param dbType
 * @throws Exception
 */
private static void processNextLevel(TypeHandler typeHandler, final String updateLevelAndRoute, final String nextNodeQueryStr, final TreeTableModel treeTableModel, final HashMap pidsMap, List ids, final int nodeLevel, Connection conn, final int dbType) throws Exception {
    // 修改节点level和节点路径
    batchUpdateByJdbc(typeHandler, updateLevelAndRoute, ids, 500, new InsertRowCallbackHandler() {

        public void process(PreparedStatement pst, int index, Object rowData) throws SQLException {
            String id = ((List) rowData).get(0).toString();
            // 获得父节点id和父节点路径
            String pid = ((List) rowData).get(2).toString();
            String nodeRoute = (String) pidsMap.get(pid);
            int size = treeTableModel.getIdLength();
            if (nodeRoute == null || nodeRoute.trim().equals("")) {
                nodeRoute = "";
                if (!treeTableModel.isChar() || treeTableModel.isAppendZero()) {
                    // 负数
                    if (NumberUtil.isInteger(pid) && pid.indexOf("-") == 0) {
                        nodeRoute = nodeRoute.concat("-").concat(StringUtil.addLeftZero2Len(pid.substring(1), size - 1));
                    } else {
                        nodeRoute = nodeRoute.concat(StringUtil.addLeftZero2Len(pid, size));
                    }
                } else {
                    nodeRoute = nodeRoute.concat(StringUtil.addRightBlank2Len(pid, size));
                }
            } else {
                nodeRoute = nodeRoute.trim();
            }
            // update 2018-1-9 增加判断是否以逗号结尾,解决修改过程中出现双逗号问题
            if (!nodeRoute.endsWith(treeTableModel.getSplitSign())) {
                nodeRoute = nodeRoute.concat(treeTableModel.getSplitSign());
            }
            // 回置节点的nodeRoute值
            if (!treeTableModel.isChar() || treeTableModel.isAppendZero()) {
                nodeRoute = nodeRoute.concat(StringUtil.addLeftZero2Len(id, size));
            } else {
                nodeRoute = nodeRoute.concat(StringUtil.addRightBlank2Len(id, size));
            }
            ((List) rowData).set(1, nodeRoute);
            // 节点等级
            pst.setInt(1, nodeLevel);
            // 节点路径(当节点路径长度不做补充统一长度操作,则末尾自动加上一个分割符)
            pst.setString(2, nodeRoute + ((size < 2) ? treeTableModel.getSplitSign() : ""));
            if (treeTableModel.isChar()) {
                pst.setString(3, id);
            } else {
                pst.setLong(3, Long.parseLong(id));
            }
        }
    }, null, false, conn, dbType);
    // 处理节点的下一层次
    int size = ids.size();
    int fromIndex = 0;
    int toIndex = -1;
    // 避免in()中的参数过多,每次500个
    String inStrs;
    List subIds = null;
    List nextIds = null;
    boolean exist = false;
    while (toIndex < size) {
        fromIndex = toIndex + 1;
        toIndex += 500;
        if (toIndex >= size - 1) {
            toIndex = size - 1;
            exist = true;
        }
        if (fromIndex >= toIndex) {
            subIds = new ArrayList();
            subIds.add(ids.get(toIndex));
        } else {
            subIds = ids.subList(fromIndex, toIndex + 1);
        }
        inStrs = combineQueryInStr(subIds, 0, null, treeTableModel.isChar());
        // 获取下一层节点
        nextIds = findByJdbcQuery(typeHandler, nextNodeQueryStr.replaceFirst("\\$\\{inStr\\}", inStrs), null, null, null, null, conn, dbType, false, null, SqlToyConstants.FETCH_SIZE, -1);
        // 递归处理下一层
        if (nextIds != null && !nextIds.isEmpty()) {
            processNextLevel(typeHandler, updateLevelAndRoute, nextNodeQueryStr, treeTableModel, CollectionUtil.hashList(subIds, 0, 1, true), nextIds, nodeLevel + 1, conn, dbType);
        }
        if (exist) {
            break;
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) InsertRowCallbackHandler(org.sagacity.sqltoy.callback.InsertRowCallbackHandler)

Aggregations

PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InsertRowCallbackHandler (org.sagacity.sqltoy.callback.InsertRowCallbackHandler)2