Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: simplify dialect detection in Spring Data JDBC sample #1738

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

package com.google.cloud.spanner.sample;

import com.google.cloud.spanner.jdbc.JdbcSqlException;
import com.google.rpc.Code;
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
import io.opentelemetry.api.OpenTelemetry;
import java.util.Objects;
import javax.annotation.Nonnull;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.PostgresDialect;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;

Expand All @@ -48,42 +45,12 @@ public Dialect jdbcDialect(@Nonnull NamedParameterJdbcOperations operations) {

/** 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 @@ -817,7 +817,7 @@ public void testRunApplication() {
SpringApplication.run(Application.class).close();

assertEquals(
44,
42,
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream()
.filter(request -> !request.getSql().equals("SELECT 1"))
.count());
Expand Down
Loading