Skip to content

Commit d7bd88b

Browse files
Support all the array types
1 parent 3161f83 commit d7bd88b

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ 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-
if(typeName != null) {
63+
if (typeName != null) {
6464
for (JdbcDataType type : JdbcDataType.values()) {
65-
if (type.getTypeName().equalsIgnoreCase(typeName) || type.getAliases().contains(typeName.toLowerCase())) {
65+
if (type.matches(typeName)) {
6666
return new JdbcArray(type, elements);
6767
}
6868
}
@@ -94,7 +94,8 @@ private JdbcArray(JdbcDataType type, Object[] elements) throws SQLException {
9494
elements.getClass().getComponentType(), elements.length);
9595
} else {
9696
Class<?> clazz = type.getJavaClass();
97-
if(elements.length > 0 && type.getSupportedJavaClasses().contains(elements[0].getClass())) {
97+
if (elements.length > 0
98+
&& type.getSupportedJavaClasses().contains(elements[0].getClass())) {
9899
clazz = elements[0].getClass();
99100
}
100101
this.data = java.lang.reflect.Array.newInstance(clazz, elements.length);

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

Lines changed: 23 additions & 18 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;
@@ -25,14 +26,12 @@
2526
import java.sql.Timestamp;
2627
import java.sql.Types;
2728
import java.time.LocalDate;
28-
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.Collections;
3131
import java.util.HashSet;
3232
import java.util.List;
3333
import java.util.Set;
3434
import java.util.UUID;
35-
import java.util.stream.Collectors;
3635

3736
/** Enum for mapping Cloud Spanner data types to Java classes and JDBC SQL {@link Types}. */
3837
enum JdbcDataType {
@@ -61,11 +60,6 @@ public List<Boolean> getArrayElements(ResultSet rs, int columnIndex) {
6160
public Type getSpannerType() {
6261
return Type.bool();
6362
}
64-
65-
@Override
66-
public List<String> getAliases() {
67-
return Collections.singletonList("boolean");
68-
}
6963
},
7064
BYTES {
7165
@Override
@@ -233,13 +227,11 @@ public List<Long> getArrayElements(ResultSet rs, int columnIndex) {
233227
public Type getSpannerType() {
234228
return Type.int64();
235229
}
236-
237-
@Override
238-
public List<String> getAliases() {
239-
return Arrays.asList("bigint", "integer");
240-
}
241230
},
242231
NUMERIC {
232+
233+
private final Set<String> aliases = new HashSet<>(Collections.singletonList("decimal"));
234+
243235
@Override
244236
public int getSqlType() {
245237
return Types.NUMERIC;
@@ -266,8 +258,8 @@ public Type getSpannerType() {
266258
}
267259

268260
@Override
269-
public List<String> getAliases() {
270-
return Collections.singletonList("decimal");
261+
public Set<String> getAliases() {
262+
return aliases;
271263
}
272264
},
273265
PG_NUMERIC {
@@ -297,6 +289,8 @@ public Type getSpannerType() {
297289
}
298290
},
299291
STRING {
292+
private final Set<String> aliases = new HashSet<>(Arrays.asList("varchar", "text"));
293+
300294
@Override
301295
public int getSqlType() {
302296
return Types.NVARCHAR;
@@ -323,8 +317,8 @@ public Type getSpannerType() {
323317
}
324318

325319
@Override
326-
public List<String> getAliases() {
327-
return Arrays.asList("text");
320+
public Set<String> getAliases() {
321+
return aliases;
328322
}
329323
},
330324
JSON {
@@ -528,8 +522,19 @@ public Type getSpannerType() {
528522

529523
public abstract Type getSpannerType();
530524

531-
public List<String> getAliases() {
532-
return new ArrayList<>();
525+
public Set<String> getAliases() {
526+
return Collections.emptySet();
527+
}
528+
529+
/***
530+
* @param typeName type of the column
531+
* @return true if type name matches current type name or matches with one of postgres aliases
532+
* or if it matches equivalent postgres type.
533+
*/
534+
public boolean matches(String typeName) {
535+
return getTypeName().equalsIgnoreCase(typeName)
536+
|| getAliases().contains(typeName.toLowerCase())
537+
|| getSpannerType().getSpannerTypeName(Dialect.POSTGRESQL).equalsIgnoreCase(typeName);
533538
}
534539

535540
// TODO: Implement and use this method for all types.

0 commit comments

Comments
 (0)