InsertExecutor extends AbstractDMLBaseExecutor<T, S> { protected List<Object> getPkValuesByAuto() throws SQLException { Map<String, ColumnMeta> pkMetaMap = getTableMeta().getPrimaryKeyMap(); if (pkMetaMap.size() != 1) { throw new NotSupportYetException(); } ColumnMeta pkMeta = pkMetaMap.values().iterator().next(); if (!pkMeta.isAutoincrement()) { throw new ShouldNeverHappenException(); } ResultSet genKeys = null; try { genKeys = statementProxy.getTargetStatement().getGeneratedKeys(); } catch (SQLException e) { if (ERR_SQL_STATE.equalsIgnoreCase(e.getSQLState())) { LOGGER.warn("Fail to get auto-generated keys, use \'SELECT LAST_INSERT_ID()\' instead. Be cautious, statement could be polluted. Recommend you set the statement to return generated keys."); genKeys = statementProxy.getTargetStatement().executeQuery("SELECT LAST_INSERT_ID()"); } else { throw e; } } List<Object> pkValues = new ArrayList<>(); while (genKeys.next()) { Object v = genKeys.getObject(1); pkValues.add(v); } return pkValues; } InsertExecutor(StatementProxy statementProxy, StatementCallback statementCallback,
                          SQLRecognizer sqlRecognizer);   }
@Test public void testGetPkValuesByAuto_NotSupportYetException() { Assertions.assertThrows(NotSupportYetException.class, () -> { doReturn(tableMeta).when(insertExecutor).getTableMeta(); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, new ColumnMeta()); columnMetaMap.put(USER_ID_COLUMN, new ColumnMeta()); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); insertExecutor.getPkValuesByAuto(); }); }
@Test public void testGetPkValuesByAuto_ShouldNeverHappenException() { Assertions.assertThrows(ShouldNeverHappenException.class, () -> { doReturn(tableMeta).when(insertExecutor).getTableMeta(); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); ColumnMeta columnMeta = mock(ColumnMeta.class); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(false); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); insertExecutor.getPkValuesByAuto(); }); }
@Test public void testGetPkValuesByAuto_SQLException() { Assertions.assertThrows(SQLException.class, () -> { doReturn(tableMeta).when(insertExecutor).getTableMeta(); ColumnMeta columnMeta = mock(ColumnMeta.class); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(true); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(statementProxy.getTargetStatement()).thenReturn(preparedStatement); when(preparedStatement.getGeneratedKeys()).thenThrow(new SQLException()); insertExecutor.getPkValuesByAuto(); }); }
@Test public void testGetPkValuesByAuto_SQLException_WarnLog() throws SQLException { doReturn(tableMeta).when(insertExecutor).getTableMeta(); ColumnMeta columnMeta = mock(ColumnMeta.class); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(true); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(statementProxy.getTargetStatement()).thenReturn(preparedStatement); SQLException e = new SQLException("test warn log", InsertExecutor.ERR_SQL_STATE, 1); when(preparedStatement.getGeneratedKeys()).thenThrow(e); ResultSet genKeys = mock(ResultSet.class); when(statementProxy.getTargetStatement().executeQuery("SELECT LAST_INSERT_ID()")).thenReturn(genKeys); Assertions.assertTrue(insertExecutor.getPkValuesByAuto().isEmpty()); }
@Test public void testGetPkValuesByAuto_GeneratedKeys_NoResult() throws SQLException { doReturn(tableMeta).when(insertExecutor).getTableMeta(); ColumnMeta columnMeta = mock(ColumnMeta.class); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(true); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(statementProxy.getTargetStatement()).thenReturn(preparedStatement); ResultSet resultSet = mock(ResultSet.class); when(preparedStatement.getGeneratedKeys()).thenReturn(resultSet); when(resultSet.next()).thenReturn(false); when(resultSet.getObject(1)).thenReturn(PK_VALUE); List pkValuesByAuto = insertExecutor.getPkValuesByAuto(); Assertions.assertEquals(pkValuesByAuto.size(),0); }
@Test public void testGetPkValuesByAuto_GeneratedKeys_HasResult() throws SQLException { doReturn(tableMeta).when(insertExecutor).getTableMeta(); ColumnMeta columnMeta = mock(ColumnMeta.class); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(true); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(statementProxy.getTargetStatement()).thenReturn(preparedStatement); ResultSet resultSet = mock(ResultSet.class); when(preparedStatement.getGeneratedKeys()).thenReturn(resultSet); when(resultSet.next()).thenReturn(true).thenReturn(false); when(resultSet.getObject(1)).thenReturn(PK_VALUE); List<Object> pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); List pkValuesByAuto = insertExecutor.getPkValuesByAuto(); Assertions.assertEquals(pkValuesByAuto, pkValues); }
@Test public void testGetPkValuesByAuto_ExecuteQuery_HasResult() throws SQLException { doReturn(tableMeta).when(insertExecutor).getTableMeta(); ColumnMeta columnMeta = mock(ColumnMeta.class); Map<String, ColumnMeta> columnMetaMap = new HashMap<>(); columnMetaMap.put(ID_COLUMN, columnMeta); when(columnMeta.isAutoincrement()).thenReturn(true); when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(statementProxy.getTargetStatement()).thenReturn(preparedStatement); when(preparedStatement.getGeneratedKeys()).thenThrow(new SQLException("", InsertExecutor.ERR_SQL_STATE)); ResultSet resultSet = mock(ResultSet.class); when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet); when(resultSet.next()).thenReturn(true).thenReturn(false); when(resultSet.getObject(1)).thenReturn(PK_VALUE); List<Object> pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); List pkValuesByAuto = insertExecutor.getPkValuesByAuto(); Assertions.assertEquals(pkValuesByAuto, pkValues); }