Skip to content

Commit 1514834

Browse files
committed
Cleanup of similar OutputBuffers
1 parent 95d3fd7 commit 1514834

File tree

3 files changed

+72
-103
lines changed

3 files changed

+72
-103
lines changed

src/main/java/org/utplsql/api/outputBuffer/AbstractOutputBuffer.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package org.utplsql.api.outputBuffer;
22

3-
import oracle.jdbc.OracleCallableStatement;
4-
import oracle.jdbc.OracleConnection;
5-
import oracle.jdbc.OraclePreparedStatement;
6-
import oracle.jdbc.OracleTypes;
73
import org.utplsql.api.reporter.Reporter;
84

95
import java.io.PrintStream;
10-
import java.sql.Connection;
11-
import java.sql.ResultSet;
12-
import java.sql.SQLException;
6+
import java.sql.*;
137
import java.util.ArrayList;
148
import java.util.List;
159
import java.util.function.Consumer;
@@ -68,4 +62,51 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
6862
});
6963
}
7064

65+
protected abstract PreparedStatement getLinesStatement( Connection conn ) throws SQLException;
66+
67+
protected abstract CallableStatement getLinesCursorStatement( Connection conn ) throws SQLException;
68+
69+
/**
70+
* Print the lines as soon as they are produced and call the callback passing the new line.
71+
* @param conn DB connection
72+
* @param onLineFetched the callback to be called
73+
* @throws SQLException any sql errors
74+
*/
75+
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {
76+
77+
try (PreparedStatement pstmt = getLinesStatement(conn)) {
78+
79+
pstmt.setString(1, getReporter().getId());
80+
try (ResultSet resultSet = pstmt.executeQuery() ) {
81+
while (resultSet.next())
82+
onLineFetched.accept(resultSet.getString(1));
83+
}
84+
}
85+
}
86+
87+
/**
88+
* Get all lines from output buffer and return it as a list of strings.
89+
* @param conn DB connection
90+
* @return the lines
91+
* @throws SQLException any sql errors
92+
*/
93+
public List<String> fetchAll(Connection conn) throws SQLException {
94+
95+
try (CallableStatement cstmt = getLinesCursorStatement(conn)) {
96+
97+
cstmt.execute();
98+
99+
try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {
100+
101+
List<String> outputLines = new ArrayList<>();
102+
while (resultSet.next()) {
103+
outputLines.add(resultSet.getString("text"));
104+
}
105+
return outputLines;
106+
}
107+
}
108+
}
109+
110+
111+
71112
}
Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package org.utplsql.api.outputBuffer;
22

3-
import oracle.jdbc.OracleCallableStatement;
4-
import oracle.jdbc.OracleConnection;
5-
import oracle.jdbc.OraclePreparedStatement;
63
import oracle.jdbc.OracleTypes;
74
import org.utplsql.api.reporter.Reporter;
85

9-
import java.io.PrintStream;
6+
import java.sql.CallableStatement;
107
import java.sql.Connection;
11-
import java.sql.ResultSet;
8+
import java.sql.PreparedStatement;
129
import java.sql.SQLException;
13-
import java.util.ArrayList;
14-
import java.util.List;
15-
import java.util.function.Consumer;
1610

1711
/** Compatibility Output-Buffer for 3.0.0 - 3.0.4
1812
*
@@ -24,52 +18,18 @@ class CompatibilityOutputBufferPre310 extends AbstractOutputBuffer {
2418
super(reporter);
2519
}
2620

27-
28-
/**
29-
* Print the lines as soon as they are produced and call the callback passing the new line.
30-
* @param conn DB connection
31-
* @param onLineFetched the callback to be called
32-
* @throws SQLException any sql errors
33-
*/
34-
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {
35-
36-
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
37-
38-
try (OraclePreparedStatement pstmt = (OraclePreparedStatement)oraConn.prepareStatement("select * from table(ut_output_buffer.get_lines(?))")) {
39-
40-
pstmt.setORAData(1, getReporter());
41-
try (ResultSet resultSet = pstmt.executeQuery() ) {
42-
while (resultSet.next())
43-
onLineFetched.accept(resultSet.getString(1));
44-
}
45-
}
21+
@Override
22+
protected PreparedStatement getLinesStatement(Connection conn) throws SQLException {
23+
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))");
24+
pstmt.setString(1, getReporter().getId());
25+
return pstmt;
4626
}
4727

