KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SimpleExpression.java,v 1.11 2005/07/14 18:17:16 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4
5 import java.sql.Types JavaDoc;
6
7 import org.hibernate.Criteria;
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.SessionFactoryImplementor;
10 import org.hibernate.engine.TypedValue;
11 import org.hibernate.type.Type;
12
13 /**
14  * superclass for "simple" comparisons (with SQL binary operators)
15  * @author Gavin King
16  */

17 public class SimpleExpression implements Criterion {
18
19     private final String JavaDoc propertyName;
20     private final Object JavaDoc value;
21     private boolean ignoreCase;
22     private final String JavaDoc op;
23
24     protected SimpleExpression(String JavaDoc propertyName, Object JavaDoc value, String JavaDoc op) {
25         this.propertyName = propertyName;
26         this.value = value;
27         this.op = op;
28     }
29
30     protected SimpleExpression(String JavaDoc propertyName, Object JavaDoc value, String JavaDoc op, boolean ignoreCase) {
31         this.propertyName = propertyName;
32         this.value = value;
33         this.ignoreCase = ignoreCase;
34         this.op = op;
35     }
36
37     public SimpleExpression ignoreCase() {
38         ignoreCase = true;
39         return this;
40     }
41
42     public String JavaDoc toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
43     throws HibernateException {
44
45         String JavaDoc[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
46         Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
47         StringBuffer JavaDoc fragment = new StringBuffer JavaDoc();
48         if (columns.length>1) fragment.append('(');
49         for ( int i=0; i<columns.length; i++ ) {
50             SessionFactoryImplementor factory = criteriaQuery.getFactory();
51             boolean lower = ignoreCase && type.sqlTypes( factory )[i]==Types.VARCHAR;
52             if (lower) {
53                 fragment.append( factory.getDialect().getLowercaseFunction() )
54                     .append('(');
55             }
56             fragment.append( columns[0] );
57             if (lower) fragment.append(')');
58             fragment.append( getOp() ).append("?");
59             if ( i<columns.length-1 ) fragment.append(" and ");
60         }
61         if (columns.length>1) fragment.append(')');
62         return fragment.toString();
63
64     }
65
66     public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
67     throws HibernateException {
68         Object JavaDoc icvalue = ignoreCase ? value.toString().toLowerCase() : value;
69         return new TypedValue[] { criteriaQuery.getTypedValue(criteria, propertyName, icvalue) };
70     }
71
72     public String JavaDoc toString() {
73         return propertyName + getOp() + value;
74     }
75
76     protected final String JavaDoc getOp() {
77         return op;
78     }
79
80 }
81
Popular Tags