1
- package org .utplsql .api ;
1
+ package org .utplsql .api . outputBuffer ;
2
2
3
3
import org .utplsql .api .reporter .Reporter ;
4
- import oracle .jdbc .OracleTypes ;
5
4
6
5
import java .io .PrintStream ;
7
6
import java .sql .*;
8
7
import java .util .ArrayList ;
9
8
import java .util .List ;
9
+ import java .util .function .Consumer ;
10
10
11
11
/**
12
12
* Fetches the lines produced by a reporter.
13
+ *
14
+ * @author vinicius
15
+ * @author pesse
13
16
*/
14
- public class OutputBuffer {
17
+ abstract class AbstractOutputBuffer implements OutputBuffer {
15
18
16
19
private Reporter reporter ;
17
20
18
21
/**
19
- * Creates a new OutputBuffer .
22
+ * Creates a new DefaultOutputBuffer .
20
23
* @param reporter the reporter to be used
21
24
*/
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
+
23
29
this .reporter = reporter ;
24
30
}
25
31
@@ -56,27 +62,24 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
56
62
});
57
63
}
58
64
65
+ protected abstract PreparedStatement getLinesStatement ( Connection conn ) throws SQLException ;
66
+
67
+ protected abstract CallableStatement getLinesCursorStatement ( Connection conn ) throws SQLException ;
68
+
59
69
/**
60
70
* Print the lines as soon as they are produced and call the callback passing the new line.
61
71
* @param conn DB connection
62
- * @param cb the callback to be called
72
+ * @param onLineFetched the callback to be called
63
73
* @throws SQLException any sql errors
64
74
*/
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
+ }
80
83
}
81
84
}
82
85
@@ -87,34 +90,22 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
87
90
* @throws SQLException any sql errors
88
91
*/
89
92
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 ;
103
105
}
104
- return outputLines ;
105
- } finally {
106
- if (resultSet != null )
107
- resultSet .close ();
108
- if (callableStatement != null )
109
- callableStatement .close ();
110
106
}
111
107
}
112
108
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
+
119
110
120
111
}
0 commit comments