|
| 1 | +package io.github.utplsql; |
| 2 | + |
| 3 | +import io.github.utplsql.types.DocumentationReporter; |
| 4 | +import io.github.utplsql.types.CustomTypes; |
| 5 | +import oracle.jdbc.OracleTypes; |
| 6 | + |
| 7 | +import java.sql.*; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by Vinicius Avellar on 12/04/2017. |
| 12 | + */ |
| 13 | +public final class UTPLSQL { |
| 14 | + |
| 15 | + private static Connection sConnection; |
| 16 | + |
| 17 | + private UTPLSQL() {} |
| 18 | + |
| 19 | + public static void init(String url, String user, String password) throws SQLException { |
| 20 | + UTPLSQL.init(DriverManager.getConnection(url, user, password)); |
| 21 | + } |
| 22 | + |
| 23 | + public static void init(Connection conn) throws SQLException { |
| 24 | + sConnection = conn; |
| 25 | + createTypeMap(); |
| 26 | + } |
| 27 | + |
| 28 | + public static void close() { |
| 29 | + if (sConnection != null) |
| 30 | + try { sConnection.close(); } catch (SQLException ignored) {} |
| 31 | + } |
| 32 | + |
| 33 | + protected static Connection getConnection() { |
| 34 | + if (sConnection == null) |
| 35 | + throw new RuntimeException("Connection not initialized."); |
| 36 | + |
| 37 | + return sConnection; |
| 38 | + } |
| 39 | + |
| 40 | + public static String newSysGuid() throws SQLException { |
| 41 | + CallableStatement callableStatement = null; |
| 42 | + try { |
| 43 | + callableStatement = sConnection.prepareCall("BEGIN :id := sys_guid(); END;"); |
| 44 | + callableStatement.registerOutParameter(":id", OracleTypes.RAW); |
| 45 | + callableStatement.executeUpdate(); |
| 46 | + return callableStatement.getString(":id"); |
| 47 | + } finally { |
| 48 | + if (callableStatement != null) |
| 49 | + callableStatement.close(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void createTypeMap() throws SQLException { |
| 54 | + Map typeMap = sConnection.getTypeMap(); |
| 55 | + typeMap.put(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), DocumentationReporter.class); |
| 56 | + sConnection.setTypeMap(typeMap); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments