%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.util.CountHelper |
|
|
1 | package org.apache.torque.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | /* Clear the select columns. */ |
|
101 | 0 | c.getSelectColumns().clear(); |
102 | 0 | c.getOrderByColumns().clear(); |
103 | 0 | c.getGroupByColumns().clear(); |
104 | ||
105 | UniqueList criteriaSelectModifiers; |
|
106 | 0 | criteriaSelectModifiers = c.getSelectModifiers(); |
107 | ||
108 | 0 | boolean distinct = false; |
109 | 0 | if( criteriaSelectModclass="keyword">ifiers != null && |
110 | criteriaSelectModifiers.size() > 0 && |
|
111 | criteriaSelectModifiers.contains( SqlEnum.DISTINCT.toString() ) ) |
|
112 | { |
|
113 | 0 | criteriaSelectModifiers.remove( SqlEnum.DISTINCT.toString() ); |
114 | 0 | distinct = true; |
115 | } |
|
116 | ||
117 | 0 | StringBuffer countStr = new StringBuffer( "COUNT(" ); |
118 | 0 | countStr.append( distinct == true ? SqlEnum.DISTINCT.toString() : "" ); |
119 | 0 | countStr.append( columnName ); |
120 | 0 | countStr.append( ")" ); |
121 | ||
122 | 0 | c.addSelectColumn( countStr.toString() ); |
123 | ||
124 | List result; |
|
125 | 0 | if( conn == null ) |
126 | { |
|
127 | 0 | result = BasePeer.doSelect( c ); |
128 | } |
|
129 | else |
|
130 | { |
|
131 | 0 | result = BasePeer.doSelect( c, conn ); |
132 | } |
|
133 | 0 | Record record = (Record) result.get(0); |
134 | try { |
|
135 | 0 | return record.getValue(1).asInt(); |
136 | } |
|
137 | 0 | catch (DataSetException e) { |
138 | 0 | throw new TorqueException(e); |
139 | } |
|
140 | } |
|
141 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |