1616
1717package com .google .cloud .spanner .jdbc ;
1818
19+ import static com .google .cloud .spanner .jdbc .JdbcStatement .ALL_COLUMNS ;
20+ import static com .google .cloud .spanner .jdbc .JdbcStatement .isNullOrEmpty ;
21+
1922import com .google .api .client .util .Preconditions ;
2023import com .google .cloud .spanner .CommitResponse ;
2124import com .google .cloud .spanner .Mutation ;
2528import com .google .cloud .spanner .connection .ConnectionOptions ;
2629import com .google .cloud .spanner .connection .SavepointSupport ;
2730import com .google .cloud .spanner .connection .TransactionMode ;
31+ import com .google .common .collect .ImmutableList ;
2832import com .google .common .collect .Iterators ;
2933import java .sql .Array ;
3034import java .sql .Blob ;
@@ -49,9 +53,10 @@ class JdbcConnection extends AbstractJdbcConnection {
4953 "Only result sets with concurrency CONCUR_READ_ONLY are supported" ;
5054 private static final String ONLY_CLOSE_CURSORS_AT_COMMIT =
5155 "Only result sets with holdability CLOSE_CURSORS_AT_COMMIT are supported" ;
52- static final String ONLY_NO_GENERATED_KEYS = "Only NO_GENERATED_KEYS are supported" ;
5356 static final String IS_VALID_QUERY = "SELECT 1" ;
5457
58+ static final ImmutableList <String > NO_GENERATED_KEY_COLUMNS = ImmutableList .of ();
59+
5560 private Map <String , Class <?>> typeMap = new HashMap <>();
5661
5762 JdbcConnection (String connectionUrl , ConnectionOptions options ) throws SQLException {
@@ -66,8 +71,13 @@ public Statement createStatement() throws SQLException {
6671
6772 @ Override
6873 public JdbcPreparedStatement prepareStatement (String sql ) throws SQLException {
74+ return prepareStatement (sql , NO_GENERATED_KEY_COLUMNS );
75+ }
76+
77+ private JdbcPreparedStatement prepareStatement (
78+ String sql , ImmutableList <String > generatedKeyColumns ) throws SQLException {
6979 checkClosed ();
70- return new JdbcPreparedStatement (this , sql );
80+ return new JdbcPreparedStatement (this , sql , generatedKeyColumns );
7181 }
7282
7383 @ Override
@@ -299,22 +309,26 @@ public PreparedStatement prepareStatement(
299309
300310 @ Override
301311 public PreparedStatement prepareStatement (String sql , int autoGeneratedKeys ) throws SQLException {
302- checkClosed ();
303- JdbcPreconditions .checkSqlFeatureSupported (
304- autoGeneratedKeys == Statement .NO_GENERATED_KEYS , ONLY_NO_GENERATED_KEYS );
305- return prepareStatement (sql );
312+ return prepareStatement (
313+ sql ,
314+ autoGeneratedKeys == Statement .RETURN_GENERATED_KEYS
315+ ? ALL_COLUMNS
316+ : NO_GENERATED_KEY_COLUMNS );
306317 }
307318
308319 @ Override
309320 public PreparedStatement prepareStatement (String sql , int [] columnIndexes ) throws SQLException {
310- checkClosed ();
311- return prepareStatement (sql );
321+ // This should preferably have returned an error, but the initial version of the driver just
322+ // accepted and ignored this. Starting to throw an error now would be a breaking change.
323+ // TODO: Consider throwing an Unsupported error for the next major version bump.
324+ return prepareStatement (sql , NO_GENERATED_KEY_COLUMNS );
312325 }
313326
314327 @ Override
315328 public PreparedStatement prepareStatement (String sql , String [] columnNames ) throws SQLException {
316- checkClosed ();
317- return prepareStatement (sql );
329+ return prepareStatement (
330+ sql ,
331+ isNullOrEmpty (columnNames ) ? NO_GENERATED_KEY_COLUMNS : ImmutableList .copyOf (columnNames ));
318332 }
319333
320334 @ Override
0 commit comments