Class BoundingboxJdbcFunction

java.lang.Object
ch.tocco.nice2.persist.core.api.hibernate.query.AbstractJdbcFunction
ch.tocco.nice2.optional.geolocation.impl.jdbc.BoundingboxJdbcFunction
All Implemented Interfaces:
JdbcFunction

@Component public class BoundingboxJdbcFunction extends AbstractJdbcFunction
Creates a boolean expression that evaluates to true, if a given point is within the bounds of a given rectangle.

It expects exactly 6 arguments in order, where

1. `x` is the x coordinate of a certain position
2. `y` is the y coordinate of a certain position
3. `ul.y` is the y coordinate of the upper left corner of the rectangle
4. `ul.x` is the x coordinate of the upper left corner of the rectangle
5. `lr.y` is the y coordinate of the lower right corner of the rectangle
6. `lr.x` is the x coordinate of the lower right corner of the rectangle
The function writes the following expression in SQL:

(x >= ul.x) and (x < = lr.x) and (y >= lr.y) and (y <= ul.y)
Selecting those (x,y) points that are within the bounds of (ul, lr).

Example usage

Use it in queries like the following example (here, lat=y and lng=x, respectively, as used in geographical coordinate system):

Query q = ctx.compileQuery("find Cities where INBOUNDINGBOX(ps_lat, pos_lng, :ul_lat, :ul_lng, :lr_lat, :lr_lng) == true");
Position zurich = new Position(...);
Position[] box = zurich.getBoundingBox(Distance.kilometer("20"));
q.setParameter("ul_lat", box[0].getLatitude());
q.setParameter("ul_lng", box[0].getLongitude());
q.setParameter("lr_lat", box[1].getLatitude());
q.setParameter("lr_lng", box[1].getLongitude());
This will select all cities that are at maximum 20km (are within a square around Zurich with side length=40km) away from Zurich (assuming that the "Cities" entity has two fields "pos_lat" and "pos_lng" of respective type.)
  • Constructor Details

    • BoundingboxJdbcFunction

      public BoundingboxJdbcFunction(TypeManager typeManager)
  • Method Details

    • render

      protected void render(JdbcFunctionWriter writer, List<? extends org.hibernate.sql.ast.tree.SqlAstNode> arguments)
      Specified by:
      render in class AbstractJdbcFunction
    • getName

      public String getName()
    • getFunctionArgumentTypeResolver

      public org.hibernate.query.sqm.produce.function.FunctionArgumentTypeResolver getFunctionArgumentTypeResolver(org.hibernate.type.spi.TypeConfiguration typeConfiguration)
      Description copied from interface: JdbcFunction
      Per default StandardFunctionArgumentTypeResolvers.ARGUMENT_OR_IMPLIED_RESULT_TYPE is used. Sometimes a more specific function resolver must be defined. For example the third argument of `BIRTHDAYIN` must be a string. Without a custom resolver Hibernate tries to convert "3 days" to a LocalDate which results in an exception.
      Specified by:
      getFunctionArgumentTypeResolver in interface JdbcFunction
      Overrides:
      getFunctionArgumentTypeResolver in class AbstractJdbcFunction
    • validateArguments

      public void validateArguments(jakarta.persistence.criteria.Expression<?>[] arguments) throws JdbcFunctionException
      Throws:
      JdbcFunctionException
    • argumentCount

      public int argumentCount()
      Returns:
      the number of expected arguments.