com.scalar.db.sql.springdata.ScalarDbDialectProvider Maven / Gradle / Ivy
package com.scalar.db.sql.springdata;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.springframework.data.jdbc.repository.config.DialectResolver;
import org.springframework.data.jdbc.repository.config.DialectResolver.DefaultDialectProvider;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;
/**
* An implementation of ${@link DialectResolver.JdbcDialectProvider} to return ${@link
* ScalarDbDialect}.
*/
public class ScalarDbDialectProvider implements DialectResolver.JdbcDialectProvider {
@Override
@Nonnull
public Optional getDialect(@Nonnull JdbcOperations operations) {
Optional scalarDbDialect =
Optional.ofNullable(
operations.execute((ConnectionCallback) ScalarDbDialectProvider::getDialect));
if (scalarDbDialect.isPresent()) {
return scalarDbDialect;
} else {
return new DefaultDialectProvider().getDialect(operations);
}
}
@Nullable
private static Dialect getDialect(@Nonnull Connection connection) throws SQLException {
DatabaseMetaData metaData = connection.getMetaData();
String name = metaData.getDatabaseProductName().toLowerCase(Locale.ENGLISH);
if (name.contains("scalardb")) {
return ScalarDbDialect.INSTANCE;
}
return null;
}
}