Skip to content

Commit ebd4cdb

Browse files
authored
Merge pull request #56 from utPLSQL/feature/small_improvements_refactoring
Feature/small improvements refactoring
2 parents 04fa93d + c8ffe2a commit ebd4cdb

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

Diff for: src/main/java/org/utplsql/api/DBHelper.java

+30-16
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@ private DBHelper() {}
2020
* @throws SQLException any database error
2121
*/
2222
public static String newSysGuid(Connection conn) throws SQLException {
23-
CallableStatement callableStatement = null;
24-
try {
25-
callableStatement = conn.prepareCall("BEGIN ? := sys_guid(); END;");
23+
assert conn != null;
24+
try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_guid(); END;")) {
2625
callableStatement.registerOutParameter(1, OracleTypes.RAW);
2726
callableStatement.executeUpdate();
2827
return callableStatement.getString(1);
29-
} finally {
30-
if (callableStatement != null)
31-
callableStatement.close();
3228
}
3329
}
3430

@@ -39,26 +35,22 @@ public static String newSysGuid(Connection conn) throws SQLException {
3935
* @throws SQLException any database error
4036
*/
4137
public static String getCurrentSchema(Connection conn) throws SQLException {
42-
CallableStatement callableStatement = null;
43-
try {
44-
callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;");
38+
assert conn != null;
39+
try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;")) {
4540
callableStatement.registerOutParameter(1, Types.VARCHAR);
4641
callableStatement.executeUpdate();
4742
return callableStatement.getString(1);
48-
} finally {
49-
if (callableStatement != null)
50-
callableStatement.close();
5143
}
5244
}
5345

5446
/** Returns the Frameworks version string of the given connection
5547
*
5648
* @param conn Active db connection
57-
* @return
58-
* @throws SQLException
49+
* @return Version-string of the utPLSQL framework
50+
* @throws SQLException any database error
5951
*/
60-
public static Version getDatabaseFrameworkVersion( Connection conn )
61-
throws SQLException {
52+
public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLException {
53+
assert conn != null;
6254
Version result = new Version("");
6355
try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
6456
{
@@ -78,11 +70,32 @@ public static Version getDatabaseFrameworkVersion( Connection conn )
7870
return result;
7971
}
8072

73+
/** Returns the Oracle database Version from a given connection object
74+
*
75+
* @param conn Connection-Object
76+
* @return Returns version-string of the Oracle Database product component
77+
* @throws SQLException any database error
78+
*/
79+
public static String getOracleDatabaseVersion( Connection conn ) throws SQLException {
80+
assert conn != null;
81+
String result = null;
82+
try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'"))
83+
{
84+
ResultSet rs = stmt.executeQuery();
85+
86+
if ( rs.next() )
87+
result = rs.getString(1);
88+
}
89+
90+
return result;
91+
}
92+
8193
/**
8294
* Enable the dbms_output buffer with unlimited size.
8395
* @param conn the connection
8496
*/
8597
public static void enableDBMSOutput(Connection conn) {
98+
assert conn != null;
8699
try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.enable(NULL); END;")) {
87100
call.execute();
88101
} catch (SQLException e) {
@@ -95,6 +108,7 @@ public static void enableDBMSOutput(Connection conn) {
95108
* @param conn the connection
96109
*/
97110
public static void disableDBMSOutput(Connection conn) {
111+
assert conn != null;
98112
try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.disable(); END;")) {
99113
call.execute();
100114
} catch (SQLException e) {

Diff for: src/main/java/org/utplsql/api/JavaApiVersionInfo.java

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public class JavaApiVersionInfo {
1414
public static String getVersion() {
1515
return MAVEN_PROJECT_VERSION + "." + BUILD_NO;
1616
}
17+
public static String getInfo() { return MAVEN_PROJECT_NAME + " " + getVersion(); }
1718

1819
}

Diff for: src/test/java/org/utplsql/api/DBHelperIT.java

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.sql.SQLException;
66

77
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
89

910
public class DBHelperIT extends AbstractDatabaseTest {
1011

@@ -14,4 +15,10 @@ public void getFrameworkVersion() throws SQLException {
1415
assertEquals(true, v.isValid());
1516
}
1617

18+
@Test
19+
public void getOracleDatabaseVersion() throws SQLException {
20+
String databaseVersion = DBHelper.getOracleDatabaseVersion(getConnection());
21+
assertNotNull(databaseVersion);
22+
}
23+
1724
}

0 commit comments

Comments
 (0)