in src/main/java/com/aliyun/odps/jdbc/OdpsPreparedStatement.java [511:593]
private String convertJavaTypeToSqlString(Object x) throws SQLException {
if (Byte.class.isInstance(x)) {
return String.format("%sY", x.toString());
} else if (Short.class.isInstance(x)) {
return String.format("%sS", x.toString());
} else if (Integer.class.isInstance(x)) {
return x.toString();
} else if (Long.class.isInstance(x)) {
return String.format("%sL", x.toString());
} else if (Float.class.isInstance(x)) {
return String.format("%sf", x.toString());
} else if (Double.class.isInstance(x)) {
return String.format("%s", x.toString());
} else if (BigDecimal.class.isInstance(x)) {
return String.format("%sBD", x.toString());
} else if (String.class.isInstance(x)) {
if (isIllegal((String) x)) {
throw new IllegalArgumentException("");
}
return "'" + x + "'";
} else if (byte[].class.isInstance(x)) {
try {
String charset = getConnection().getCharset();
if (charset != null) {
String str = new String((byte[]) x, charset);
if (isIllegal(str)) {
throw new IllegalArgumentException("");
}
return "'" + str + "'";
} else {
throw new SQLException("charset is null");
}
} catch (UnsupportedEncodingException e) {
throw new SQLException(e.getMessage(), e);
}
} else if (java.util.Date.class.isInstance(x)) {
Calendar.Builder calendarBuilder = new Calendar.Builder()
.setCalendarType("iso8601")
.setLenient(true);
Calendar calendar = calendarBuilder.build();
if (java.sql.Timestamp.class.isInstance(x)) {
return String.format("TIMESTAMP'%s'", x);
} else if (java.sql.Date.class.isInstance(x)) {
// MaxCompute DATE
DATE_FORMAT.get().setCalendar(calendar);
return String.format("DATE'%s'", DATE_FORMAT.get().format(x));
} else if (java.sql.Time.class.isInstance(x)) {
DATETIME_FORMAT.get().setCalendar(calendar);
return String.format("DATETIME'%s'", DATETIME_FORMAT.get().format(x));
} else {
// MaxCompute DATETIME
DATE_FORMAT.get().setCalendar(calendar);
return String.format("DATE'%s'", DATE_FORMAT.get().format(x));
}
} else if (x instanceof LocalDate) {
return String.format("DATE'%s'", LOCAL_DATE_FORMAT.get().format((LocalDate) x));
} else if (x instanceof ZonedDateTime) {
return String.format("DATETIME'%s'",
ZONED_DATETIME_FORMAT.get().format((ZonedDateTime) x));
} else if (x instanceof Instant) {
return String.format("TIMESTAMP'%s'",
ZONED_TIMESTAMP_FORMAT.get().format((Instant) x));
} else if (Boolean.class.isInstance(x)) {
return x.toString().toUpperCase();
} else if (x == null || x.equals(Types.NULL)) {
return "NULL";
} else if (Binary.class.isInstance(x)) {
return String.format("unhex('%s')", x);
} else if (Varchar.class.isInstance(x)) {
if (isIllegal(x.toString())) {
throw new IllegalArgumentException("");
}
return "'" + x.toString() + "'";
} else if (Char.class.isInstance(x)) {
if (isIllegal(x.toString())) {
throw new IllegalArgumentException("");
}
return "'" + x.toString() + "'";
} else {
throw new SQLException("unrecognized Java class: " + x.getClass().getName());
}
}