KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > IlikeExpression


1 //$Id: IlikeExpression.java,v 1.9 2005/02/12 07:19:13 steveebersole Exp $
2
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 /**
12  * A case-insensitive "like"
13  * @author Gavin King
14  */

15 public class IlikeExpression implements Criterion {
16
17     private final String JavaDoc propertyName;
18     private final Object JavaDoc value;
19
20     protected IlikeExpression(String JavaDoc propertyName, Object JavaDoc value) {
21         this.propertyName = propertyName;
22         this.value = value;
23     }
24
25     protected IlikeExpression(String JavaDoc propertyName, String JavaDoc value, MatchMode matchMode) {
26         this( propertyName, matchMode.toMatchString(value) );
27     }
28
29     public String JavaDoc toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
30     throws HibernateException {
31         Dialect dialect = criteriaQuery.getFactory().getDialect();
32         String JavaDoc[] 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         //TODO: get SQL rendering out of this package!
42
}
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 JavaDoc toString() {
50         return propertyName + " ilike " + value;
51     }
52
53 }
54
Popular Tags