Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ class JdbcArray implements Array {
* the elements array is not compatible with the base type of the array.
*/
static JdbcArray createArray(String typeName, Object[] elements) throws SQLException {
for (JdbcDataType type : JdbcDataType.values()) {
if (type.getTypeName().equalsIgnoreCase(typeName)) {
return new JdbcArray(type, elements);
if (typeName != null) {
for (JdbcDataType type : JdbcDataType.values()) {
if (type.matches(typeName)) {
return new JdbcArray(type, elements);
}
}
}
throw JdbcSqlExceptionFactory.of(
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.jdbc;

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

@Override
public int getSqlType() {
Expand Down Expand Up @@ -184,6 +186,11 @@ public List<Double> getArrayElements(ResultSet rs, int columnIndex) {
public Type getSpannerType() {
return Type.float64();
}

@Override
public Set<String> getAliases() {
return aliases;
}
},
INT64 {
private final Set<Class<?>> classes =
Expand Down Expand Up @@ -220,6 +227,9 @@ public Type getSpannerType() {
}
},
NUMERIC {

private final Set<String> aliases = new HashSet<>(Collections.singletonList("decimal"));

@Override
public int getSqlType() {
return Types.NUMERIC;
Expand All @@ -244,6 +254,11 @@ public List<BigDecimal> getArrayElements(ResultSet rs, int columnIndex) {
public Type getSpannerType() {
return Type.numeric();
}

@Override
public Set<String> getAliases() {
return aliases;
}
},
PG_NUMERIC {
@Override
Expand Down Expand Up @@ -272,6 +287,8 @@ public Type getSpannerType() {
}
},
STRING {
private final Set<String> aliases = new HashSet<>(Arrays.asList("varchar", "text"));

@Override
public int getSqlType() {
return Types.NVARCHAR;
Expand All @@ -296,6 +313,11 @@ public List<String> getArrayElements(ResultSet rs, int columnIndex) {
public Type getSpannerType() {
return Type.string();
}

@Override
public Set<String> getAliases() {
return aliases;
}
},
JSON {
@Override
Expand Down Expand Up @@ -498,6 +520,21 @@ public Type getSpannerType() {

public abstract Type getSpannerType();

public Set<String> getAliases() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to getPostgreSQLAliases()? The matches(..) method below confused me at first, and that was partly because it seemed like this could be any type of alias for the type.

return Collections.emptySet();
}

/***
* @param typeName type of the column
* @return true if type name matches current type name or matches with one of postgres aliases
* or if it matches equivalent postgres type.
*/
public boolean matches(String typeName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public?

return getTypeName().equalsIgnoreCase(typeName)
|| getAliases().contains(typeName.toLowerCase())
|| getSpannerType().getSpannerTypeName(Dialect.POSTGRESQL).equalsIgnoreCase(typeName);
}

// TODO: Implement and use this method for all types.
public int getPrecision() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ SELECT TABLE_CATALOG AS "TABLE_CAT", TABLE_SCHEMA AS "TABLE_SCHEM", TABLE_NAME A
WHEN DATA_TYPE = 'jsonb' THEN -9
WHEN DATA_TYPE = 'timestamp with time zone' THEN 93
END AS "DATA_TYPE",
DATA_TYPE AS "TYPE_NAME",
CASE
WHEN DATA_TYPE LIKE 'ARRAY' THEN
CASE
WHEN spanner_type LIKE '%[]' THEN
CONCAT('_', REPLACE(spanner_type, '[]', ''))
ELSE spanner_type
END
ELSE DATA_TYPE
END AS "TYPE_NAME",
CASE
WHEN DATA_TYPE LIKE 'ARRAY' THEN 0
WHEN DATA_TYPE = 'boolean' THEN NULL
Expand Down