1 package org.hibernate.criterion; 3 4 5 import java.sql.Types ; 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 17 public class SimpleExpression implements Criterion { 18 19 private final String propertyName; 20 private final Object value; 21 private boolean ignoreCase; 22 private final String op; 23 24 protected SimpleExpression(String propertyName, Object value, String op) { 25 this.propertyName = propertyName; 26 this.value = value; 27 this.op = op; 28 } 29 30 protected SimpleExpression(String propertyName, Object value, String 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 toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 43 throws HibernateException { 44 45 String [] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); 46 Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName); 47 StringBuffer fragment = new StringBuffer (); 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 icvalue = ignoreCase ? value.toString().toLowerCase() : value; 69 return new TypedValue[] { criteriaQuery.getTypedValue(criteria, propertyName, icvalue) }; 70 } 71 72 public String toString() { 73 return propertyName + getOp() + value; 74 } 75 76 protected final String getOp() { 77 return op; 78 } 79 80 } 81 | Popular Tags |