Skip to content

Commit 02d584b

Browse files
chore: Support text/varchar type in JDBC
1 parent ab8fcf9 commit 02d584b

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ class JdbcArray implements Array {
6060
* the elements array is not compatible with the base type of the array.
6161
*/
6262
static JdbcArray createArray(String typeName, Object[] elements) throws SQLException {
63-
for (JdbcDataType type : JdbcDataType.values()) {
64-
if (type.getTypeName().equalsIgnoreCase(typeName)) {
65-
return new JdbcArray(type, elements);
63+
if (typeName != null) {
64+
for (JdbcDataType type : JdbcDataType.values()) {
65+
if (type.matches(typeName)) {
66+
return new JdbcArray(type, elements);
67+
}
6668
}
6769
}
6870
throw JdbcSqlExceptionFactory.of(

src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.jdbc;
1818

19+
import com.google.cloud.spanner.Dialect;
1920
import com.google.cloud.spanner.ResultSet;
2021
import com.google.cloud.spanner.Struct;
2122
import com.google.cloud.spanner.Type;
@@ -154,6 +155,7 @@ public Type getSpannerType() {
154155
},
155156
FLOAT64 {
156157
private final Set<Class<?>> classes = new HashSet<>(Arrays.asList(Float.class, Double.class));
158+
private final Set<String> aliases = new HashSet<>(Collections.singletonList("float8"));
157159

158160
@Override
159161
public int getSqlType() {
@@ -184,6 +186,11 @@ public List<Double> getArrayElements(ResultSet rs, int columnIndex) {
184186
public Type getSpannerType() {
185187
return Type.float64();
186188
}
189+
190+
@Override
191+
public Set<String> getAliases() {
192+
return aliases;
193+
}
187194
},
188195
INT64 {
189196
private final Set<Class<?>> classes =
@@ -220,6 +227,9 @@ public Type getSpannerType() {
220227
}
221228
},
222229
NUMERIC {
230+
231+
private final Set<String> aliases = new HashSet<>(Collections.singletonList("decimal"));
232+
223233
@Override
224234
public int getSqlType() {
225235
return Types.NUMERIC;
@@ -244,6 +254,11 @@ public List<BigDecimal> getArrayElements(ResultSet rs, int columnIndex) {
244254
public Type getSpannerType() {
245255
return Type.numeric();
246256
}
257+
258+
@Override
259+
public Set<String> getAliases() {
260+
return aliases;
261+
}
247262
},
248263
PG_NUMERIC {
249264
@Override
@@ -272,6 +287,8 @@ public Type getSpannerType() {
272287
}
273288
},
274289
STRING {
290+
private final Set<String> aliases = new HashSet<>(Arrays.asList("varchar", "text"));
291+
275292
@Override
276293
public int getSqlType() {
277294
return Types.NVARCHAR;
@@ -296,6 +313,11 @@ public List<String> getArrayElements(ResultSet rs, int columnIndex) {
296313
public Type getSpannerType() {
297314
return Type.string();
298315
}
316+
317+
@Override
318+
public Set<String> getAliases() {
319+
return aliases;
320+
}
299321
},
300322
JSON {
301323
@Override
@@ -498,6 +520,21 @@ public Type getSpannerType() {
498520

499521
public abstract Type getSpannerType();
500522

523+
public Set<String> getAliases() {
524+
return Collections.emptySet();
525+
}
526+
527+
/***
528+
* @param typeName type of the column
529+
* @return true if type name matches current type name or matches with one of postgres aliases
530+
* or if it matches equivalent postgres type.
531+
*/
532+
public boolean matches(String typeName) {
533+
return getTypeName().equalsIgnoreCase(typeName)
534+
|| getAliases().contains(typeName.toLowerCase())
535+
|| getSpannerType().getSpannerTypeName(Dialect.POSTGRESQL).equalsIgnoreCase(typeName);
536+
}
537+
501538
// TODO: Implement and use this method for all types.
502539
public int getPrecision() {
503540
throw new UnsupportedOperationException();

src/main/resources/com/google/cloud/spanner/jdbc/postgresql/DatabaseMetaData_GetColumns.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ SELECT TABLE_CATALOG AS "TABLE_CAT", TABLE_SCHEMA AS "TABLE_SCHEM", TABLE_NAME A
2828
WHEN DATA_TYPE = 'jsonb' THEN -9
2929
WHEN DATA_TYPE = 'timestamp with time zone' THEN 93
3030
END AS "DATA_TYPE",
31-
DATA_TYPE AS "TYPE_NAME",
31+
CASE
32+
WHEN DATA_TYPE LIKE 'ARRAY' THEN
33+
CASE
34+
WHEN spanner_type LIKE '%[]' THEN
35+
CONCAT('_',
36+
REPLACE(
37+
REPLACE(
38+
REPLACE(spanner_type, '[]', ''),
39+
'character varying', 'varchar'),
40+
'boolean', 'bool'))
41+
ELSE spanner_type
42+
END
43+
ELSE DATA_TYPE
44+
END AS "TYPE_NAME",
3245
CASE
3346
WHEN DATA_TYPE LIKE 'ARRAY' THEN 0
3447
WHEN DATA_TYPE = 'boolean' THEN NULL

0 commit comments

Comments
 (0)