Example usage for org.hibernate.criterion CriteriaQuery getColumn

List of usage examples for org.hibernate.criterion CriteriaQuery getColumn

Introduction

In this page you can find the example usage for org.hibernate.criterion CriteriaQuery getColumn.

Prototype

public String getColumn(Criteria criteria, String propertyPath) throws HibernateException;

Source Link

Document

Resolve a property path to the name of the column it maps to.

Usage

From source file:com.gisgraphy.hibernate.criterion.DistanceRestriction.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String columnName = criteriaQuery.getColumn(criteria, GisFeature.LOCATION_COLUMN_NAME);
    StringBuffer result = new StringBuffer("( st_distance_sphere(").append(columnName).append(", ?) <=")
            .append(this.distance).append(")");
    return useIndex
            ? result.append(" AND ").append(GisHelper.getBoundingBox(criteriaQuery.getSQLAlias(criteria),
                    this.point.getY(), this.point.getX(), distance)).toString()
            : result.toString();//from   ww w.j  ava 2s.  c  om

}

From source file:com.gisgraphy.hibernate.criterion.DistanceRestrictionTest.java

License:Open Source License

@Test
public void testDistanceRestrictionPointDoubleBooleanShouldUseIndexIfUseIndexTrue() {
    CriteriaQuery criteriaQuery = EasyMock.createMock(CriteriaQuery.class);
    EasyMock.expect(criteriaQuery.getColumn((Criteria) EasyMock.anyObject(),
            EasyMock.eq(GisFeature.LOCATION_COLUMN_NAME))).andReturn("").once();
    EasyMock.expect(criteriaQuery.getSQLAlias((Criteria) EasyMock.anyObject())).andReturn("").once();
    EasyMock.replay(criteriaQuery);// w  ww .  j ava  2  s  . co  m
    DistanceRestriction dr = new DistanceRestriction(GisgraphyTestHelper.createPoint(3F, 4F), 4D, true);
    String sqlString = dr.toSqlString(null, criteriaQuery);
    assertTrue(sqlString.contains(" && "));
    EasyMock.verify(criteriaQuery);
}

From source file:com.gisgraphy.hibernate.criterion.DistanceRestrictionTest.java

License:Open Source License

@Test
public void testDistanceRestrictionPointDoubleBooleanShouldNotUseIndexIfUseIndexFalse() {
    CriteriaQuery criteriaQuery = EasyMock.createMock(CriteriaQuery.class);
    EasyMock.expect(criteriaQuery.getColumn((Criteria) EasyMock.anyObject(),
            EasyMock.eq(GisFeature.LOCATION_COLUMN_NAME))).andReturn(" test ").once();
    EasyMock.replay(criteriaQuery);/* w  w  w  .j  av  a  2s.  c o  m*/
    DistanceRestriction dr = new DistanceRestriction(GisgraphyTestHelper.createPoint(3F, 4F), 4D, false);
    String sqlString = dr.toSqlString(null, criteriaQuery);
    assertTrue(!sqlString.contains(" && "));
    EasyMock.verify(criteriaQuery);
}

From source file:com.gisgraphy.hibernate.criterion.FulltextRestriction.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String columnName = criteriaQuery.getColumn(criteria, ts_vectorColumnName);
    StringBuffer result = new StringBuffer("(").append(columnName).append(" @@ plainto_tsquery('simple',?)")
            .append(")");
    return result.toString();

}

From source file:com.gisgraphy.hibernate.criterion.IntersectsRestriction.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String columnName = criteriaQuery.getColumn(criteria, this.columnName);
    StringBuffer result = new StringBuffer(columnName).append(" && ").append(" ? ");
    return result.toString();
}

From source file:com.gisgraphy.hibernate.projection.SpatialProjection.java

License:Open Source License

/**
* projection to get the distance_sphere between a point and a LineString
* 
* @param point/*from  ww  w.j  a  v  a 2 s  .  c  o  m*/
*                the point to get the distance
* @param lineStringColumnName the name of the lineString column
* @return the projection
* @see #distance(Point, String)
*/
public static SimpleProjection distance_pointToLine(final Point point, final String lineStringColumnName) {
    return new SimpleProjection() {

        private static final long serialVersionUID = -7424596977297450115L;

        /*
         * (non-Javadoc)
         * 
         * @see org.hibernate.criterion.Projection#getTypes(org.hibernate.Criteria,
         *      org.hibernate.criterion.CriteriaQuery)
         */
        public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new Type[] { Hibernate.DOUBLE };
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.hibernate.criterion.Projection#toSqlString(org.hibernate.Criteria,
         *      int, org.hibernate.criterion.CriteriaQuery)
         */
        public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
                throws HibernateException {
            String columnName = criteriaQuery.getColumn(criteria, lineStringColumnName);
            String pointAsString = new StringBuffer("st_GeometryFromText( 'POINT(").append(point.getX())
                    .append(" ").append(point.getY()).append(")',").append(SRID.WGS84_SRID.getSRID())
                    .append(")").toString();

            /*String lineMerge = new StringBuffer(LINEMERGE_FUNCTION)
            .append("(")
            .append(columnName)
            .append(")").toString();*/
            String shape = columnName;

            String sqlString = new StringBuffer().append(DISTANCE_SPHERE_FUNCTION).append("(")
                    .append(pointAsString).append(",").append(ST_CLOSEST_POINT).append("(").append(shape)
                    .append(",").append(pointAsString).append(")").append(")").append("as y").append(position)
                    .append("_").toString();

            return sqlString;
        }

    };
}

