|
| 1 | +package io.github.utplsql; |
| 2 | + |
| 3 | +import oracle.jdbc.OracleTypes; |
| 4 | + |
| 5 | +import java.sql.CallableStatement; |
| 6 | +import java.sql.PreparedStatement; |
| 7 | +import java.sql.ResultSet; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by Vinicius on 13/04/2017. |
| 14 | + */ |
| 15 | +public class OutputBuffer { |
| 16 | + |
| 17 | + private String reporterId; |
| 18 | + |
| 19 | + public OutputBuffer(String reporterId) { |
| 20 | + this.reporterId = reporterId; |
| 21 | + } |
| 22 | + |
| 23 | + public String getReporterId() { |
| 24 | + return reporterId; |
| 25 | + } |
| 26 | + |
| 27 | + public void setReporterId(String reporterId) { |
| 28 | + this.reporterId = reporterId; |
| 29 | + } |
| 30 | + |
| 31 | + public List<String> getLines() throws SQLException { |
| 32 | + PreparedStatement preparedStatement = null; |
| 33 | + ResultSet resultSet = null; |
| 34 | + try { |
| 35 | + preparedStatement = UTPLSQL.getConnection() |
| 36 | + .prepareStatement("SELECT * FROM TABLE(ut_output_buffer.get_lines(?, 1))"); |
| 37 | + |
| 38 | + preparedStatement.setString(1, getReporterId()); |
| 39 | + resultSet = preparedStatement.executeQuery(); |
| 40 | + |
| 41 | + List<String> outputLines = new ArrayList<>(); |
| 42 | + while (resultSet.next()) { |
| 43 | + outputLines.add(resultSet.getString(1)); |
| 44 | + } |
| 45 | + return outputLines; |
| 46 | + } finally { |
| 47 | + if (resultSet != null) |
| 48 | + resultSet.close(); |
| 49 | + if (preparedStatement != null) |
| 50 | + preparedStatement.close(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public List<String> getAllLines() throws SQLException { |
| 55 | + CallableStatement callableStatement = null; |
| 56 | + ResultSet resultSet = null; |
| 57 | + try { |
| 58 | + callableStatement = UTPLSQL.getConnection() |
| 59 | + .prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;"); |
| 60 | + |
| 61 | + callableStatement.registerOutParameter(1, OracleTypes.CURSOR); |
| 62 | + callableStatement.setString(2, getReporterId()); |
| 63 | + callableStatement.execute(); |
| 64 | + |
| 65 | + resultSet = (ResultSet) callableStatement.getObject(1); |
| 66 | + |
| 67 | + List<String> outputLines = new ArrayList<>(); |
| 68 | + while (resultSet.next()) { |
| 69 | + outputLines.add(resultSet.getString("text")); |
| 70 | + } |
| 71 | + return outputLines; |
| 72 | + } finally { |
| 73 | + if (resultSet != null) |
| 74 | + resultSet.close(); |
| 75 | + if (callableStatement != null) |
| 76 | + callableStatement.close(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments