Skip to content

Commit b9fcae9

Browse files
authored
feat: return is_autoincrement for getColumns (#1944)
Spanner now supports auto-increment columns through IDENTITY columns. This change adds support for this to the DatabaseMetaData#getColumns() method.
1 parent dc9af80 commit b9fcae9

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, COLU
7878
NULL AS SCOPE_SCHEMA,
7979
NULL AS SCOPE_TABLE,
8080
NULL AS SOURCE_DATA_TYPE,
81-
'NO' AS IS_AUTOINCREMENT,
81+
IS_IDENTITY AS IS_AUTOINCREMENT,
8282
CASE
8383
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
8484
ELSE 'YES'

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ SELECT TABLE_CATALOG AS "TABLE_CAT", TABLE_SCHEMA AS "TABLE_SCHEM", TABLE_NAME A
7272
NULL AS "SCOPE_SCHEMA",
7373
NULL AS "SCOPE_TABLE",
7474
NULL AS "SOURCE_DATA_TYPE",
75-
'NO' AS "IS_AUTOINCREMENT",
75+
IS_IDENTITY AS "IS_AUTOINCREMENT",
7676
CASE
7777
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
7878
ELSE 'YES'

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ private static final class Column {
118118
private final Integer charOctetLength;
119119
private final boolean computed;
120120
private final String defaultValue;
121+
private final boolean autoIncrement;
121122

122123
private Column(
123124
String name,
@@ -138,7 +139,8 @@ private Column(
138139
nullable,
139140
charOctetLength,
140141
false,
141-
null);
142+
null,
143+
false);
142144
}
143145

144146
private Column(
@@ -151,7 +153,8 @@ private Column(
151153
boolean nullable,
152154
Integer charOctetLength,
153155
boolean computed,
154-
String defaultValue) {
156+
String defaultValue,
157+
boolean autoIncrement) {
155158
this.name = name;
156159
this.type = type;
157160
this.typeName = typeName;
@@ -162,14 +165,17 @@ private Column(
162165
this.charOctetLength = charOctetLength;
163166
this.computed = computed;
164167
this.defaultValue = defaultValue;
168+
this.autoIncrement = autoIncrement;
165169
}
166170
}
167171

168172
private static final List<Column> EXPECTED_COLUMNS =
169173
Arrays.asList(
170174
new Column("ColInt64", Types.BIGINT, "INT64", 19, null, 10, false, null),
171-
new Column("ColFloat64", Types.DOUBLE, "FLOAT64", 15, 16, 2, false, null, false, "0.0"),
172-
new Column("ColFloat32", Types.REAL, "FLOAT32", 15, 16, 2, false, null, false, "0.0"),
175+
new Column(
176+
"ColFloat64", Types.DOUBLE, "FLOAT64", 15, 16, 2, false, null, false, "0.0", false),
177+
new Column(
178+
"ColFloat32", Types.REAL, "FLOAT32", 15, 16, 2, false, null, false, "0.0", false),
173179
new Column("ColBool", Types.BOOLEAN, "BOOL", null, null, null, false, null),
174180
new Column(
175181
"ColString",
@@ -181,7 +187,8 @@ private Column(
181187
false,
182188
100,
183189
false,
184-
"'Hello World!'"),
190+
"'Hello World!'",
191+
false),
185192
new Column(
186193
"ColStringMax", Types.NVARCHAR, "STRING(MAX)", 2621440, null, null, false, 2621440),
187194
new Column("ColBytes", Types.BINARY, "BYTES(100)", 100, null, null, false, null),
@@ -196,7 +203,8 @@ private Column(
196203
false,
197204
null,
198205
false,
199-
"DATE '2000-01-01'"),
206+
"DATE '2000-01-01'",
207+
false),
200208
new Column("ColTimestamp", Types.TIMESTAMP, "TIMESTAMP", 35, null, null, false, null),
201209
new Column("ColCommitTS", Types.TIMESTAMP, "TIMESTAMP", 35, null, null, false, null),
202210
new Column("ColNumeric", Types.NUMERIC, "NUMERIC", 15, null, 10, false, null),
@@ -243,7 +251,10 @@ private Column(
243251
true,
244252
2621440,
245253
true,
246-
null));
254+
null,
255+
false),
256+
new Column(
257+
"ColIdentity", Types.BIGINT, "INT64", 19, null, 10, true, null, false, null, true));
247258

248259
@Test
249260
public void testGetColumns() throws SQLException {
@@ -301,8 +312,11 @@ public void testGetColumns() throws SQLException {
301312
assertNull(rs.getString("SCOPE_TABLE"));
302313
assertEquals(0, rs.getShort("SOURCE_DATA_TYPE"));
303314
assertTrue(rs.wasNull());
304-
assertEquals("NO", rs.getString("IS_AUTOINCREMENT"));
305315
assertEquals(col.computed ? "YES" : "NO", rs.getString("IS_GENERATEDCOLUMN"));
316+
// TODO: Remove check when the emulator correctly returns IS_IDENTITY
317+
if (!EmulatorSpannerHelper.isUsingEmulator()) {
318+
assertEquals(col.autoIncrement ? "YES" : "NO", rs.getString("IS_AUTOINCREMENT"));
319+
}
306320
assertEquals(24, rs.getMetaData().getColumnCount());
307321
}
308322
assertFalse(rs.next());

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ public void test09_MetaData_FromQuery() throws SQLException {
11291129
try (PreparedStatement ps =
11301130
con.prepareStatement("SELECT * FROM TableWithAllColumnTypes WHERE ColInt64=?")) {
11311131
ResultSetMetaData metadata = ps.getMetaData();
1132-
assertEquals(26, metadata.getColumnCount());
1132+
assertEquals(27, metadata.getColumnCount());
11331133
int index = 0;
11341134
assertEquals("ColInt64", metadata.getColumnLabel(++index));
11351135
assertEquals("ColFloat64", metadata.getColumnLabel(++index));
@@ -1157,6 +1157,7 @@ public void test09_MetaData_FromQuery() throws SQLException {
11571157
assertEquals("ColNumericArray", metadata.getColumnLabel(++index));
11581158
assertEquals("ColJsonArray", metadata.getColumnLabel(++index));
11591159
assertEquals("ColComputed", metadata.getColumnLabel(++index));
1160+
assertEquals("ColIdentity", metadata.getColumnLabel(++index));
11601161
}
11611162
}
11621163
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ CREATE TABLE TableWithAllColumnTypes (
9696
ColJsonArray ARRAY<JSON>,
9797

9898
ColComputed STRING(MAX) AS (CONCAT(COALESCE(ColString, ''), ' ', COALESCE(ColStringMax, ''))) STORED,
99+
ColIdentity INT64 GENERATED BY DEFAULT AS IDENTITY (BIT_REVERSED_POSITIVE),
99100
) PRIMARY KEY (ColInt64)
100101
;
101102

0 commit comments

Comments
 (0)