HHH-14805 Fix test class for Sql Server

This commit is contained in:
Karel Maesen 2022-01-23 17:04:36 +01:00
parent 4b7b6135fe
commit a35669f11b
3 changed files with 47 additions and 29 deletions

View File

@ -26,6 +26,8 @@ import org.hibernate.type.BasicTypeReference;
import org.hibernate.type.BasicTypeRegistry;
import org.hibernate.type.StandardBasicTypes;
import static org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers.useFirstNonNull;
public class SqlServerSqmFunctionDescriptors extends BaseSqmFunctionDescriptors {
private final BasicTypeRegistry typeRegistry;
@ -47,7 +49,10 @@ public class SqlServerSqmFunctionDescriptors extends BaseSqmFunctionDescriptors
new Method( "STDimension", exactly( 1 ), invariant( StandardBasicTypes.INTEGER ) )
);
map.put( CommonSpatialFunction.ST_ENVELOPE.getKey(), new Method( "STEnvelope", exactly( 1 ), sameAsFirst() ) );
map.put(
CommonSpatialFunction.ST_ENVELOPE.getKey(),
new Method( "STEnvelope", exactly( 1 ), useFirstNonNull() )
);
map.put(
CommonSpatialFunction.ST_SRID.getKey(),
@ -69,7 +74,10 @@ public class SqlServerSqmFunctionDescriptors extends BaseSqmFunctionDescriptors
new Method( "STIsSimple", exactly( 1 ), invariant( StandardBasicTypes.BOOLEAN ) )
);
map.put( CommonSpatialFunction.ST_BOUNDARY.getKey(), new Method( "STBoundary", exactly( 1 ), sameAsFirst() ) );
map.put(
CommonSpatialFunction.ST_BOUNDARY.getKey(),
new Method( "STBoundary", exactly( 1 ), useFirstNonNull() )
);
map.put(
CommonSpatialFunction.ST_OVERLAPS.getKey(),
new Method( "STOverlaps", exactly( 2 ), invariant( StandardBasicTypes.BOOLEAN ) )
@ -111,25 +119,30 @@ public class SqlServerSqmFunctionDescriptors extends BaseSqmFunctionDescriptors
new Method( "STDistance", exactly( 2 ), invariant( StandardBasicTypes.DOUBLE ) )
);
map.put( CommonSpatialFunction.ST_BUFFER.getKey(), new Method( "STBuffer", exactly( 2 ), sameAsFirst() ) );
map.put(
CommonSpatialFunction.ST_BUFFER.getKey(),
new Method( "STBuffer", exactly( 2 ),
useFirstNonNull()
)
);
map.put(
CommonSpatialFunction.ST_CONVEXHULL.getKey(),
new Method( "STConvexHull", exactly( 1 ), sameAsFirst() )
new Method( "STConvexHull", exactly( 1 ), useFirstNonNull() )
);
map.put(
CommonSpatialFunction.ST_DIFFERENCE.getKey(),
new Method( "STDifference", exactly( 2 ), sameAsFirst() )
new Method( "STDifference", exactly( 2 ), useFirstNonNull() )
);
map.put(
CommonSpatialFunction.ST_INTERSECTION.getKey(),
new Method( "STIntersection", exactly( 2 ), sameAsFirst() )
new Method( "STIntersection", exactly( 2 ), useFirstNonNull() )
);
map.put(
CommonSpatialFunction.ST_SYMDIFFERENCE.getKey(),
new Method( "STSymDifference", exactly( 2 ), sameAsFirst() )
new Method( "STSymDifference", exactly( 2 ), useFirstNonNull() )
);
map.put( CommonSpatialFunction.ST_UNION.getKey(), new Method( "STUnion", exactly( 2 ), sameAsFirst() ) );
map.put( CommonSpatialFunction.ST_UNION.getKey(), new Method( "STUnion", exactly( 2 ), useFirstNonNull() ) );
map.put(
CommonSpatialFunction.ST_RELATE.getKey(),
new Method( "STRelate", exactly( 3 ), invariant( StandardBasicTypes.BOOLEAN ) )
@ -146,10 +159,6 @@ public class SqlServerSqmFunctionDescriptors extends BaseSqmFunctionDescriptors
return StandardFunctionReturnTypeResolvers.invariant( typeRegistry.resolve( tpe ) );
}
FunctionReturnTypeResolver sameAsFirst() {
return StandardFunctionReturnTypeResolvers.useArgType( 1 );
}
}
class Property extends NamedSqmFunctionDescriptor {

View File

@ -5,10 +5,18 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.integration.functions;
/*
* 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.oracle;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.spatial.HibernateSpatialConfigurationSettings;
import org.hibernate.spatial.integration.functions.CommonFunctionTests;
import org.hibernate.spatial.testing.dialects.oracle.OracleSTNativeSqlTemplates;
import org.hibernate.testing.orm.junit.RequiresDialect;
@ -18,11 +26,11 @@ import org.hibernate.testing.orm.junit.Setting;
/**
* Only for Oracle: run the tests in "OGC_STRICT" mode (i.e. using the SQL MultiMedia functions)
*/
@RequiresDialect( value = OracleDialect.class)
@RequiresDialect(value = OracleDialect.class)
@ServiceRegistry(settings = {
@Setting(name = HibernateSpatialConfigurationSettings.ORACLE_OGC_STRICT, value = "true")
})
public class OracleSQLMMFunctionTests extends CommonFunctionTests{
public class OracleSQLMMFunctionTests extends CommonFunctionTests {
public OracleSQLMMFunctionTests() {
this.templates = new OracleSTNativeSqlTemplates();
}

View File

@ -12,6 +12,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.SpatialTestBase;
@ -20,16 +22,10 @@ import org.hibernate.spatial.testing.domain.GeomEntity;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.polygon;
import static org.geolatte.geom.builder.DSL.ring;
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
@ -63,13 +59,10 @@ public class TestGeometryConstructionWithParameter extends SpatialTestBase {
}
@TestFactory
@Disabled("Broken because of NULL argument return type")
public Stream<DynamicTest> testFunctions() {
return Arrays.stream( CommonSpatialFunction.values() )
.filter( f ->
f.getType() == CommonSpatialFunction.Type.CONSTRUCTION &&
isSupported( f ) &&
templateAvailable( f ) )
.filter( f -> f.getType() == CommonSpatialFunction.Type.CONSTRUCTION && isSupported( f ) && templateAvailable(
f ) )
.map( this::buildTestFunction );
}
@ -82,14 +75,22 @@ public class TestGeometryConstructionWithParameter extends SpatialTestBase {
return () -> {
scope.inSession( session -> {
String hql = templates.get( func );
session.createQuery( hql , GeomEntity.class )
.setParameter( "poly", filterGeometry )
.getResultList();
hql = adaptToDialect( session, hql );
session.createQuery( hql, GeomEntity.class ).setParameter( "poly", filterGeometry ).getResultList();
//we just check that this parses for now.
} );
};
}
private String adaptToDialect(SessionImplementor session, String hql) {
//special case for SqlServer.
// the cast ensures that SQL Server interprets the passed object (jdbc byte array) as a geometry
if ( session.getSessionFactory().getJdbcServices().getDialect() instanceof SQLServerDialect ) {
hql = hql.replace( ":poly", "cast( :poly as org.geolatte.geom.Geometry)" );
}
return hql;
}
private String buildName(CommonSpatialFunction func) {
return func.getKey().getName();
}