HHH-12608 Support for ST_Dwithin() in DB2

This commit is contained in:
Karel Maesen 2018-08-12 17:08:06 +02:00 committed by Guillaume Smet
parent 7eb0cee178
commit 6eaaa49faa
2 changed files with 28 additions and 2 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.db2; package org.hibernate.spatial.dialect.db2;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -14,6 +15,7 @@ import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor; import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor;
import org.hibernate.spatial.GeolatteGeometryType; import org.hibernate.spatial.GeolatteGeometryType;
@ -25,6 +27,9 @@ import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction; import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation; import org.hibernate.spatial.SpatialRelation;
import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import static java.lang.String.format;
/** /**
* @author David Adler, Adtech Geospatial * @author David Adler, Adtech Geospatial
@ -223,6 +228,7 @@ public class DB2SpatialDialect extends DB2Dialect implements SpatialDialect {
) ); ) );
// Register non-SFS functions listed in Hibernate Spatial // Register non-SFS functions listed in Hibernate Spatial
registerFunction( "dwithin", new DWithinFunction());
// // The srid parameter needs to be explicitly cast to INTEGER to avoid a -245 SQLCODE, // // The srid parameter needs to be explicitly cast to INTEGER to avoid a -245 SQLCODE,
// // ambiguous parameter. // // ambiguous parameter.
@ -244,7 +250,7 @@ public class DB2SpatialDialect extends DB2Dialect implements SpatialDialect {
@Override @Override
public String getDWithinSQL(String columnName) { public String getDWithinSQL(String columnName) {
return "db2gse.ST_Distance(" + columnName + ",?) < ?"; return "db2gse.ST_Intersects(" + columnName + ", db2gse.ST_Buffer(?, ?, 'METER')) = 1";
} }
@Override @Override
@ -317,4 +323,24 @@ public class DB2SpatialDialect extends DB2Dialect implements SpatialDialect {
public boolean supportsFiltering() { public boolean supportsFiltering() {
return false; return false;
} }
private static class DWithinFunction extends StandardSQLFunction {
public DWithinFunction() {
super( "db2gse.ST_Dwithin" , StandardBasicTypes.NUMERIC_BOOLEAN);
}
public String render(Type firstArgumentType, final List args, final SessionFactoryImplementor factory) {
StringBuilder sb = new StringBuilder( "db2gse.ST_Intersects( " );
sb.append( (String)args.get(0) ) //
.append(", db2gse.ST_Buffer(")
.append((String)args.get(1) )
.append(", ")
.append((String)args.get(2) )
.append(", 'METER'))");
return sb.toString();
}
}
} }

View File

@ -88,7 +88,7 @@ public class DB2ExpectationsFactory extends AbstractExpectationsFactory {
@Override @Override
protected NativeSQLStatement createNativeDwithinStatement(Point geom, double distance) { protected NativeSQLStatement createNativeDwithinStatement(Point geom, double distance) {
String sql = "select t.id, DB2GSE.ST_dwithin(t.geom, DB2GSE.ST_GeomFromText(?, 4326), " + distance + " ) from GeomTest t where DB2GSE.ST_dwithin(t.geom, DB2GSE.ST_GeomFromText(?, 4326), " + distance + ") = 1 and db2gse.st_srid(t.geom) = 4326"; String sql = "select t.id, DB2GSE.ST_dwithin(DB2GSE.ST_GeomFromText(?, 4326), t.geom, " + distance + " , 'METER') from GeomTest t where DB2GSE.ST_dwithin(DB2GSE.ST_GeomFromText(?, 4326), t.geom, " + distance + ", 'METER') = 1 and db2gse.st_srid(t.geom) = 4326";
return createNativeSQLStatementAllWKTParams( sql, geom.toText() ); return createNativeSQLStatementAllWKTParams( sql, geom.toText() );
} }