From 6f001e17981d91df07caccd0764d200bdfa018f6 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 21 Dec 2012 18:25:41 +0100 Subject: [PATCH] Moved some useful JDBC utility methods from FieldTypeHelper to the public JDBCUtils class --- .../java/org/jooq/impl/FieldTypeHelper.java | 59 ++++++++----------- .../java/org/jooq/tools/jdbc/JDBCUtils.java | 48 +++++++++++++++ 2 files changed, 72 insertions(+), 35 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java index 2b036b3963..b8b996e363 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java @@ -38,6 +38,8 @@ package org.jooq.impl; import static org.jooq.SQLDialect.CUBRID; import static org.jooq.SQLDialect.POSTGRES; +import static org.jooq.tools.jdbc.JDBCUtils.wasNull; +import static org.jooq.tools.jdbc.JDBCUtils.safeFree; import static org.jooq.tools.reflect.Reflect.on; import java.math.BigDecimal; @@ -75,7 +77,6 @@ import org.jooq.UDTRecord; import org.jooq.exception.SQLDialectNotSupportedException; import org.jooq.tools.Convert; import org.jooq.tools.JooqLogger; -import org.jooq.tools.jdbc.JDBCUtils; import org.jooq.types.DayToSecond; import org.jooq.types.UByte; import org.jooq.types.UInteger; @@ -125,7 +126,7 @@ public final class FieldTypeHelper { return (T) stream.readBlob(); } else if (type == Boolean.class) { - return (T) checkWasNull(stream, Boolean.valueOf(stream.readBoolean())); + return (T) wasNull(stream, Boolean.valueOf(stream.readBoolean())); } else if (type == BigInteger.class) { BigDecimal result = stream.readBigDecimal(); @@ -135,7 +136,7 @@ public final class FieldTypeHelper { return (T) stream.readBigDecimal(); } else if (type == Byte.class) { - return (T) checkWasNull(stream, Byte.valueOf(stream.readByte())); + return (T) wasNull(stream, Byte.valueOf(stream.readByte())); } else if (type == byte[].class) { @@ -147,7 +148,7 @@ public final class FieldTypeHelper { return (T) (blob == null ? null : blob.getBytes(1, (int) blob.length())); } finally { - JDBCUtils.safeFree(blob); + safeFree(blob); } } else { @@ -161,19 +162,19 @@ public final class FieldTypeHelper { return (T) stream.readDate(); } else if (type == Double.class) { - return (T) checkWasNull(stream, Double.valueOf(stream.readDouble())); + return (T) wasNull(stream, Double.valueOf(stream.readDouble())); } else if (type == Float.class) { - return (T) checkWasNull(stream, Float.valueOf(stream.readFloat())); + return (T) wasNull(stream, Float.valueOf(stream.readFloat())); } else if (type == Integer.class) { - return (T) checkWasNull(stream, Integer.valueOf(stream.readInt())); + return (T) wasNull(stream, Integer.valueOf(stream.readInt())); } else if (type == Long.class) { - return (T) checkWasNull(stream, Long.valueOf(stream.readLong())); + return (T) wasNull(stream, Long.valueOf(stream.readLong())); } else if (type == Short.class) { - return (T) checkWasNull(stream, Short.valueOf(stream.readShort())); + return (T) wasNull(stream, Short.valueOf(stream.readShort())); } else if (type == String.class) { return (T) stream.readString(); @@ -383,7 +384,7 @@ public final class FieldTypeHelper { return (T) rs.getBlob(index); } else if (type == Boolean.class) { - return (T) checkWasNull(rs, Boolean.valueOf(rs.getBoolean(index))); + return (T) wasNull(rs, Boolean.valueOf(rs.getBoolean(index))); } else if (type == BigInteger.class) { // The SQLite JDBC driver doesn't support BigDecimals @@ -405,7 +406,7 @@ public final class FieldTypeHelper { } } else if (type == Byte.class) { - return (T) checkWasNull(rs, Byte.valueOf(rs.getByte(index))); + return (T) wasNull(rs, Byte.valueOf(rs.getByte(index))); } else if (type == byte[].class) { return (T) rs.getBytes(index); @@ -417,19 +418,19 @@ public final class FieldTypeHelper { return (T) getDate(ctx.getDialect(), rs, index); } else if (type == Double.class) { - return (T) checkWasNull(rs, Double.valueOf(rs.getDouble(index))); + return (T) wasNull(rs, Double.valueOf(rs.getDouble(index))); } else if (type == Float.class) { - return (T) checkWasNull(rs, Float.valueOf(rs.getFloat(index))); + return (T) wasNull(rs, Float.valueOf(rs.getFloat(index))); } else if (type == Integer.class) { - return (T) checkWasNull(rs, Integer.valueOf(rs.getInt(index))); + return (T) wasNull(rs, Integer.valueOf(rs.getInt(index))); } else if (type == Long.class) { - return (T) checkWasNull(rs, Long.valueOf(rs.getLong(index))); + return (T) wasNull(rs, Long.valueOf(rs.getLong(index))); } else if (type == Short.class) { - return (T) checkWasNull(rs, Short.valueOf(rs.getShort(index))); + return (T) wasNull(rs, Short.valueOf(rs.getShort(index))); } else if (type == String.class) { return (T) rs.getString(index); @@ -648,18 +649,6 @@ public final class FieldTypeHelper { } } - private static T checkWasNull(SQLInput stream, T value) throws SQLException { - return stream.wasNull() ? null : value; - } - - private static T checkWasNull(ResultSet rs, T value) throws SQLException { - return rs.wasNull() ? null : value; - } - - private static T checkWasNull(CallableStatement statement, T value) throws SQLException { - return statement.wasNull() ? null : value; - } - @SuppressWarnings("unchecked") private static T getEnumType(Class type, String literal) throws SQLException { try { @@ -688,7 +677,7 @@ public final class FieldTypeHelper { return (T) stmt.getBlob(index); } else if (type == Boolean.class) { - return (T) checkWasNull(stmt, Boolean.valueOf(stmt.getBoolean(index))); + return (T) wasNull(stmt, Boolean.valueOf(stmt.getBoolean(index))); } else if (type == BigInteger.class) { BigDecimal result = stmt.getBigDecimal(index); @@ -698,7 +687,7 @@ public final class FieldTypeHelper { return (T) stmt.getBigDecimal(index); } else if (type == Byte.class) { - return (T) checkWasNull(stmt, Byte.valueOf(stmt.getByte(index))); + return (T) wasNull(stmt, Byte.valueOf(stmt.getByte(index))); } else if (type == byte[].class) { return (T) stmt.getBytes(index); @@ -710,19 +699,19 @@ public final class FieldTypeHelper { return (T) stmt.getDate(index); } else if (type == Double.class) { - return (T) checkWasNull(stmt, Double.valueOf(stmt.getDouble(index))); + return (T) wasNull(stmt, Double.valueOf(stmt.getDouble(index))); } else if (type == Float.class) { - return (T) checkWasNull(stmt, Float.valueOf(stmt.getFloat(index))); + return (T) wasNull(stmt, Float.valueOf(stmt.getFloat(index))); } else if (type == Integer.class) { - return (T) checkWasNull(stmt, Integer.valueOf(stmt.getInt(index))); + return (T) wasNull(stmt, Integer.valueOf(stmt.getInt(index))); } else if (type == Long.class) { - return (T) checkWasNull(stmt, Long.valueOf(stmt.getLong(index))); + return (T) wasNull(stmt, Long.valueOf(stmt.getLong(index))); } else if (type == Short.class) { - return (T) checkWasNull(stmt, Short.valueOf(stmt.getShort(index))); + return (T) wasNull(stmt, Short.valueOf(stmt.getShort(index))); } else if (type == String.class) { return (T) stmt.getString(index); diff --git a/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java index 348111c0b8..abc8219498 100644 --- a/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/JDBCUtils.java @@ -36,9 +36,12 @@ package org.jooq.tools.jdbc; import java.sql.Blob; +import java.sql.CallableStatement; import java.sql.Clob; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLInput; import java.sql.Statement; /** @@ -121,6 +124,51 @@ public class JDBCUtils { } } + /** + * Convenient way to check if a JDBC-originated record was null + *

+ * This is useful to check if primitive types obtained from the JDBC API + * were actually SQL NULL values. + * + * @param stream The data source from which a value was read + * @param value The value that was read + * @return The value or null if the + * {@link SQLInput#wasNull()} is true + */ + public static final T wasNull(SQLInput stream, T value) throws SQLException { + return stream.wasNull() ? null : value; + } + + /** + * Convenient way to check if a JDBC-originated record was null + *

+ * This is useful to check if primitive types obtained from the JDBC API + * were actually SQL NULL values. + * + * @param rs The data source from which a value was read + * @param value The value that was read + * @return The value or null if the + * {@link ResultSet#wasNull()} is true + */ + public static final T wasNull(ResultSet rs, T value) throws SQLException { + return rs.wasNull() ? null : value; + } + + /** + * Convenient way to check if a JDBC-originated record was null + *

+ * This is useful to check if primitive types obtained from the JDBC API + * were actually SQL NULL values. + * + * @param statement The data source from which a value was read + * @param value The value that was read + * @return The value or null if the + * {@link CallableStatement#wasNull()} is true + */ + public static final T wasNull(CallableStatement statement, T value) throws SQLException { + return statement.wasNull() ? null : value; + } + /** * No instances */