|
16 | 16 |
|
17 | 17 | package com.google.cloud.spanner.sample; |
18 | 18 |
|
19 | | -import com.google.cloud.spanner.jdbc.JdbcSqlException; |
20 | | -import com.google.rpc.Code; |
21 | | -import java.util.Objects; |
| 19 | +import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection; |
22 | 20 | import org.springframework.context.annotation.Configuration; |
23 | | -import org.springframework.dao.DataAccessException; |
24 | | -import org.springframework.dao.IncorrectResultSizeDataAccessException; |
| 21 | +import org.springframework.jdbc.core.ConnectionCallback; |
25 | 22 | import org.springframework.jdbc.core.JdbcOperations; |
26 | 23 |
|
27 | 24 | @Configuration |
28 | 25 | public class JdbcConfiguration { |
29 | 26 |
|
30 | 27 | /** Returns true if the current database is a Cloud Spanner PostgreSQL database. */ |
31 | 28 | public static boolean isCloudSpannerPG(JdbcOperations operations) { |
32 | | - try { |
33 | | - Long value = |
34 | | - operations.queryForObject( |
35 | | - "select 1 " |
36 | | - + "from information_schema.database_options " |
37 | | - + "where schema_name='public' " |
38 | | - + "and option_name='database_dialect' " |
39 | | - + "and option_value='POSTGRESQL'", |
40 | | - Long.class); |
41 | | - // Shouldn't really be anything else than 1 if the query succeeded, but this avoids complaints |
42 | | - // from the compiler. |
43 | | - if (Objects.equals(1L, value)) { |
44 | | - return true; |
45 | | - } |
46 | | - } catch (IncorrectResultSizeDataAccessException exception) { |
47 | | - // This indicates that it is a valid Cloud Spanner database, but not one that uses the |
48 | | - // PostgreSQL dialect. |
49 | | - throw new RuntimeException( |
50 | | - "The selected Cloud Spanner database does not use the PostgreSQL dialect"); |
51 | | - } catch (DataAccessException exception) { |
52 | | - if (exception.getCause() instanceof JdbcSqlException) { |
53 | | - JdbcSqlException jdbcSqlException = (JdbcSqlException) exception.getCause(); |
54 | | - if (jdbcSqlException.getCode() == Code.PERMISSION_DENIED |
55 | | - || jdbcSqlException.getCode() == Code.NOT_FOUND) { |
56 | | - throw new RuntimeException( |
57 | | - "Failed to get the dialect of the Cloud Spanner database. " |
58 | | - + "Please check that the selected database exists and that you have permission to access it. " |
59 | | - + "Cause: " |
60 | | - + exception.getCause().getMessage(), |
61 | | - exception.getCause()); |
62 | | - } |
63 | | - } |
64 | | - // ignore and fall through |
65 | | - } catch (Throwable exception) { |
66 | | - // ignore and fall through |
67 | | - } |
68 | | - return false; |
| 29 | + return Boolean.TRUE.equals( |
| 30 | + operations.execute( |
| 31 | + (ConnectionCallback<Boolean>) |
| 32 | + connection -> |
| 33 | + connection.isWrapperFor(CloudSpannerJdbcConnection.class) |
| 34 | + && com.google.cloud.spanner.Dialect.POSTGRESQL.equals( |
| 35 | + connection.unwrap(CloudSpannerJdbcConnection.class).getDialect()))); |
69 | 36 | } |
70 | 37 | } |
0 commit comments