@Override public StatementContext handle(UpdateNode statementNode, StatementContext context) { BatchItem batchItem = context.getCurrentBatch(); batchItem.setStatementTreeNode(statementNode); /** 解析表信息 */ this.parseTableInfo(batchItem, statementNode); return context; }
@Override public StatementContext handle(InsertNode statementNode, StatementContext context) { BatchItem batchItem = context.getCurrentBatch(); if (statementNode.getResultSetNode() instanceof RowsResultSetNode) { // 写入多列,先不考虑性能,这里会将所有rows从新拆分,然后按照batch的方式处理 Collection<ParameterCallback<?>> callbacks = batchItem.getCallbacks(); RowsResultSetNode rowsNode = (RowsResultSetNode) statementNode.getResultSetNode(); List<RowResultSetNode> rows = rowsNode.getRows(); List<RowResultSetNode> tempRows = new ArrayList<RowResultSetNode>(rows); context.clearBatch(); for (int i = 0; i < tempRows.size(); i++) { rows.clear(); rows.add(tempRows.get(i)); NodeTreeToSql sqlTree = new NodeTreeToSql(); try { String sql = sqlTree.toString(statementNode); SQLParser parser = StatementHelper.createSQLParser(); InsertNode insertNode = (InsertNode) parser.parseStatement(sql); batchItem = context.getCurrentBatch(); batchItem.addAll(callbacks); batchItem.setSql(sql); batchItem.setStatementTreeNode(insertNode); this.parseTableInfo(batchItem, insertNode); context.addBatch(sql); } catch (Exception e) { logger.error("generate sql error!", e); } } } else { /** 解析表信息 */ this.parseTableInfo(batchItem, statementNode); batchItem.setStatementTreeNode(statementNode); } if (context.getBaches().size() > 1) { this.resetCallbacks(context.getBaches()); } return context; }