48-
/**
49-
* Get all lines from output buffer and return it as a list of strings.
50-
* @param conn DB connection
51-
* @return the lines
52-
* @throws SQLException any sql errors
53-
*/
54-
public List<String> fetchAll(Connection conn) throws SQLException {
55-
56-
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
57-
58-
try (OracleCallableStatement cstmt = (OracleCallableStatement)oraConn.prepareCall("{? = call ?.get_lines_cursor() }")) {
59-
60-
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
61-
cstmt.setORAData(2, getReporter());
62-
63-
cstmt.execute();
64-
65-
try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {
66-
67-
List<String> outputLines = new ArrayList<>();
68-
while (resultSet.next()) {
69-
outputLines.add(resultSet.getString("text"));
70-
}
71-
return outputLines;
72-
}
73-
}
28+
@Override
29+
protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLException {
30+
CallableStatement cstmt = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
31+
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
32+
cstmt.setString(2, getReporter().getId());
33+
return cstmt;
7434
}
7535
}

src/main/java/org/utplsql/api/outputBuffer/DefaultOutputBuffer.java

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,20 @@ class DefaultOutputBuffer extends AbstractOutputBuffer {
2929
super(reporter);
3030
}
3131

32-
/**
33-
* Print the lines as soon as they are produced and call the callback passing the new line.
34-
* @param conn DB connection
35-
* @param onLineFetched the callback to be called
36-
* @throws SQLException any sql errors
37-
*/
38-
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {
39-
32+
@Override
33+
protected PreparedStatement getLinesStatement(Connection conn) throws SQLException {
4034
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
41-
42-
try (OraclePreparedStatement pstmt = (OraclePreparedStatement)oraConn.prepareStatement("select * from table(?.get_lines())")) {
43-
44-
pstmt.setORAData(1, getReporter());
45-
try (ResultSet resultSet = pstmt.executeQuery() ) {
46-
while (resultSet.next())
47-
onLineFetched.accept(resultSet.getString(1));
48-
}
49-
}
35+
OraclePreparedStatement pstmt = (OraclePreparedStatement)oraConn.prepareStatement("select * from table(?.get_lines())");
36+
pstmt.setORAData(1, getReporter());
37+
return pstmt;
5038
}
5139

52-
/**
53-
* Get all lines from output buffer and return it as a list of strings.
54-
* @param conn DB connection
55-
* @return the lines
56-
* @throws SQLException any sql errors
57-
*/
58-
public List<String> fetchAll(Connection conn) throws SQLException {
59-
40+
@Override
41+
protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLException {
6042
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
61-
62-
try (OracleCallableStatement cstmt = (OracleCallableStatement)oraConn.prepareCall("{? = call ?.get_lines_cursor() }")) {
63-
64-
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
65-
cstmt.setORAData(2, getReporter());
66-
67-
cstmt.execute();
68-
69-
try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {
70-
71-
List<String> outputLines = new ArrayList<>();
72-
while (resultSet.next()) {
73-
outputLines.add(resultSet.getString("text"));
74-
}
75-
return outputLines;
76-
}
77-
}
43+
OracleCallableStatement cstmt = (OracleCallableStatement) oraConn.prepareCall("{? = call ?.get_lines_cursor() }");
44+
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
45+
cstmt.setORAData(2, getReporter());
46+
return cstmt;
7847
}
79-
8048
}

0 commit comments

Comments
 (0)