HHH-14802 Update CockroachDB support for 6

This commit is contained in:
Karel Maesen 2021-08-26 21:37:06 +02:00
parent bc44d65eb3
commit 9316c161c7
24 changed files with 155 additions and 707 deletions

View File

@ -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]
====

View File

@ -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',

View File

@ -87,6 +87,9 @@ public class CockroachDialect extends Dialect {
registerColumnType( Types.NCLOB, "string" );
registerColumnType( Types.JAVA_OBJECT, "json" );
//register geometry type
registerColumnType( 5432, "geometry" );
}
@Override

View File

@ -11,21 +11,24 @@ import java.util.HashMap;
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<Class<? extends Dialect>,
Function<ServiceRegistry, ContributorImplementor>> ContributorMap = new HashMap<>();
Function<ServiceRegistry, ContributorImplementor>> 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 @@ class 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;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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" );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
* <code>org.hibernate.spatial.criterion.SpatialFilterExpression</code>s
* 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 );
}
}

View File

@ -27,12 +27,8 @@ public class PostgisDialectContributor implements ContributorImplementor {
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

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
* <p>
* 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<SqlAstNode> 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( " ", "(", ")" ) );
// }
// }
}

View File

@ -11,11 +11,16 @@ import java.util.Map;
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 );
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 .
* <p>
* 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 );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 .
* <p>
* 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 );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 .
* <p>
* 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 );
}
}

View File

@ -28,7 +28,7 @@ public class PostgisSqmFunctionDescriptors implements KeyedSqmFunctionDescriptor
private final Map<FunctionKey, SqmFunctionDescriptor> map = new HashMap<>();
PostgisSqmFunctionDescriptors(ServiceRegistry serviceRegistry) {
public PostgisSqmFunctionDescriptors(ServiceRegistry serviceRegistry) {
Arrays.stream( values() )
.forEach( cf -> map.put( cf.getKey(), toPGFunction( cf ) ) );
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
* <code>org.hibernatespatial.criterion.SpatialRelateExpression</code>s
* into prepared statements.
* <p/>
*
* @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
* <code>SpatialRelation</code>).
*
* @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
* <code>org.hibernate.spatial.criterion.SpatialFilterExpression</code>s
* 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 <code>SpatialAggregate</code>
*
* @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 <code>DWithinExpression</code>.
*
* @param columnName The geometry column to test against
*
* @return The SQL fragment when parsing a <code>DWithinExpression</code>.
*/
public String getDWithinSQL(String columnName) {
return "ST_DWithin(" + columnName + ",?,?)";
}
/**
* Returns the SQL fragment when parsing a <code>HavingSridExpression</code>.
*
* @param columnName The geometry column to test against
*
* @return The SQL fragment for a <code>HavingSridExpression</code>.
*/
public String getHavingSridSQL(String columnName) {
return "( ST_srid(" + columnName + ") = ?)";
}
/**
* Returns the SQL fragment when parsing a <code>IsEmptyExpression</code> or
* <code>IsNotEmpty</code> 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 <code>SpatialDialect</code> supports a specific filtering function.
* <p> This is intended to signal DB-support for fast window queries, or MBR-overlap queries.</p>
*
* @return True if filtering is supported
*/
public boolean supportsFiltering() {
return true;
}
/**
* Does this dialect supports the specified <code>SpatialFunction</code>.
*
* @param function <code>SpatialFunction</code>
*
* @return True if this <code>SpatialDialect</code> supports the spatial function specified by the function parameter.
*/
public boolean supports(SpatialFunction function) {
return ( postgisFunctions.get( function.toString() ) != null );
}
}

View File

@ -20,8 +20,6 @@ import javax.persistence.GenerationType;
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.Ignore;
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;

View File

@ -11,6 +11,8 @@ import java.io.File;
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 class TestSpatialSchemaGeneration {
.execute( EnumSet.of( TargetType.SCRIPT ), SchemaExport.Action.BOTH, metadata );
final List<String> 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<String> geomElems = Arrays.asList( "geometry", "geom" );
assertThat( result, stringContainsInOrder( geomElems ) );
}
}

View File

@ -16,6 +16,7 @@ package org.hibernate.spatial.integration.functions;
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 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Dynamic tests for "common" spatial functions.
*
* <p>
* The tests here are dynamic across several dimensions:
* The tests here are dynamic across several dimensions:
* <ul>
* <li>the spatial function (in so far as actually supported by the dialect)</li>
* <li>the Geometry model (JTS or Geolatte)</li>
@ -55,12 +56,22 @@ public class CommonFunctionTests extends SpatialTestDataProvider
implements SessionFactoryScopeAware {
private SessionFactoryScope scope;
private Set<String> 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 class CommonFunctionTests extends SpatialTestDataProvider
@TestFactory
public Stream<DynamicTest> 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 class CommonFunctionTests extends SpatialTestDataProvider
}
private boolean isSupported(FunctionTestTemplate.Builder builder) {
return supportedFunctions.contains( builder.function.name() );
}
protected Stream<DynamicTest> buildTests(FunctionTestTemplate template) {
return Stream.of(
template.getFunctionName(),

View File

@ -17,15 +17,15 @@ import java.util.Objects;
public interface RowObjectMapper<T> {
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 @@ class Data {
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) {

View File

@ -34,7 +34,7 @@ public class RequireFunctionExtension implements ExecutionCondition {
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<Object> testInstance = context.getTestInstance();
if ( testInstance.isEmpty() ) {
if ( !testInstance.isPresent() ) {
return ENABLED;
}

View File

@ -53,7 +53,7 @@ public class TestSupportFactories {
return MariaDBTestSupport.class;
}
if ( ( dialect instanceof SpatialDialect ) && CockroachDialect.class.isAssignableFrom( dialect.getClass() ) ) {
if ( CockroachDialect.class.isAssignableFrom( dialect.getClass() ) ) {
return CockroachDBTestSupport.class;
}

View File

@ -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 class CockroachDBTestSupport extends TestSupport {
}
}
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 );
}
};
}
}

View File

@ -57,7 +57,7 @@ public class PostgisTestSupport extends TestSupport {
return new GeomCodec() {
@Override
public Geometry<?> toGeometry(Object in) {
return PGGeometryTypeDescriptor.INSTANCE_WKB_1.toGeometry( in );
return PGGeometryTypeDescriptor.INSTANCE_WKB_2.toGeometry( in );
}
@Override

View File

@ -6,7 +6,6 @@
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#
# 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