Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression In directory sc8-pr-cvs1:/tmp/cvs-serv15856/sf/hibernate/expression Added Files: AndExpression.java EqExpression.java Expression.java GeExpression.java GtExpression.java LeExpression.java LikeExpression.java LogicalExpression.java LtExpression.java NotExpression.java OrExpression.java SQLExpression.java SimpleExpression.java Log Message: added new criteria + expression API (experimental) --- NEW FILE: AndExpression.java --- package net.sf.hibernate.expression; /** * */ public class AndExpression extends LogicalExpression { String getOp() { return "and"; } AndExpression(Expression lhs, Expression rhs) { super(lhs, rhs); } } --- NEW FILE: EqExpression.java --- //$Id: EqExpression.java,v 1.1 2003/03/08 06:31:22 oneovthafew Exp $ package net.sf.hibernate.expression; /** * */ public class EqExpression extends SimpleExpression { EqExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return "="; } } --- NEW FILE: Expression.java --- //$Id: Expression.java,v 1.1 2003/03/08 06:31:22 oneovthafew Exp $ package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; import net.sf.hibernate.persister.Queryable; import net.sf.hibernate.type.Type; /** * An object-oriented representation of expressions that may be used as constraints * in a <tt>Criteria</tt>. The <tt>expression</tt> package may be used by applications * as a framework for building new kinds of <tt>Expression</tt>. However, it is * intended that most applications will simply use the built-in expression types via * the static accessors on this class. * * @see net.sf.hibernate.Criteria */ public abstract class Expression { private static final Object[] NO_OBJECTS = new Object[0]; private static final Type[] NO_TYPES = new Type[0]; /** * Apply an "equal" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression eq(String propertyName, Object value) { return new EqExpression(propertyName, value); } /** * Apply a "like" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression like(String propertyName, Object value) { return new LikeExpression(propertyName, value); } /** * Apply a "greater than" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression gt(String propertyName, Object value) { return new GtExpression(propertyName, value); } /** * Apply a "less than" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression lt(String propertyName, Object value) { return new LtExpression(propertyName, value); } /** * Apply a "less than or equal" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression le(String propertyName, Object value) { return new LeExpression(propertyName, value); } /** * Apply a "greater than or equal" constraint to the named property * @param propertyName * @param value * @return Expression */ public static Expression ge(String propertyName, Object value) { return new GtExpression(propertyName, value); } /** * Return the conjuction of two expressions * * @param lhs * @param rhs * @return Expression */ public static Expression and(Expression lhs, Expression rhs) { return new AndExpression(lhs, rhs); } /** * Return the disjuction of two expressions * * @param lhs * @param rhs * @return Expression */ public static Expression or(Expression lhs, Expression rhs) { return new OrExpression(lhs, rhs); } /** * Return the negation of an expression * * @param expression * @return Expression */ public static Expression not(Expression expression) { return new NotExpression(expression); } /** * Apply a constraint expressed in SQL, with the given JDBC * parameters * * @param sql * @param values * @param types * @return Expression */ public static Expression sql(String sql, Object[] values, Type[] types) { return new SQLExpression(sql, values, types); } /** * Apply a constraint expressed in SQL, with the given JDBC * parameter * * @param sql * @param value * @param type * @return Expression */ public static Expression sql(String sql, Object value, Type type) { return new SQLExpression(sql, new Object[] { value }, new Type[] { type } ); } /** * Apply a constraint expressed in SQL * * @param sql * @return Expression */ public static Expression sql(String sql) { return new SQLExpression(sql, NO_OBJECTS, NO_TYPES); } /** * Render and SQL fragment * * @param sessionFactory * @param persistentClass * @param alias * @return String * @throws HibernateException */ public abstract String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException; /** * Return typed values for all parameters in the rendered SQL fragment * * @param sessionFactory * @param persistentClass * @return TypedValue[] * @throws HibernateException */ public abstract TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException; /** * For cosmetic purposes only! * * @see java.lang.Object#toString() */ public abstract String toString(); protected String[] getColumns(SessionFactoryImplementor sessionFactory, Class persistentClass, String property, String alias) throws HibernateException { return ( (Queryable) sessionFactory.getPersister(persistentClass) ).toColumns(alias, property); } protected TypedValue getTypedValue(SessionFactoryImplementor sessionFactory, Class persistentClass, String propertyName, Object value) throws HibernateException { return new TypedValue( ( (Queryable) sessionFactory.getPersister(persistentClass) ).getPropertyType(propertyName), value ); } } --- NEW FILE: GeExpression.java --- package net.sf.hibernate.expression; /** * */ public class GeExpression extends SimpleExpression { GeExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return ">="; } } --- NEW FILE: GtExpression.java --- package net.sf.hibernate.expression; /** * */ public class GtExpression extends SimpleExpression { GtExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return ">"; } } --- NEW FILE: LeExpression.java --- package net.sf.hibernate.expression; /** * */ public class LeExpression extends SimpleExpression { LeExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return "<="; } } --- NEW FILE: LikeExpression.java --- package net.sf.hibernate.expression; /** * */ public class LikeExpression extends SimpleExpression { LikeExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return " like "; } } --- NEW FILE: LogicalExpression.java --- package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; /** * */ public abstract class LogicalExpression extends Expression { protected Expression lhs; protected Expression rhs; LogicalExpression(Expression lhs, Expression rhs) { this.lhs = lhs; this.rhs = rhs; } /** * @see net.sf.hibernate.expression.Expression#getTypedValues(net.sf.hibernate.engine.SessionFactoryImplementor, java.lang.Class) */ public TypedValue[] getTypedValues( SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { TypedValue[] lhstv = lhs.getTypedValues(sessionFactory, persistentClass); TypedValue[] rhstv = rhs.getTypedValues(sessionFactory, persistentClass); TypedValue[] result = new TypedValue[ lhstv.length + rhstv.length ]; System.arraycopy(lhstv, 0, result, 0, lhstv.length); System.arraycopy(rhstv, 0, result, lhstv.length, rhstv.length); return result; } public String toSqlString( SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException { return '(' + lhs.toSqlString(sessionFactory, persistentClass, alias) + ' ' + getOp() + ' ' + rhs.toSqlString(sessionFactory, persistentClass, alias) + ')'; } abstract String getOp(); public String toString() { return lhs.toString() + ' ' + getOp() + ' ' + rhs.toString(); } } --- NEW FILE: LtExpression.java --- package net.sf.hibernate.expression; /** * */ public class LtExpression extends SimpleExpression { LtExpression(String propertyName, Object value) { super(propertyName, value); } /** * @see net.sf.hibernate.expression.SimpleExpression#getOp() */ String getOp() { return "<"; } } --- NEW FILE: NotExpression.java --- //$Id: NotExpression.java,v 1.1 2003/03/08 06:31:22 oneovthafew Exp $ package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; /** */ public class NotExpression extends Expression { private Expression expression; NotExpression(Expression expression) { this.expression = expression; } /** * @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 { return "not " + expression.toSqlString(sessionFactory, persistentClass, alias); } /** * @see net.sf.hibernate.expression.Expression#getTypedValues(net.sf.hibernate.engine.SessionFactoryImplementor, java.lang.Class) */ public TypedValue[] getTypedValues( SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return expression.getTypedValues(sessionFactory, persistentClass); } /** * @see java.lang.Object#toString() */ public String toString() { return "not " + expression.toString(); } } --- NEW FILE: OrExpression.java --- package net.sf.hibernate.expression; /** * */ public class OrExpression extends LogicalExpression { String getOp() { return "or"; } OrExpression(Expression lhs, Expression rhs) { super(lhs, rhs); } } --- NEW FILE: SQLExpression.java --- //$Id: SQLExpression.java,v 1.1 2003/03/08 06:31:22 oneovthafew Exp $ package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; import net.sf.hibernate.type.Type; /** * */ public class SQLExpression extends Expression { private String sql; private TypedValue[] typedValues; /** * @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 { return sql; } /** * @see net.sf.hibernate.expression.Expression#getTypedValues(net.sf.hibernate.engine.SessionFactoryImplementor, java.lang.Class) */ public TypedValue[] getTypedValues( SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return typedValues; } /** * @see java.lang.Object#toString() */ public String toString() { return sql; } SQLExpression(String sql, Object[] values, Type[] types) { typedValues = new TypedValue[values.length]; for ( int i=0; i<typedValues.length; i++ ) { typedValues[i] = new TypedValue( types[i], values[i] ); } } } --- NEW FILE: SimpleExpression.java --- package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; import net.sf.hibernate.engine.TypedValue; import net.sf.hibernate.util.StringHelper; /** * */ public abstract class SimpleExpression extends Expression { private final String propertyName; private final Object value; SimpleExpression(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 { return StringHelper.join( " and ", StringHelper.suffix( getColumns(sessionFactory, persistentClass, propertyName, alias), getOp() + "?" ) ); //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) }; } public String toString() { return propertyName + getOp() + value; } abstract String getOp(); } |