Skip to content

Commit 9aa9a1f

Browse files
authored
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
1 parent ac77715 commit 9aa9a1f

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, COLU
7272
NULL AS SCOPE_TABLE,
7373
NULL AS SOURCE_DATA_TYPE,
7474
'NO' AS IS_AUTOINCREMENT,
75-
'NO' AS IS_GENERATEDCOLUMN
75+
CASE
76+
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
77+
ELSE 'YES'
78+
END AS IS_GENERATEDCOLUMN
7679
FROM INFORMATION_SCHEMA.COLUMNS C
7780
WHERE UPPER(C.TABLE_CATALOG) LIKE ?
7881
AND UPPER(C.TABLE_SCHEMA) LIKE ?

src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcDatabaseMetaDataTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.google.cloud.spanner.IntegrationTest;
2727
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
28+
import com.google.cloud.spanner.testing.EmulatorSpannerHelper;
2829
import java.sql.Connection;
2930
import java.sql.DatabaseMetaData;
3031
import java.sql.ResultSet;
@@ -53,7 +54,8 @@ public class ITJdbcDatabaseMetaDataTest extends ITAbstractJdbcTest {
5354

5455
@BeforeClass
5556
public static void skipOnEmulator() {
56-
assumeFalse("foreign keys are not supported on the emulator", env.getTestHelper().isEmulator());
57+
assumeFalse(
58+
"foreign keys are not supported on the emulator", EmulatorSpannerHelper.isUsingEmulator());
5759
}
5860

5961
@Override
@@ -70,6 +72,7 @@ private static final class Column {
7072
private final Integer radix;
7173
private final boolean nullable;
7274
private final Integer charOctetLength;
75+
private final boolean computed;
7376

7477
private Column(
7578
String name,
@@ -80,6 +83,19 @@ private Column(
8083
Integer radix,
8184
boolean nullable,
8285
Integer charOctetLength) {
86+
this(name, type, typeName, colSize, decimalDigits, radix, nullable, charOctetLength, false);
87+
}
88+
89+
private Column(
90+
String name,
91+
int type,
92+
String typeName,
93+
Integer colSize,
94+
Integer decimalDigits,
95+
Integer radix,
96+
boolean nullable,
97+
Integer charOctetLength,
98+
boolean computed) {
8399
this.name = name;
84100
this.type = type;
85101
this.typeName = typeName;
@@ -88,6 +104,7 @@ private Column(
88104
this.radix = radix;
89105
this.nullable = nullable;
90106
this.charOctetLength = charOctetLength;
107+
this.computed = computed;
91108
}
92109
}
93110

@@ -133,7 +150,17 @@ private Column(
133150
new Column("ColDateArray", Types.ARRAY, "ARRAY<DATE>", 10, null, null, true, null),
134151
new Column(
135152
"ColTimestampArray", Types.ARRAY, "ARRAY<TIMESTAMP>", 35, null, null, true, null),
136-
new Column("ColNumericArray", Types.ARRAY, "ARRAY<NUMERIC>", 15, null, 10, true, null));
153+
new Column("ColNumericArray", Types.ARRAY, "ARRAY<NUMERIC>", 15, null, 10, true, null),
154+
new Column(
155+
"ColComputed",
156+
Types.NVARCHAR,
157+
"STRING(MAX)",
158+
2621440,
159+
null,
160+
null,
161+
true,
162+
2621440,
163+
true));
137164

138165
@Test
139166
public void testGetColumns() throws SQLException {
@@ -195,7 +222,7 @@ public void testGetColumns() throws SQLException {
195222
assertThat(rs.getShort("SOURCE_DATA_TYPE"), is(equalTo((short) 0)));
196223
assertThat(rs.wasNull(), is(true));
197224
assertThat(rs.getString("IS_AUTOINCREMENT"), is(equalTo("NO")));
198-
assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo("NO")));
225+
assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo(col.computed ? "YES" : "NO")));
199226
assertThat(rs.getMetaData().getColumnCount(), is(equalTo(24)));
200227

201228
pos++;

src/test/resources/com/google/cloud/spanner/jdbc/it/CreateMusicTables.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ CREATE TABLE TableWithAllColumnTypes (
8484
ColBytesMaxArray ARRAY<BYTES(MAX)>,
8585
ColDateArray ARRAY<DATE>,
8686
ColTimestampArray ARRAY<TIMESTAMP>,
87-
ColNumericArray ARRAY<NUMERIC>
87+
ColNumericArray ARRAY<NUMERIC>,
88+
89+
ColComputed STRING(MAX) AS (CONCAT(COALESCE(ColString, ''), ' ', COALESCE(ColStringMax, ''))) STORED,
8890
) PRIMARY KEY (ColInt64)
8991
;
9092

0 commit comments

Comments
 (0)