HHH-10148 - SpatialDialect for SqlServer2012

This commit is contained in:
Karel Maesen 2018-09-19 23:37:30 +02:00
parent 59749984cb
commit f68d08abc1
22 changed files with 445 additions and 117 deletions

View File

@ -10,7 +10,7 @@ hibernate.dialect org.hibernate.spatial.dialect.db2.DB2SpatialDialect
hibernate.connection.driver_class com.ibm.db2.jcc.DB2Driver
hibernate.connection.url jdbc:db2://localhost:50000/hibern8
hibernate.connection.username db2inst1
hibernate.connection.password dbinst1-pwd
hibernate.connection.password password
hibernate.connection.pool_size 5

View File

@ -7,7 +7,7 @@
hibernate.dialect org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://hibern8.cctaez8ywvn2.eu-west-1.rds.amazonaws.com:3306/hibernate_orm_test
hibernate.connection.url jdbc:mysql://localhost:3306/hibern8
hibernate.connection.username hibernateormtest
hibernate.connection.password hibernateormtest

View File

@ -4,4 +4,4 @@
* 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>.
*/
jdbcDependency "com.oracle.jdbc:ojdbc6:11.1.0.7.0"
jdbcDependency "mysql:mysql-connector-java:8.0.12"

View File

@ -0,0 +1,25 @@
#
# 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>.
#
hibernate.dialect org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/hibern8
hibernate.connection.username hibernateormtest
hibernate.connection.password hibernateormtest
hibernate.connection.pool_size 5
hibernate.show_sql true
hibernate.format_sql true
hibernate.max_fetch_depth 5
hibernate.cache.region_prefix hibernate.test
hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory

View File

@ -4,4 +4,4 @@
* 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>.
*/
jdbcDependency "com.oracle.jdbc:ojdbc6:11.1.0.7.0"
jdbcDependency "com.oracle:ojdbc7:12.1.0.2.0"

View File

@ -7,7 +7,7 @@
hibernate.dialect org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.url jdbc:oracle:thin:@localhost:1521/orcl12c
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:ORCLCDB
hibernate.connection.username C##hibernate
hibernate.connection.password hibernate

View File

@ -4,4 +4,4 @@
* 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>.
*/
jdbcDependency "com.oracle.jdbc:ojdbc6:11.1.0.7.0"
jdbcDependency "com.oracle:ojdbc7:12.1.0.2.0"

View File

@ -7,7 +7,7 @@
hibernate.dialect org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.url jdbc:oracle:thin:@localhost:1521/orcl12c
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:ORCLCDB
hibernate.connection.username C##hibernate
hibernate.connection.password hibernate

View File

@ -0,0 +1,8 @@
/*
* 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>.
*/
jdbcDependency "com.oracle:ojdbc7:12.1.0.2.0"

View File

@ -7,7 +7,7 @@
hibernate.dialect org.hibernate.spatial.dialect.oracle.OracleSpatialSDO10gDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.url jdbc:oracle:thin:@localhost:1521/orcl12c
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:ORCLCDB
hibernate.connection.username C##hibernate
hibernate.connection.password hibernate

View File

@ -8,9 +8,9 @@
hibernate.test.new_metadata_mappings = true
hibernate.dialect org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect
hibernate.connection.driver_class org.postgresql.Driver
hibernate.connection.url jdbc:postgresql://hbpg96.cctaez8ywvn2.eu-west-1.rds.amazonaws.com:5432/hibernate
hibernate.connection.username hibbrtru
hibernate.connection.password QilTygcxHwk1
hibernate.connection.url jdbc:postgresql://localhost:9432
hibernate.connection.username hibern8
hibernate.connection.password hibern8
hibernate.connection.pool_size 5

View File

@ -9,7 +9,7 @@ hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDi
hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url jdbc:sqlserver://localhost:1433;databaseName=TestDb
hibernate.connection.username hibern8
hibernate.connection.password hibern8Pass
hibernate.connection.password langpaswoord123A%1
hibernate.connection.pool_size 5

View File

@ -0,0 +1,11 @@
/*
* 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>.
*/
repositories {
mavenLocal( )
}
jdbcDependency 'com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8'

View File

