Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression In directory sc8-pr-cvs1:/tmp/cvs-serv31647/hibernate/expression Modified Files: AndExpression.java Expression.java GeExpression.java GtExpression.java LeExpression.java LikeExpression.java LogicalExpression.java LtExpression.java OrExpression.java SQLExpression.java SimpleExpression.java Added Files: BetweenExpression.java InExpression.java NotNullExpression.java NullExpression.java Order.java Log Message: expanded Criteria API allowed unmapped-class queries with new from syntax --- NEW FILE: BetweenExpression.java --- //$Id: BetweenExpression.java,v 1.1 2003/03/09 04:04:06 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.util.StringHelper; /** * */ public class BetweenExpression extends Expression { private final String propertyName; private final Object lo; private final Object hi; BetweenExpression(String propertyName, Object lo, Object hi) { this.propertyName = propertyName; this.lo = lo; this.hi = hi; } /** * @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), " between ? and ?" ) ); //TODO: get SQL rendering out of this package! } public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return new TypedValue[] { getTypedValue(sessionFactory, persistentClass, propertyName, lo), getTypedValue(sessionFactory, persistentClass, propertyName, hi) }; } public String toString() { return propertyName + " between " + lo + " and " + hi; } } --- NEW FILE: InExpression.java --- //$Id: InExpression.java,v 1.1 2003/03/09 04:04:06 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.util.StringHelper; /** * */ public class InExpression extends Expression { private final String propertyName; private final Object[] values; InExpression(String propertyName, Object[] values) { this.propertyName = propertyName; this.values = values; } /** * @see net.sf.hibernate.expression.Expression#toSqlString(net.sf.hibernate.SessionFactory) */ public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException { String params = StringHelper.repeat( "?, ", values.length-1 ); if ( values.length>0 ) params+="?"; return getColumns(sessionFactory, persistentClass, propertyName, alias) + " in (" + params + ')'; //TODO: get SQL rendering out of this package! } public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { TypedValue[] tvs = new TypedValue[ values.length ]; for ( int i=0; i<tvs.length; i++ ) { tvs[i] = getTypedValue( sessionFactory, persistentClass, propertyName, values[i] ); } return tvs; } public String toString() { return propertyName + " in (" + StringHelper.toString(values) + ')'; } } --- NEW FILE: NotNullExpression.java --- //$Id: NotNullExpression.java,v 1.1 2003/03/09 04:04:06 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.util.StringHelper; /** * */ public class NotNullExpression extends Expression { private final String propertyName; private static final TypedValue[] NO_VALUES = new TypedValue[0]; NotNullExpression(String propertyName) { this.propertyName = propertyName; } /** * @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), " is not null" ) ); //TODO: get SQL rendering out of this package! } public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return NO_VALUES; } public String toString() { return propertyName + " is not null"; } } --- NEW FILE: NullExpression.java --- //$Id: NullExpression.java,v 1.1 2003/03/09 04:04:06 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.util.StringHelper; /** * */ public class NullExpression extends Expression { private final String propertyName; private static final TypedValue[] NO_VALUES = new TypedValue[0]; NullExpression(String propertyName) { this.propertyName = propertyName; } /** * @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), " is null" ) ); //TODO: get SQL rendering out of this package! } public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass) throws HibernateException { return NO_VALUES; } public String toString() { return propertyName + " is null"; } } --- NEW FILE: Order.java --- //$Id: Order.java,v 1.1 2003/03/09 04:04:06 oneovthafew Exp $ package net.sf.hibernate.expression; import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; /** * Represents an order imposed upon a <tt>Criteria</tt> result set */ public class Order { private boolean ascending; private String propertyName; /** * Constructor for Order. */ protected Order(String propertyName, boolean ascending) { this.propertyName = propertyName; this.ascending = ascending; } /** * Render the SQL fragment * * @param sessionFactory * @param persistentClass * @param alias * @return String * @throws HibernateException */ public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias) throws HibernateException { String[] columns = Expression.getColumns(sessionFactory, persistentClass, propertyName, alias); if (columns.length!=1) throw new HibernateException("Cannot order by multi-column property: " + propertyName); return columns[0] + ( ascending ? " asc" : " desc" ); } /** * Ascending order * * @param propertyName * @return Order */ public static Order asc(String propertyName) { return new Order(propertyName, true); } /** * Descending order * * @param propertyName * @return Order */ public static Order desc(String propertyName) { return new Order(propertyName, false); } } Index: AndExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/AndExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AndExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- AndExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: Expression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/Expression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Expression.java 8 Mar 2003 06:31:22 -0000 1.1 --- Expression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 2,5 **** --- 2,7 ---- package net.sf.hibernate.expression; + import java.util.Collection; + import net.sf.hibernate.HibernateException; import net.sf.hibernate.engine.SessionFactoryImplementor; *************** *** 77,80 **** --- 79,123 ---- } /** + * Apply a "between" constraint to the named property + * @param propertyName + * @param value + * @return Expression + */ + public static Expression between(String propertyName, Object lo, Object hi) { + return new BetweenExpression(propertyName, lo, hi); + } + /** + * Apply an "in" constraint to the named property + * @param propertyName + * @param values + * @return Expression + */ + public static Expression in(String propertyName, Object[] values) { + return new InExpression(propertyName, values); + } + /** + * Apply an "in" constraint to the named property + * @param propertyName + * @param values + * @return Expression + */ + public static Expression in(String propertyName, Collection values) { + return new InExpression( propertyName, values.toArray() ); + } + /** + * Apply an "is null" constraint to the named property + * @return Expression + */ + public static Expression isNull(String propertyName) { + return new NullExpression(propertyName); + } + /** + * Apply an "is not null" constraint to the named property + * @return Expression + */ + public static Expression isNotNull(String propertyName) { + return new NotNullExpression(propertyName); + } + /** * Return the conjuction of two expressions * *************** *** 107,111 **** /** * Apply a constraint expressed in SQL, with the given JDBC ! * parameters * * @param sql --- 150,155 ---- /** * Apply a constraint expressed in SQL, with the given JDBC ! * parameters. Any occurrences of <tt>$alias</tt> will be ! * replaced by the table alias. * * @param sql *************** *** 119,123 **** /** * Apply a constraint expressed in SQL, with the given JDBC ! * parameter * * @param sql --- 163,168 ---- /** * Apply a constraint expressed in SQL, with the given JDBC ! * parameter. Any occurrences of <tt>$alias</tt> will be replaced ! * by the table alias. * * @param sql *************** *** 130,134 **** } /** ! * Apply a constraint expressed in SQL * * @param sql --- 175,180 ---- } /** ! * Apply a constraint expressed in SQL. Any occurrences of <tt>$alias</tt> ! * will be replaced by the table alias. * * @param sql *************** *** 140,144 **** /** ! * Render and SQL fragment * * @param sessionFactory --- 186,190 ---- /** ! * Render the SQL fragment * * @param sessionFactory *************** *** 166,174 **** 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 ); } --- 212,220 ---- public abstract String toString(); ! protected static String[] getColumns(SessionFactoryImplementor sessionFactory, Class persistentClass, String property, String alias) throws HibernateException { return ( (Queryable) sessionFactory.getPersister(persistentClass) ).toColumns(alias, property); } ! protected static TypedValue getTypedValue(SessionFactoryImplementor sessionFactory, Class persistentClass, String propertyName, Object value) throws HibernateException { return new TypedValue( ( (Queryable) sessionFactory.getPersister(persistentClass) ).getPropertyType(propertyName), value ); } Index: GeExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/GeExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GeExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- GeExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: GtExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/GtExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GtExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- GtExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: LeExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/LeExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** LeExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- LeExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: LikeExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/LikeExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** LikeExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- LikeExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: LogicalExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/LogicalExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** LogicalExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- LogicalExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: LtExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/LtExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** LtExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- LtExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: OrExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/OrExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** OrExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- OrExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; Index: SQLExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/SQLExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SQLExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- SQLExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 6,9 **** --- 6,10 ---- import net.sf.hibernate.engine.TypedValue; import net.sf.hibernate.type.Type; + import net.sf.hibernate.util.StringHelper; /** *************** *** 23,27 **** String alias) throws HibernateException { ! return sql; } --- 24,28 ---- String alias) throws HibernateException { ! return StringHelper.replace(sql, "$alias", alias); } Index: SimpleExpression.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/expression/SimpleExpression.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SimpleExpression.java 8 Mar 2003 06:31:22 -0000 1.1 --- SimpleExpression.java 9 Mar 2003 04:04:06 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package net.sf.hibernate.expression; |