PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/SqlServerSupport.java

https://github.com/sebersole/hibernate-core
Java | 117 lines | 89 code | 19 blank | 9 comment | 2 complexity | e959c4b64d57455fa439d309b8f647b2 MD5 | raw file
  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.spatial.dialect.sqlserver;
  8. import java.io.Serializable;
  9. import java.util.Map;
  10. import org.hibernate.boot.model.TypeContributions;
  11. import org.hibernate.dialect.function.SQLFunction;
  12. import org.hibernate.service.ServiceRegistry;
  13. import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor;
  14. import org.hibernate.spatial.GeolatteGeometryType;
  15. import org.hibernate.spatial.JTSGeometryJavaTypeDescriptor;
  16. import org.hibernate.spatial.JTSGeometryType;
  17. import org.hibernate.spatial.SpatialDialect;
  18. import org.hibernate.spatial.SpatialFunction;
  19. import org.hibernate.spatial.SpatialRelation;
  20. /**
  21. * Created by Karel Maesen, Geovise BVBA on 19/09/2018.
  22. */
  23. class SqlServerSupport implements SpatialDialect, Serializable {
  24. private SqlServerFunctions functions = new SqlServerFunctions();
  25. Iterable<? extends Map.Entry<String, SQLFunction>> functionsToRegister() {
  26. return functions;
  27. }
  28. void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
  29. typeContributions.contributeType( new GeolatteGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
  30. typeContributions.contributeType( new JTSGeometryType( SqlServer2008GeometryTypeDescriptor.INSTANCE ) );
  31. typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
  32. typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
  33. }
  34. @Override
  35. public String getSpatialRelateSQL(String columnName, int spatialRelation) {
  36. final String stfunction;
  37. switch ( spatialRelation ) {
  38. case SpatialRelation.WITHIN:
  39. stfunction = "STWithin";
  40. break;
  41. case SpatialRelation.CONTAINS:
  42. stfunction = "STContains";
  43. break;
  44. case SpatialRelation.CROSSES:
  45. stfunction = "STCrosses";
  46. break;
  47. case SpatialRelation.OVERLAPS:
  48. stfunction = "STOverlaps";
  49. break;
  50. case SpatialRelation.DISJOINT:
  51. stfunction = "STDisjoint";
  52. break;
  53. case SpatialRelation.INTERSECTS:
  54. stfunction = "STIntersects";
  55. break;
  56. case SpatialRelation.TOUCHES:
  57. stfunction = "STTouches";
  58. break;
  59. case SpatialRelation.EQUALS:
  60. stfunction = "STEquals";
  61. break;
  62. default:
  63. throw new IllegalArgumentException(
  64. "Spatial relation is not known by this dialect"
  65. );
  66. }
  67. return columnName + "." + stfunction + "(?) = 1";
  68. }
  69. @Override
  70. public String getSpatialFilterExpression(String columnName) {
  71. return columnName + ".Filter(?) = 1";
  72. }
  73. @Override
  74. public String getSpatialAggregateSQL(String columnName, int aggregation) {
  75. throw new UnsupportedOperationException( "No spatial aggregate SQL functions." );
  76. }
  77. @Override
  78. public String getDWithinSQL(String columnName) {
  79. throw new UnsupportedOperationException( "SQL Server has no DWithin function." );
  80. }
  81. @Override
  82. public String getHavingSridSQL(String columnName) {
  83. return columnName + ".STSrid = (?)";
  84. }
  85. @Override
  86. public String getIsEmptySQL(String columnName, boolean isEmpty) {
  87. final String base = "(" + columnName + ".STIsEmpty() ";
  88. return isEmpty ? base + " = 1 )" : base + " = 0 )";
  89. }
  90. @Override
  91. public boolean supportsFiltering() {
  92. return true;
  93. }
  94. @Override
  95. public boolean supports(SpatialFunction function) {
  96. return ( functions.get( function.toString() ) != null );
  97. }
  98. }