@ -0,0 +1,23 @@
#
# 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>.
#
hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2012SpatialDialect
hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url jdbc:sqlserver://localhost:1433;databaseName=TestDb
hibernate.connection.username hibern8
hibernate.connection.password hibern8Pass
hibernate.connection.pool_size 5
hibernate.show_sql true
hibernate.format_sql true
hibernate.max_fetch_depth 5
hibernate.cache.region_prefix hibernate.test
hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory

View File

@ -0,0 +1,78 @@
#!/usr/bin/env bash
WD=$(dirname $0)
docker run -h db2server_db2_22 \
--name db2_11 \
-p 50000:50000 -p 55000:55000\
--privileged=true \
--env-file db2_11_env \
-v ${WD}/../../../hibernate-spatial-docker-dbs/db2_11 \
--detach ibmcom/db2express-c:latest
#The followin steps need to be executed
#(This will need to end up in a script)
#
## First connect to the docker and switch to user db2inst
# docker exec -i -t db2 /bin/bash
#
# su - db2inst1
#
## create the database with 8K pagesize , connect to it and enable
#
#db2 create database hibern8 pagesize 8 k
#db2 connect to hibern8
#db2se enable_db hibern8
## generate the ewkt.sql script:
#cat > ewkt.sql <<EOF
#create or replace function db2gse.asewkt(geometry db2gse.st_geometry)
#returns clob(2G)
#specific db2gse.asewkt1
#language sql
#deterministic
#no external action
#reads sql data
#return 'srid=' || varchar(db2gse.st_srsid(geometry)) || ';' || db2gse.st_astext(geometry);
#
#create or replace function db2gse.geomfromewkt(instring varchar(32000))
#returns db2gse.st_geometry
#specific db2gse.fromewkt1
#language sql
#deterministic
#no external action
#reads sql data
#return db2gse.st_geometry(
#substr(instring,posstr(instring,';')+1, length(instring) - posstr(instring,';')),
#integer(substr(instring,posstr(instring,'=')+1,posstr(instring,';')-(posstr(instring,'=')+1))));
#
#create transform for db2gse.st_geometry ewkt (
# from sql with function db2gse.asewkt(db2gse.st_geometry),
# to sql with function db2gse.geomfromewkt(varchar(32000)) );
#
#drop transform db2_program for db2gse.st_geometry;
#create transform for db2gse.st_geometry db2_program (
# from sql with function db2gse.asewkt(db2gse.st_geometry),
# to sql with function db2gse.geomfromewkt(varchar(32000)) );
#EOF
## run the ewkt.sql script (see hibernate documentation)
#db2 -tvf ./ewkt.sql
### generate the EPSPG:4326 SRS
#db2se create_srs hibern8 \
# -srsName EPSG4326 \
# -srsId 4326 \
# -coordsysName GCS_WGS_1984 \
# -xOffset -180 \
# -xScale 1000000 \
# -yOffset -90 \
# -zOffset 0 \
# -zScale 1 \
# -mOffset 0 \
# -mScale 1
#

View File

@ -0,0 +1,15 @@
LICENSE=accept
DB2INSTANCE=db2inst1
DB2INST1_PASSWORD=Eeyoim4Y
DBNAME=
BLU=false
ENABLE_ORACLE_COMPATIBILITY=false
UPDATEAVAIL=NO
TO_CREATE_SAMPLEDB=false
REPODB=false
IS_OSXFS=false
PERSISTENT_HOME=true
HADR_ENABLED=false
ETCD_ENDPOINT=
ETCD_USERNAME=
ETCD_PASSWORD=

View File

