1111import org .utplsql .api .testRunner .TestRunnerStatement ;
1212import org .utplsql .api .testRunner .TestRunnerStatementProvider ;
1313
14+ import javax .annotation .Nullable ;
1415import java .sql .Connection ;
1516import java .sql .SQLException ;
1617import java .util .Objects ;
2526public class CompatibilityProxy {
2627
2728 public static final String UTPLSQL_COMPATIBILITY_VERSION = "3" ;
28- private static final String UTPLSQL_API_VERSION = "3.1.1" ;
2929 private final DatabaseInformation databaseInformation ;
30- private Version databaseVersion ;
30+ private Version utPlsqlVersion ;
31+ private Version realDbPlsqlVersion ;
3132 private boolean compatible = false ;
3233
3334 public CompatibilityProxy (Connection conn ) throws SQLException {
34- this (conn , false , null );
35+ this (conn , null , null );
3536 }
3637
37- public CompatibilityProxy (Connection conn , DatabaseInformation databaseInformation ) throws SQLException {
38- this (conn , false , databaseInformation );
38+ @ Deprecated
39+ public CompatibilityProxy (Connection conn , boolean skipCompatibilityCheck ) throws SQLException {
40+ this (conn , skipCompatibilityCheck , null );
3941 }
4042
41- public CompatibilityProxy (Connection conn , boolean skipCompatibilityCheck ) throws SQLException {
42- this (conn , skipCompatibilityCheck , null );
43+ @ Deprecated
44+ public CompatibilityProxy (Connection conn , boolean skipCompatibilityCheck , @ Nullable DatabaseInformation databaseInformation ) throws SQLException {
45+ this (conn , skipCompatibilityCheck ? Version .LATEST : null , databaseInformation );
46+ }
47+
48+ public CompatibilityProxy (Connection conn , @ Nullable DatabaseInformation databaseInformation ) throws SQLException {
49+ this (conn , null , databaseInformation );
4350 }
4451
45- public CompatibilityProxy (Connection conn , boolean skipCompatibilityCheck , DatabaseInformation databaseInformation ) throws SQLException {
52+ public CompatibilityProxy (Connection conn , @ Nullable Version assumedUtPlsVersion ) throws SQLException {
53+ this (conn , assumedUtPlsVersion , null );
54+ }
55+
56+ public CompatibilityProxy (Connection conn , @ Nullable Version assumedUtPlsqlVersion , @ Nullable DatabaseInformation databaseInformation ) throws SQLException {
4657 this .databaseInformation = (databaseInformation != null )
4758 ? databaseInformation
4859 : new DefaultDatabaseInformation ();
4960
50- if (skipCompatibilityCheck ) {
51- doExpectCompatibility ();
61+ realDbPlsqlVersion = this .databaseInformation .getUtPlsqlFrameworkVersion (conn );
62+ if ( assumedUtPlsqlVersion != null ) {
63+ utPlsqlVersion = assumedUtPlsqlVersion ;
64+ compatible = utPlsqlVersion .getNormalizedString ().startsWith (UTPLSQL_COMPATIBILITY_VERSION );
5265 } else {
5366 doCompatibilityCheckWithDatabase (conn );
5467 }
@@ -62,18 +75,18 @@ public CompatibilityProxy(Connection conn, boolean skipCompatibilityCheck, Datab
6275 * @throws SQLException
6376 */
6477 private void doCompatibilityCheckWithDatabase (Connection conn ) throws SQLException {
65- databaseVersion = databaseInformation . getUtPlsqlFrameworkVersion ( conn ) ;
78+ utPlsqlVersion = realDbPlsqlVersion ;
6679 Version clientVersion = Version .create (UTPLSQL_COMPATIBILITY_VERSION );
6780
68- if (databaseVersion == null ) {
81+ if (utPlsqlVersion == null ) {
6982 throw new DatabaseNotCompatibleException ("Could not get database version" , clientVersion , null , null );
7083 }
7184
72- if (databaseVersion .getMajor () == null ) {
73- throw new DatabaseNotCompatibleException ("Illegal database version: " + databaseVersion .toString (), clientVersion , databaseVersion , null );
85+ if (utPlsqlVersion .getMajor () == null ) {
86+ throw new DatabaseNotCompatibleException ("Illegal database version: " + utPlsqlVersion .toString (), clientVersion , utPlsqlVersion , null );
7487 }
7588
76- if (OptionalFeatures .FRAMEWORK_COMPATIBILITY_CHECK .isAvailableFor (databaseVersion )) {
89+ if (OptionalFeatures .FRAMEWORK_COMPATIBILITY_CHECK .isAvailableFor (utPlsqlVersion )) {
7790 try {
7891 compatible = versionCompatibilityCheck (conn , UTPLSQL_COMPATIBILITY_VERSION , null );
7992 } catch (SQLException e ) {
@@ -84,14 +97,6 @@ private void doCompatibilityCheckWithDatabase(Connection conn) throws SQLExcepti
8497 }
8598 }
8699
87- /**
88- * Just prepare the proxy to expect compatibility, expecting the database framework to be the same version as the API
89- */
90- private void doExpectCompatibility () {
91- databaseVersion = Version .create (UTPLSQL_API_VERSION );
92- compatible = true ;
93- }
94-
95100 /**
96101 * Check the utPLSQL version compatibility.
97102 *
@@ -121,10 +126,10 @@ private boolean versionCompatibilityCheck(Connection conn, String requested, Str
121126 private boolean versionCompatibilityCheckPre303 (String requested ) {
122127 Version requestedVersion = Version .create (requested );
123128
124- Objects .requireNonNull (databaseVersion .getMajor (), "Illegal database Version: " + databaseVersion .toString ());
125- return databaseVersion .getMajor ().equals (requestedVersion .getMajor ())
129+ Objects .requireNonNull (utPlsqlVersion .getMajor (), "Illegal database Version: " + utPlsqlVersion .toString ());
130+ return utPlsqlVersion .getMajor ().equals (requestedVersion .getMajor ())
126131 && (requestedVersion .getMinor () == null
127- || requestedVersion .getMinor ().equals (databaseVersion .getMinor ()));
132+ || requestedVersion .getMinor ().equals (utPlsqlVersion .getMinor ()));
128133 }
129134
130135 /**
@@ -133,16 +138,29 @@ private boolean versionCompatibilityCheckPre303(String requested) {
133138 */
134139 public void failOnNotCompatible () throws DatabaseNotCompatibleException {
135140 if (!isCompatible ()) {
136- throw new DatabaseNotCompatibleException (databaseVersion );
141+ throw new DatabaseNotCompatibleException (utPlsqlVersion );
137142 }
138143 }
139144
140145 public boolean isCompatible () {
141146 return compatible ;
142147 }
143148
144- public Version getDatabaseVersion () {
145- return databaseVersion ;
149+ @ Deprecated
150+ public Version getDatabaseVersion () { return utPlsqlVersion ; }
151+
152+ public Version getUtPlsqlVersion () {
153+ return utPlsqlVersion ;
154+ }
155+
156+ public Version getRealDbPlsqlVersion () { return realDbPlsqlVersion ; }
157+
158+ public String getVersionDescription () {
159+ if ( utPlsqlVersion != realDbPlsqlVersion ) {
160+ return realDbPlsqlVersion .toString () + " (Assumed: " + utPlsqlVersion .toString () + ")" ;
161+ } else {
162+ return utPlsqlVersion .toString ();
163+ }
146164 }
147165
148166 /**
@@ -154,7 +172,7 @@ public Version getDatabaseVersion() {
154172 * @throws SQLException
155173 */
156174 public TestRunnerStatement getTestRunnerStatement (TestRunnerOptions options , Connection conn ) throws SQLException {
157- return TestRunnerStatementProvider .getCompatibleTestRunnerStatement (databaseVersion , options , conn );
175+ return TestRunnerStatementProvider .getCompatibleTestRunnerStatement (utPlsqlVersion , options , conn );
158176 }
159177
160178 /**
@@ -166,6 +184,6 @@ public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Con
166184 * @throws SQLException
167185 */
168186 public OutputBuffer getOutputBuffer (Reporter reporter , Connection conn ) throws SQLException {
169- return OutputBufferProvider .getCompatibleOutputBuffer (databaseVersion , reporter , conn );
187+ return OutputBufferProvider .getCompatibleOutputBuffer (utPlsqlVersion , reporter , conn );
170188 }
171189}
0 commit comments