From 9aa9a1f8f673554ae71e78937007166f220dd255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Thu, 10 Dec 2020 12:54:51 +0100 Subject: [PATCH] feat: report whether column is generated in JDBC metadata (#291) Cloud Spanner now supports generated columns. These were reported as normal columns in the JDBC metadata. These are now reported correctly as generated columns. Fixes #290 --- .../jdbc/DatabaseMetaData_GetColumns.sql | 5 ++- .../jdbc/it/ITJdbcDatabaseMetaDataTest.java | 33 +++++++++++++++++-- .../spanner/jdbc/it/CreateMusicTables.sql | 4 ++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/resources/com/google/cloud/spanner/jdbc/DatabaseMetaData_GetColumns.sql b/src/main/resources/com/google/cloud/spanner/jdbc/DatabaseMetaData_GetColumns.sql index 8b29c15b..69b1f6a8 100644 --- a/src/main/resources/com/google/cloud/spanner/jdbc/DatabaseMetaData_GetColumns.sql +++ b/src/main/resources/com/google/cloud/spanner/jdbc/DatabaseMetaData_GetColumns.sql @@ -72,7 +72,10 @@ SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, COLU NULL AS SCOPE_TABLE, NULL AS SOURCE_DATA_TYPE, 'NO' AS IS_AUTOINCREMENT, - 'NO' AS IS_GENERATEDCOLUMN + CASE + WHEN (IS_GENERATED = 'NEVER') THEN 'NO' + ELSE 'YES' + END AS IS_GENERATEDCOLUMN FROM INFORMATION_SCHEMA.COLUMNS C WHERE UPPER(C.TABLE_CATALOG) LIKE ? AND UPPER(C.TABLE_SCHEMA) LIKE ? diff --git a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcDatabaseMetaDataTest.java b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcDatabaseMetaDataTest.java index 711363c2..75553998 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcDatabaseMetaDataTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcDatabaseMetaDataTest.java @@ -25,6 +25,7 @@ import com.google.cloud.spanner.IntegrationTest; import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest; +import com.google.cloud.spanner.testing.EmulatorSpannerHelper; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -53,7 +54,8 @@ public class ITJdbcDatabaseMetaDataTest extends ITAbstractJdbcTest { @BeforeClass public static void skipOnEmulator() { - assumeFalse("foreign keys are not supported on the emulator", env.getTestHelper().isEmulator()); + assumeFalse( + "foreign keys are not supported on the emulator", EmulatorSpannerHelper.isUsingEmulator()); } @Override @@ -70,6 +72,7 @@ private static final class Column { private final Integer radix; private final boolean nullable; private final Integer charOctetLength; + private final boolean computed; private Column( String name, @@ -80,6 +83,19 @@ private Column( Integer radix, boolean nullable, Integer charOctetLength) { + this(name, type, typeName, colSize, decimalDigits, radix, nullable, charOctetLength, false); + } + + private Column( + String name, + int type, + String typeName, + Integer colSize, + Integer decimalDigits, + Integer radix, + boolean nullable, + Integer charOctetLength, + boolean computed) { this.name = name; this.type = type; this.typeName = typeName; @@ -88,6 +104,7 @@ private Column( this.radix = radix; this.nullable = nullable; this.charOctetLength = charOctetLength; + this.computed = computed; } } @@ -133,7 +150,17 @@ private Column( new Column("ColDateArray", Types.ARRAY, "ARRAY", 10, null, null, true, null), new Column( "ColTimestampArray", Types.ARRAY, "ARRAY", 35, null, null, true, null), - new Column("ColNumericArray", Types.ARRAY, "ARRAY", 15, null, 10, true, null)); + new Column("ColNumericArray", Types.ARRAY, "ARRAY", 15, null, 10, true, null), + new Column( + "ColComputed", + Types.NVARCHAR, + "STRING(MAX)", + 2621440, + null, + null, + true, + 2621440, + true)); @Test public void testGetColumns() throws SQLException { @@ -195,7 +222,7 @@ public void testGetColumns() throws SQLException { assertThat(rs.getShort("SOURCE_DATA_TYPE"), is(equalTo((short) 0))); assertThat(rs.wasNull(), is(true)); assertThat(rs.getString("IS_AUTOINCREMENT"), is(equalTo("NO"))); - assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo("NO"))); + assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo(col.computed ? "YES" : "NO"))); assertThat(rs.getMetaData().getColumnCount(), is(equalTo(24))); pos++; diff --git a/src/test/resources/com/google/cloud/spanner/jdbc/it/CreateMusicTables.sql b/src/test/resources/com/google/cloud/spanner/jdbc/it/CreateMusicTables.sql index 4d05cdfb..6a8c4616 100644 --- a/src/test/resources/com/google/cloud/spanner/jdbc/it/CreateMusicTables.sql +++ b/src/test/resources/com/google/cloud/spanner/jdbc/it/CreateMusicTables.sql @@ -84,7 +84,9 @@ CREATE TABLE TableWithAllColumnTypes ( ColBytesMaxArray ARRAY, ColDateArray ARRAY, ColTimestampArray ARRAY, - ColNumericArray ARRAY + ColNumericArray ARRAY, + + ColComputed STRING(MAX) AS (CONCAT(COALESCE(ColString, ''), ' ', COALESCE(ColStringMax, ''))) STORED, ) PRIMARY KEY (ColInt64) ;