1 package org.hibernate.criterion; 3 4 import java.io.Serializable ; 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.type.Type; 11 12 16 public class Order implements Serializable { 17 18 private boolean ascending; 19 private boolean ignoreCase; 20 private String propertyName; 21 22 public String toString() { 23 return propertyName + ' ' + (ascending?"asc":"desc"); 24 } 25 26 public Order ignoreCase() { 27 ignoreCase = true; 28 return this; 29 } 30 31 34 protected Order(String propertyName, boolean ascending) { 35 this.propertyName = propertyName; 36 this.ascending = ascending; 37 } 38 39 43 public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 44 throws HibernateException { 45 String [] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); 46 Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName); 47 StringBuffer fragment = new StringBuffer (); 48 for ( int i=0; i<columns.length; i++ ) { 49 SessionFactoryImplementor factory = criteriaQuery.getFactory(); 50 boolean lower = ignoreCase && type.sqlTypes( factory )[i]==Types.VARCHAR; 51 if (lower) { 52 fragment.append( factory.getDialect().getLowercaseFunction() ) 53 .append('('); 54 } 55 fragment.append( columns[i] ); 56 if (lower) fragment.append(')'); 57 fragment.append( ascending ? " asc" : " desc" ); 58 if ( i<columns.length-1 ) fragment.append(", "); 59 } 60 return fragment.toString(); 61 } 62 63 69 public static Order asc(String propertyName) { 70 return new Order(propertyName, true); 71 } 72 73 79 public static Order desc(String propertyName) { 80 return new Order(propertyName, false); 81 } 82 83 } 84 | Popular Tags |