@SuppressWarnings("unchecked") private static <T> T pgFromString(Class<? extends T> type, String string) throws SQLException { if (string == null) { return null; } else if (type == Blob.class) { // Not supported } else if (type == Boolean.class) { return (T) Boolean.valueOf(string); } else if (type == BigInteger.class) { return (T) new BigInteger(string); } else if (type == BigDecimal.class) { return (T) new BigDecimal(string); } else if (type == Byte.class) { return (T) Byte.valueOf(string); } else if (type == byte[].class) { return (T) PostgresUtils.toBytes(string); } else if (type == Clob.class) { // Not supported } else if (type == Date.class) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); return (T) new Date(pgParseDate(string, f).getTime()); } else if (type == Double.class) { return (T) Double.valueOf(string); } else if (type == Float.class) { return (T) Float.valueOf(string); } else if (type == Integer.class) { return (T) Integer.valueOf(string); } else if (type == Long.class) { return (T) Long.valueOf(string); } else if (type == Short.class) { return (T) Short.valueOf(string); } else if (type == String.class) { return (T) string; } else if (type == Time.class) { SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss"); return (T) new Time(pgParseDate(string, f).getTime()); } else if (type == Timestamp.class) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return (T) new Timestamp(pgParseDate(string, f).getTime()); } else if (type == UByte.class) { return (T) UByte.valueOf(string); } else if (type == UShort.class) { return (T) UShort.valueOf(string); } else if (type == UInteger.class) { return (T) UInteger.valueOf(string); } else if (type == ULong.class) { return (T) ULong.valueOf(string); } else if (type.isArray()) { return (T) pgNewArray(type, string); } else if (ArrayRecord.class.isAssignableFrom(type)) { // Not supported } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, string); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, string); } else if (UDTRecord.class.isAssignableFrom(type)) { return (T) pgNewUDTRecord(type, string); } throw new UnsupportedOperationException("Class " + type + " is not supported"); }
@SuppressWarnings("unchecked") private static <T> T getFromResultSet(ExecuteContext ctx, Class<? extends T> type, int index) throws SQLException { ResultSet rs = ctx.resultSet(); if (type == Blob.class) { return (T) rs.getBlob(index); } else if (type == Boolean.class) { return (T) checkWasNull(rs, Boolean.valueOf(rs.getBoolean(index))); } else if (type == BigInteger.class) { // The SQLite JDBC driver doesn't support BigDecimals if (ctx.getDialect() == SQLDialect.SQLITE) { return Convert.convert(rs.getString(index), (Class<? extends T>) BigInteger.class); } else { BigDecimal result = rs.getBigDecimal(index); return (T) (result == null ? null : result.toBigInteger()); } } else if (type == BigDecimal.class) { // The SQLite JDBC driver doesn't support BigDecimals if (ctx.getDialect() == SQLDialect.SQLITE) { return Convert.convert(rs.getString(index), (Class<? extends T>) BigDecimal.class); } else { return (T) rs.getBigDecimal(index); } } else if (type == Byte.class) { return (T) checkWasNull(rs, Byte.valueOf(rs.getByte(index))); } else if (type == byte[].class) { return (T) rs.getBytes(index); } else if (type == Clob.class) { return (T) rs.getClob(index); } else if (type == Date.class) { return (T) getDate(ctx.getDialect(), rs, index); } else if (type == Double.class) { return (T) checkWasNull(rs, Double.valueOf(rs.getDouble(index))); } else if (type == Float.class) { return (T) checkWasNull(rs, Float.valueOf(rs.getFloat(index))); } else if (type == Integer.class) { return (T) checkWasNull(rs, Integer.valueOf(rs.getInt(index))); } else if (type == Long.class) { return (T) checkWasNull(rs, Long.valueOf(rs.getLong(index))); } else if (type == Short.class) { return (T) checkWasNull(rs, Short.valueOf(rs.getShort(index))); } else if (type == String.class) { return (T) rs.getString(index); } else if (type == Time.class) { return (T) getTime(ctx.getDialect(), rs, index); } else if (type == Timestamp.class) { return (T) getTimestamp(ctx.getDialect(), rs, index); } else if (type == YearToMonth.class) { if (ctx.getDialect() == POSTGRES) { Object object = rs.getObject(index); return (T) (object == null ? null : PostgresUtils.toYearToMonth(object)); } else { String string = rs.getString(index); return (T) (string == null ? null : YearToMonth.valueOf(string)); } } else if (type == DayToSecond.class) { if (ctx.getDialect() == POSTGRES) { Object object = rs.getObject(index); return (T) (object == null ? null : PostgresUtils.toDayToSecond(object)); } else { String string = rs.getString(index); return (T) (string == null ? null : DayToSecond.valueOf(string)); } } else if (type == UByte.class) { String string = rs.getString(index); return (T) (string == null ? null : UByte.valueOf(string)); } else if (type == UShort.class) { String string = rs.getString(index); return (T) (string == null ? null : UShort.valueOf(string)); } else if (type == UInteger.class) { String string = rs.getString(index); return (T) (string == null ? null : UInteger.valueOf(string)); } else if (type == ULong.class) { String string = rs.getString(index); return (T) (string == null ? null : ULong.valueOf(string)); } // The type byte[] is handled earlier. byte[][] can be handled here else if (type.isArray()) { switch (ctx.getDialect()) { case POSTGRES: { return pgGetArray(ctx, type, index); } default: // Note: due to a HSQLDB bug, it is not recommended to call rs.getObject() here: // See https://sourceforge.net/tracker/?func=detail&aid=3181365&group_id=23316&atid=378131 return (T) convertArray(rs.getArray(index), (Class<? extends Object[]>) type); } } else if (ArrayRecord.class.isAssignableFrom(type)) { return (T) getArrayRecord(ctx, rs.getArray(index), (Class<? extends ArrayRecord<?>>) type); } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, rs.getString(index)); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, rs.getObject(index)); } else if (UDTRecord.class.isAssignableFrom(type)) { switch (ctx.getDialect()) { case POSTGRES: return (T) pgNewUDTRecord(type, rs.getObject(index)); } return (T) rs.getObject(index, DataTypes.udtRecords()); } else if (Result.class.isAssignableFrom(type)) { ResultSet nested = (ResultSet) rs.getObject(index); return (T) getNewFactory(ctx).fetch(nested); } else { return (T) rs.getObject(index); } }
@SuppressWarnings("unchecked") public static <T> T getFromStatement(ExecuteContext ctx, Class<? extends T> type, int index) throws SQLException { CallableStatement stmt = (CallableStatement) ctx.statement(); if (type == Blob.class) { return (T) stmt.getBlob(index); } else if (type == Boolean.class) { return (T) checkWasNull(stmt, Boolean.valueOf(stmt.getBoolean(index))); } else if (type == BigInteger.class) { BigDecimal result = stmt.getBigDecimal(index); return (T) (result == null ? null : result.toBigInteger()); } else if (type == BigDecimal.class) { return (T) stmt.getBigDecimal(index); } else if (type == Byte.class) { return (T) checkWasNull(stmt, Byte.valueOf(stmt.getByte(index))); } else if (type == byte[].class) { return (T) stmt.getBytes(index); } else if (type == Clob.class) { return (T) stmt.getClob(index); } else if (type == Date.class) { return (T) stmt.getDate(index); } else if (type == Double.class) { return (T) checkWasNull(stmt, Double.valueOf(stmt.getDouble(index))); } else if (type == Float.class) { return (T) checkWasNull(stmt, Float.valueOf(stmt.getFloat(index))); } else if (type == Integer.class) { return (T) checkWasNull(stmt, Integer.valueOf(stmt.getInt(index))); } else if (type == Long.class) { return (T) checkWasNull(stmt, Long.valueOf(stmt.getLong(index))); } else if (type == Short.class) { return (T) checkWasNull(stmt, Short.valueOf(stmt.getShort(index))); } else if (type == String.class) { return (T) stmt.getString(index); } else if (type == Time.class) { return (T) stmt.getTime(index); } else if (type == Timestamp.class) { return (T) stmt.getTimestamp(index); } else if (type == YearToMonth.class) { if (ctx.getDialect() == POSTGRES) { Object object = stmt.getObject(index); return (T) (object == null ? null : PostgresUtils.toYearToMonth(object)); } else { String string = stmt.getString(index); return (T) (string == null ? null : YearToMonth.valueOf(string)); } } else if (type == DayToSecond.class) { if (ctx.getDialect() == POSTGRES) { Object object = stmt.getObject(index); return (T) (object == null ? null : PostgresUtils.toDayToSecond(object)); } else { String string = stmt.getString(index); return (T) (string == null ? null : DayToSecond.valueOf(string)); } } else if (type == UByte.class) { String string = stmt.getString(index); return (T) (string == null ? null : UByte.valueOf(string)); } else if (type == UShort.class) { String string = stmt.getString(index); return (T) (string == null ? null : UShort.valueOf(string)); } else if (type == UInteger.class) { String string = stmt.getString(index); return (T) (string == null ? null : UInteger.valueOf(string)); } else if (type == ULong.class) { String string = stmt.getString(index); return (T) (string == null ? null : ULong.valueOf(string)); } // The type byte[] is handled earlier. byte[][] can be handled here else if (type.isArray()) { return (T) convertArray(stmt.getObject(index), (Class<? extends Object[]>) type); } else if (ArrayRecord.class.isAssignableFrom(type)) { return (T) getArrayRecord(ctx, stmt.getArray(index), (Class<? extends ArrayRecord<?>>) type); } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, stmt.getString(index)); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, stmt.getString(index)); } else if (UDTRecord.class.isAssignableFrom(type)) { switch (ctx.getDialect()) { case POSTGRES: return (T) pgNewUDTRecord(type, stmt.getObject(index)); } return (T) stmt.getObject(index, DataTypes.udtRecords()); } else if (Result.class.isAssignableFrom(type)) { ResultSet nested = (ResultSet) stmt.getObject(index); return (T) getNewFactory(ctx).fetch(nested); } else { return (T) stmt.getObject(index); } }
@SuppressWarnings("unchecked") public static <T> T getFromSQLInput(Configuration configuration, SQLInput stream, Field<T> field) throws SQLException { Class<? extends T> type = field.getType(); DataType<T> dataType = field.getDataType(); if (type == Blob.class) { return (T) stream.readBlob(); } else if (type == Boolean.class) { return (T) checkWasNull(stream, Boolean.valueOf(stream.readBoolean())); } else if (type == BigInteger.class) { BigDecimal result = stream.readBigDecimal(); return (T) (result == null ? null : result.toBigInteger()); } else if (type == BigDecimal.class) { return (T) stream.readBigDecimal(); } else if (type == Byte.class) { return (T) checkWasNull(stream, Byte.valueOf(stream.readByte())); } else if (type == byte[].class) { // [#1327] Oracle cannot deserialise BLOBs as byte[] from SQLInput if (dataType.isLob()) { Blob blob = null; try { blob = stream.readBlob(); return (T) (blob == null ? null : blob.getBytes(1, (int) blob.length())); } finally { Util.safeFree(blob); } } else { return (T) stream.readBytes(); } } else if (type == Clob.class) { return (T) stream.readClob(); } else if (type == Date.class) { return (T) stream.readDate(); } else if (type == Double.class) { return (T) checkWasNull(stream, Double.valueOf(stream.readDouble())); } else if (type == Float.class) { return (T) checkWasNull(stream, Float.valueOf(stream.readFloat())); } else if (type == Integer.class) { return (T) checkWasNull(stream, Integer.valueOf(stream.readInt())); } else if (type == Long.class) { return (T) checkWasNull(stream, Long.valueOf(stream.readLong())); } else if (type == Short.class) { return (T) checkWasNull(stream, Short.valueOf(stream.readShort())); } else if (type == String.class) { return (T) stream.readString(); } else if (type == Time.class) { return (T) stream.readTime(); } else if (type == Timestamp.class) { return (T) stream.readTimestamp(); } else if (type == YearToMonth.class) { String string = stream.readString(); return (T) (string == null ? null : YearToMonth.valueOf(string)); } else if (type == DayToSecond.class) { String string = stream.readString(); return (T) (string == null ? null : DayToSecond.valueOf(string)); } else if (type == UByte.class) { String string = stream.readString(); return (T) (string == null ? null : UByte.valueOf(string)); } else if (type == UShort.class) { String string = stream.readString(); return (T) (string == null ? null : UShort.valueOf(string)); } else if (type == UInteger.class) { String string = stream.readString(); return (T) (string == null ? null : UInteger.valueOf(string)); } else if (type == ULong.class) { String string = stream.readString(); return (T) (string == null ? null : ULong.valueOf(string)); } // The type byte[] is handled earlier. byte[][] can be handled here else if (type.isArray()) { Array result = stream.readArray(); return (T) (result == null ? null : result.getArray()); } else if (ArrayRecord.class.isAssignableFrom(type)) { return (T) getArrayRecord(configuration, stream.readArray(), (Class<? extends ArrayRecord<?>>) type); } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, stream.readString()); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, stream.readObject()); } else if (UDTRecord.class.isAssignableFrom(type)) { return (T) stream.readObject(); } else { return (T) stream.readObject(); } }