Skip to content

Commit 6bb42c6

Browse files
committed
SQL: Move away internally from JDBCType to SQLType (#33913)
Replace JDBCType enum with SQLType interface for extensibility Fix #33094 (cherry picked from commit 17605bf)
1 parent eb2bb17 commit 6bb42c6

File tree

13 files changed

+303
-264
lines changed

13 files changed

+303
-264
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.sql.RowIdLifetime;
2020
import java.sql.SQLException;
2121
import java.sql.SQLFeatureNotSupportedException;
22+
import java.sql.SQLType;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425

@@ -1124,11 +1125,11 @@ private static List<ColumnInfo> columnInfo(String tableName, Object... cols) thr
11241125
Object obj = cols[i];
11251126
if (obj instanceof String) {
11261127
String name = obj.toString();
1127-
JDBCType type = JDBCType.VARCHAR;
1128+
SQLType type = JDBCType.VARCHAR;
11281129
if (i + 1 < cols.length) {
11291130
// check if the next item it's a type
1130-
if (cols[i + 1] instanceof JDBCType) {
1131-
type = (JDBCType) cols[i + 1];
1131+
if (cols[i + 1] instanceof SQLType) {
1132+
type = (SQLType) cols[i + 1];
11321133
i++;
11331134
}
11341135
// it's not, use the default and move on

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public int getParameterType(int param) throws SQLException {
5454

5555
@Override
5656
public String getParameterTypeName(int param) throws SQLException {
57-
return paramInfo(param).type.name();
57+
return paramInfo(param).type.getName();
5858
}
5959

6060
@Override

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatement.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.sql.SQLDataException;
2727
import java.sql.SQLException;
2828
import java.sql.SQLFeatureNotSupportedException;
29+
import java.sql.SQLType;
2930
import java.sql.SQLXML;
3031
import java.sql.Struct;
3132
import java.sql.Time;
@@ -69,20 +70,20 @@ public int executeUpdate() throws SQLException {
6970
throw new SQLFeatureNotSupportedException("Writes not supported");
7071
}
7172

72-
private void setParam(int parameterIndex, Object value, int type) throws SQLException {
73+
private void setParam(int parameterIndex, Object value, SQLType type) throws SQLException {
7374
checkOpen();
7475

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

80-
query.setParam(parameterIndex, value, JDBCType.valueOf(type));
81+
query.setParam(parameterIndex, value, type);
8182
}
8283

8384
@Override
8485
public void setNull(int parameterIndex, int sqlType) throws SQLException {
85-
setParam(parameterIndex, null, sqlType);
86+
setParam(parameterIndex, null, JDBCType.valueOf(sqlType));
8687
}
8788

8889
@Override
@@ -181,7 +182,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQ
181182
@Override
182183
public void setObject(int parameterIndex, Object x) throws SQLException {
183184
if (x == null) {
184-
setParam(parameterIndex, null, Types.NULL);
185+
setParam(parameterIndex, null, JDBCType.NULL);
185186
return;
186187
}
187188

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

339340
// set the null value on the type and exit
340341
if (x == null) {
341-
setParam(parameterIndex, null, targetSqlType);
342+
setParam(parameterIndex, null, JDBCType.valueOf(targetSqlType));
342343
return;
343344
}
344345

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

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

383-
setParam(parameterIndex, dateToSet, Types.TIMESTAMP);
384+
setParam(parameterIndex, dateToSet, JDBCType.TIMESTAMP);
384385
return;
385386
} else if (targetJDBCType == JDBCType.VARCHAR) {
386-
setParam(parameterIndex, String.valueOf(x), Types.VARCHAR);
387+
setParam(parameterIndex, String.valueOf(x), JDBCType.VARCHAR);
387388
return;
388389
}
389390
// anything else other than VARCHAR and TIMESTAMP is not supported in this JDBC driver
@@ -399,9 +400,9 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
399400
|| x instanceof Float
400401
|| x instanceof Double
401402
|| x instanceof String) {
402-
setParam(parameterIndex,
403-
TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)),
404-
targetSqlType);
403+
setParam(parameterIndex,
404+
TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)),
405+
JDBCType.valueOf(targetSqlType));
405406
return;
406407
}
407408

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

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

417418
for (Class<?> clazz:unsupportedTypes) {

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.sql.RowId;
2626
import java.sql.SQLException;
2727
import java.sql.SQLFeatureNotSupportedException;
28+
import java.sql.SQLType;
2829
import java.sql.SQLWarning;
2930
import java.sql.SQLXML;
3031
import java.sql.Statement;
@@ -133,7 +134,7 @@ public String getString(int columnIndex) throws SQLException {
133134

134135
@Override
135136
public boolean getBoolean(int columnIndex) throws SQLException {
136-
return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false;
137+
return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false;
137138
}
138139

139140
@Override
@@ -245,7 +246,7 @@ public Date getDate(String columnLabel) throws SQLException {
245246

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

341-
JDBCType columnType = cursor.columns().get(columnIndex - 1).type;
342+
SQLType columnType = cursor.columns().get(columnIndex - 1).type;
342343

343344
return TypeConverter.convert(val, columnType, type);
344345
}

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public int getColumnType(int column) throws SQLException {
114114

115115
@Override
116116
public String getColumnTypeName(int column) throws SQLException {
117-
return column(column).type.name();
117+
return column(column).type.getName();
118118
}
119119

120120
@Override

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111

1212
import java.sql.JDBCType;
1313
import java.sql.SQLException;
14+
import java.sql.SQLType;
1415
import java.util.Arrays;
1516
import java.util.List;
1617
import java.util.stream.Collectors;
1718

1819
class PreparedQuery {
1920

2021
static class ParamInfo {
21-
JDBCType type;
22+
SQLType type;
2223
Object value;
2324

24-
ParamInfo(Object value, JDBCType type) {
25+
ParamInfo(Object value, SQLType type) {
2526
this.value = value;
2627
this.type = type;
2728
}
@@ -43,7 +44,7 @@ ParamInfo getParam(int param) throws JdbcSQLException {
4344
return params[param - 1];
4445
}
4546

46-
void setParam(int param, Object value, JDBCType type) throws JdbcSQLException {
47+
void setParam(int param, Object value, SQLType type) throws JdbcSQLException {
4748
if (param < 1 || param > params.length) {
4849
throw new JdbcSQLException("Invalid parameter index [" + param + "]");
4950
}

0 commit comments

Comments
 (0)