1- package org .utplsql .api ;
1+ package org .utplsql .api . outputBuffer ;
22
3+ import oracle .jdbc .OracleCallableStatement ;
4+ import oracle .jdbc .OracleConnection ;
5+ import oracle .jdbc .OraclePreparedStatement ;
36import org .utplsql .api .reporter .Reporter ;
47import oracle .jdbc .OracleTypes ;
58
9+ import javax .xml .transform .Result ;
610import java .io .PrintStream ;
711import java .sql .*;
812import java .util .ArrayList ;
913import java .util .List ;
14+ import java .util .function .Consumer ;
1015
1116/**
1217 * Fetches the lines produced by a reporter.
18+ *
19+ * @author vinicius
20+ * @author pesse
1321 */
14- public class OutputBuffer {
22+ public class DefaultOutputBuffer implements OutputBuffer {
1523
1624 private Reporter reporter ;
1725
1826 /**
19- * Creates a new OutputBuffer .
27+ * Creates a new DefaultOutputBuffer .
2028 * @param reporter the reporter to be used
2129 */
22- public OutputBuffer (Reporter reporter ) {
30+ public DefaultOutputBuffer (Reporter reporter ) {
31+
32+ assert reporter .isInit () : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters" ;
33+ assert reporter .hasOutput () : "Reporter has no output. Please use NonOutputBuffer instead" ;
34+
2335 this .reporter = reporter ;
2436 }
2537
@@ -59,24 +71,20 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
5971 /**
6072 * Print the lines as soon as they are produced and call the callback passing the new line.
6173 * @param conn DB connection
62- * @param cb the callback to be called
74+ * @param onLineFetched the callback to be called
6375 * @throws SQLException any sql errors
6476 */
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 ().getId ());
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 ();
77+ public void fetchAvailable (Connection conn , Consumer <String > onLineFetched ) throws SQLException {
78+
79+ OracleConnection oraConn = conn .unwrap (OracleConnection .class );
80+
81+ try (OraclePreparedStatement pstmt = (OraclePreparedStatement )oraConn .prepareStatement ("select * from table(?.get_lines())" )) {
82+
83+ pstmt .setORAData (1 , getReporter ());
84+ try (ResultSet resultSet = pstmt .executeQuery () ) {
85+ while (resultSet .next ())
86+ onLineFetched .accept (resultSet .getString (1 ));
87+ }
8088 }
8189 }
8290
@@ -87,34 +95,25 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
8795 * @throws SQLException any sql errors
8896 */
8997 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 ().getId ());
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" ));
98+
99+ OracleConnection oraConn = conn .unwrap (OracleConnection .class );
100+
101+ try (OracleCallableStatement cstmt = (OracleCallableStatement )oraConn .prepareCall ("{? = call ?.get_lines_cursor() }" )) {
102+
103+ cstmt .registerOutParameter (1 , OracleTypes .CURSOR );
104+ cstmt .setORAData (2 , getReporter ());
105+
106+ cstmt .execute ();
107+
108+ try ( ResultSet resultSet = (ResultSet ) cstmt .getObject (1 )) {
109+
110+ List <String > outputLines = new ArrayList <>();
111+ while (resultSet .next ()) {
112+ outputLines .add (resultSet .getString ("text" ));
113+ }
114+ return outputLines ;
103115 }
104- return outputLines ;
105- } finally {
106- if (resultSet != null )
107- resultSet .close ();
108- if (callableStatement != null )
109- callableStatement .close ();
110116 }
111117 }
112118
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- }
119-
120119}
0 commit comments