1 package org.hibernate.criterion; 3 4 5 import org.hibernate.Criteria; 6 import org.hibernate.HibernateException; 7 import org.hibernate.dialect.Dialect; 8 import org.hibernate.dialect.PostgreSQLDialect; 9 import org.hibernate.engine.TypedValue; 10 11 15 public class IlikeExpression implements Criterion { 16 17 private final String propertyName; 18 private final Object value; 19 20 protected IlikeExpression(String propertyName, Object value) { 21 this.propertyName = propertyName; 22 this.value = value; 23 } 24 25 protected IlikeExpression(String propertyName, String value, MatchMode matchMode) { 26 this( propertyName, matchMode.toMatchString(value) ); 27 } 28 29 public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 30 throws HibernateException { 31 Dialect dialect = criteriaQuery.getFactory().getDialect(); 32 String [] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); 33 if (columns.length!=1) throw new HibernateException("ilike may only be used with single-column properties"); 34 if ( dialect instanceof PostgreSQLDialect ) { 35 return columns[0] + " ilike ?"; 36 } 37 else { 38 return dialect.getLowercaseFunction() + '(' + columns[0] + ") like ?"; 39 } 40 41 } 43 44 public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) 45 throws HibernateException { 46 return new TypedValue[] { criteriaQuery.getTypedValue( criteria, propertyName, value.toString().toLowerCase() ) }; 47 } 48 49 public String toString() { 50 return propertyName + " ilike " + value; 51 } 52 53 } 54 | Popular Tags |