1 package org.hibernate.criterion; 3 4 import org.hibernate.Criteria; 5 import org.hibernate.Hibernate; 6 import org.hibernate.HibernateException; 7 import org.hibernate.type.Type; 8 9 13 public class CountProjection extends AggregateProjection { 14 15 private boolean distinct; 16 17 protected CountProjection(String prop) { 18 super("count", prop); 19 } 20 21 public String toString() { 22 if(distinct) { 23 return "distinct " + super.toString(); 24 } else { 25 return super.toString(); 26 } 27 } 28 29 public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) 30 throws HibernateException { 31 return new Type[] { Hibernate.INTEGER }; 32 } 33 34 public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) 35 throws HibernateException { 36 StringBuffer buf = new StringBuffer (); 37 buf.append("count("); 38 if (distinct) buf.append("distinct "); 39 return buf.append( criteriaQuery.getColumn(criteria, propertyName) ) 40 .append(") as y") 41 .append(position) 42 .append('_') 43 .toString(); 44 } 45 46 public CountProjection setDistinct() { 47 distinct = true; 48 return this; 49 } 50 51 } 52 | Popular Tags |