-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOutputBufferProvider.java
More file actions
72 lines (61 loc) · 2.53 KB
/
OutputBufferProvider.java
File metadata and controls
72 lines (61 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.utplsql.api.outputBuffer;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleTypes;
import org.utplsql.api.Version;
import org.utplsql.api.exception.InvalidVersionException;
import org.utplsql.api.reporter.Reporter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
public class OutputBufferProvider {
private OutputBufferProvider() {
}
/**
* Returns an OutputBuffer compatible with the given databaseVersion
* If we are at 3.1.0 or greater, returns an OutputBuffer based upon the information whether the Reporter has Output or not
*
* @param databaseVersion
* @param reporter
* @param conn
* @return
* @throws SQLException
*/
public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Reporter reporter, Connection conn) throws SQLException {
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
try {
if (databaseVersion.isGreaterOrEqualThan(Version.V3_1_0)) {
if (hasOutput(reporter, oraConn)) {
return new DefaultOutputBuffer(reporter);
} else {
return new NonOutputBuffer(reporter);
}
}
} catch (InvalidVersionException ignored) {
}
// If we couldn't find an appropriate OutputBuffer, return the Pre310-Compatibility-Buffer
return new CompatibilityOutputBufferPre310(reporter);
}
private static boolean hasOutput(Reporter reporter, OracleConnection oraConn) throws SQLException {
String sql =
"declare " +
" l_result int;" +
"begin " +
" begin " +
" execute immediate '" +
" begin " +
" :x := case ' || dbms_assert.simple_sql_name( ? ) || '() is of (ut_output_reporter_base) when true then 1 else 0 end;" +
" end;'" +
" using out l_result;" +
" end;" +
" ? := l_result;" +
"end;";
try (CallableStatement stmt = oraConn.prepareCall(sql)) {
stmt.setQueryTimeout(3);
stmt.setString(1, reporter.getTypeName());
stmt.registerOutParameter(2, OracleTypes.INTEGER);
stmt.execute();
int result = stmt.getInt(2);
return result == 1;
}
}
}