@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(); } }
@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); } }
/** Return the expression to be rendered when the RHS is an interval type */ private final Field<T> getIntervalExpression(Configuration configuration) { SQLDialect dialect = configuration.getDialect(); int sign = (operator == ADD) ? 1 : -1; switch (dialect) { case ASE: case SYBASE: case SQLSERVER: { if (rhs.get(0).getType() == YearToMonth.class) { return field( "{dateadd}(mm, {0}, {1})", getDataType(), val(sign * rhsAsYTM().intValue()), lhs); } else { // SQL Server needs this cast. Field<Timestamp> lhsAsTS = lhs.cast(Timestamp.class); DayToSecond interval = rhsAsDTS(); // Be careful with 32-bit INT arithmetic. Sybase ASE // may fatally overflow when using micro-second precision if (interval.getNano() != 0) { return field( "{dateadd}(ss, {0}, {dateadd}(us, {1}, {2}))", getDataType(), val(sign * (long) interval.getTotalSeconds()), val(sign * interval.getMicro()), lhsAsTS); } else { return field( "{dateadd}(ss, {0}, {1})", getDataType(), val(sign * (long) interval.getTotalSeconds()), lhsAsTS); } } } case CUBRID: case MYSQL: { Interval interval = rhsAsInterval(); if (operator == SUBTRACT) { interval = interval.neg(); } if (rhs.get(0).getType() == YearToMonth.class) { return field( "{date_add}({0}, {interval} {1} {year_month})", getDataType(), lhs, val(interval, String.class)); } else { if (dialect == MYSQL) { return field( "{date_add}({0}, {interval} {1} {day_microsecond})", getDataType(), lhs, val(interval, String.class)); } else { return field( "{date_add}({0}, {interval} {1} {day_millisecond})", getDataType(), lhs, val(interval, String.class)); } } } case DB2: { if (rhs.get(0).getType() == YearToMonth.class) { if (operator == ADD) { return lhs.add(field("{0} month", val(rhsAsYTM().intValue()))); } else { return lhs.sub(field("{0} month", val(rhsAsYTM().intValue()))); } } else { // DB2 needs this cast if lhs is of type DATE. DataType<T> type = lhs.getDataType(); if (operator == ADD) { return lhs.cast(Timestamp.class) .add(field("{0} microseconds", val(rhsAsDTS().getTotalMicro()))) .cast(type); } else { return lhs.cast(Timestamp.class) .sub(field("{0} microseconds", val(rhsAsDTS().getTotalMicro()))) .cast(type); } } } case DERBY: case HSQLDB: { if (rhs.get(0).getType() == YearToMonth.class) { return field( "{fn {timestampadd}({sql_tsi_month}, {0}, {1}) }", getDataType(), val(sign * rhsAsYTM().intValue()), lhs); } else { return field( "{fn {timestampadd}({sql_tsi_second}, {0}, {1}) }", getDataType(), val(sign * (long) rhsAsDTS().getTotalSeconds()), lhs); } } case FIREBIRD: { if (rhs.get(0).getType() == YearToMonth.class) { return field( "{dateadd}({month}, {0}, {1})", getDataType(), val(sign * rhsAsYTM().intValue()), lhs); } else { return field( "{dateadd}({millisecond}, {0}, {1})", getDataType(), val(sign * (long) rhsAsDTS().getTotalMilli()), lhs); } } case H2: { if (rhs.get(0).getType() == YearToMonth.class) { return field( "{dateadd}('month', {0}, {1})", getDataType(), val(sign * rhsAsYTM().intValue()), lhs); } else { return field( "{dateadd}('ms', {0}, {1})", getDataType(), val(sign * (long) rhsAsDTS().getTotalMilli()), lhs); } } case INGRES: { throw new SQLDialectNotSupportedException( "Date time arithmetic not supported in Ingres. Contributions welcome!"); } case SQLITE: { String prefix = (sign > 0) ? "+" : "-"; if (rhs.get(0).getType() == YearToMonth.class) { return field( "{datetime}({0}, '" + prefix + rhsAsYTM().intValue() + " months')", getDataType(), lhs); } else { return field( "{datetime}({0}, '" + prefix + rhsAsDTS().getTotalSeconds() + " seconds')", getDataType(), lhs); } } case ORACLE: case POSTGRES: default: return new DefaultExpression(); } }