HHH-14905 - Verify that custom JavaType and JdbcType registration combo works

This commit is contained in:
Steve Ebersole 2021-11-04 11:08:18 -05:00
parent 34cdd3077c
commit 31eb3b82c5
19 changed files with 230 additions and 646 deletions

View File

@ -64,7 +64,6 @@ dependencies {
testImplementation libraries.mockito_inline
testImplementation libraries.jodaTime
testImplementation libraries.assertj
testImplementation 'org.orbisgis:h2gis:1.5.0'
testRuntimeOnly libraries.byteBuddy
testRuntimeOnly libraries.jakarta_weld

View File

@ -163,22 +163,35 @@ public class InferredBasicValueResolver {
);
}
else {
// here we have the legacy case
// - we mimic how this used to be done
// see if there is a registered BasicType for this JavaType and, if so, use it.
// this mimics the legacy handling
final BasicType registeredType = typeConfiguration.getBasicTypeRegistry().getRegisteredType( reflectedJtd.getJavaType() );
if ( registeredType != null ) {
// so here is the legacy resolution
legacyType = resolveSqlTypeIndicators( stdIndicators, registeredType, reflectedJtd );
jdbcMapping = legacyType;
}
else if ( reflectedJtd instanceof SerializableJavaTypeDescriptor || reflectedJtd.getJavaType() instanceof Serializable ) {
legacyType = new SerializableType<>( reflectedJtd );
jdbcMapping = legacyType;
}
else {
// let this fall through to the exception creation below
legacyType = null;
jdbcMapping = null;
// there was not a "legacy" BasicType registration, so use `JavaType#getRecommendedJdbcType`, if
// one, to create a mapping
final JdbcType recommendedJdbcType = reflectedJtd.getRecommendedJdbcType( stdIndicators );
if ( recommendedJdbcType != null ) {
jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(
reflectedJtd,
recommendedJdbcType
);
legacyType = jdbcMapping;
}
else if ( reflectedJtd instanceof SerializableJavaTypeDescriptor || reflectedJtd.getJavaType() instanceof Serializable ) {
legacyType = new SerializableType<>( reflectedJtd );
jdbcMapping = legacyType;
}
else {
// let this fall through to the exception creation below
legacyType = null;
jdbcMapping = null;
}
}
}
}

View File

@ -147,6 +147,7 @@ public class H2Dialect extends Dialect {
registerColumnType( SqlTypes.UUID, "uuid" );
registerColumnType( SqlTypes.INTERVAL_SECOND, "interval second($p,$s)" );
registerColumnType( SqlTypes.GEOMETRY, "geometry" );
registerColumnType( SqlTypes.ARRAY, "array" );
}
@Override

View File

