InsertExecutor extends AbstractDMLBaseExecutor<T, S> { protected List<Object> getPkValuesByColumn() throws SQLException { SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizer; final int pkIndex = getPkIndex(); List<Object> pkValues = null; if (statementProxy instanceof PreparedStatementProxy) { PreparedStatementProxy preparedStatementProxy = (PreparedStatementProxy) statementProxy; List<List<Object>> insertRows = recognizer.getInsertRows(); if (insertRows != null && !insertRows.isEmpty()) { ArrayList<Object>[] parameters = preparedStatementProxy.getParameters(); final int rowSize = insertRows.size(); if (rowSize == 1) { Object pkValue = insertRows.get(0).get(pkIndex); if (PLACEHOLDER.equals(pkValue)) { pkValues = parameters[pkIndex]; } else { int finalPkIndex = pkIndex; pkValues = insertRows.stream().map(insertRow -> insertRow.get(finalPkIndex)).collect(Collectors.toList()); } } else { int totalPlaceholderNum = -1; pkValues = new ArrayList<>(rowSize); for (int i = 0; i < rowSize; i++) { List<Object> row = insertRows.get(i); Object pkValue = row.get(pkIndex); int currentRowPlaceholderNum = -1; for (Object r : row) { if (PLACEHOLDER.equals(r)) { totalPlaceholderNum += 1; currentRowPlaceholderNum += 1; } } if (PLACEHOLDER.equals(pkValue)) { int idx = pkIndex; if (i != 0) { idx = totalPlaceholderNum - currentRowPlaceholderNum + pkIndex; } ArrayList<Object> parameter = parameters[idx]; for (Object obj : parameter) { pkValues.add(obj); } } else { pkValues.add(pkValue); } } } } } else { List<List<Object>> insertRows = recognizer.getInsertRows(); pkValues = new ArrayList<>(insertRows.size()); for (List<Object> row : insertRows) { pkValues.add(row.get(pkIndex)); } } if (pkValues == null) { throw new ShouldNeverHappenException(); } boolean b = this.checkPkValues(pkValues); if (!b) { throw new NotSupportYetException("not support sql [" + sqlRecognizer.getOriginalSQL() + "]"); } if (pkValues.size() == 1 && pkValues.get(0) instanceof SqlSequenceExpr) { pkValues = getPkValuesBySequence(pkValues.get(0)); } else if (pkValues.size() == 1 && pkValues.get(0) instanceof SqlMethodExpr) { pkValues = getPkValuesByAuto(); } else if (pkValues.size() > 0 && pkValues.get(0) instanceof Null) { pkValues = getPkValuesByAuto(); } return pkValues; } InsertExecutor(StatementProxy statementProxy, StatementCallback statementCallback,
                          SQLRecognizer sqlRecognizer);   }
@Test public void testGetPkValuesByColumn() throws SQLException { mockInsertColumns(); mockInsertRows(); mockParametersOfOnePk(); doReturn(tableMeta).when(insertExecutor).getTableMeta(); when(tableMeta.getPkName()).thenReturn(ID_COLUMN); List<Object> pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); List pkValuesByColumn = insertExecutor.getPkValuesByColumn(); Assertions.assertEquals(pkValuesByColumn, pkValues); }
@Test public void testGetPkValuesByColumn_Exception() { Assertions.assertThrows(ShouldNeverHappenException.class, () -> { mockInsertColumns(); mockParameters(); doReturn(tableMeta).when(insertExecutor).getTableMeta(); when(tableMeta.getPkName()).thenReturn(ID_COLUMN); insertExecutor.getPkValuesByColumn(); }); }