Search in sources :

Example 1 with SpatialDialect

use of org.hibernate.spatial.SpatialDialect in project hibernate-orm by hibernate.

the class ExpressionUtil method getSpatialDialect.

/**
 * Determines the {@code SpatialDialect} for the specified {@code CriteriaQuery}, and checks if the
 * specified function is supported.
 *
 * @param criteriaQuery The {@code CriteriaQuery} for which the dialect is sought
 * @param function The function for which to check support
 *
 * @return The {@code SpatialDialect} associated with the specified {@code CriteriaQuery}
 *
 * @throws HibernateException If the dialect for the specified {@code CriteriaQuery} is not a {@code SpatialDialect}.
 * or the specified {@code SpatialFunction} is not supported by the dialect.
 */
public static SpatialDialect getSpatialDialect(CriteriaQuery criteriaQuery, SpatialFunction function) {
    final Dialect dialect = criteriaQuery.getFactory().getDialect();
    if (!(dialect instanceof SpatialDialect)) {
        throw new HibernateException("A spatial expression requires a spatial dialect.");
    }
    final SpatialDialect spatialDialect = (SpatialDialect) dialect;
    if (!spatialDialect.supports(function)) {
        throw new HibernateException(function + " function not supported by this dialect");
    }
    return spatialDialect;
}
Also used : SpatialDialect(org.hibernate.spatial.SpatialDialect) HibernateException(org.hibernate.HibernateException) Dialect(org.hibernate.dialect.Dialect) SpatialDialect(org.hibernate.spatial.SpatialDialect)

Example 2 with SpatialDialect

use of org.hibernate.spatial.SpatialDialect in project hibernate-orm by hibernate.

the class SpatialProjections method extent.

/**
 * Applies an extent projection to the specified geometry function
 * <p>
 * <p>The extent of a set of {@code Geometry}s is the union of their bounding boxes.</p>
 *
 * @param propertyName The property to use for calculating the extent
 *
 * @return an extent-projection for the specified property.
 */
public static Projection extent(final String propertyName) {
    return new SimpleProjection() {

        public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new Type[] { criteriaQuery.getType(criteria, propertyName) };
        }

        public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) throws HibernateException {
            final StringBuilder stbuf = new StringBuilder();
            final SessionFactoryImplementor factory = criteriaQuery.getFactory();
            final String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
            final Dialect dialect = factory.getDialect();
            if (dialect instanceof SpatialDialect) {
                final SpatialDialect seDialect = (SpatialDialect) dialect;
                stbuf.append(seDialect.getSpatialAggregateSQL(columns[0], SpatialAggregate.EXTENT));
                stbuf.append(" as y").append(position).append('_');
                return stbuf.toString();
            }
            return null;
        }
    };
}
Also used : Type(org.hibernate.type.Type) SpatialDialect(org.hibernate.spatial.SpatialDialect) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CriteriaQuery(org.hibernate.criterion.CriteriaQuery) Dialect(org.hibernate.dialect.Dialect) SpatialDialect(org.hibernate.spatial.SpatialDialect) Criteria(org.hibernate.Criteria) SimpleProjection(org.hibernate.criterion.SimpleProjection)

Example 3 with SpatialDialect

use of org.hibernate.spatial.SpatialDialect in project hibernate-orm by hibernate.

the class SpatialRelateExpression method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, this.propertyName);
    final Dialect dialect = factory.getDialect();
    if (dialect instanceof SpatialDialect) {
        final SpatialDialect seDialect = (SpatialDialect) dialect;
        return seDialect.getSpatialRelateSQL(columns[0], spatialRelation);
    } else {
        throw new IllegalStateException("Dialect must be spatially enabled dialect");
    }
}
Also used : SpatialDialect(org.hibernate.spatial.SpatialDialect) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Dialect(org.hibernate.dialect.Dialect) SpatialDialect(org.hibernate.spatial.SpatialDialect)

Example 4 with SpatialDialect

use of org.hibernate.spatial.SpatialDialect in project hibernate-orm by hibernate.

the class DWithinExpression method getTypedValues.

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final SpatialDialect spatialDialect = ExpressionUtil.getSpatialDialect(criteriaQuery, SpatialFunction.dwithin);
    TypedValue typedDistanceValue = new TypedValue(StandardBasicTypes.DOUBLE, distance);
    if (spatialDialect instanceof OracleSpatial10gDialect) {
        typedDistanceValue = new TypedValue(StandardBasicTypes.STRING, "distance=" + distance);
    }
    return new TypedValue[] { criteriaQuery.getTypedValue(criteria, propertyName, geometry), typedDistanceValue };
}
Also used : SpatialDialect(org.hibernate.spatial.SpatialDialect) OracleSpatial10gDialect(org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 5 with SpatialDialect

use of org.hibernate.spatial.SpatialDialect in project hibernate-orm by hibernate.

the class DWithinExpression method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String column = ExpressionUtil.findColumn(propertyName, criteria, criteriaQuery);
    final SpatialDialect spatialDialect = ExpressionUtil.getSpatialDialect(criteriaQuery, SpatialFunction.dwithin);
    return spatialDialect.getDWithinSQL(column);
}
Also used : SpatialDialect(org.hibernate.spatial.SpatialDialect)

Aggregations

SpatialDialect (org.hibernate.spatial.SpatialDialect)9 Dialect (org.hibernate.dialect.Dialect)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Criteria (org.hibernate.Criteria)1 HibernateException (org.hibernate.HibernateException)1 CriteriaQuery (org.hibernate.criterion.CriteriaQuery)1 SimpleProjection (org.hibernate.criterion.SimpleProjection)1 TypedValue (org.hibernate.engine.spi.TypedValue)1 OracleSpatial10gDialect (org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect)1 Type (org.hibernate.type.Type)1