1- package org .utplsql .api ;
1+ package org .utplsql .api . outputBuffer ;
22
33import org .utplsql .api .reporter .Reporter ;
4- import oracle .jdbc .OracleTypes ;
54
65import java .io .PrintStream ;
76import java .sql .*;
87import java .util .ArrayList ;
98import java .util .List ;
9+ import java .util .function .Consumer ;
1010
1111/**
1212 * Fetches the lines produced by a reporter.
13+ *
14+ * @author vinicius
15+ * @author pesse
1316 */
14- public class OutputBuffer {
17+ abstract class AbstractOutputBuffer implements OutputBuffer {
1518
1619 private Reporter reporter ;
1720
1821 /**
19- * Creates a new OutputBuffer .
22+ * Creates a new DefaultOutputBuffer .
2023 * @param reporter the reporter to be used
2124 */
22- public OutputBuffer (Reporter reporter ) {
25+ AbstractOutputBuffer (Reporter reporter ) {
26+
27+ assert reporter .isInit () : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters" ;
28+
2329 this .reporter = reporter ;
2430 }
2531
@@ -56,27 +62,24 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
5662 });
5763 }
5864
65+ protected abstract PreparedStatement getLinesStatement ( Connection conn ) throws SQLException ;
66+
67+ protected abstract CallableStatement getLinesCursorStatement ( Connection conn ) throws SQLException ;
68+
5969 /**
6070 * Print the lines as soon as they are produced and call the callback passing the new line.
6171 * @param conn DB connection
62- * @param cb the callback to be called
72+ * @param onLineFetched the callback to be called
6373 * @throws SQLException any sql errors
6474 */
65- public void fetchAvailable (Connection conn , Callback cb ) throws SQLException {
66- PreparedStatement preparedStatement = null ;
67- ResultSet resultSet = null ;
68- try {
69- preparedStatement = conn .prepareStatement ("SELECT * FROM table(ut_output_buffer.get_lines(?))" );
70- preparedStatement .setString (1 , getReporter ().getReporterId ());
71- resultSet = preparedStatement .executeQuery ();
72-
73- while (resultSet .next ())
74- cb .onLineFetched (resultSet .getString (1 ));
75- } finally {
76- if (resultSet != null )
77- resultSet .close ();
78- if (preparedStatement != null )
79- preparedStatement .close ();
75+ public void fetchAvailable (Connection conn , Consumer <String > onLineFetched ) throws SQLException {
76+
77+ try (PreparedStatement pstmt = getLinesStatement (conn )) {
78+
79+ try (ResultSet resultSet = pstmt .executeQuery () ) {
80+ while (resultSet .next ())
81+ onLineFetched .accept (resultSet .getString (1 ));
82+ }
8083 }
8184 }
8285
@@ -87,34 +90,22 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
8790 * @throws SQLException any sql errors
8891 */
8992 public List <String > fetchAll (Connection conn ) throws SQLException {
90- CallableStatement callableStatement = null ;
91- ResultSet resultSet = null ;
92- try {
93- callableStatement = conn .prepareCall ("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;" );
94- callableStatement .registerOutParameter (1 , OracleTypes .CURSOR );
95- callableStatement .setString (2 , getReporter ().getReporterId ());
96- callableStatement .execute ();
97-
98- resultSet = (ResultSet ) callableStatement .getObject (1 );
99-
100- List <String > outputLines = new ArrayList <>();
101- while (resultSet .next ()) {
102- outputLines .add (resultSet .getString ("text" ));
93+
94+ try (CallableStatement cstmt = getLinesCursorStatement (conn )) {
95+
96+ cstmt .execute ();
97+
98+ try ( ResultSet resultSet = (ResultSet ) cstmt .getObject (1 )) {
99+
100+ List <String > outputLines = new ArrayList <>();
101+ while (resultSet .next ()) {
102+ outputLines .add (resultSet .getString ("text" ));
103+ }
104+ return outputLines ;
103105 }
104- return outputLines ;
105- } finally {
106- if (resultSet != null )
107- resultSet .close ();
108- if (callableStatement != null )
109- callableStatement .close ();
110106 }
111107 }
112108
113- /**
114- * Callback to be called when a new line is available from the output buffer.
115- */
116- public interface Callback {
117- void onLineFetched (String s );
118- }
109+
119110
120111}
0 commit comments