PageRenderTime 77ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/mysql/MySQL56SpatialDialect.java

https://github.com/sebersole/hibernate-core
Java | 152 lines | 105 code | 19 blank | 28 comment | 2 complexity | e038a703f3c6cd13929197c4fd7d888d 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.mysql;
  8. /**
  9. * @author Karel Maesen, Geovise BVBA
  10. * creation-date: 10/9/13
  11. */
  12. import java.util.Map;
  13. import org.hibernate.HibernateException;
  14. import org.hibernate.boot.model.TypeContributions;
  15. import org.hibernate.dialect.MySQL55Dialect;
  16. import org.hibernate.dialect.function.SQLFunction;
  17. import org.hibernate.dialect.function.StandardSQLFunction;
  18. import org.hibernate.service.ServiceRegistry;
  19. import org.hibernate.spatial.SpatialDialect;
  20. import org.hibernate.spatial.SpatialFunction;
  21. import org.hibernate.spatial.SpatialRelation;
  22. import org.hibernate.type.StandardBasicTypes;
  23. import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
  24. /**
  25. * Extends the MySQL5Dialect by including support for the spatial operators.
  26. * <p>
  27. * This <code>SpatialDialect</code> uses the ST_* spatial operators that operate on exact geometries which have been
  28. * added in MySQL version 5.6.1. Previous versions of MySQL only supported operators that operated on Minimum Bounding
  29. * Rectangles (MBR's). This dialect my therefore produce different results than the other MySQL spatial dialects.
  30. *
  31. * @author Karel Maesen
  32. */
  33. public class MySQL56SpatialDialect extends MySQL55Dialect implements SpatialDialect {
  34. private MySQLSpatialDialect dialectDelegate = new MySQLSpatialDialect();
  35. /**
  36. * Constructs the dialect
  37. */
  38. public MySQL56SpatialDialect() {
  39. super();
  40. registerColumnType(
  41. MySQLGeometryTypeDescriptor.INSTANCE.getSqlType(),
  42. "GEOMETRY"
  43. );
  44. final MySQL5SpatialFunctions functionsToRegister = overrideObjectShapeFunctions( new MySQL5SpatialFunctions() );
  45. for ( Map.Entry<String, SQLFunction> entry : functionsToRegister ) {
  46. registerFunction( entry.getKey(), entry.getValue() );
  47. }
  48. }
  49. private MySQL5SpatialFunctions overrideObjectShapeFunctions(MySQL5SpatialFunctions mysqlFunctions) {
  50. mysqlFunctions.put( "contains", new StandardSQLFunction( "ST_Contains", StandardBasicTypes.BOOLEAN ) );
  51. mysqlFunctions.put( "crosses", new StandardSQLFunction( "ST_Crosses", StandardBasicTypes.BOOLEAN ) );
  52. mysqlFunctions.put( "disjoint", new StandardSQLFunction( "ST_Disjoint", StandardBasicTypes.BOOLEAN ) );
  53. mysqlFunctions.put( "equals", new StandardSQLFunction( "ST_Equals", StandardBasicTypes.BOOLEAN ) );
  54. mysqlFunctions.put( "intersects", new StandardSQLFunction( "ST_Intersects", StandardBasicTypes.BOOLEAN ) );
  55. mysqlFunctions.put( "overlaps", new StandardSQLFunction( "ST_Overlaps", StandardBasicTypes.BOOLEAN ) );
  56. mysqlFunctions.put( "touches", new StandardSQLFunction( "ST_Touches", StandardBasicTypes.BOOLEAN ) );
  57. mysqlFunctions.put( "within", new StandardSQLFunction( "ST_Within", StandardBasicTypes.BOOLEAN ) );
  58. return mysqlFunctions;
  59. }
  60. @Override
  61. public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
  62. return dialectDelegate.remapSqlTypeDescriptor( sqlTypeDescriptor );
  63. }
  64. @Override
  65. public String getTypeName(int code, long length, int precision, int scale) throws HibernateException {
  66. return dialectDelegate.getTypeName( code, length, precision, scale );
  67. }
  68. /**
  69. * Allows the Dialect to contribute additional types
  70. *
  71. * @param typeContributions Callback to contribute the types
  72. * @param serviceRegistry The service registry
  73. */
  74. @Override
  75. public void contributeTypes(
  76. TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
  77. super.contributeTypes( typeContributions, serviceRegistry );
  78. dialectDelegate.contributeTypes( typeContributions, serviceRegistry );
  79. }
  80. @Override
  81. public String getSpatialRelateSQL(String columnName, int spatialRelation) {
  82. switch ( spatialRelation ) {
  83. case SpatialRelation.WITHIN:
  84. return " ST_Within(" + columnName + ",?)";
  85. case SpatialRelation.CONTAINS:
  86. return " ST_Contains(" + columnName + ", ?)";
  87. case SpatialRelation.CROSSES:
  88. return " ST_Crosses(" + columnName + ", ?)";
  89. case SpatialRelation.OVERLAPS:
  90. return " ST_Overlaps(" + columnName + ", ?)";
  91. case SpatialRelation.DISJOINT:
  92. return " ST_Disjoint(" + columnName + ", ?)";
  93. case SpatialRelation.INTERSECTS:
  94. return " ST_Intersects(" + columnName + ", ?)";
  95. case SpatialRelation.TOUCHES:
  96. return " ST_Touches(" + columnName + ", ?)";
  97. case SpatialRelation.EQUALS:
  98. return " ST_Equals(" + columnName + ", ?)";
  99. default:
  100. throw new IllegalArgumentException(
  101. "Spatial relation is not known by this dialect"
  102. );
  103. }
  104. }
  105. @Override
  106. public String getSpatialFilterExpression(String columnName) {
  107. return dialectDelegate.getSpatialFilterExpression( columnName );
  108. }
  109. @Override
  110. public String getSpatialAggregateSQL(String columnName, int aggregation) {
  111. return dialectDelegate.getSpatialAggregateSQL( columnName, aggregation );
  112. }
  113. @Override
  114. public String getDWithinSQL(String columnName) {
  115. return dialectDelegate.getDWithinSQL( columnName );
  116. }
  117. @Override
  118. public String getHavingSridSQL(String columnName) {
  119. return dialectDelegate.getHavingSridSQL( columnName );
  120. }
  121. @Override
  122. public String getIsEmptySQL(String columnName, boolean isEmpty) {
  123. return dialectDelegate.getIsEmptySQL( columnName, isEmpty );
  124. }
  125. @Override
  126. public boolean supportsFiltering() {
  127. return dialectDelegate.supportsFiltering();
  128. }
  129. @Override
  130. public boolean supports(SpatialFunction function) {
  131. return dialectDelegate.supports( function );
  132. }
  133. }