@Test public void testExecuteSqlScriptNullLines() throws Exception { expectedEx.expect(Exception.class); expectedEx.expectMessage("No SQL command to be executed."); SQLScriptService.executeSqlScript(null, sqlInputs); }
public static String executeSqlScript(List<String> lines, SQLInputs sqlInputs) throws Exception { if (lines == null || lines.isEmpty()) { throw new Exception("No SQL command to be executed."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { try { connection.setReadOnly(false); } catch (Exception e) { } try (final Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency())) { statement.setQueryTimeout(sqlInputs.getTimeout()); boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); int updateCount = 0; if (lines.size() > 1) { for (String line : lines) { statement.addBatch(line); } int[] updateCounts = statement.executeBatch(); for (int i : updateCounts) { if (i > 0) { updateCount += i; } } } else { statement.execute(lines.get(0)); updateCount = statement.getUpdateCount(); } sqlInputs.setIUpdateCount(updateCount); connection.commit(); connection.setAutoCommit(autoCommit); } catch (SQLException e) { final String dbType = sqlInputs.getDbType(); if (SYBASE_DB_TYPE.equalsIgnoreCase(dbType)) { if (lines.get(0).trim().toLowerCase().startsWith("dump")) { return SQLUtils.processDumpException(e); } else if (lines.get(0).trim().toLowerCase().startsWith("load")) { return SQLUtils.processLoadException(e); } } else throw e; } } return "Command completed successfully"; }
SQLScriptService { public static String executeSqlScript(List<String> lines, SQLInputs sqlInputs) throws Exception { if (lines == null || lines.isEmpty()) { throw new Exception("No SQL command to be executed."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { try { connection.setReadOnly(false); } catch (Exception e) { } try (final Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency())) { statement.setQueryTimeout(sqlInputs.getTimeout()); boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); int updateCount = 0; if (lines.size() > 1) { for (String line : lines) { statement.addBatch(line); } int[] updateCounts = statement.executeBatch(); for (int i : updateCounts) { if (i > 0) { updateCount += i; } } } else { statement.execute(lines.get(0)); updateCount = statement.getUpdateCount(); } sqlInputs.setIUpdateCount(updateCount); connection.commit(); connection.setAutoCommit(autoCommit); } catch (SQLException e) { final String dbType = sqlInputs.getDbType(); if (SYBASE_DB_TYPE.equalsIgnoreCase(dbType)) { if (lines.get(0).trim().toLowerCase().startsWith("dump")) { return SQLUtils.processDumpException(e); } else if (lines.get(0).trim().toLowerCase().startsWith("load")) { return SQLUtils.processLoadException(e); } } else throw e; } } return "Command completed successfully"; } }
SQLScriptService { public static String executeSqlScript(List<String> lines, SQLInputs sqlInputs) throws Exception { if (lines == null || lines.isEmpty()) { throw new Exception("No SQL command to be executed."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { try { connection.setReadOnly(false); } catch (Exception e) { } try (final Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency())) { statement.setQueryTimeout(sqlInputs.getTimeout()); boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); int updateCount = 0; if (lines.size() > 1) { for (String line : lines) { statement.addBatch(line); } int[] updateCounts = statement.executeBatch(); for (int i : updateCounts) { if (i > 0) { updateCount += i; } } } else { statement.execute(lines.get(0)); updateCount = statement.getUpdateCount(); } sqlInputs.setIUpdateCount(updateCount); connection.commit(); connection.setAutoCommit(autoCommit); } catch (SQLException e) { final String dbType = sqlInputs.getDbType(); if (SYBASE_DB_TYPE.equalsIgnoreCase(dbType)) { if (lines.get(0).trim().toLowerCase().startsWith("dump")) { return SQLUtils.processDumpException(e); } else if (lines.get(0).trim().toLowerCase().startsWith("load")) { return SQLUtils.processLoadException(e); } } else throw e; } } return "Command completed successfully"; }  }
SQLScriptService { public static String executeSqlScript(List<String> lines, SQLInputs sqlInputs) throws Exception { if (lines == null || lines.isEmpty()) { throw new Exception("No SQL command to be executed."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { try { connection.setReadOnly(false); } catch (Exception e) { } try (final Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency())) { statement.setQueryTimeout(sqlInputs.getTimeout()); boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); int updateCount = 0; if (lines.size() > 1) { for (String line : lines) { statement.addBatch(line); } int[] updateCounts = statement.executeBatch(); for (int i : updateCounts) { if (i > 0) { updateCount += i; } } } else { statement.execute(lines.get(0)); updateCount = statement.getUpdateCount(); } sqlInputs.setIUpdateCount(updateCount); connection.commit(); connection.setAutoCommit(autoCommit); } catch (SQLException e) { final String dbType = sqlInputs.getDbType(); if (SYBASE_DB_TYPE.equalsIgnoreCase(dbType)) { if (lines.get(0).trim().toLowerCase().startsWith("dump")) { return SQLUtils.processDumpException(e); } else if (lines.get(0).trim().toLowerCase().startsWith("load")) { return SQLUtils.processLoadException(e); } } else throw e; } } return "Command completed successfully"; }  static String executeSqlScript(List<String> lines, SQLInputs sqlInputs); }
SQLScriptService { public static String executeSqlScript(List<String> lines, SQLInputs sqlInputs) throws Exception { if (lines == null || lines.isEmpty()) { throw new Exception("No SQL command to be executed."); } ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { try { connection.setReadOnly(false); } catch (Exception e) { } try (final Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency())) { statement.setQueryTimeout(sqlInputs.getTimeout()); boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); int updateCount = 0; if (lines.size() > 1) { for (String line : lines) { statement.addBatch(line); } int[] updateCounts = statement.executeBatch(); for (int i : updateCounts) { if (i > 0) { updateCount += i; } } } else { statement.execute(lines.get(0)); updateCount = statement.getUpdateCount(); } sqlInputs.setIUpdateCount(updateCount); connection.commit(); connection.setAutoCommit(autoCommit); } catch (SQLException e) { final String dbType = sqlInputs.getDbType(); if (SYBASE_DB_TYPE.equalsIgnoreCase(dbType)) { if (lines.get(0).trim().toLowerCase().startsWith("dump")) { return SQLUtils.processDumpException(e); } else if (lines.get(0).trim().toLowerCase().startsWith("load")) { return SQLUtils.processLoadException(e); } } else throw e; } } return "Command completed successfully"; }  static String executeSqlScript(List<String> lines, SQLInputs sqlInputs);  }