1 package org.apache.torque.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 /***
20 * This is a utility class which eases counting of Datasets
21 *
22 * @author <a href="mailto:Martin.Goulet@sungard.com">Martin Goulet</a>
23 * @author <a href="mailto:eric.lambert@sungard.com">Eric Lambert</a>
24 * @author <a href="mailto:sebastien.paquette@sungard.com">Sebastien Paquette</a>
25 * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
26 * @version $Id: CountHelper.java 239636 2005-08-24 12:38:09Z henning $
27 */
28 import java.sql.Connection;
29 import java.util.List;
30
31 import org.apache.torque.TorqueException;
32
33 import com.workingdogs.village.DataSetException;
34 import com.workingdogs.village.Record;
35
36 public class CountHelper
37 {
38 /***
39 * The COUNT function returns the number of rows in a query.
40 * Does not use a connection, hardcode the column to "*" and
41 * set the distinct qualifier to false.
42 * Only use this function if you have added additional constraints to
43 * the criteria, otherwise Torque does not know which table it should
44 * count the datasets in.
45 *
46 * @param c Criteria to get the count for.
47 * @return number of rows matching the query provided
48 * @throws TorqueException if the query could not be executed
49 */
50 public int count( Criteria c ) throws TorqueException
51 {
52 return count( c, null, "*" );
53 }
54
55 /***
56 * The COUNT function returns the number of rows in a query.
57 * Hard code the distinct parameter to false and set the column to "*".
58 * Only use this function if you have added additional constraints to
59 * the criteria, otherwise Torque does not know which table it should
60 * count the datasets in.
61 *
62 * @param c Criteria to get the count for.
63 * @param conn Connection to use
64 * @return number of rows matching the query provided
65 * @throws TorqueException if the query could not be executed
66 */
67 public int count( Criteria c, Connection conn ) throws TorqueException
68 {
69 return count( c, conn, "*" );
70 }
71
72 /***
73 * Returns the number of rows in a query.
74 *
75 * @param c Criteria to get the count for.
76 * @param columnName Name of database Column which is counted. Preferably,
77 * use the primary key here.
78 * @return number of rows matching the query provided
79 * @throws TorqueException if the query could not be executed
80 */
81 public int count( Criteria c, String columnName )
82 throws TorqueException
83 {
84 return count( c, null, columnName );
85 }
86
87 /***
88 * Returns the number of rows in a query.
89 *
90 * @param c Criteria to get the count for.
91 * @param conn Connection to use
92 * @param columnName Name of database Column which is counted. Preferably,
93 * use the primary key here.
94 * @return number of rows matching the query provided
95 * @throws TorqueException if the query could not be executed
96 */
97 public int count( Criteria c, Connection conn, String columnName )
98 throws TorqueException
99 {
100
101 c.getSelectColumns().clear();
102 c.getOrderByColumns().clear();
103 c.getGroupByColumns().clear();
104
105 UniqueList criteriaSelectModifiers;
106 criteriaSelectModifiers = c.getSelectModifiers();
107
108 boolean distinct = false;
109 if( criteriaSelectModifiers != null &&
110 criteriaSelectModifiers.size() > 0 &&
111 criteriaSelectModifiers.contains( SqlEnum.DISTINCT.toString() ) )
112 {
113 criteriaSelectModifiers.remove( SqlEnum.DISTINCT.toString() );
114 distinct = true;
115 }
116
117 StringBuffer countStr = new StringBuffer( "COUNT(" );
118 countStr.append( distinct == true ? SqlEnum.DISTINCT.toString() : "" );
119 countStr.append( columnName );
120 countStr.append( ")" );
121
122 c.addSelectColumn( countStr.toString() );
123
124 List result;
125 if( conn == null )
126 {
127 result = BasePeer.doSelect( c );
128 }
129 else
130 {
131 result = BasePeer.doSelect( c, conn );
132 }
133 Record record = (Record) result.get(0);
134 try {
135 return record.getValue(1).asInt();
136 }
137 catch (DataSetException e) {
138 throw new TorqueException(e);
139 }
140 }
141 }