Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression In directory sc8-pr-cvs1:/tmp/cvs-serv23801/hibernate/expression Modified Files: Expression.java Added Files: Conjunction.java Disjunction.java IlikeExpression.java Junction.java Log Message: added con/dis-junctions and ilike operator --- NEW FILE: Conjunction.java --- package net.sf.hibernate.expression; /** * */ public class Conjunction extends Junction { /** * @see net.sf.hibernate.expression.Junction#getOp() */ String getOp() { return " and "; } } --- NEW FILE: Disjunction.java --- package net.sf.hibernate.expression; /** * */ public class Disjunction extends Junction { /** * @see net.sf.hibernate.expression.Junction#getOp() */ String getOp() { return " or "; } } --- NEW FILE: IlikeExpression.java --- //$Id: IlikeExpression.java,v 1.1 2003/03/12 08:24:23 oneovthafew Exp $ package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.dialect.Dialect; import net.sf.hibernate.dialect.PostgreSQLDialect; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; /** * */ public class IlikeExpression extends Expression { private final String propertyName; private final Object value; IlikeExpression(String propertyName, Object value) { this.propertyName = propertyName; this.value = value; } /** * @see net.sf.hibernate.expression.Expression#toSqlString(net.sf.hibernate.SessionFactory) */ public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException { Dialect dialect = sessionFactory.getDialect(); String[] columns = getColumns(sessionFactory, persistentClass, propertyName, alias); if (columns.length!=1) throw new HibernateException("ilike may only be used with single-column properties"); if ( dialect instanceof PostgreSQLDialect ) { return columns[0] + " ilike ?"; } else { return dialect.getLowercaseFunction() + '(' + columns[0] + ") like ?"; } //TODO: get SQL rendering out of this package! } public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return new TypedValue[] { getTypedValue( sessionFactory, persistentClass, propertyName, value.toString().toLowerCase() ) }; } public String toString() { return propertyName + " ilike " + value; } } --- NEW FILE: Junction.java --- package net.sf.hibernate.expression; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; import net.sf.hibernate.util.StringHelper; /** * @author Administrator * */ public abstract class Junction extends Expression { private List expressions = new ArrayList(); public Junction add(Expression expression) { expressions.add(expression); return this; } abstract String getOp(); /** * @see net.sf.hibernate.expression.Expression#getTypedValues(net.sf.hibernate.engine.SessionFactoryImplementor, java.lang.Class) */ public TypedValue[] getTypedValues( SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { ArrayList typedValues = new ArrayList(); Iterator iter = expressions.iterator(); while ( iter.hasNext() ) { TypedValue[] subvalues = ( (Expression) iter.next() ).getTypedValues(sessionFactory, persistentClass); for ( int i=0; i<subvalues.length; i++ ) { typedValues.add( subvalues[i] ); } } return (TypedValue[]) typedValues.toArray( new TypedValue[ typedValues.size() ] ); } /** * @see net.sf.hibernate.expression.Expression#toSqlString(net.sf.hibernate.engine.SessionFactoryImplementor, java.lang.Class, java.lang.String) */ public String toSqlString( SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException { if ( expressions.size()==0 ) return "1=1"; StringBuffer buffer = new StringBuffer() .append('('); Iterator iter = expressions.iterator(); while ( iter.hasNext() ) { buffer.append( ( (Expression) iter.next() ).toSqlString(sessionFactory, persistentClass, alias) ); if ( iter.hasNext() ) buffer.append( getOp() ); } return buffer.append(')').toString(); } /** * @see java.lang.Object#toString() */ public String toString() { return '(' + StringHelper.join( getOp(), expressions.iterator() ) + ')'; } } Index: Expression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/Expression.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Expression.java 9 Mar 2003 04:04:06 -0000 1.2 --- Expression.java 12 Mar 2003 08:24:23 -0000 1.3 *************** *** 3,6 **** --- 3,8 ---- import java.util.Collection; + import java.util.Iterator; + import java.util.Map; import net.sf.hibernate.HibernateException; *************** *** 43,46 **** --- 45,59 ---- } /** + * A case-insensitive "like", similar to Postgres <tt>ilike</tt> + * operator + * + * @param propertyName + * @param value + * @return Expression + */ + public static Expression ilike(String propertyName, Object value) { + return new IlikeExpression(propertyName, value); + } + /** * Apply a "greater than" constraint to the named property * @param propertyName *************** *** 183,186 **** --- 196,234 ---- public static Expression sql(String sql) { return new SQLExpression(sql, NO_OBJECTS, NO_TYPES); + } + + /** + * Group expressions together in a single conjunction (A and B and C...) + * + * @return Conjunction + */ + public static Conjunction conjunction() { + return new Conjunction(); + } + + /** + * Group expressions together in a single disjunction (A or B or C...) + * + * @return Conjunction + */ + public static Disjunction disjunction() { + return new Disjunction(); + } + + /** + * Apply an "equals" constraint to each property in the + * key set of a <tt>Map</tt> + * + * @param propertyNameValues a map from property names to values + * @return Expression + */ + public static Expression allEq(Map propertyNameValues) { + Conjunction conj = conjunction(); + Iterator iter = propertyNameValues.entrySet().iterator(); + while ( iter.hasNext() ) { + Map.Entry me = (Map.Entry) iter.next(); + conj.add( eq( (String) me.getKey(), me.getValue() ) ); + } + return conj; } |