@ -16,7 +16,6 @@ import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.hibernate.CacheMode;
import org.hibernate.Internal;
import org.hibernate.LockOptions;
import org.hibernate.ScrollMode;
import org.hibernate.cache.spi.QueryKey;
@ -90,28 +89,6 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
);
}
@Internal
public <R> List<R> list(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer,
ListResultsConsumer.UniqueSemantic uniqueSemantic,
Function<Function<String, PreparedStatement>, DeferredResultSetAccess> resultSetAccessCreator) {
// Only do auto flushing for top level queries
return executeQuery(
jdbcSelect,
executionContext,
rowTransformer,
(sql) -> executionContext.getSession()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql ),
resultSetAccessCreator,
ListResultsConsumer.instance( uniqueSemantic )
);
}
@Override
public <R> ScrollableResultsImplementor<R> scroll(
JdbcSelect jdbcSelect,
@ -135,26 +112,6 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
);
}
@Internal
public <R> ScrollableResultsImplementor<R> scroll(
JdbcSelect jdbcSelect,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer,
Function<Function<String, PreparedStatement>, DeferredResultSetAccess> resultSetAccessCreator) {
// Only do auto flushing for top level queries
return executeQuery(
jdbcSelect,
executionContext,
rowTransformer,
(sql) -> executionContext.getSession()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql ),
resultSetAccessCreator,
ScrollableResultsConsumer.instance()
);
}
@Override
public <R> Stream<R> stream(
JdbcSelect jdbcSelect,
@ -175,25 +132,6 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
return stream.onClose( scrollableResults::close );
}
@Internal
public <R> Stream<R> stream(
JdbcSelect jdbcSelect,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer,
Function<Function<String, PreparedStatement>, DeferredResultSetAccess> resultSetAccessCreator) {
final ScrollableResultsImplementor<R> scrollableResults = scroll(
jdbcSelect,
executionContext,
rowTransformer,
resultSetAccessCreator
);
final ScrollableResultsIterator<R> iterator = new ScrollableResultsIterator<>( scrollableResults );
final Spliterator<R> spliterator = Spliterators.spliteratorUnknownSize( iterator, Spliterator.NONNULL );
final Stream<R> stream = StreamSupport.stream( spliterator, false );
return stream.onClose( scrollableResults::close );
}
private <T, R> T executeQuery(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
@ -225,7 +163,6 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
}
}
}
private <T, R> T doExecuteQuery(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
@ -233,38 +170,19 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
RowTransformer<R> rowTransformer,
Function<String, PreparedStatement> statementCreator,
ResultsConsumer<T, R> resultsConsumer) {
return executeQuery(
jdbcSelect,
executionContext,
rowTransformer,
(sql) -> executionContext.getSession()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql ),
(stmntCreator) -> new DeferredResultSetAccess(
jdbcSelect,
jdbcParameterBindings,
executionContext,
statementCreator
),
resultsConsumer
);
}
private <T, R> T executeQuery(
JdbcSelect jdbcSelect,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer,
Function<String,PreparedStatement> statementCreator,
Function<Function<String, PreparedStatement>, DeferredResultSetAccess> resultSetAccessCreator,
ResultsConsumer<T,R> resultsConsumer) {
final DeferredResultSetAccess resultSetAccess = resultSetAccessCreator.apply( statementCreator );
final DeferredResultSetAccess deferredResultSetAccess = new DeferredResultSetAccess(
jdbcSelect,
jdbcParameterBindings,
executionContext,
statementCreator
);
final JdbcValues jdbcValues = resolveJdbcValuesSource(
executionContext.getQueryIdentifier( resultSetAccess.getFinalSql() ),
executionContext.getQueryIdentifier( deferredResultSetAccess.getFinalSql() ),
jdbcSelect,
resultsConsumer.canResultsBeCached(),
executionContext,
resultSetAccess
deferredResultSetAccess
);
if ( rowTransformer == null ) {
@ -333,7 +251,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
// because these lock options are only for Initializers.
// If we wouldn't omit this, the follow on lock requests would be no-ops,
// because the EntityEntrys would already have the desired lock mode
resultSetAccess.usesFollowOnLocking()
deferredResultSetAccess.usesFollowOnLocking()
? LockOptions.NONE
: executionContext.getQueryOptions().getLockOptions(),
rowTransformer,
@ -445,8 +363,8 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
}
else {
SqlExecLogger.INSTANCE.debugf( "Skipping reading Query result cache data: cache-enabled = %s, cache-mode = %s",
queryCacheEnabled,
cacheMode.name()
queryCacheEnabled,
cacheMode.name()
);
cachedResults = null;
if ( queryCacheEnabled && canBeCached ) {

View File

@ -11,6 +11,7 @@ import java.io.InputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import org.hibernate.HibernateException;
@ -19,6 +20,8 @@ import org.hibernate.engine.jdbc.BinaryStream;
import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
/**
* Descriptor for general {@link Serializable} handling.
@ -59,6 +62,14 @@ public class SerializableJavaTypeDescriptor<T extends Serializable> extends Abst
return (MutabilityPlan<T>) SerializableMutabilityPlan.INSTANCE;
}
@Override
public JdbcType getRecommendedJdbcType(JdbcTypeDescriptorIndicators indicators) {
final int typeCode = indicators.isLob()
? Types.BLOB
: Types.LONGVARBINARY;
return indicators.getTypeConfiguration().getJdbcTypeDescriptorRegistry().getDescriptor( typeCode );
}
public String toString(T value) {
return PrimitiveByteArrayJavaTypeDescriptor.INSTANCE.toString( toBytes( value ) );
}

View File

@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.array; /**
* @author Steve Ebersole
*/
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Id;
import jakarta.persistence.Basic;
@Entity(name = "Post")
@Table(name = "Post")
public class Post {
@Id
public Integer id;
@Basic
public String title;
@Basic
public String[] tags;
private Post() {
// for Hibernate use
}
public Post(Integer id, String title) {
this.id = id;
this.title = title;
}
public Post(Integer id, String title, String... tags) {
this.id = id;
this.title = title;
this.tags = tags;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
}

View File

@ -4,53 +4,46 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;
package org.hibernate.orm.test.mapping.type.contribution.array;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure.CustomConnectionProviderInitiator;
import org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure.CustomJdbcServicesInitiator;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry.JavaService;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
/**
* @author Steve Ebersole
*/
@BootstrapServiceRegistry(
javaServices = @JavaService( role = TypeContributor.class, impl = JtsTypeContributor.class )
javaServices = @JavaService( role = TypeContributor.class, impl = StringArrayTypeContributor.class )
)
@ServiceRegistry(
initiators = { CustomJdbcServicesInitiator.class, CustomConnectionProviderInitiator.class }
)
@DomainModel( annotatedClasses = FirePoint.class )
@DomainModel(annotatedClasses = Post.class)
@SessionFactory
public class JtsContributorTests {
@RequiresDialect(H2Dialect.class)
public class StringArrayContributorTests {
@Test
public void simpleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
// save one with a Point
final GeometryFactory geometryFactory = new GeometryFactory();
session.save( new FirePoint( 1, "alpha", geometryFactory.createPoint( new Coordinate( 1, 2, 3 ) ) ) );
// save one with tags
session.save( new Post( 1, "with tags", "tag1", "tag2" ) );
// and one without (null)
session.save( new FirePoint( 2, "bravo", null ) );
session.save( new Post( 2, "no tags" ) );
} );
scope.inTransaction( (session) -> {
session.createQuery( "select fp.coordinate from FirePoint fp" ).list();
session.createQuery( "select p.tags from Post p" ).list();
} );
}
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete FirePoint" ).executeUpdate() );
scope.inTransaction( (session) -> session.createQuery( "delete Post" ).executeUpdate() );
}
}

View File

@ -0,0 +1,87 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.array;
import java.sql.Types;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import static org.hibernate.internal.util.StringHelper.WHITESPACE;
/**
* @author Steve Ebersole
*/
public class StringArrayJavaType implements BasicJavaType<String[]> {
/**
* Singleton access
*/
public static final StringArrayJavaType INSTANCE = new StringArrayJavaType();
@Override
public Class<String[]> getJavaTypeClass() {
return String[].class;
}
@Override
public JdbcType getRecommendedJdbcType(JdbcTypeDescriptorIndicators context) {
return context.getTypeConfiguration().getJdbcTypeDescriptorRegistry().getDescriptor( Types.ARRAY );
}
@Override
public String[] fromString(CharSequence string) {
if ( string == null ) {
return null;
}
return StringHelper.split( WHITESPACE, string.toString() );
}
@SuppressWarnings("unchecked")
@Override
public <X> X unwrap(String[] value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( type.isArray() ) {
assert type.getComponentType() == String.class;
return (X) value;
}
if ( type == String.class ) {
return (X) ArrayHelper.toString( value );
}
throw unsupported( type );
}
private <X> RuntimeException unsupported(Class<X> type) {
return new RuntimeException( "String[] cannot be handled as `" + type.getName() + "`" );
}
@Override
public <X> String[] wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( value instanceof String[] ) {
return (String[]) value;
}
if ( value instanceof CharSequence ) {
return fromString( (CharSequence) value );
}
throw unsupported( value.getClass() );
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;
package org.hibernate.orm.test.mapping.type.contribution.array;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
@ -12,71 +12,68 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.BasicBinder;
import org.hibernate.type.descriptor.jdbc.BasicExtractor;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.locationtech.jts.geom.Point;
import org.hibernate.type.spi.TypeConfiguration;
/**
* @author Steve Ebersole
*/
public class PointJdbcType implements JdbcType {
public static final int POINT_TYPE_CODE = SqlTypes.GEOMETRY;
@Override
public String getFriendlyName() {
return "POINT";
}
public class StringArrayJdbcType implements JdbcType {
@Override
public int getJdbcTypeCode() {
return POINT_TYPE_CODE;
return Types.ARRAY;
}
private final ValueBinder<Point> binder = new BasicBinder<Point>( PointJavaType.INSTANCE, this ) {
@Override
public <T> BasicJavaType<T> getJdbcRecommendedJavaTypeMapping(Integer precision, Integer scale, TypeConfiguration typeConfiguration) {
return (BasicJavaType<T>) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( String[].class );
}
private final ValueBinder<String[]> binder = new BasicBinder<String[]>( StringArrayJavaType.INSTANCE, this ) {
@Override
protected void doBindNull(PreparedStatement st, int index, WrapperOptions options) throws SQLException {
// according to documentation, can be treated as character data
st.setNull( index, Types.VARCHAR );
st.setNull( index, Types.ARRAY );
}
@Override
protected void doBind(PreparedStatement st, Point value, int index, WrapperOptions options) throws SQLException {
protected void doBind(PreparedStatement st, String[] value, int index, WrapperOptions options) throws SQLException {
st.setObject( index,value );
}
@Override
protected void doBindNull(CallableStatement st, String name, WrapperOptions options) throws SQLException {
// according to documentation, can be treated as character data
st.setNull( name, Types.VARCHAR );
st.setNull( name, Types.ARRAY );
}
@Override
protected void doBind(CallableStatement st, Point value, String name, WrapperOptions options) throws SQLException {
protected void doBind(CallableStatement st, String[] value, String name, WrapperOptions options) throws SQLException {
st.setObject( name, value );
}
};
private final ValueExtractor<Point> extractor = new BasicExtractor<Point>( PointJavaType.INSTANCE, this ) {
private final ValueExtractor<String[]> extractor = new BasicExtractor<String[]>( StringArrayJavaType.INSTANCE, this ) {
@Override
protected Point doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
return rs.getObject( paramIndex, Point.class );
protected String[] doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
return (String[]) rs.getObject( paramIndex );
}
@Override
protected Point doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return statement.getObject( index, Point.class );
protected String[] doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return (String[]) statement.getObject( index );
}
@Override
protected Point doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return statement.getObject( name, Point.class );
protected String[] doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return (String[]) statement.getObject( name );
}
};

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;
package org.hibernate.orm.test.mapping.type.contribution.array;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
@ -13,10 +13,10 @@ import org.hibernate.service.ServiceRegistry;
/**
* TypeContributor for applying the H2GIS JTS type descriptors
*/
public class JtsTypeContributor implements TypeContributor {
public class StringArrayTypeContributor implements TypeContributor {
@Override
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
typeContributions.contributeJavaTypeDescriptor( PointJavaType.INSTANCE );
typeContributions.contributeJdbcTypeDescriptor( new PointJdbcType() );
typeContributions.contributeJavaTypeDescriptor( StringArrayJavaType.INSTANCE );
typeContributions.contributeJdbcTypeDescriptor( new StringArrayJdbcType() );
}
}

View File

@ -1,59 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;
import org.hibernate.annotations.JavaType;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.locationtech.jts.geom.Point;
/**
* @author Steve Ebersole
*/
@Entity(name = "FirePoint")
@Table(name = "FirePoint")
public class FirePoint {
@Id
private Integer id;
@Basic
private String name;
@Basic
@JavaType( PointJavaType.class )
private Point coordinate;
private FirePoint() {
}
public FirePoint(Integer id, String name, Point coordinate) {
this.id = id;
this.name = name;
this.coordinate = coordinate;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
}

View File

@ -1,63 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.locationtech.jts.geom.Point;
/**
* @author Steve Ebersole
*/
public class PointJavaType implements BasicJavaType<Point> {
/**
* Singleton access
*/
public static final PointJavaType INSTANCE = new PointJavaType();
@Override
public Class<Point> getJavaTypeClass() {
return Point.class;
}
@Override
public JdbcType getRecommendedJdbcType(JdbcTypeDescriptorIndicators context) {
return context
.getTypeConfiguration()
.getJdbcTypeDescriptorRegistry()
.getDescriptor( PointJdbcType.POINT_TYPE_CODE );
}
@Override
public Point fromString(CharSequence string) {
throw unsupported( String.class );
}
private static UnsupportedOperationException unsupported(Class<?> type) {
return new UnsupportedOperationException( "At the moment, Point cannot be converted to/from other types : " + type.getName() );
}
@SuppressWarnings("unchecked")
@Override
public <X> X unwrap(Point value, Class<X> type, WrapperOptions options) {
if ( value == null || type.isInstance( value ) ) {
return (X) value;
}
throw unsupported( type );
}
@Override
public <X> Point wrap(X value, WrapperOptions options) {
if ( value == null || value instanceof Point ) {
return (Point) value;
}
throw unsupported( value.getClass() );
}
}

View File

@ -1,76 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable;
import org.h2gis.utilities.SFSUtilities;
/**
* @author Steve Ebersole
*/
public class CustomConnectionProvider implements ConnectionProvider, Stoppable {
private final ConnectionProvider delegate;
public CustomConnectionProvider(Map configurationValues, ServiceRegistryImplementor registry) {
delegate = ConnectionProviderInitiator.INSTANCE.initiateService( configurationValues, registry );
( (Configurable) delegate ).configure( configurationValues );
( (ServiceRegistryAwareService) delegate ).injectServices( registry );
}
@Override
public Connection getConnection() throws SQLException {
final Connection wrappedConnection = SFSUtilities.wrapConnection( delegate.getConnection() );
try {
final Statement statement = wrappedConnection.createStatement();
statement.execute( "CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR \"org.h2gis.functions.factory.H2GISFunctions.load\"" );
statement.execute( "CALL H2GIS_SPATIAL()" );
return wrappedConnection;
}
catch (SQLException e) {
throw new JDBCConnectionException( "Error creating H2GIS wrapped Connection", e );
}
}
@Override
public void closeConnection(Connection conn) throws SQLException {
delegate.closeConnection( conn );
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}
@Override
public void stop() {
if ( delegate instanceof Stoppable ) {
( (Stoppable) delegate ).stop();
}
}
}

View File

@ -1,28 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* @author Steve Ebersole
*/
public class CustomConnectionProviderInitiator implements StandardServiceInitiator<ConnectionProvider> {
@Override
public ConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return new CustomConnectionProvider( configurationValues, registry );
}
@Override
public Class<ConnectionProvider> getServiceInitiated() {
return ConnectionProvider.class;
}
}

View File

@ -1,92 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.util.List;
import java.util.stream.Stream;
import org.hibernate.ScrollMode;
import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl;
import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.exec.spi.JdbcSelect;
import org.hibernate.sql.exec.spi.JdbcSelectExecutor;
import org.hibernate.sql.results.spi.ListResultsConsumer;
import org.hibernate.sql.results.spi.RowTransformer;
/**
* @author Steve Ebersole
*/
public class CustomJdbcSelectExecutor implements JdbcSelectExecutor {
private final JdbcSelectExecutorStandardImpl standardSelectExecutor;
public CustomJdbcSelectExecutor() {
this.standardSelectExecutor = JdbcSelectExecutorStandardImpl.INSTANCE;
}
@Override
public <R> List<R> list(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer,
ListResultsConsumer.UniqueSemantic uniqueSemantic) {
return standardSelectExecutor.list(
jdbcSelect,
jdbcParameterBindings,
executionContext,
rowTransformer,
uniqueSemantic,
(preparedStatementFunction) -> new CustomResultSetAccess(
jdbcSelect,
jdbcParameterBindings,
executionContext,
preparedStatementFunction
)
);
}
@Override
public <R> ScrollableResultsImplementor<R> scroll
(JdbcSelect jdbcSelect,
ScrollMode scrollMode,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer) {
return standardSelectExecutor.scroll(
jdbcSelect,
executionContext,
rowTransformer,
(preparedStatementFunction) -> new CustomResultSetAccess(
jdbcSelect,
jdbcParameterBindings,
executionContext,
preparedStatementFunction
)
);
}
@Override
public <R> Stream<R> stream(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext,
RowTransformer<R> rowTransformer) {
return standardSelectExecutor.stream(
jdbcSelect,
executionContext,
rowTransformer,
(preparedStatementFunction) -> new CustomResultSetAccess(
jdbcSelect,
jdbcParameterBindings,
executionContext,
preparedStatementFunction
)
);
}
}

View File

@ -1,96 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.util.Map;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.sql.exec.spi.JdbcMutationExecutor;
import org.hibernate.sql.exec.spi.JdbcSelectExecutor;
/**
* @author Steve Ebersole
*/
public class CustomJdbcServices implements JdbcServices, ServiceRegistryAwareService, Configurable {
private final JdbcServices delegate;
private final CustomJdbcSelectExecutor selectExecutor;
public CustomJdbcServices(JdbcServices delegate) {
this.delegate = delegate;
selectExecutor = new CustomJdbcSelectExecutor();
}
@Override
public JdbcSelectExecutor getJdbcSelectExecutor() {
return selectExecutor;
}
@Override
public JdbcEnvironment getJdbcEnvironment() {
return delegate.getJdbcEnvironment();
}
@Override
public JdbcConnectionAccess getBootstrapJdbcConnectionAccess() {
return delegate.getBootstrapJdbcConnectionAccess();
}
@Override
public Dialect getDialect() {
return delegate.getDialect();
}
@Override
public SqlStatementLogger getSqlStatementLogger() {
return delegate.getSqlStatementLogger();
}
@Override
public SqlExceptionHelper getSqlExceptionHelper() {
return delegate.getSqlExceptionHelper();
}
@Override
public ExtractedDatabaseMetaData getExtractedMetaDataSupport() {
return delegate.getExtractedMetaDataSupport();
}
@Override
public LobCreator getLobCreator(LobCreationContext lobCreationContext) {
return delegate.getLobCreator( lobCreationContext );
}
@Override
public JdbcMutationExecutor getJdbcMutationExecutor() {
return delegate.getJdbcMutationExecutor();
}
@Override
public void configure(Map configurationValues) {
if ( delegate instanceof Configurable ) {
( (Configurable) delegate ).configure( configurationValues );
}
}
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
if ( delegate instanceof ServiceRegistryAwareService ) {
( (ServiceRegistryAwareService) delegate ).injectServices( serviceRegistry );
}
}
}

View File

@ -1,30 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.engine.jdbc.internal.JdbcServicesInitiator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* @author Steve Ebersole
*/
public class CustomJdbcServicesInitiator implements StandardServiceInitiator<JdbcServices> {
@Override
public JdbcServices initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final JdbcServices standard = JdbcServicesInitiator.INSTANCE.initiateService( configurationValues, registry );
return new CustomJdbcServices( standard );
}
@Override
public Class<JdbcServices> getServiceInitiated() {
return JdbcServices.class;
}
}

View File

@ -1,37 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.type.contribution.jts.infrastructure;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.Function;
import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.exec.spi.JdbcSelect;
import org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess;
import org.h2gis.utilities.SpatialResultSet;
/**
* @author Steve Ebersole
*/
public class CustomResultSetAccess extends DeferredResultSetAccess {
public CustomResultSetAccess(
JdbcSelect jdbcSelect,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext,
Function<String, PreparedStatement> statementCreator) {
super( jdbcSelect, jdbcParameterBindings, executionContext, statementCreator );
}
@Override
protected ResultSet wrapResultSet(ResultSet resultSet) throws SQLException {
return resultSet.unwrap( SpatialResultSet.class );
}
}

View File

@ -1,14 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
/**
* Verify support for contributing Java and JDBC type extensions using
* H2 JTS support.
*
* Smoke test for hibernate-spatial
*/
package org.hibernate.orm.test.mapping.type.contribution.jts;