|
| 1 | +package org.utplsql.api.compatibility; |
| 2 | + |
| 3 | +import org.utplsql.api.DBHelper; |
| 4 | +import org.utplsql.api.TestRunnerOptions; |
| 5 | +import org.utplsql.api.Version; |
| 6 | +import org.utplsql.api.exception.DatabaseNotCompatibleException; |
| 7 | +import org.utplsql.api.testRunner.TestRunnerStatement; |
| 8 | +import org.utplsql.api.testRunner.TestRunnerStatementProvider; |
| 9 | + |
| 10 | +import java.sql.CallableStatement; |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Types; |
| 14 | + |
| 15 | +/** Class to check compatibility with database framework and also to give several specific implementations depending |
| 16 | + * on the version of the connected framework. |
| 17 | + * If one skips the compatibility check, the Proxy acts as like the framework has the same version as the API |
| 18 | + * |
| 19 | + * @author pesse |
| 20 | + */ |
| 21 | +public class CompatibilityProxy { |
| 22 | + |
| 23 | + public static final String UTPLSQL_API_VERSION = "3.0.4"; |
| 24 | + public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0"; |
| 25 | + |
| 26 | + private Version databaseVersion; |
| 27 | + private boolean compatible = false; |
| 28 | + |
| 29 | + public CompatibilityProxy( Connection conn ) throws SQLException |
| 30 | + { |
| 31 | + this(conn, false); |
| 32 | + } |
| 33 | + |
| 34 | + public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException |
| 35 | + { |
| 36 | + if ( skipCompatibilityCheck ) |
| 37 | + doExpectCompatibility(); |
| 38 | + else |
| 39 | + doCompatibilityCheckWithDatabase(conn); |
| 40 | + } |
| 41 | + |
| 42 | + /** Receives the current framework version from database and checks - depending on the framework version - whether |
| 43 | + * the API version is compatible or not. |
| 44 | + * |
| 45 | + * @param conn |
| 46 | + * @throws SQLException |
| 47 | + */ |
| 48 | + private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException |
| 49 | + { |
| 50 | + databaseVersion = DBHelper.getDatabaseFrameworkVersion(conn); |
| 51 | + |
| 52 | + if (frameworkHasCompatibilityCheck()) { |
| 53 | + try { |
| 54 | + compatible = versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION, null); |
| 55 | + } catch (SQLException e) { |
| 56 | + throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), new Version(UTPLSQL_COMPATIBILITY_VERSION), new Version("Unknown"), e); |
| 57 | + } |
| 58 | + } else |
| 59 | + compatible = versionCompatibilityCheckPre303(UTPLSQL_COMPATIBILITY_VERSION); |
| 60 | + } |
| 61 | + |
| 62 | + /** Just prepare the proxy to expect compatibility, expecting the database framework to be the same version as the API |
| 63 | + * |
| 64 | + */ |
| 65 | + private void doExpectCompatibility() |
| 66 | + { |
| 67 | + databaseVersion = new Version(UTPLSQL_API_VERSION); |
| 68 | + compatible = true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check the utPLSQL version compatibility. |
| 73 | + * @param conn the connection |
| 74 | + * @return true if the requested utPLSQL version is compatible with the one installed on database |
| 75 | + * @throws SQLException any database error |
| 76 | + */ |
| 77 | + private boolean versionCompatibilityCheck(Connection conn, String requested, String current) |
| 78 | + throws SQLException { |
| 79 | + CallableStatement callableStatement = null; |
| 80 | + try { |
| 81 | + callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;"); |
| 82 | + callableStatement.registerOutParameter(1, Types.SMALLINT); |
| 83 | + callableStatement.setString(2, requested); |
| 84 | + |
| 85 | + if (current == null) |
| 86 | + callableStatement.setNull(3, Types.VARCHAR); |
| 87 | + else |
| 88 | + callableStatement.setString(3, current); |
| 89 | + |
| 90 | + callableStatement.executeUpdate(); |
| 91 | + return callableStatement.getInt(1) == 1; |
| 92 | + } catch (SQLException e) { |
| 93 | + if (e.getErrorCode() == 6550) |
| 94 | + return false; |
| 95 | + else |
| 96 | + throw e; |
| 97 | + } finally { |
| 98 | + if (callableStatement != null) |
| 99 | + callableStatement.close(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Simple fallback check for compatiblity: Major and Minor version must be equal |
| 104 | + * |
| 105 | + * @param requested |
| 106 | + * @return |
| 107 | + */ |
| 108 | + private boolean versionCompatibilityCheckPre303( String requested ) |
| 109 | + { |
| 110 | + Version requesteVersion = new Version(requested); |
| 111 | + |
| 112 | + if ( databaseVersion.getMajor() == requesteVersion.getMajor() && (requesteVersion.getMinor() == null || databaseVersion.getMinor() == requesteVersion.getMinor()) ) |
| 113 | + return true; |
| 114 | + else |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + /** Checks if framework has the compatibility check, which is since 3.0.3 |
| 119 | + * |
| 120 | + * @return Whether framework is >= 3.0.3 or not |
| 121 | + */ |
| 122 | + private boolean frameworkHasCompatibilityCheck() |
| 123 | + { |
| 124 | + if ( databaseVersion.getMajor() >= 3 && databaseVersion.getMinor() >= 0 && databaseVersion.getBugfix() >= 3 ) // Compatibility check is included since 3.0.3 |
| 125 | + return true; |
| 126 | + else |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + /** Checks if actual API-version is compatible with utPLSQL database version and throws a DatabaseNotCompatibleException if not |
| 131 | + * Throws a DatabaseNotCompatibleException if version compatibility can not be checked. |
| 132 | + * |
| 133 | + */ |
| 134 | + public void failOnNotCompatible() throws DatabaseNotCompatibleException |
| 135 | + { |
| 136 | + if ( !isCompatible() ) |
| 137 | + throw new DatabaseNotCompatibleException( databaseVersion ); |
| 138 | + } |
| 139 | + |
| 140 | + public boolean isCompatible() |
| 141 | + { |
| 142 | + return compatible; |
| 143 | + } |
| 144 | + |
| 145 | + public Version getDatabaseVersion() |
| 146 | + { |
| 147 | + return databaseVersion; |
| 148 | + } |
| 149 | + |
| 150 | + /** Returns a TestRunnerStatement compatible with the current framework |
| 151 | + * |
| 152 | + * @param options |
| 153 | + * @param conn |
| 154 | + * @return |
| 155 | + * @throws SQLException |
| 156 | + */ |
| 157 | + public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Connection conn) throws SQLException |
| 158 | + { |
| 159 | + return TestRunnerStatementProvider.getCompatibleTestRunnerStatement(databaseVersion, options, conn); |
| 160 | + } |
| 161 | +} |
0 commit comments