I've been using the Criteria APIs, which are really nice. However, I noticed the need for a case insensitive ordering. Perhaps this is already in CVS?

I created an IOrder class (attached), but the problem is that currently Order is not an Interface, but a class, so I can't use it with the "addOrder" method.

Could it either become an interface or could the Order class be extended to provide case insensitivity?

Thanks,
Dolan Halbrook
//$Id: Order.java,v 1.2.2.2 2003/08/12 12:51:00 oneovthafew Exp $
package net.sf.hibernate.expression;

import java.util.HashMap;
import java.util.Map;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.dialect.Dialect;
import net.sf.hibernate.engine.SessionFactoryImplementor;

/**
 * Represents a case-insensitive order imposed upon a <tt>Criteria</tt> result set
 * @author Gavin King
 * @author Dolan Halbrook
 */
public class IOrder {

        private boolean ascending;
        private String propertyName;

        /**
         * Constructor for Order.
         */
        protected IOrder(String propertyName, boolean ascending) {
                this.propertyName = propertyName;
                this.ascending = ascending;
        }

        /**
         * Render the SQL fragment
         *
         * @param sessionFactory
         * @param persistentClass
         * @param alias
         * @return String
         * @throws net.sf.hibernate.HibernateException
         */
        public String toSqlString(SessionFactoryImplementor sessionFactory, Class 
persistentClass, String alias) throws HibernateException {
                Dialect dialect = sessionFactory.getDialect();
        String[] columns = AbstractCriterion.getColumns(sessionFactory, 
persistentClass, propertyName, alias, EMPTY_MAP);
                if (columns.length!=1) throw new HibernateException("Cannot order by 
multi-column property: " + propertyName);
        return dialect.getLowercaseFunction() + '(' + columns[0] + ")" + ( ascending ? 
" asc" : " desc" );

        }

        /**
         * Ascending order
         *
         * @param propertyName
         * @return Order
         */
        public static IOrder asc(String propertyName) {
                return new IOrder(propertyName, true);
        }

        /**
         * Descending order
         *
         * @param propertyName
         * @return Order
         */
        public static IOrder desc(String propertyName) {
                return new IOrder(propertyName, false);
        }

        private static final Map EMPTY_MAP = new HashMap();

}

Reply via email to