| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JdbcUtils |
|
| 7.2;7.2 |
| 1 | package liquibase.util; | |
| 2 | ||
| 3 | import java.sql.Blob; | |
| 4 | import java.sql.Clob; | |
| 5 | import java.sql.ResultSet; | |
| 6 | import java.sql.SQLException; | |
| 7 | import java.sql.Statement; | |
| 8 | import java.sql.Types; | |
| 9 | import java.util.Collection; | |
| 10 | ||
| 11 | import liquibase.exception.DatabaseException; | |
| 12 | ||
| 13 | 0 | public abstract class JdbcUtils { |
| 14 | ||
| 15 | /** | |
| 16 | * Constant that indicates an unknown (or unspecified) SQL type. | |
| 17 | * | |
| 18 | * @see java.sql.Types | |
| 19 | */ | |
| 20 | public static final int TYPE_UNKNOWN = Integer.MIN_VALUE; | |
| 21 | ||
| 22 | /** | |
| 23 | * Close the given JDBC Statement and ignore any thrown exception. This is useful for typical finally blocks in | |
| 24 | * manual JDBC code. | |
| 25 | * | |
| 26 | * @param stmt | |
| 27 | * the JDBC Statement to close (may be <code>null</code>) | |
| 28 | */ | |
| 29 | public static void closeStatement(Statement stmt) { | |
| 30 | 0 | if (stmt != null) { |
| 31 | try { | |
| 32 | 0 | stmt.close(); |
| 33 | 0 | } catch (SQLException ex) { |
| 34 | // logger.debug("Could not close JDBC Statement", ex); | |
| 35 | 0 | } catch (Throwable ex) { |
| 36 | // We don't trust the JDBC driver: It might throw RuntimeException or Error. | |
| 37 | // logger.debug("Unexpected exception on closing JDBC Statement", ex); | |
| 38 | 0 | } |
| 39 | } | |
| 40 | 0 | } |
| 41 | ||
| 42 | /** | |
| 43 | * Close the given JDBC ResultSet and ignore any thrown exception. This is useful for typical finally blocks in | |
| 44 | * manual JDBC code. | |
| 45 | * | |
| 46 | * @param rs | |
| 47 | * the JDBC ResultSet to close (may be <code>null</code>) | |
| 48 | */ | |
| 49 | public static void closeResultSet(ResultSet rs) { | |
| 50 | 0 | if (rs != null) { |
| 51 | try { | |
| 52 | 0 | rs.close(); |
| 53 | 0 | } catch (SQLException ex) { |
| 54 | // logger.debug("Could not close JDBC ResultSet", ex); | |
| 55 | 0 | } catch (Throwable ex) { |
| 56 | // We don't trust the JDBC driver: It might throw RuntimeException or Error. | |
| 57 | // logger.debug("Unexpected exception on closing JDBC ResultSet", ex); | |
| 58 | 0 | } |
| 59 | } | |
| 60 | 0 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Retrieve a JDBC column value from a ResultSet, using the most appropriate value type. The returned value should | |
| 64 | * be a detached value object, not having any ties to the active ResultSet: in particular, it should not be a Blob | |
| 65 | * or Clob object but rather a byte array respectively String representation. | |
| 66 | * <p> | |
| 67 | * Uses the <code>getObject(index)</code> method, but includes additional "hacks" to get around Oracle 10g returning | |
| 68 | * a non-standard object for its TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns leaving out | |
| 69 | * the time portion: These columns will explicitly be extracted as standard <code>java.sql.Timestamp</code> object. | |
| 70 | * | |
| 71 | * @param rs | |
| 72 | * is the ResultSet holding the data | |
| 73 | * @param index | |
| 74 | * is the column index | |
| 75 | * @return the value object | |
| 76 | * @throws SQLException | |
| 77 | * if thrown by the JDBC API | |
| 78 | * @see java.sql.Blob | |
| 79 | * @see java.sql.Clob | |
| 80 | * @see java.sql.Timestamp | |
| 81 | */ | |
| 82 | public static Object getResultSetValue(ResultSet rs, int index) throws SQLException { | |
| 83 | 0 | Object obj = rs.getObject(index); |
| 84 | 0 | if (obj instanceof Blob) { |
| 85 | 0 | obj = rs.getBytes(index); |
| 86 | 0 | } else if (obj instanceof Clob) { |
| 87 | 0 | obj = rs.getString(index); |
| 88 | 0 | } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) { |
| 89 | 0 | obj = rs.getTimestamp(index); |
| 90 | 0 | } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) { |
| 91 | 0 | String metaDataClassName = rs.getMetaData().getColumnClassName(index); |
| 92 | 0 | if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) { |
| 93 | 0 | obj = rs.getTimestamp(index); |
| 94 | } else { | |
| 95 | 0 | obj = rs.getDate(index); |
| 96 | } | |
| 97 | 0 | } else if (obj != null && obj instanceof java.sql.Date) { |
| 98 | 0 | if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) { |
| 99 | 0 | obj = rs.getTimestamp(index); |
| 100 | } | |
| 101 | } | |
| 102 | 0 | return obj; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Check whether the given SQL type is numeric. | |
| 107 | * | |
| 108 | * @param sqlType | |
| 109 | * the SQL type to be checked | |
| 110 | * @return whether the type is numeric | |
| 111 | */ | |
| 112 | public static boolean isNumeric(int sqlType) { | |
| 113 | 0 | return Types.BIT == sqlType || Types.BIGINT == sqlType || Types.DECIMAL == sqlType || Types.DOUBLE == sqlType |
| 114 | || Types.FLOAT == sqlType || Types.INTEGER == sqlType || Types.NUMERIC == sqlType | |
| 115 | || Types.REAL == sqlType || Types.SMALLINT == sqlType || Types.TINYINT == sqlType; | |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Return a single result object from the given Collection. | |
| 120 | * <p> | |
| 121 | * Throws an exception if 0 or more than 1 element found. | |
| 122 | * | |
| 123 | * @param results | |
| 124 | * the result Collection (can be <code>null</code>) | |
| 125 | * @return the single result object | |
| 126 | */ | |
| 127 | public static Object requiredSingleResult(Collection results) throws DatabaseException { | |
| 128 | 0 | int size = (results != null ? results.size() : 0); |
| 129 | 0 | if (size == 0) { |
| 130 | 0 | throw new DatabaseException("Empty result set, expected one row"); |
| 131 | } | |
| 132 | 0 | if (results.size() > 1) { |
| 133 | 0 | throw new DatabaseException("Result set larger than one row"); |
| 134 | } | |
| 135 | 0 | return results.iterator().next(); |
| 136 | } | |
| 137 | ||
| 138 | } |