in src/main/java/org/mariadb/jdbc/internal/com/read/resultset/UpdatableResultSet.java [616:817]
private void updateInternalObject(
final int parameterIndex, final Object obj, final int targetSqlType, final long scaleOrLength)
throws SQLException {
switch (targetSqlType) {
case Types.ARRAY:
case Types.DATALINK:
case Types.JAVA_OBJECT:
case Types.REF:
case Types.ROWID:
case Types.SQLXML:
case Types.STRUCT:
throw ExceptionFactory.INSTANCE.notSupported("Type not supported");
default:
break;
}
if (obj == null) {
updateNull(parameterIndex);
} else if (obj instanceof String) {
if (targetSqlType == Types.BLOB) {
throw ExceptionFactory.INSTANCE.create("Cannot convert a String to a Blob");
}
String str = (String) obj;
try {
switch (targetSqlType) {
case Types.BIT:
case Types.BOOLEAN:
updateBoolean(parameterIndex, !("false".equalsIgnoreCase(str) || "0".equals(str)));
break;
case Types.TINYINT:
updateByte(parameterIndex, Byte.parseByte(str));
break;
case Types.SMALLINT:
updateShort(parameterIndex, Short.parseShort(str));
break;
case Types.INTEGER:
updateInt(parameterIndex, Integer.parseInt(str));
break;
case Types.DOUBLE:
case Types.FLOAT:
updateDouble(parameterIndex, Double.valueOf(str));
break;
case Types.REAL:
updateFloat(parameterIndex, Float.valueOf(str));
break;
case Types.BIGINT:
updateLong(parameterIndex, Long.valueOf(str));
break;
case Types.DECIMAL:
case Types.NUMERIC:
updateBigDecimal(parameterIndex, new BigDecimal(str));
break;
case Types.CLOB:
case Types.NCLOB:
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
updateString(parameterIndex, str);
break;
case Types.TIMESTAMP:
if (str.startsWith("0000-00-00")) {
updateTimestamp(parameterIndex, null);
} else {
updateTimestamp(parameterIndex, Timestamp.valueOf(str));
}
break;
case Types.TIME:
updateTime(parameterIndex, Time.valueOf((String) obj));
break;
case Types.TIME_WITH_TIMEZONE:
parameterHolders[parameterIndex - 1] =
new OffsetTimeParameter(
OffsetTime.parse(str), timeZone, options.useFractionalSeconds, options);
break;
case Types.TIMESTAMP_WITH_TIMEZONE:
parameterHolders[parameterIndex - 1] =
new ZonedDateTimeParameter(
ZonedDateTime.parse(str, BasePrepareStatement.SPEC_ISO_ZONED_DATE_TIME),
timeZone,
options.useFractionalSeconds,
options);
break;
default:
throw ExceptionFactory.INSTANCE.create(
"Could not convert [" + str + "] to " + targetSqlType);
}
} catch (IllegalArgumentException e) {
throw ExceptionFactory.INSTANCE.create(
"Could not convert [" + str + "] to " + targetSqlType, e);
}
} else if (obj instanceof Number) {
Number bd = (Number) obj;
switch (targetSqlType) {
case Types.TINYINT:
updateByte(parameterIndex, bd.byteValue());
break;
case Types.SMALLINT:
updateShort(parameterIndex, bd.shortValue());
break;
case Types.INTEGER:
updateInt(parameterIndex, bd.intValue());
break;
case Types.BIGINT:
updateLong(parameterIndex, bd.longValue());
break;
case Types.FLOAT:
case Types.DOUBLE:
updateDouble(parameterIndex, bd.doubleValue());
break;
case Types.REAL:
updateFloat(parameterIndex, bd.floatValue());
break;
case Types.DECIMAL:
case Types.NUMERIC:
if (obj instanceof BigDecimal) {
updateBigDecimal(parameterIndex, (BigDecimal) obj);
} else if (obj instanceof Double || obj instanceof Float) {
updateDouble(parameterIndex, bd.doubleValue());
} else {
updateLong(parameterIndex, bd.longValue());
}
break;
case Types.BIT:
updateBoolean(parameterIndex, bd.shortValue() != 0);
break;
case Types.CHAR:
case Types.VARCHAR:
updateString(parameterIndex, bd.toString());
break;
default:
throw ExceptionFactory.INSTANCE.create(
"Could not convert [" + bd + "] to " + targetSqlType);
}
} else if (obj instanceof byte[]) {
if (targetSqlType == Types.BINARY
|| targetSqlType == Types.VARBINARY
|| targetSqlType == Types.LONGVARBINARY) {
updateBytes(parameterIndex, (byte[]) obj);
} else {
throw ExceptionFactory.INSTANCE.create(
"Can only convert a byte[] to BINARY, VARBINARY or LONGVARBINARY");
}
} else if (obj instanceof Time) {
updateTime(parameterIndex, (Time) obj); // it is just a string anyway
} else if (obj instanceof Timestamp) {
updateTimestamp(parameterIndex, (Timestamp) obj);
} else if (obj instanceof Date) {
updateDate(parameterIndex, (Date) obj);
} else if (obj instanceof java.util.Date) {
long timemillis = ((java.util.Date) obj).getTime();
if (targetSqlType == Types.DATE) {
updateDate(parameterIndex, new Date(timemillis));
} else if (targetSqlType == Types.TIME) {
updateTime(parameterIndex, new Time(timemillis));
} else if (targetSqlType == Types.TIMESTAMP) {
updateTimestamp(parameterIndex, new Timestamp(timemillis));
}
} else if (obj instanceof Boolean) {
updateBoolean(parameterIndex, (Boolean) obj);
} else if (obj instanceof Blob) {
updateBlob(parameterIndex, (Blob) obj);
} else if (obj instanceof Clob) {
updateClob(parameterIndex, (Clob) obj);
} else if (obj instanceof InputStream) {
updateBinaryStream(parameterIndex, (InputStream) obj, scaleOrLength);
} else if (obj instanceof Reader) {
updateCharacterStream(parameterIndex, (Reader) obj, scaleOrLength);
} else if (obj instanceof LocalDateTime) {
updateTimestamp(parameterIndex, Timestamp.valueOf((LocalDateTime) obj));
} else if (obj instanceof Instant) {
updateTimestamp(parameterIndex, Timestamp.from((Instant) obj));
} else if (obj instanceof LocalDate) {
updateDate(parameterIndex, Date.valueOf((LocalDate) obj));
} else if (obj instanceof OffsetDateTime) {
parameterHolders[parameterIndex - 1] =
new ZonedDateTimeParameter(
((OffsetDateTime) obj).toZonedDateTime(),
timeZone,
options.useFractionalSeconds,
options);
} else if (obj instanceof OffsetTime) {
parameterHolders[parameterIndex - 1] =
new OffsetTimeParameter(
(OffsetTime) obj, timeZone, options.useFractionalSeconds, options);
} else if (obj instanceof ZonedDateTime) {
parameterHolders[parameterIndex - 1] =
new ZonedDateTimeParameter(
(ZonedDateTime) obj, timeZone, options.useFractionalSeconds, options);
} else if (obj instanceof LocalTime) {
updateTime(parameterIndex, Time.valueOf((LocalTime) obj));
} else {
throw ExceptionFactory.INSTANCE.create(
"Could not set parameter in setObject, could not convert: "
+ obj.getClass()
+ " to "
+ targetSqlType);
}
}