Skip to content

Commit

Permalink
docs: simplify dialect detection for MyBatis sample (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite authored Sep 12, 2024
1 parent f229a62 commit 01eade8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,22 @@

package com.google.cloud.spanner.sample;

import com.google.cloud.spanner.jdbc.JdbcSqlException;
import com.google.rpc.Code;
import java.util.Objects;
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;

@Configuration
public class JdbcConfiguration {

/** Returns true if the current database is a Cloud Spanner PostgreSQL database. */
public static boolean isCloudSpannerPG(JdbcOperations operations) {
try {
Long value =
operations.queryForObject(
"select 1 "
+ "from information_schema.database_options "
+ "where schema_name='public' "
+ "and option_name='database_dialect' "
+ "and option_value='POSTGRESQL'",
Long.class);
// Shouldn't really be anything else than 1 if the query succeeded, but this avoids complaints
// from the compiler.
if (Objects.equals(1L, value)) {
return true;
}
} catch (IncorrectResultSizeDataAccessException exception) {
// This indicates that it is a valid Cloud Spanner database, but not one that uses the
// PostgreSQL dialect.
throw new RuntimeException(
"The selected Cloud Spanner database does not use the PostgreSQL dialect");
} catch (DataAccessException exception) {
if (exception.getCause() instanceof JdbcSqlException) {
JdbcSqlException jdbcSqlException = (JdbcSqlException) exception.getCause();
if (jdbcSqlException.getCode() == Code.PERMISSION_DENIED
|| jdbcSqlException.getCode() == Code.NOT_FOUND) {
throw new RuntimeException(
"Failed to get the dialect of the Cloud Spanner database. "
+ "Please check that the selected database exists and that you have permission to access it. "
+ "Cause: "
+ exception.getCause().getMessage(),
exception.getCause());
}
}
// ignore and fall through
} catch (Throwable exception) {
// ignore and fall through
}
return false;
return Boolean.TRUE.equals(
operations.execute(
(ConnectionCallback<Boolean>)
connection ->
connection.isWrapperFor(CloudSpannerJdbcConnection.class)
&& com.google.cloud.spanner.Dialect.POSTGRESQL.equals(
connection.unwrap(CloudSpannerJdbcConnection.class).getDialect())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ public void testRunApplication() {
Application.main(new String[] {});

assertEquals(
40,
39,
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream()
.filter(
request ->
Expand Down

0 comments on commit 01eade8

Please sign in to comment.