1616
1717package com .google .cloud .spanner .jdbc ;
1818
19+ import com .google .cloud .spanner .Dialect ;
1920import com .google .cloud .spanner .ResultSet ;
2021import com .google .cloud .spanner .Struct ;
2122import com .google .cloud .spanner .Type ;
2526import java .sql .Timestamp ;
2627import java .sql .Types ;
2728import java .time .LocalDate ;
28- import java .util .ArrayList ;
2929import java .util .Arrays ;
3030import java .util .Collections ;
3131import java .util .HashSet ;
3232import java .util .List ;
3333import java .util .Set ;
3434import 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}. */
3837enum 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