Skip to content

Commit

Permalink
SQL: Move away internally from JDBCType to SQLType (#33913)
Browse files Browse the repository at this point in the history
Replace JDBCType enum with SQLType interface for extensibility

Fix #33094
  • Loading branch information
costin authored and kcm committed Oct 30, 2018
1 parent efe67e7 commit 3351da5
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLType;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -1124,11 +1125,11 @@ private static List<ColumnInfo> columnInfo(String tableName, Object... cols) thr
Object obj = cols[i];
if (obj instanceof String) {
String name = obj.toString();
JDBCType type = JDBCType.VARCHAR;
SQLType type = JDBCType.VARCHAR;
if (i + 1 < cols.length) {
// check if the next item it's a type
if (cols[i + 1] instanceof JDBCType) {
type = (JDBCType) cols[i + 1];
if (cols[i + 1] instanceof SQLType) {
type = (SQLType) cols[i + 1];
i++;
}
// it's not, use the default and move on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public int getParameterType(int param) throws SQLException {

@Override
public String getParameterTypeName(int param) throws SQLException {
return paramInfo(param).type.name();
return paramInfo(param).type.getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLType;
import java.sql.SQLXML;
import java.sql.Struct;
import java.sql.Time;
Expand Down Expand Up @@ -69,20 +70,20 @@ public int executeUpdate() throws SQLException {
throw new SQLFeatureNotSupportedException("Writes not supported");
}

private void setParam(int parameterIndex, Object value, int type) throws SQLException {
private void setParam(int parameterIndex, Object value, SQLType type) throws SQLException {
checkOpen();

if (parameterIndex < 0 || parameterIndex > query.paramCount()) {
throw new SQLException("Invalid parameter index [ " + parameterIndex + "; needs to be between 1 and [" + query.paramCount() +
"]");
}

query.setParam(parameterIndex, value, JDBCType.valueOf(type));
query.setParam(parameterIndex, value, type);
}

@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
setParam(parameterIndex, null, sqlType);
setParam(parameterIndex, null, JDBCType.valueOf(sqlType));
}

@Override
Expand Down Expand Up @@ -181,7 +182,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQ
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
if (x == null) {
setParam(parameterIndex, null, Types.NULL);
setParam(parameterIndex, null, JDBCType.NULL);
return;
}

Expand Down Expand Up @@ -338,7 +339,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale

// set the null value on the type and exit
if (x == null) {
setParam(parameterIndex, null, targetSqlType);
setParam(parameterIndex, null, JDBCType.valueOf(targetSqlType));
return;
}

Expand All @@ -348,7 +349,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
throw new SQLFeatureNotSupportedException(
"Conversion from type byte[] to " + targetJDBCType + " not supported");
}
setParam(parameterIndex, x, Types.VARBINARY);
setParam(parameterIndex, x, JDBCType.VARBINARY);
return;
}

Expand All @@ -357,7 +358,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
|| x instanceof Date
|| x instanceof LocalDateTime
|| x instanceof Time
|| x instanceof java.util.Date)
|| x instanceof java.util.Date)
{
if (targetJDBCType == JDBCType.TIMESTAMP) {
// converting to {@code java.util.Date} because this is the type supported by {@code XContentBuilder} for serialization
Expand All @@ -380,10 +381,10 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
dateToSet = (java.util.Date) x;
}

setParam(parameterIndex, dateToSet, Types.TIMESTAMP);
setParam(parameterIndex, dateToSet, JDBCType.TIMESTAMP);
return;
} else if (targetJDBCType == JDBCType.VARCHAR) {
setParam(parameterIndex, String.valueOf(x), Types.VARCHAR);
setParam(parameterIndex, String.valueOf(x), JDBCType.VARCHAR);
return;
}
// anything else other than VARCHAR and TIMESTAMP is not supported in this JDBC driver
Expand All @@ -399,9 +400,9 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
|| x instanceof Float
|| x instanceof Double
|| x instanceof String) {
setParam(parameterIndex,
TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)),
targetSqlType);
setParam(parameterIndex,
TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)),
JDBCType.valueOf(targetSqlType));
return;
}

Expand All @@ -410,8 +411,8 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
}

private void checkKnownUnsupportedTypes(Object x) throws SQLFeatureNotSupportedException {
List<Class<?>> unsupportedTypes = new ArrayList<Class<?>>(Arrays.asList(Struct.class, Array.class, SQLXML.class,
RowId.class, Ref.class, Blob.class, NClob.class, Clob.class, LocalDate.class, LocalTime.class,
List<Class<?>> unsupportedTypes = new ArrayList<>(Arrays.asList(Struct.class, Array.class, SQLXML.class,
RowId.class, Ref.class, Blob.class, NClob.class, Clob.class, LocalDate.class, LocalTime.class,
OffsetTime.class, OffsetDateTime.class, URL.class, BigDecimal.class));

for (Class<?> clazz:unsupportedTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLType;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Statement;
Expand Down Expand Up @@ -133,7 +134,7 @@ public String getString(int columnIndex) throws SQLException {

@Override
public boolean getBoolean(int columnIndex) throws SQLException {
return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false;
return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false;
}

@Override
Expand Down Expand Up @@ -245,7 +246,7 @@ public Date getDate(String columnLabel) throws SQLException {

private Long dateTime(int columnIndex) throws SQLException {
Object val = column(columnIndex);
JDBCType type = cursor.columns().get(columnIndex - 1).type;
SQLType type = cursor.columns().get(columnIndex - 1).type;
try {
// TODO: the B6 appendix of the jdbc spec does mention CHAR, VARCHAR, LONGVARCHAR, DATE, TIMESTAMP as supported
// jdbc types that should be handled by getDate and getTime methods. From all of those we support VARCHAR and
Expand Down Expand Up @@ -338,7 +339,7 @@ private <T> T convert(int columnIndex, Class<T> type) throws SQLException {
return null;
}

JDBCType columnType = cursor.columns().get(columnIndex - 1).type;
SQLType columnType = cursor.columns().get(columnIndex - 1).type;

return TypeConverter.convert(val, columnType, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public int getColumnType(int column) throws SQLException {

@Override
public String getColumnTypeName(int column) throws SQLException {
return column(column).type.name();
return column(column).type.getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

import java.sql.JDBCType;
import java.sql.SQLException;
import java.sql.SQLType;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class PreparedQuery {

static class ParamInfo {
JDBCType type;
SQLType type;
Object value;

ParamInfo(Object value, JDBCType type) {
ParamInfo(Object value, SQLType type) {
this.value = value;
this.type = type;
}
Expand All @@ -43,7 +44,7 @@ ParamInfo getParam(int param) throws JdbcSQLException {
return params[param - 1];
}

void setParam(int param, Object value, JDBCType type) throws JdbcSQLException {
void setParam(int param, Object value, SQLType type) throws JdbcSQLException {
if (param < 1 || param > params.length) {
throw new JdbcSQLException("Invalid parameter index [" + param + "]");
}
Expand Down
Loading

0 comments on commit 3351da5

Please sign in to comment.