From source file:com.gisgraphy.hibernate.projection.SpatialProjection.java

License:Open Source License

private static SimpleProjection distance_function(final Point point, final String locationColumnName,
        final String distanceFunction) {
    return new SimpleProjection() {

        /**//from   w w  w.j a  v  a  2  s. com
         * 
         */
        private static final long serialVersionUID = -8771843067497785957L;

        /*
         * (non-Javadoc)
         * 
         * @see org.hibernate.criterion.Projection#getTypes(org.hibernate.Criteria,
         *      org.hibernate.criterion.CriteriaQuery)
         */
        public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new Type[] { Hibernate.DOUBLE };
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.hibernate.criterion.Projection#toSqlString(org.hibernate.Criteria,
         *      int, org.hibernate.criterion.CriteriaQuery)
         */
        public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
                throws HibernateException {
            String columnName = criteriaQuery.getColumn(criteria, locationColumnName);
            StringBuffer sb = new StringBuffer();
            String sqlString = sb.append(distanceFunction).append("(").append(columnName)
                    .append(", st_GeometryFromText( 'POINT(").append(point.getX()).append(" ")
                    .append(point.getY()).append(")',").append(SRID.WGS84_SRID.getSRID()).append(")) as y")
                    .append(position).append("_").toString();
            return sqlString;
        }

    };

}

From source file:edu.utah.further.core.data.hibernate.query.GroupByHavingProjection.java

License:Apache License

@Override
public String toGroupSqlString(final Criteria criteria, final CriteriaQuery criteriaQuery)
        throws HibernateException {
    final StringBuffer sb = new StringBuffer(50);

    final Type identifierType = criteriaQuery.getIdentifierType(criteria);
    if (identifierType.isComponentType()) {
        appendComponentIds(sb, (ComponentType) identifierType, criteriaQuery, criteria, null);
    } else {//w ww . ja v a  2s . c o m
        sb.append(criteriaQuery.getColumn(criteria, groupByProperty));
    }

    // Append HAVING clause
    sb.append(" having ");
    sb.append("(");
    // Remove the alias
    final String proj = projection.toSqlString(criteria, 0xCAFEBABE, criteriaQuery);
    sb.append(proj.substring(0, proj.indexOf("as")).trim());
    sb.append(")");

    sb.append(SPACE_STRING).append(op.getSqloperator()).append(SPACE_STRING).append(value);

    return sb.toString();
}

From source file:edu.utah.further.core.data.hibernate.query.GroupByHavingProjection.java

License:Apache License

/**
 * Private helper method to construct component ids with an optional alias.
 *
 * @param sb//from   w  ww  .  j  a  v a 2s.  c  om
 *            a {@link StringBuffer}
 * @param componentType
 *            the {@link ComponentType}
 * @param criteriaQuery
 *            the {@link CriteriaQuery}
 * @param criteria
 *            the {@link Criteria}
 * @param position
 *            the position of the alias, appened if >= 0
 */
private void appendComponentIds(final StringBuffer sb, final ComponentType componentType,
        final CriteriaQuery criteriaQuery, final Criteria criteria, final Integer position) {
    final String[] idProperties = componentType.getPropertyNames();
    int currPos = position != null ? position.intValue() : -1;
    for (int i = 0; i < idProperties.length; i++) {
        sb.append(criteriaQuery.getColumn(criteria, groupByProperty + "." + idProperties[i]));

        if (currPos >= 0) {
            sb.append(" as y").append(currPos).append('_');
            currPos++;
        }

        if (i + 1 < idProperties.length) {
            sb.append(", ");
        }
    }
}

From source file:org.jasig.ssp.util.hibernate.MultipleCountProjection.java

License:Apache License

public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
        throws HibernateException {
    StringBuffer buf = new StringBuffer();
    buf.append("count(");
    if (distinct)
        buf.append("distinct ");
    String[] properties = propertyName.split(";");
    //concat(cast(col), cast_col))
    if (properties.length == 1) {
        buf.append(criteriaQuery.getColumn(criteria, properties[0]));
    } else {/* www.ja v  a 2s.  c  om*/
        String concatSql = null;
        for (int i = 0; i < properties.length; i++) {
            String asSqlStr = asSqlStr(properties[i], criteria, criteriaQuery);
            if (concatSql == null) {
                concatSql = asSqlStr;
                continue;
            }
            concatSql = this.sqlConcatLiterals(concatSql, asSqlStr, criteria, criteriaQuery);
        }
        buf.append(concatSql);
    }
    buf.append(") as y");
    buf.append(position);
    buf.append('_');
    return buf.toString();
}