@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(); }); }
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 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 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); }
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);  }
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);   }