Skip to content

Commit 5d725d5

Browse files
committed
Added version comparison check function
1 parent 08ffc6a commit 5d725d5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/main/java/org/utplsql/api/DBHelper.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
public final class DBHelper {
1414

15+
public static final String UTPLSQL_VERSION = "3.0.3";
16+
1517
private DBHelper() {}
1618

1719
/**
@@ -52,4 +54,46 @@ public static String getCurrentSchema(Connection conn) throws SQLException {
5254
}
5355
}
5456

57+
/**
58+
* Check the utPLSQL version compatibility.
59+
* @param conn the connection
60+
* @return true if the requested utPLSQL version is compatible with the one installed on database
61+
* @throws SQLException any database error
62+
*/
63+
public static boolean versionCompatibilityCheck(Connection conn, String requested, String current)
64+
throws SQLException {
65+
CallableStatement callableStatement = null;
66+
try {
67+
callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;");
68+
callableStatement.registerOutParameter(1, Types.SMALLINT);
69+
callableStatement.setString(2, requested);
70+
71+
if (current == null)
72+
callableStatement.setNull(3, Types.VARCHAR);
73+
else
74+
callableStatement.setString(3, current);
75+
76+
callableStatement.executeUpdate();
77+
return callableStatement.getInt(1) == 1;
78+
} catch (SQLException e) {
79+
if (e.getErrorCode() == 6550)
80+
return false;
81+
else
82+
throw e;
83+
} finally {
84+
if (callableStatement != null)
85+
callableStatement.close();
86+
}
87+
}
88+
89+
public static boolean versionCompatibilityCheck(Connection conn, String requested)
90+
throws SQLException {
91+
return versionCompatibilityCheck(conn, requested, null);
92+
}
93+
94+
public static boolean versionCompatibilityCheck(Connection conn)
95+
throws SQLException {
96+
return versionCompatibilityCheck(conn, UTPLSQL_VERSION);
97+
}
98+
5599
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.utplsql.api.rules.DatabaseRule;
7+
8+
import java.sql.SQLException;
9+
10+
public class DBHelperTest {
11+
12+
@Rule
13+
public final DatabaseRule db = new DatabaseRule();
14+
15+
@Test
16+
public void compatibleVersion() {
17+
try {
18+
boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.0.0", "3.0.0");
19+
Assert.assertTrue(isCompatible);
20+
} catch (SQLException e) {
21+
e.printStackTrace();
22+
Assert.fail();
23+
}
24+
}
25+
26+
@Test
27+
public void incompatibleVersion() {
28+
try {
29+
boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.1.0", "3.0.0");
30+
Assert.assertFalse(isCompatible);
31+
} catch (SQLException e) {
32+
e.printStackTrace();
33+
Assert.fail();
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)