Browse Source
Reuse H2 dialect settings in R2DBC-specific H2 dialect. Refactor ArrayColumns support classes, into toplevel-types. Let R2DBC H2 sublass the relational H2Dialect to decouple from Postgres. See #1287 Original pull request #1297pull/1304/head
9 changed files with 152 additions and 93 deletions
@ -1,34 +1,57 @@
@@ -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(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,47 @@
@@ -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); |
||||
} |
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* 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.relational.core.dialect; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* {@link ArrayColumns} support using the actual object type or {@link Class#isPrimitive() boxed primitives} Java types. |
||||
* |
||||
* @author Mark Paluch |
||||
* @since 3.0 |
||||
* @see ClassUtils#resolvePrimitiveIfNecessary |
||||
*/ |
||||
public class ObjectArrayColumns implements ArrayColumns { |
||||
|
||||
public static final ObjectArrayColumns INSTANCE = new ObjectArrayColumns(); |
||||
|
||||
@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(ArrayColumns.unwrapComponentType(userType)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue