KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > CountProjection


1 //$Id: CountProjection.java,v 1.11 2005/02/12 07:19:13 steveebersole Exp $
2
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 /**
10  * A count
11  * @author Gavin King
12  */

13 public class CountProjection extends AggregateProjection {
14
15     private boolean distinct;
16
17     protected CountProjection(String JavaDoc prop) {
18         super("count", prop);
19     }
20
21     public String JavaDoc 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 JavaDoc toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
35     throws HibernateException {
36         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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