diff --git a/documentation/src/main/asciidoc/userguide/chapters/query/spatial/Spatial.adoc b/documentation/src/main/asciidoc/userguide/chapters/query/spatial/Spatial.adoc index 2baffcb4341c..ab983d02fa72 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/query/spatial/Spatial.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/query/spatial/Spatial.adoc @@ -192,7 +192,7 @@ The `GEOGRAPHY` type is not currently supported. ==== CockroachDB:: -The dialect `CockroachDB202SpatialDialect` support the `GEOMETRY` type in CockroachDB v20.2 and later. +The dialect `CockroachDB202` support the `GEOMETRY` type in CockroachDB v20.2 and later. [NOTE] ==== diff --git a/gradle/databases.gradle b/gradle/databases.gradle index 1bfa1b18e23d..904c8ef16190 100644 --- a/gradle/databases.gradle +++ b/gradle/databases.gradle @@ -148,7 +148,7 @@ ext { 'connection.init_sql' : '' ], postgis : [ - 'db.dialect' : 'org.hibernate.dialect.PostgreSQLDialect', + 'db.dialect' : 'org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect', 'jdbc.driver': 'org.postgresql.Driver', 'jdbc.user' : 'hibernate_orm_test', 'jdbc.pass' : 'hibernate_orm_test', diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java index 6faab7115939..3edf375edac4 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java @@ -87,6 +87,9 @@ public CockroachDialect(int version) { registerColumnType( Types.NCLOB, "string" ); registerColumnType( Types.JAVA_OBJECT, "json" ); + + //register geometry type + registerColumnType( 5432, "geometry" ); } @Override diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/contributor/ContributorResolver.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/contributor/ContributorResolver.java index 30865c9b95a0..eb0850339968 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/contributor/ContributorResolver.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/contributor/ContributorResolver.java @@ -11,21 +11,24 @@ import java.util.Map; import java.util.function.Function; +import org.hibernate.dialect.CockroachDialect; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.service.ServiceRegistry; +import org.hibernate.spatial.dialect.cockroachdb.CockroachDbContributor; import org.hibernate.spatial.dialect.postgis.PostgisDialectContributor; class ContributorResolver { private final static Map, - Function> ContributorMap = new HashMap<>(); + Function> CONTRIBUTOR_MAP = new HashMap<>(); static { //TypeContributorImplementor - ContributorMap.put( PostgreSQLDialect.class, PostgisDialectContributor::new ); + CONTRIBUTOR_MAP.put( PostgreSQLDialect.class, PostgisDialectContributor::new ); + CONTRIBUTOR_MAP.put( CockroachDialect.class, CockroachDbContributor::new ); } private ContributorResolver() { @@ -34,9 +37,9 @@ private ContributorResolver() { static ContributorImplementor resolveSpatialtypeContributorImplementor(ServiceRegistry serviceRegistry) { JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); Dialect dialect = jdbcServices.getDialect(); - for ( Class dialectClass : ContributorMap.keySet() ) { + for ( Class dialectClass : CONTRIBUTOR_MAP.keySet() ) { if ( dialectClass.isAssignableFrom( dialect.getClass() ) ) { - return ContributorMap.get( dialectClass ).apply( serviceRegistry ); + return CONTRIBUTOR_MAP.get( dialectClass ).apply( serviceRegistry ); } } return null; diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDBSpatialSupport.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDBSpatialSupport.java deleted file mode 100644 index 7389598d5e6f..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDBSpatialSupport.java +++ /dev/null @@ -1,46 +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 . - */ - -package org.hibernate.spatial.dialect.cockroachdb; - -import org.hibernate.boot.model.TypeContributions; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor; -import org.hibernate.spatial.GeolatteGeometryType; -import org.hibernate.spatial.JTSGeometryJavaTypeDescriptor; -import org.hibernate.spatial.JTSGeometryType; -import org.hibernate.spatial.SpatialDialect; -import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor; -import org.hibernate.spatial.dialect.postgis.PostgisFunctions; -import org.hibernate.spatial.dialect.postgis.PostgisSupport; - -public class CockroachDBSpatialSupport extends PostgisSupport implements SpatialDialect { - - CockroachDBSpatialSupport() { - super( new CockroachDBSpatialFunctions() ); - } - - @Override - public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { - typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); - typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); - - typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE ); - typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE ); - } - -} - -class CockroachDBSpatialFunctions extends PostgisFunctions { - - CockroachDBSpatialFunctions() { - super(); - this.functionMap.remove( "geomunion" ); - } - -} - diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDbContributor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDbContributor.java new file mode 100644 index 000000000000..3c9b3f60d671 --- /dev/null +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDbContributor.java @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +package org.hibernate.spatial.dialect.cockroachdb; + +import org.hibernate.boot.model.TypeContributions; +import org.hibernate.query.sqm.function.SqmFunctionRegistry; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.spatial.FunctionKey; +import org.hibernate.spatial.GeolatteGeometryType; +import org.hibernate.spatial.HSMessageLogger; +import org.hibernate.spatial.JTSGeometryType; +import org.hibernate.spatial.contributor.ContributorImplementor; +import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor; +import org.hibernate.spatial.dialect.postgis.PostgisSqmFunctionDescriptors; + +public class CockroachDbContributor implements ContributorImplementor { + + private final ServiceRegistry serviceRegistry; + + public CockroachDbContributor(ServiceRegistry serviceRegistry) { + this.serviceRegistry = serviceRegistry; + } + + @Override + public void contributeTypes(TypeContributions typeContributions) { + HSMessageLogger.LOGGER.typeContributions( this.getClass().getCanonicalName() ); + typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); + typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); + } + + @Override + public void contributeFunctions(SqmFunctionRegistry functionRegistry) { + HSMessageLogger.LOGGER.functionContributions( this.getClass().getCanonicalName() ); + PostgisSqmFunctionDescriptors postgisFunctions = new PostgisSqmFunctionDescriptors( getServiceRegistry() ); + + postgisFunctions.asMap() + .forEach( (key, desc) -> { + if ( isUnsupported( key ) ) { + return; + } + functionRegistry.register( key.getName(), desc ); + key.getAltName().ifPresent( altName -> functionRegistry.registerAlternateKey( + altName, + key.getName() + ) ); + } ); + } + + private boolean isUnsupported(FunctionKey key) { + return key.getName().equalsIgnoreCase( "st_union" ); + } + + @Override + public ServiceRegistry getServiceRegistry() { + return this.serviceRegistry; + } +} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java deleted file mode 100644 index 9cd5813c52e0..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java +++ /dev/null @@ -1,82 +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 . - */ - -package org.hibernate.spatial.dialect.cockroachdb; - -import org.hibernate.boot.model.TypeContributions; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.spatial.SpatialDialect; -import org.hibernate.spatial.SpatialFunction; -import org.hibernate.spatial.dialect.SpatialFunctionsRegistry; - -public interface CockroachSpatialDialectTrait extends SpatialDialect { - - CockroachDBSpatialSupport DELEGATE = new CockroachDBSpatialSupport(); - - default SpatialFunctionsRegistry functionsToRegister() { - return DELEGATE.functionsToRegister(); - - } - - default String getSpatialRelateSQL(String columnName, int spatialRelation) { - return DELEGATE.getSpatialRelateSQL( columnName, spatialRelation ); - } - - default void delegateContributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { - DELEGATE.contributeTypes( typeContributions, serviceRegistry ); - } - - /** - * Returns the SQL fragment for the SQL WHERE-expression when parsing - * org.hibernate.spatial.criterion.SpatialFilterExpressions - * into prepared statements. - * - * @param columnName The name of the geometry-typed column to which the filter is - * be applied - * - * @return Rhe SQL fragment for the {@code SpatialFilterExpression} - */ - default String getSpatialFilterExpression(String columnName) { - return DELEGATE.getSpatialFilterExpression( columnName ); - } - - - default String getSpatialAggregateSQL(String columnName, int aggregation) { - return DELEGATE.getSpatialAggregateSQL( columnName, aggregation ); - } - - - default String getDWithinSQL(String columnName) { - return DELEGATE.getDWithinSQL( columnName ); - } - - - default String getHavingSridSQL(String columnName) { - return DELEGATE.getHavingSridSQL( columnName ); - } - - - default String getIsEmptySQL(String columnName, boolean isEmpty) { - return DELEGATE.getIsEmptySQL( columnName, isEmpty ); - } - - - default boolean supportsFiltering() { - return DELEGATE.supportsFiltering(); - } - - - default boolean supports(SpatialFunction function) { - return DELEGATE.supports( function ); - } - - - default boolean isSpatial(int typeCode) { - return DELEGATE.isSpatial( typeCode ); - } - -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisDialectContributor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisDialectContributor.java index 641548319c1a..948fbe2e8107 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisDialectContributor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisDialectContributor.java @@ -27,12 +27,8 @@ public PostgisDialectContributor(ServiceRegistry serviceRegistry) { public void contributeTypes(TypeContributions typeContributions) { HSMessageLogger.LOGGER.typeContributions( this.getClass().getCanonicalName() ); - typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) ); - typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) ); - - //Isn't this redundant? - typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE ); - typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE ); + typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); + typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) ); } @Override diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java deleted file mode 100644 index 3ed489f41b47..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java +++ /dev/null @@ -1,253 +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 . - */ -package org.hibernate.spatial.dialect.postgis; - -import java.util.List; -import java.util.Locale; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.hibernate.QueryException; -import org.hibernate.dialect.function.StandardSQLFunction; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor; -import org.hibernate.query.sqm.produce.function.ArgumentsValidator; -import org.hibernate.query.sqm.produce.function.NamedFunctionDescriptorBuilder; -import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; -import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; -import org.hibernate.spatial.SpatialFunction; -import org.hibernate.spatial.dialect.SpatialFunctionsRegistry; -import org.hibernate.sql.ast.SqlAstTranslator; -import org.hibernate.sql.ast.spi.SqlAppender; -import org.hibernate.sql.ast.tree.SqlAstNode; -import org.hibernate.sql.ast.tree.predicate.Predicate; -import org.hibernate.type.StandardBasicTypes; -import org.hibernate.type.Type; - -import static org.hibernate.spatial.CommonSpatialFunction.ST_ASTEXT; -import static org.hibernate.spatial.CommonSpatialFunction.ST_GEOMETRYTYPE; - -/** - * Functions registered in all Postgis Dialects - *

- * Created by Karel Maesen, Geovise BVBA on 29/10/16. - * @deprecated replaced by DialectSpatialSqmFunctionDescriptors - */ -@Deprecated -public class PostgisFunctions extends SpatialFunctionsRegistry { - - public PostgisFunctions() { - - put( - "dimension", new StandardSQLFunction( - "st_dimension", - StandardBasicTypes.INTEGER - ) - ); - - put( - "srid", new StandardSQLFunction( - "st_srid", - StandardBasicTypes.INTEGER - ) - ); - put( - "envelope", new StandardSQLFunction( - "st_envelope" - ) - ); - put( - "makeenvelope", new StandardSQLFunction( - "st_makeenvelope" - ) - ); - - put( - "asbinary", new StandardSQLFunction( - "st_asbinary", - StandardBasicTypes.BINARY - ) - ); - put( - "isempty", new StandardSQLFunction( - "st_isempty", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "issimple", new StandardSQLFunction( - "st_issimple", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "boundary", new StandardSQLFunction( - "st_boundary" - ) - ); - - // Register functions for spatial relation constructs - put( - "overlaps", new StandardSQLFunction( - "st_overlaps", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "intersects", new StandardSQLFunction( - "st_intersects", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "equals", new StandardSQLFunction( - "st_equals", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "contains", new StandardSQLFunction( - "st_contains", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "crosses", new StandardSQLFunction( - "st_crosses", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "disjoint", new StandardSQLFunction( - "st_disjoint", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "touches", new StandardSQLFunction( - "st_touches", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "within", new StandardSQLFunction( - "st_within", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "relate", new StandardSQLFunction( - "st_relate", - StandardBasicTypes.BOOLEAN - ) - ); - - // register the spatial analysis functions - put( - "distance", new StandardSQLFunction( - "st_distance", - StandardBasicTypes.DOUBLE - ) - ); - put( - "buffer", new StandardSQLFunction( "st_buffer" - ) - ); - put( - "convexhull", new StandardSQLFunction( - "st_convexhull" - ) - ); - put( - "difference", new StandardSQLFunction( - "st_difference" - ) - ); - put( - "intersection", new StandardSQLFunction( - "st_intersection" - ) - ); - put( - "symdifference", - new StandardSQLFunction( "st_symdifference" ) - ); - put( - "geomunion", new StandardSQLFunction( - "st_union" - ) - ); - - //register Spatial Aggregate function - put( - "extent", new ExtentFunction() - ); - -// //register Spatial Filter function -// put( -// SpatialFunction.filter.name(), new FilterFunction() -// ); - - //other common functions - put( - "dwithin", new StandardSQLFunction( - "st_dwithin", - StandardBasicTypes.BOOLEAN - ) - ); - put( - "transform", new StandardSQLFunction( - "st_transform" - ) - ); - } - - private static class ExtentFunction extends StandardSQLFunction { - - public ExtentFunction() { - super( "st_extent" ); - } - - - @Override - public void render( - SqlAppender sqlAppender, - List sqlAstArguments, - Predicate filter, - SqlAstTranslator translator) { - super.render( sqlAppender, sqlAstArguments, filter, translator ); - sqlAppender.appendSql( "::geometry" ); - } - } - -// private static class FilterFunction extends StandardSQLFunction { -// -// public FilterFunction() { -// super( "&&" ); -// } -// -// @Override -// public String render( -// Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) { -// int argumentCount = arguments.size(); -// if ( argumentCount != 2 ) { -// throw new QueryException( String.format( -// Locale.ENGLISH, -// "2 arguments expected, received %d", -// argumentCount -// ) ); -// } -// -// return Stream.of( -// String.valueOf( arguments.get( 0 ) ), -// getRenderedName( arguments ), -// String.valueOf( arguments.get( 1 ) ) -// ).collect( Collectors.joining( " ", "(", ")" ) ); -// } -// } - -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java index 5120e2dbc231..38232141b5e9 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java @@ -11,11 +11,16 @@ import org.hibernate.boot.model.TypeContributions; import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; import org.hibernate.service.ServiceRegistry; @Deprecated public class PostgisPG10Dialect extends PostgreSQLDialect { + public PostgisPG10Dialect(DialectResolutionInfo resolutionInfo) { + super( resolutionInfo ); + } + public PostgisPG10Dialect() { super( 100 ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java deleted file mode 100644 index b18421047673..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java +++ /dev/null @@ -1,32 +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 . - */ -package org.hibernate.spatial.dialect.postgis; - -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; - -/** - * Extends the {@code PostgreSQL91Dialect} to add support for the Postgis spatial types, functions and operators . - *

- * Created by Karel Maesen, Geovise BVBA on 01/11/16. - * - * @deprecated A SpatialDialect is no longer required. Use the standard Dialect for this database. - */ -@Deprecated -public class PostgisPG91Dialect extends PostgreSQLDialect { - public PostgisPG91Dialect(DialectResolutionInfo info) { - super( info ); - } - - public PostgisPG91Dialect() { - super(); - } - - public PostgisPG91Dialect(int version) { - super( version ); - } -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java deleted file mode 100644 index dec4cb2ec0a9..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java +++ /dev/null @@ -1,34 +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 . - */ -package org.hibernate.spatial.dialect.postgis; - -import org.hibernate.dialect.PostgreSQL92Dialect; -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; -import org.hibernate.spatial.SpatialDialect; - -/** - * Extends the {@code PostgreSQL92Dialect} to add support for the Postgis spatial types, functions and operators . - *

- * Created by Karel Maesen, Geovise BVBA on 01/11/16. - * - * @deprecated A SpatialDialect is no longer required. Use the standard Dialect for this database. - */ -@Deprecated -public class PostgisPG92Dialect extends PostgreSQLDialect { - public PostgisPG92Dialect(DialectResolutionInfo info) { - super( info ); - } - - public PostgisPG92Dialect() { - super(); - } - - public PostgisPG92Dialect(int version) { - super( version ); - } -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java deleted file mode 100644 index 801f8f3d9ee4..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java +++ /dev/null @@ -1,31 +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 . - */ -package org.hibernate.spatial.dialect.postgis; - -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; - -/** - * * Extends the {@code PostgreSQL9Dialect} to add support for the Postgis spatial types, functions and operators . - *

- * Created by Karel Maesen, Geovise BVBA on 01/11/16. - * - * @deprecated A SpatialDialect is no longer required. Use the standard Dialect for this database. - */ -@Deprecated -public class PostgisPG9Dialect extends PostgreSQLDialect { - public PostgisPG9Dialect(DialectResolutionInfo info) { - } - - public PostgisPG9Dialect() { - super(); - } - - public PostgisPG9Dialect(int version) { - super( version ); - } -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSqmFunctionDescriptors.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSqmFunctionDescriptors.java index 75639a47c674..e052bb13457b 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSqmFunctionDescriptors.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSqmFunctionDescriptors.java @@ -28,7 +28,7 @@ public class PostgisSqmFunctionDescriptors implements KeyedSqmFunctionDescriptor private final Map map = new HashMap<>(); - PostgisSqmFunctionDescriptors(ServiceRegistry serviceRegistry) { + public PostgisSqmFunctionDescriptors(ServiceRegistry serviceRegistry) { Arrays.stream( values() ) .forEach( cf -> map.put( cf.getKey(), toPGFunction( cf ) ) ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java deleted file mode 100644 index db6e3e98d400..000000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java +++ /dev/null @@ -1,196 +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 . - */ -package org.hibernate.spatial.dialect.postgis; - -import java.io.Serializable; -import java.sql.Types; - -import org.hibernate.annotations.Remove; -import org.hibernate.boot.model.TypeContributions; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor; -import org.hibernate.spatial.GeolatteGeometryType; -import org.hibernate.spatial.JTSGeometryJavaTypeDescriptor; -import org.hibernate.spatial.JTSGeometryType; -import org.hibernate.spatial.SpatialAggregate; -import org.hibernate.spatial.SpatialDialect; -import org.hibernate.spatial.SpatialFunction; -import org.hibernate.spatial.SpatialRelation; -import org.hibernate.spatial.dialect.SpatialFunctionsRegistry; - -/** - * Created by Karel Maesen, Geovise BVBA on 29/10/16. - */ -@Deprecated -public class PostgisSupport implements SpatialDialect, Serializable { - - private final SpatialFunctionsRegistry postgisFunctions; - - public PostgisSupport(SpatialFunctionsRegistry functions) { - postgisFunctions = functions; - } - - public PostgisSupport() { - postgisFunctions = new PostgisFunctions(); - } - - public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { - typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) ); - typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) ); - - typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE ); - typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE ); - } - - public SpatialFunctionsRegistry functionsToRegister() { - return postgisFunctions; - } - - public boolean isSpatial(int typeCode){ - return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_1.getJdbcTypeCode(); - } - - /** - * Returns the SQL fragment for the SQL WHERE-clause when parsing - * org.hibernatespatial.criterion.SpatialRelateExpressions - * into prepared statements. - *

- * - * @param columnName The name of the geometry-typed column to which the relation is - * applied - * @param spatialRelation The type of spatial relation (as defined in - * SpatialRelation). - * - * @return SQL fragment {@code SpatialRelateExpression} - */ - - public String getSpatialRelateSQL(String columnName, int spatialRelation) { - switch ( spatialRelation ) { - case SpatialRelation.WITHIN: - return " ST_within(" + columnName + ",?)"; - case SpatialRelation.CONTAINS: - return " ST_contains(" + columnName + ", ?)"; - case SpatialRelation.CROSSES: - return " ST_crosses(" + columnName + ", ?)"; - case SpatialRelation.OVERLAPS: - return " ST_overlaps(" + columnName + ", ?)"; - case SpatialRelation.DISJOINT: - return " ST_disjoint(" + columnName + ", ?)"; - case SpatialRelation.INTERSECTS: - return " ST_intersects(" + columnName - + ", ?)"; - case SpatialRelation.TOUCHES: - return " ST_touches(" + columnName + ", ?)"; - case SpatialRelation.EQUALS: - return " ST_equals(" + columnName + ", ?)"; - default: - throw new IllegalArgumentException( - "Spatial relation is not known by this dialect" - ); - } - } - - /** - * Returns the SQL fragment for the SQL WHERE-expression when parsing - * org.hibernate.spatial.criterion.SpatialFilterExpressions - * into prepared statements. - * - * @param columnName The name of the geometry-typed column to which the filter is - * be applied - * - * @return Rhe SQL fragment for the {@code SpatialFilterExpression} - */ - - public String getSpatialFilterExpression(String columnName) { - return "(" + columnName + " && ? ) "; - } - - /** - * Returns the SQL fragment for the specfied Spatial aggregate expression. - * - * @param columnName The name of the Geometry property - * @param aggregation The type of SpatialAggregate - * - * @return The SQL fragment for the projection - */ - - public String getSpatialAggregateSQL(String columnName, int aggregation) { - switch ( aggregation ) { - case SpatialAggregate.EXTENT: - final StringBuilder stbuf = new StringBuilder(); - stbuf.append( "st_extent(" ).append( columnName ).append( ")::geometry" ); - return stbuf.toString(); - default: - throw new IllegalArgumentException( - "Aggregation of type " - + aggregation + " are not supported by this dialect" - ); - } - } - - /** - * Returns The SQL fragment when parsing a DWithinExpression. - * - * @param columnName The geometry column to test against - * - * @return The SQL fragment when parsing a DWithinExpression. - */ - - public String getDWithinSQL(String columnName) { - return "ST_DWithin(" + columnName + ",?,?)"; - } - - /** - * Returns the SQL fragment when parsing a HavingSridExpression. - * - * @param columnName The geometry column to test against - * - * @return The SQL fragment for a HavingSridExpression. - */ - - public String getHavingSridSQL(String columnName) { - return "( ST_srid(" + columnName + ") = ?)"; - } - - /** - * Returns the SQL fragment when parsing a IsEmptyExpression or - * IsNotEmpty expression. - * - * @param columnName The geometry column - * @param isEmpty Whether the geometry is tested for empty or non-empty - * - * @return The SQL fragment for the isempty function - */ - - public String getIsEmptySQL(String columnName, boolean isEmpty) { - final String emptyExpr = " ST_IsEmpty(" + columnName + ") "; - return isEmpty ? emptyExpr : "( NOT " + emptyExpr + ")"; - } - - /** - * Returns true if this SpatialDialect supports a specific filtering function. - *

This is intended to signal DB-support for fast window queries, or MBR-overlap queries.

- * - * @return True if filtering is supported - */ - - public boolean supportsFiltering() { - return true; - } - - /** - * Does this dialect supports the specified SpatialFunction. - * - * @param function SpatialFunction - * - * @return True if this SpatialDialect supports the spatial function specified by the function parameter. - */ - - public boolean supports(SpatialFunction function) { - return ( postgisFunctions.get( function.toString() ) != null ); - } -} diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java index 6e15cb35bcad..4dd2647b6536 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java @@ -20,8 +20,6 @@ import javax.persistence.Id; import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.spatial.GeolatteGeometryType; -import org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect; import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -30,7 +28,6 @@ import org.junit.Test; import org.geolatte.geom.C2D; -import org.geolatte.geom.Geometry; import org.geolatte.geom.Point; import org.geolatte.geom.Polygon; import org.geolatte.geom.crs.CoordinateReferenceSystem; diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/TestSpatialSchemaGeneration.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/TestSpatialSchemaGeneration.java index 96f2ba6ebf6d..9c9651da93d5 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/TestSpatialSchemaGeneration.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/TestSpatialSchemaGeneration.java @@ -11,6 +11,8 @@ import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; import java.util.EnumSet; import java.util.List; import java.util.Locale; @@ -53,6 +55,7 @@ public void testCreatedSchemaHasGeometryField(SessionFactoryScope scope) throws .execute( EnumSet.of( TargetType.SCRIPT ), SchemaExport.Action.BOTH, metadata ); final List sqlLines = Files.readAllLines( output.toPath(), Charset.defaultCharset() ); String result = sqlLines.stream().collect( Collectors.joining( " " ) ).toLowerCase( Locale.ROOT ); - assertThat( result, stringContainsInOrder( List.of( "geometry", "geom" ) ) ); + Iterable geomElems = Arrays.asList( "geometry", "geom" ); + assertThat( result, stringContainsInOrder( geomElems ) ); } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/CommonFunctionTests.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/CommonFunctionTests.java index bba768d649c6..f43e23c0af57 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/CommonFunctionTests.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/CommonFunctionTests.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.stream.Stream; import org.hibernate.spatial.integration.SpatialTestDataProvider; @@ -40,7 +41,7 @@ * Dynamic tests for "common" spatial functions. * *

- * The tests here are dynamic across several dimensions: + * The tests here are dynamic across several dimensions: *

    *
  • the spatial function (in so far as actually supported by the dialect)
  • *
  • the Geometry model (JTS or Geolatte)
  • @@ -55,12 +56,22 @@ public class CommonFunctionTests extends SpatialTestDataProvider implements SessionFactoryScopeAware { private SessionFactoryScope scope; + private Set supportedFunctions; List received; List expected; @Override public void injectSessionFactoryScope(SessionFactoryScope scope) { this.scope = scope; + //scope is set to null during test cleanup + if ( scope != null ) { + this.supportedFunctions = scope.getSessionFactory() + .getQueryEngine() + .getSqmFunctionRegistry() + .getFunctions() + .keySet(); + + } } @BeforeEach @@ -84,9 +95,10 @@ public void cleanup() { @TestFactory public Stream testFunction() { + return TestTemplates.all( templates, hqlOverrides ) - // TODO -- filter for supported functions + .filter( this::isSupported ) .flatMap( t -> Stream.of( t.build( Model.JTSMODEL, codec ), t.build( Model.GLMODEL, codec ) @@ -95,6 +107,10 @@ public Stream testFunction() { } + private boolean isSupported(FunctionTestTemplate.Builder builder) { + return supportedFunctions.contains( builder.function.name() ); + } + protected Stream buildTests(FunctionTestTemplate template) { return Stream.of( template.getFunctionName(), diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/RowObjectMapper.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/RowObjectMapper.java index ce88ab115a79..70b881293c4d 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/RowObjectMapper.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/functions/RowObjectMapper.java @@ -17,15 +17,15 @@ public interface RowObjectMapper { default Data apply(Object obj) { Object[] row = (Object[]) obj; - return new Data( (Integer) row[0], (T) row[1] ); + return new Data( (Number) row[0], (T) row[1] ); } } class Data { - final Integer id; + final Number id; Object datum; - Data(Integer id, Object datum) { + Data(Number id, Object datum) { this.id = id; this.datum = datum; } @@ -39,7 +39,7 @@ public boolean equals(Object o) { return false; } Data data = (Data) o; - return Objects.equals( id, data.id ) && isEquals( datum, data.datum ); + return Objects.equals( id.intValue(), data.id.intValue() ) && isEquals( datum, data.datum ); } private boolean isEquals(Object thisDatum, Object thatDatum) { diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/RequireFunctionExtension.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/RequireFunctionExtension.java index 4bc6d0cec520..c3ffb48c7ea3 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/RequireFunctionExtension.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/RequireFunctionExtension.java @@ -34,7 +34,7 @@ public class RequireFunctionExtension implements ExecutionCondition { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional testInstance = context.getTestInstance(); - if ( testInstance.isEmpty() ) { + if ( !testInstance.isPresent() ) { return ENABLED; } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/TestSupportFactories.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/TestSupportFactories.java index ceec00c345cc..f2072404559f 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/TestSupportFactories.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/TestSupportFactories.java @@ -53,7 +53,7 @@ private static Class getSupportFactoryClass(Dialect diale return MariaDBTestSupport.class; } - if ( ( dialect instanceof SpatialDialect ) && CockroachDialect.class.isAssignableFrom( dialect.getClass() ) ) { + if ( CockroachDialect.class.isAssignableFrom( dialect.getClass() ) ) { return CockroachDBTestSupport.class; } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/cockroachdb/CockroachDBTestSupport.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/cockroachdb/CockroachDBTestSupport.java index 44a0b9733f07..0dfe2cb50f0c 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/cockroachdb/CockroachDBTestSupport.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/cockroachdb/CockroachDBTestSupport.java @@ -7,12 +7,24 @@ package org.hibernate.spatial.testing.dialects.cockroachdb; +import org.hibernate.spatial.GeomCodec; +import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor; import org.hibernate.spatial.testing.AbstractExpectationsFactory; import org.hibernate.spatial.testing.datareader.TestData; import org.hibernate.spatial.testing.datareader.TestSupport; +import org.hibernate.spatial.testing.dialects.NativeSQLTemplates; +import org.hibernate.spatial.testing.dialects.postgis.PostgisNativeSQLTemplates; + +import org.geolatte.geom.Geometry; +import org.geolatte.geom.codec.Wkt; public class CockroachDBTestSupport extends TestSupport { + @Override + public NativeSQLTemplates templates() { + return new PostgisNativeSQLTemplates(); + } + @Override public TestData createTestData(TestDataPurpose purpose) { switch ( purpose ) { @@ -23,4 +35,18 @@ public TestData createTestData(TestDataPurpose purpose) { } } + public GeomCodec codec() { + return new GeomCodec() { + @Override + public Geometry toGeometry(Object in) { + return PGGeometryTypeDescriptor.INSTANCE_WKB_2.toGeometry( in ); + } + + @Override + public Object fromGeometry(Geometry in) { + return Wkt.toWkt( in, Wkt.Dialect.POSTGIS_EWKT_1 ); + } + }; + } + } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisTestSupport.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisTestSupport.java index 3892a9c72525..f590389ec4b5 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisTestSupport.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisTestSupport.java @@ -57,7 +57,7 @@ public GeomCodec codec() { return new GeomCodec() { @Override public Geometry toGeometry(Object in) { - return PGGeometryTypeDescriptor.INSTANCE_WKB_1.toGeometry( in ); + return PGGeometryTypeDescriptor.INSTANCE_WKB_2.toGeometry( in ); } @Override diff --git a/hibernate-spatial/src/test/resources/hibernate.properties b/hibernate-spatial/src/test/resources/hibernate.properties index 41d8adcfec7a..7c6759a22e33 100644 --- a/hibernate-spatial/src/test/resources/hibernate.properties +++ b/hibernate-spatial/src/test/resources/hibernate.properties @@ -6,7 +6,6 @@ # See the lgpl.txt file in the root directory or . # # Default unit/integration test config. - hibernate.dialect @db.dialect@ hibernate.connection.driver_class @jdbc.driver@ hibernate.connection.url @jdbc.url@ @@ -27,12 +26,15 @@ hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFa #hibernate.jdbc.batch_versioned_data true ## Configs for spatial databases (used during testing on local dev environment). # -# hibernate.dialect org.hibernate.dialect.PostgreSQLDialect -# hibernate.connection.driver_class org.postgresql.Driver -# hibernate.connection.url jdbc:postgresql://localhost:5432/hibernate_orm_test?preparedStatementCacheQueries=0 -# hibernate.connection.username hibernate_orm_test -hibernate.connection.password hibernate_orm_test -## +#hibernate.dialect org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect + +#hibernate.dialect org.hibernate.dialect.PostgreSQLDialect +#hibernate.connection.driver_class org.postgresql.Driver +#hibernate.connection.url jdbc:postgresql://localhost:5432/hibernate_orm_test?preparedStatementCacheQueries=0 +#hibernate.connection.username hibernate_orm_test +#hibernate.connection.password hibernate_orm_test + +>>>>>>> 5aae4945cb (Update CockroachDB support for 6) ## GeoDb (H2 spatial extension) ## #hibernate.dialect org.hibernate.spatial.dialect.h2geodb.GeoDBDialect @@ -104,3 +106,12 @@ hibernate.connection.password hibernate_orm_test #hibernate.spatial.db2.srid 4326 #hibernate.connection.username db2inst1 #hibernate.connection.password dbinst1-pwd + +## +## CockroachDB +## +#hibernate.dialect org.hibernate.dialect.CockroachDialect +#hibernate.connection.driver_class org.postgresql.Driver +#hibernate.connection.url jdbc:postgresql://localhost:26257/defaultdb?sslmode=disable&preparedStatementCacheQueries=0 +#hibernate.connection.username root +#hibernate.connection.password \ No newline at end of file