Skip to content

Use OFFSET/FETCH syntax in H2 dialect #1297

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@
* {@link org.springframework.data.relational.core.dialect.ArrayColumns} that offer JDBC-specific functionality.
*
* @author Jens Schauder
* @author Mark Paluch
* @since 2.3
*/
public interface JdbcArrayColumns extends ArrayColumns {

@Override
default Class<?> getArrayType(Class<?> userType) {

Class<?> componentType = userType;
while (componentType.isArray()) {
componentType = componentType.getComponentType();
}

return componentType;
return ArrayColumns.unwrapComponentType(userType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public JdbcArrayColumns getArraySupport() {
return ARRAY_COLUMNS;
}

static class JdbcPostgresArrayColumns extends PostgresArrayColumns implements JdbcArrayColumns {
static class JdbcPostgresArrayColumns implements JdbcArrayColumns {

@Override
public Class<?> getArrayType(Class<?> userType) {
return JdbcArrayColumns.super.getArrayType(userType);
public boolean isSupported() {
return true;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
/*
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.dialect;

import org.springframework.data.relational.core.dialect.AnsiDialect;
import org.springframework.data.relational.core.dialect.LockClause;
import org.springframework.data.relational.core.dialect.ArrayColumns;
import org.springframework.data.relational.core.dialect.ObjectArrayColumns;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Lazy;
import org.springframework.r2dbc.core.binding.BindMarkersFactory;

/**
* An SQL dialect for H2 in Postgres Compatibility mode.
* R2DBC dialect for H2.
*
* @author Mark Paluch
* @author Jens Schauder
* @author Diego Krupitza
*/
public class H2Dialect extends PostgresDialect {
public class H2Dialect extends org.springframework.data.relational.core.dialect.H2Dialect implements R2dbcDialect {

/**
* Singleton instance.
*/
public static final H2Dialect INSTANCE = new H2Dialect();

private static final BindMarkersFactory INDEXED = BindMarkersFactory.indexed("$", 1);

private final Lazy<ArrayColumns> arrayColumns = Lazy
.of(() -> new SimpleTypeArrayColumns(ObjectArrayColumns.INSTANCE, getSimpleTypeHolder()));

@Override
public BindMarkersFactory getBindMarkersFactory() {
return INDEXED;
}

@Override
public String renderForGeneratedValues(SqlIdentifier identifier) {
return identifier.getReference(getIdentifierProcessing());
}

@Override
public LockClause lock() {
// H2 Dialect does not support the same lock keywords as PostgreSQL, but it supports the ANSI SQL standard.
// see https://www.h2database.com/html/commands.html
// and https://www.h2database.com/html/features.html#compatibility
return AnsiDialect.INSTANCE.lock();
public ArrayColumns getArraySupport() {
return this.arrayColumns.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.dialect.ArrayColumns;
import org.springframework.data.relational.core.dialect.ObjectArrayColumns;
import org.springframework.data.util.Lazy;
import org.springframework.lang.NonNull;
import org.springframework.r2dbc.core.binding.BindMarkersFactory;
Expand Down Expand Up @@ -79,9 +79,8 @@ public class PostgresDialect extends org.springframework.data.relational.core.di

private static final BindMarkersFactory INDEXED = BindMarkersFactory.indexed("$", 1);

private final Lazy<ArrayColumns> arrayColumns = Lazy.of(() -> new R2dbcArrayColumns(
org.springframework.data.relational.core.dialect.PostgresDialect.INSTANCE.getArraySupport(),
getSimpleTypeHolder()));
private final Lazy<ArrayColumns> arrayColumns = Lazy
.of(() -> new SimpleTypeArrayColumns(ObjectArrayColumns.INSTANCE, getSimpleTypeHolder()));

/*
* (non-Javadoc)
Expand Down Expand Up @@ -137,37 +136,6 @@ public Collection<Object> getConverters() {
return converters;
}

private static class R2dbcArrayColumns implements ArrayColumns {

private final ArrayColumns delegate;
private final SimpleTypeHolder simpleTypeHolder;

R2dbcArrayColumns(ArrayColumns delegate, SimpleTypeHolder simpleTypeHolder) {
this.delegate = delegate;
this.simpleTypeHolder = simpleTypeHolder;
}

@Override
public boolean isSupported() {
return this.delegate.isSupported();
}

@Override
public Class<?> getArrayType(Class<?> userType) {

Class<?> typeToUse = userType;
while (typeToUse.getComponentType() != null) {
typeToUse = typeToUse.getComponentType();
}

if (!this.simpleTypeHolder.isSimpleType(typeToUse)) {
throw new IllegalArgumentException("Unsupported array type: " + ClassUtils.getQualifiedName(typeToUse));
}

return this.delegate.getArrayType(typeToUse);
}
}

/**
* If the class is present on the class path, invoke the specified consumer {@code action} with the class object,
* otherwise do nothing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.dialect;

import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.dialect.ArrayColumns;
import org.springframework.util.ClassUtils;

/**
* {@link ArrayColumns} support based on {@link SimpleTypeHolder store-native simple types}.
*
* @author Mark Paluch
* @since 3.0
*/
record SimpleTypeArrayColumns(ArrayColumns delegate, SimpleTypeHolder simpleTypeHolder) implements ArrayColumns {

@Override
public boolean isSupported() {
return this.delegate.isSupported();
}

@Override
public Class<?> getArrayType(Class<?> userType) {

Class<?> typeToUse = ArrayColumns.unwrapComponentType(userType);

if (!this.simpleTypeHolder.isSimpleType(typeToUse)) {
throw new IllegalArgumentException(
"Unsupported array type: %s".formatted(ClassUtils.getQualifiedName(typeToUse)));
}

return this.delegate.getArrayType(typeToUse);
}
}
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-1287-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.LockOptions;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* An SQL dialect for the ANSI SQL standard.
Expand Down Expand Up @@ -72,7 +70,7 @@ public Position getClausePosition() {
}
};

private final AnsiArrayColumns ARRAY_COLUMNS = new AnsiArrayColumns();
private final ArrayColumns ARRAY_COLUMNS = ObjectArrayColumns.INSTANCE;

@Override
public LimitClause limit() {
Expand All @@ -89,22 +87,6 @@ public ArrayColumns getArraySupport() {
return ARRAY_COLUMNS;
}

static class AnsiArrayColumns implements ArrayColumns {

@Override
public boolean isSupported() {
return true;
}

@Override
public Class<?> getArrayType(Class<?> userType) {

Assert.notNull(userType, "Array component type must not be null");

return ClassUtils.resolvePrimitiveIfNecessary(userType);
}
}

@Override
public IdentifierProcessing getIdentifierProcessing() {
return IdentifierProcessing.ANSI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,21 @@ public Class<?> getArrayType(Class<?> userType) {
throw new UnsupportedOperationException("Array types not supported");
}
}

/**
* Unwrap the nested {@link Class#getComponentType()} from a given {@link Class}.
*
* @param clazz the type to inspect.
* @return the unwrapped component type.
* @since 3.0
*/
static Class<?> unwrapComponentType(Class<?> clazz) {

Class<?> componentType = clazz;
while (componentType.isArray()) {
componentType = componentType.getComponentType();
}

return componentType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getOffset(long offset) {

@Override
public String getLimitOffset(long limit, long offset) {
return String.format("LIMIT %d OFFSET %d", limit, offset);
return String.format("OFFSET %d ROWS FETCH FIRST %d ROWS ONLY", offset, limit);
}

@Override
Expand Down
Loading