@ -8,8 +8,11 @@
package org.hibernate.spatial.dialect.sqlserver;
import java.util.Map;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor;
@ -28,74 +31,20 @@ import org.hibernate.type.StandardBasicTypes;
*/
public class SqlServer2008SpatialDialect extends SQLServer2008Dialect implements SpatialDialect {
/**
* The short name for this dialect
*/
public static final String SHORT_NAME = "sqlserver";
/**
* Constructs an instance
*/
final private SqlServerSupport support = new SqlServerSupport();
public SqlServer2008SpatialDialect() {
super();
registerColumnType(
SqlServer2008GeometryTypeDescriptor.INSTANCE.getSqlType(),
"GEOMETRY"
);
// registering OGC functions
// (spec_simplefeatures_sql_99-04.pdf)
for ( Map.Entry<String, SQLFunction> entry : support.functionsToRegister() ) {
registerFunction( entry.getKey(), entry.getValue() );
}
// section 2.1.1.1
// Registerfunction calls for registering geometry functions:
// first argument is the OGC standard functionname,
// second the Function as it occurs in the spatial dialect
registerFunction( "dimension", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "?1.STDimension()" ) );
registerFunction( "geometrytype", new SQLFunctionTemplate( StandardBasicTypes.STRING, "?1.STGeometryType()" ) );
registerFunction( "srid", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "?1.STSrid" ) );
registerFunction( "envelope", new SqlServerMethod( "STEnvelope" ) );
registerFunction( "astext", new SQLFunctionTemplate( StandardBasicTypes.STRING, "?1.STAsText()" ) );
registerFunction( "asbinary", new SQLFunctionTemplate( StandardBasicTypes.BINARY, "?1.STAsBinary()" ) );
registerFunction( "isempty", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIsEmpty()" ) );
registerFunction( "issimple", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIsSimple()" ) );
registerFunction( "boundary", new SqlServerMethod( "STBoundary" ) );
// section 2.1.1.2
// Register functions for spatial relation constructs
registerFunction( "contains", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STContains(?2)" ) );
registerFunction( "crosses", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STCrosses(?2)" ) );
registerFunction( "disjoint", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STDisjoint(?2)" ) );
registerFunction( "equals", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STEquals(?2)" ) );
registerFunction( "intersects", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIntersects(?2)" ) );
registerFunction( "overlaps", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STOverlaps(?2)" ) );
registerFunction( "touches", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STTouches(?2)" ) );
registerFunction( "within", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STWithin(?2)" ) );
registerFunction( "relate", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STRelate(?2,?3)" ) );
// section 2.1.1.3
// Register spatial analysis functions.
registerFunction( "distance", new SQLFunctionTemplate( StandardBasicTypes.DOUBLE, "?1.STDistance(?2)" ) );
registerFunction( "buffer", new SqlServerMethod( "STBuffer" ) );
registerFunction( "convexhull", new SqlServerMethod( "STConvexHull" ) );
registerFunction( "difference", new SqlServerMethod( "STDifference" ) );
registerFunction( "intersection", new SqlServerMethod( "STIntersection" ) );
registerFunction( "symdifference", new SqlServerMethod( "STSymDifference" ) );
registerFunction( "geomunion", new SqlServerMethod( "STUnion" ) );
// we rename OGC union to geomunion because union is a reserved SQL keyword.
// (See also postgis documentation).
// portable spatial aggregate functions
// no aggregatefunctions implemented in sql-server2000
//registerFunction("extent", new SQLFunctionTemplate(geomType, "?1.STExtent()"));
// section 2.1.9.1 methods on surfaces
registerFunction( "area", new SQLFunctionTemplate( StandardBasicTypes.DOUBLE, "?1.STArea()" ) );
registerFunction( "centroid", new SqlServerMethod( "STCentroid" ) );
registerFunction(
"pointonsurface", new SqlServerMethod( "STPointOnSurface" )
);
}
@Override
@ -104,83 +53,48 @@ public class SqlServer2008SpatialDialect extends SQLServer2008Dialect implements
typeContributions,
serviceRegistry
);
typeContributions.contributeType( new GeolatteGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
typeContributions.contributeType( new JTSGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
support.contributeTypes( typeContributions, serviceRegistry );
}
@Override
public String getSpatialRelateSQL(String columnName, int spatialRelation) {
final String stfunction;
switch ( spatialRelation ) {
case SpatialRelation.WITHIN:
stfunction = "STWithin";
break;
case SpatialRelation.CONTAINS:
stfunction = "STContains";
break;
case SpatialRelation.CROSSES:
stfunction = "STCrosses";
break;
case SpatialRelation.OVERLAPS:
stfunction = "STOverlaps";
break;
case SpatialRelation.DISJOINT:
stfunction = "STDisjoint";
break;
case SpatialRelation.INTERSECTS:
stfunction = "STIntersects";
break;
case SpatialRelation.TOUCHES:
stfunction = "STTouches";
break;
case SpatialRelation.EQUALS:
stfunction = "STEquals";
break;
default:
throw new IllegalArgumentException(
"Spatial relation is not known by this dialect"
);
}
return columnName + "." + stfunction + "(?) = 1";
return support.getSpatialRelateSQL( columnName, spatialRelation );
}
@Override
public String getSpatialFilterExpression(String columnName) {
return columnName + ".Filter(?) = 1";
return support.getSpatialFilterExpression( columnName );
}
@Override
public String getSpatialAggregateSQL(String columnName, int aggregation) {
throw new UnsupportedOperationException( "No spatial aggregate SQL functions." );
return support.getSpatialAggregateSQL( columnName, aggregation );
}
@Override
public String getDWithinSQL(String columnName) {
throw new UnsupportedOperationException( "SQL Server has no DWithin function." );
return support.getDWithinSQL( columnName );
}
@Override
public String getHavingSridSQL(String columnName) {
return columnName + ".STSrid = (?)";
return support.getHavingSridSQL( columnName );
}
@Override
public String getIsEmptySQL(String columnName, boolean isEmpty) {
final String base = "(" + columnName + ".STIsEmpty() ";
return isEmpty ? base + " = 1 )" : base + " = 0 )";
return support.getIsEmptySQL( columnName, isEmpty );
}
@Override
public boolean supportsFiltering() {
return true;
return support.supportsFiltering();
}
@Override
public boolean supports(SpatialFunction function) {
return ( getFunctions().get( function.toString() ) != null );
return support.supports( function );
}
}

View File

@ -0,0 +1,80 @@
package org.hibernate.spatial.dialect.sqlserver;
import java.util.Map;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction;
/**
* Created by Karel Maesen, Geovise BVBA on 19/09/2018.
*/
public class SqlServer2012SpatialDialect extends SQLServer2012Dialect implements SpatialDialect {
final private SqlServerSupport support = new SqlServerSupport();
public SqlServer2012SpatialDialect() {
super();
registerColumnType(
SqlServer2008GeometryTypeDescriptor.INSTANCE.getSqlType(),
"GEOMETRY"
);
for ( Map.Entry<String, SQLFunction> entry : support.functionsToRegister() ) {
registerFunction( entry.getKey(), entry.getValue() );
}
}
@Override
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
super.contributeTypes(
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
}
@Override
public String getSpatialRelateSQL(String columnName, int spatialRelation) {
return support.getSpatialRelateSQL( columnName, spatialRelation );
}
@Override
public String getSpatialFilterExpression(String columnName) {
return support.getSpatialFilterExpression( columnName );
}
@Override
public String getSpatialAggregateSQL(String columnName, int aggregation) {
return support.getSpatialAggregateSQL( columnName, aggregation );
}
@Override
public String getDWithinSQL(String columnName) {
return support.getDWithinSQL( columnName );
}
@Override
public String getHavingSridSQL(String columnName) {
return support.getHavingSridSQL( columnName );
}
@Override
public String getIsEmptySQL(String columnName, boolean isEmpty) {
return support.getIsEmptySQL( columnName, isEmpty );
}
@Override
public boolean supportsFiltering() {
return support.supportsFiltering();
}
@Override
public boolean supports(SpatialFunction function) {
return support.supports( function );
}
}

View File

@ -0,0 +1,60 @@
package org.hibernate.spatial.dialect.sqlserver;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.type.StandardBasicTypes;
/**
* Created by Karel Maesen, Geovise BVBA on 19/09/2018.
*/
class SqlServerFunctions extends SpatialFunctionsRegistry {
public SqlServerFunctions() {
put( "dimension", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "?1.STDimension()" ) );
put( "geometrytype", new SQLFunctionTemplate( StandardBasicTypes.STRING, "?1.STGeometryType()" ) );
put( "srid", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "?1.STSrid" ) );
put( "envelope", new SqlServerMethod( "STEnvelope" ) );
put( "astext", new SQLFunctionTemplate( StandardBasicTypes.STRING, "?1.STAsText()" ) );
put( "asbinary", new SQLFunctionTemplate( StandardBasicTypes.BINARY, "?1.STAsBinary()" ) );
put( "isempty", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIsEmpty()" ) );
put( "issimple", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIsSimple()" ) );
put( "boundary", new SqlServerMethod( "STBoundary" ) );
// section 2.1.1.2
// Register functions for spatial relation constructs
put( "contains", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STContains(?2)" ) );
put( "crosses", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STCrosses(?2)" ) );
put( "disjoint", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STDisjoint(?2)" ) );
put( "equals", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STEquals(?2)" ) );
put( "intersects", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STIntersects(?2)" ) );
put( "overlaps", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STOverlaps(?2)" ) );
put( "touches", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STTouches(?2)" ) );
put( "within", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STWithin(?2)" ) );
put( "relate", new SQLFunctionTemplate( StandardBasicTypes.BOOLEAN, "?1.STRelate(?2,?3)" ) );
// section 2.1.1.3
// Register spatial analysis functions.
put( "distance", new SQLFunctionTemplate( StandardBasicTypes.DOUBLE, "?1.STDistance(?2)" ) );
put( "buffer", new SqlServerMethod( "STBuffer" ) );
put( "convexhull", new SqlServerMethod( "STConvexHull" ) );
put( "difference", new SqlServerMethod( "STDifference" ) );
put( "intersection", new SqlServerMethod( "STIntersection" ) );
put( "symdifference", new SqlServerMethod( "STSymDifference" ) );
put( "geomunion", new SqlServerMethod( "STUnion" ) );
// we rename OGC union to geomunion because union is a reserved SQL keyword.
// (See also postgis documentation).
// portable spatial aggregate functions
// no aggregatefunctions implemented in sql-server2000
//put("extent", new SQLFunctionTemplate(geomType, "?1.STExtent()"));
// section 2.1.9.1 methods on surfaces
put( "area", new SQLFunctionTemplate( StandardBasicTypes.DOUBLE, "?1.STArea()" ) );
put( "centroid", new SqlServerMethod( "STCentroid" ) );
put(
"pointonsurface", new SqlServerMethod( "STPointOnSurface" )
);
}
}

View File

@ -0,0 +1,111 @@
package org.hibernate.spatial.dialect.sqlserver;
import java.io.Serializable;
import java.util.Map;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.function.SQLFunction;
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.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
/**
* Created by Karel Maesen, Geovise BVBA on 19/09/2018.
*/
class SqlServerSupport implements SpatialDialect, Serializable {
private SqlServerFunctions functions = new SqlServerFunctions();
Iterable<? extends Map.Entry<String, SQLFunction>> functionsToRegister() {
return functions;
}
void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
typeContributions.contributeType( new GeolatteGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
typeContributions.contributeType( new JTSGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
}
@Override
public String getSpatialRelateSQL(String columnName, int spatialRelation) {
final String stfunction;
switch ( spatialRelation ) {
case SpatialRelation.WITHIN:
stfunction = "STWithin";
break;
case SpatialRelation.CONTAINS:
stfunction = "STContains";
break;
case SpatialRelation.CROSSES:
stfunction = "STCrosses";
break;
case SpatialRelation.OVERLAPS:
stfunction = "STOverlaps";
break;
case SpatialRelation.DISJOINT:
stfunction = "STDisjoint";
break;
case SpatialRelation.INTERSECTS:
stfunction = "STIntersects";
break;
case SpatialRelation.TOUCHES:
stfunction = "STTouches";
break;
case SpatialRelation.EQUALS:
stfunction = "STEquals";
break;
default:
throw new IllegalArgumentException(
"Spatial relation is not known by this dialect"
);
}
return columnName + "." + stfunction + "(?) = 1";
}
@Override
public String getSpatialFilterExpression(String columnName) {
return columnName + ".Filter(?) = 1";
}
@Override
public String getSpatialAggregateSQL(String columnName, int aggregation) {
throw new UnsupportedOperationException( "No spatial aggregate SQL functions." );
}
@Override
public String getDWithinSQL(String columnName) {
throw new UnsupportedOperationException( "SQL Server has no DWithin function." );
}
@Override
public String getHavingSridSQL(String columnName) {
return columnName + ".STSrid = (?)";
}
@Override
public String getIsEmptySQL(String columnName, boolean isEmpty) {
final String base = "(" + columnName + ".STIsEmpty() ";
return isEmpty ? base + " = 1 )" : base + " = 0 )";
}
@Override
public boolean supportsFiltering() {
return true;
}
@Override
public boolean supports(SpatialFunction function) {
return ( functions.get( function.toString() ) != null );
}
}

View File

@ -48,6 +48,9 @@ public class TestSupportFactories {
if ( "org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect".equals( canonicalName ) ) {
return SQLServerTestSupport.class;
}
if ( "org.hibernate.spatial.dialect.sqlserver.SqlServer2012SpatialDialect".equals( canonicalName ) ) {
return SQLServerTestSupport.class;
}
if ( "org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect".equals( canonicalName ) ||
"org.hibernate.spatial.dialect.mysql.MySQL5InnoDBSpatialDialect".equals( canonicalName ) ) {
return MySQLTestSupport.class;