HHH-14801 Improve registering functions for Postgis

This commit is contained in:
Karel Maesen 2021-08-07 14:22:47 +02:00
parent 35f42528fb
commit 24c5099eb7
8 changed files with 51 additions and 56 deletions

View File

@ -11,32 +11,38 @@ import org.hibernate.type.BasicType;
import org.hibernate.type.StandardBasicTypes;
/**
* TODO -- documentation
* Functions commonly expected in databases, as defined
* by the SQL/MM specs
*
* @author Karel Maesen
*/
public enum CommonSpatialFunction {
ST_ASTEXT( FunctionKey.apply( "st_astext", "astext" ), StandardBasicTypes.STRING ),
ST_GEOMETRYTYPE( FunctionKey.apply( "st_geometrytype", "geometrytype" ), StandardBasicTypes.STRING ),
ST_DIMENSION( FunctionKey.apply( "st_dimension", "dimension" ), StandardBasicTypes.INTEGER ),
ST_SRID( FunctionKey.apply("st_srid", "srid"), StandardBasicTypes.INTEGER),
ST_ENVELOPE( FunctionKey.apply("st_envelope", "envelope"))
ST_ASTEXT( FunctionKey.apply( "st_astext", "astext" ), 1, StandardBasicTypes.STRING ),
ST_GEOMETRYTYPE( FunctionKey.apply( "st_geometrytype", "geometrytype" ), 1, StandardBasicTypes.STRING ),
ST_DIMENSION( FunctionKey.apply( "st_dimension", "dimension" ), 1, StandardBasicTypes.INTEGER ),
ST_SRID( FunctionKey.apply( "st_srid", "srid" ), 1, StandardBasicTypes.INTEGER ),
ST_ENVELOPE( FunctionKey.apply( "st_envelope", "envelope" ), 1 ),
;
private final FunctionKey key;
private final BasicType<?> ReturnType;
private final boolean spatialReturnType;
private final int numArgs;
CommonSpatialFunction(FunctionKey key, BasicType<?> returnType) {
CommonSpatialFunction(FunctionKey key, int numArgs, BasicType<?> returnType) {
this.key = key;
ReturnType = returnType;
spatialReturnType = false;
this.numArgs = numArgs;
}
CommonSpatialFunction(FunctionKey key) {
CommonSpatialFunction(FunctionKey key, int numArgs) {
this.key = key;
ReturnType = null;
spatialReturnType = true;
this.numArgs = numArgs;
}
@ -51,4 +57,8 @@ public enum CommonSpatialFunction {
public boolean returnsGeometry() {
return spatialReturnType;
}
public int getNumArgs() {
return numArgs;
}
}

View File

@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.postgis;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -17,6 +18,7 @@ import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.KeyedSqmFunctionDescriptors;
import org.hibernate.spatial.FunctionKey;
@ -27,46 +29,22 @@ public class PostgisSqmFunctionDescriptors implements KeyedSqmFunctionDescriptor
private final Map<FunctionKey, SqmFunctionDescriptor> map = new HashMap<>();
PostgisSqmFunctionDescriptors(ServiceRegistry serviceRegistry) {
map.put(
ST_GEOMETRYTYPE.getKey(), new NamedSqmFunctionDescriptor(
ST_GEOMETRYTYPE.getKey().getName(),
true,
StandardArgumentsValidators.exactly( 1 ),
StandardFunctionReturnTypeResolvers.invariant( ST_GEOMETRYTYPE.getReturnType() )
)
);
map.put(
ST_ASTEXT.getKey(), new NamedSqmFunctionDescriptor(
ST_ASTEXT.getKey().getName(),
true,
StandardArgumentsValidators.exactly( 1 ),
StandardFunctionReturnTypeResolvers.invariant( ST_ASTEXT.getReturnType() )
)
);
map.put(
ST_DIMENSION.getKey(), new NamedSqmFunctionDescriptor(
ST_DIMENSION.getKey().getName(),
true,
StandardArgumentsValidators.exactly( 1 ),
StandardFunctionReturnTypeResolvers.invariant( ST_DIMENSION.getReturnType() )
)
);
map.put(
ST_ENVELOPE.getKey(), new NamedSqmFunctionDescriptor(
ST_ENVELOPE.getKey().getName(),
true,
StandardArgumentsValidators.exactly( 1 ),
StandardFunctionReturnTypeResolvers.useArgType(1 )
)
);
Arrays.stream( values() )
.forEach( cf -> map.put( cf.getKey(), toPGFunction( cf ) ) );
}
public Map<FunctionKey, SqmFunctionDescriptor> asMap() {
return Collections.unmodifiableMap( map );
}
private SqmFunctionDescriptor toPGFunction(CommonSpatialFunction func) {
return new NamedSqmFunctionDescriptor(
func.getKey().getName(),
true,
StandardArgumentsValidators.exactly( func.getNumArgs() ),
func.getReturnType() != null ? StandardFunctionReturnTypeResolvers.invariant( func.getReturnType() ) :
StandardFunctionReturnTypeResolvers.useArgType( 1 )
);
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
import org.hibernate.spatial.GeomCodec;
import org.hibernate.spatial.testing.JTSGeometryEquality;
import org.hibernate.spatial.testing.NativeSQLTemplates;
import org.hibernate.spatial.testing.dialects.NativeSQLTemplates;
import org.hibernate.spatial.testing.TestSupportFactories;
import org.hibernate.spatial.testing.datareader.TestData;
import org.hibernate.spatial.testing.datareader.TestDataElement;

View File

@ -18,8 +18,7 @@ import java.util.Map;
import java.util.stream.Stream;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.FunctionKey;
import org.hibernate.spatial.testing.NativeSQLTemplates;
import org.hibernate.spatial.testing.dialects.NativeSQLTemplates;
public abstract class TestTemplates {

View File

@ -21,7 +21,7 @@ import org.hibernate.spatial.GeomCodec;
import org.hibernate.spatial.testing.AbstractExpectationsFactory;
import org.hibernate.spatial.testing.DataSourceUtils;
import org.hibernate.spatial.testing.JTSGeometryEquality;
import org.hibernate.spatial.testing.NativeSQLTemplates;
import org.hibernate.spatial.testing.dialects.NativeSQLTemplates;
import org.hibernate.spatial.testing.SQLExpressionTemplate;

View File

@ -5,7 +5,14 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.testing;
/*
* 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.testing.dialects;
import java.util.Collections;
import java.util.HashMap;
@ -22,10 +29,11 @@ public class NativeSQLTemplates {
// Note that we alias the function invocation so that
// we can map the return value to the required type
public NativeSQLTemplates() {
sqls.put( ST_ASTEXT, "select id, st_astext(geom) as result from %s" );
sqls.put( ST_GEOMETRYTYPE, "select id, st_geometrytype(geom) as result from %s" );
sqls.put( ST_DIMENSION, "select id, st_dimension(geom) as result from %s" );
sqls.put( ST_ENVELOPE, "select id, st_envelope(geom) as result from %s" );
sqls.put( ST_ASTEXT, "select id, st_astext(geom) from %s" );
sqls.put( ST_GEOMETRYTYPE, "select id, st_geometrytype(geom) from %s" );
sqls.put( ST_DIMENSION, "select id, st_dimension(geom) from %s" );
sqls.put( ST_ENVELOPE, "select id, st_envelope(geom) from %s" );
sqls.put( ST_SRID, "select id, st_srid(geom) from %s" );
}
public Map<CommonSpatialFunction, String> all() {

View File

@ -7,6 +7,6 @@
package org.hibernate.spatial.testing.dialects.postgis;
import org.hibernate.spatial.testing.NativeSQLTemplates;
import org.hibernate.spatial.testing.dialects.NativeSQLTemplates;
public class PostgisNativeSQLTemplates extends NativeSQLTemplates{}

View File

@ -12,7 +12,7 @@ import org.hibernate.spatial.GeomCodec;
import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor;
import org.hibernate.spatial.testing.AbstractExpectationsFactory;
import org.hibernate.spatial.testing.DataSourceUtils;
import org.hibernate.spatial.testing.NativeSQLTemplates;
import org.hibernate.spatial.testing.dialects.NativeSQLTemplates;
import org.hibernate.spatial.testing.SQLExpressionTemplate;
import org.hibernate.spatial.testing.datareader.TestData;
import org.hibernate.spatial.testing.datareader.TestSupport;