%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.util.CountHelper |
|
|
1 | package org.apache.torque.util; |
|
2 | ||
3 | /* |
|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
5 | * or more contributor license agreements. See the NOTICE file |
|
6 | * distributed with this work for additional information |
|
7 | * regarding copyright ownership. The ASF licenses this file |
|
8 | * to you under the Apache License, Version 2.0 (the |
|
9 | * "License"); you may not use this file except in compliance |
|
10 | * with the License. You may obtain a copy of the License at |
|
11 | * |
|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
13 | * |
|
14 | * Unless required by applicable law or agreed to in writing, |
|
15 | * software distributed under the License is distributed on an |
|
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
17 | * KIND, either express or implied. See the License for the |
|
18 | * specific language governing permissions and limitations |
|
19 | * under the License. |
|
20 | */ |
|
21 | ||
22 | /** |
|
23 | * This is a utility class which eases counting of Datasets |
|
24 | * |
|
25 | * @author <a href="mailto:Martin.Goulet@sungard.com">Martin Goulet</a> |
|
26 | * @author <a href="mailto:eric.lambert@sungard.com">Eric Lambert</a> |
|
27 | * @author <a href="mailto:sebastien.paquette@sungard.com">Sebastien Paquette</a> |
|
28 | * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a> |
|
29 | * @version $Id: CountHelper.java 473821 2006-11-11 22:37:25Z tv $ |
|
30 | */ |
|
31 | import java.sql.Connection; |
|
32 | import java.util.List; |
|
33 | ||
34 | import org.apache.torque.TorqueException; |
|
35 | ||
36 | import com.workingdogs.village.DataSetException; |
|
37 | import com.workingdogs.village.Record; |
|
38 | ||
39 | 0 | public class CountHelper |
40 | { |
|
41 | /** |
|
42 | * The COUNT function returns the number of rows in a query. |
|
43 | * Does not use a connection, hardcode the column to "*" and |
|
44 | * set the distinct qualifier to false. |
|
45 | * Only use this function if you have added additional constraints to |
|
46 | * the criteria, otherwise Torque does not know which table it should |
|
47 | * count the datasets in. |
|
48 | * |
|
49 | * @param c Criteria to get the count for. |
|
50 | * @return number of rows matching the query provided |
|
51 | * @throws TorqueException if the query could not be executed |
|
52 | */ |
|
53 | public int count(Criteria c) throws TorqueException |
|
54 | { |
|
55 | 0 | return count(c, null, "*"); |
56 | } |
|
57 | ||
58 | /** |
|
59 | * The COUNT function returns the number of rows in a query. |
|
60 | * Hard code the distinct parameter to false and set the column to "*". |
|
61 | * Only use this function if you have added additional constraints to |
|
62 | * the criteria, otherwise Torque does not know which table it should |
|
63 | * count the datasets in. |
|
64 | * |
|
65 | * @param c Criteria to get the count for. |
|
66 | * @param conn Connection to use |
|
67 | * @return number of rows matching the query provided |
|
68 | * @throws TorqueException if the query could not be executed |
|
69 | */ |
|
70 | public int count(Criteria c, Connection conn) throws TorqueException |
|
71 | { |
|
72 | 0 | return count(c, conn, "*"); |
73 | } |
|
74 | ||
75 | /** |
|
76 | * Returns the number of rows in a query. |
|
77 | * |
|
78 | * @param c Criteria to get the count for. |
|
79 | * @param columnName Name of database Column which is counted. Preferably, |
|
80 | * use the primary key here. |
|
81 | * @return number of rows matching the query provided |
|
82 | * @throws TorqueException if the query could not be executed |
|
83 | */ |
|
84 | public int count(Criteria c, String columnName) |
|
85 | throws TorqueException |
|
86 | { |
|
87 | 0 | return count(c, null, columnName); |
88 | } |
|
89 | ||
90 | /** |
|
91 | * Returns the number of rows in a query. |
|
92 | * |
|
93 | * @param c Criteria to get the count for. |
|
94 | * @param conn Connection to use |
|
95 | * @param columnName Name of database Column which is counted. Preferably, |
|
96 | * use the primary key here. |
|
97 | * @return number of rows matching the query provided |
|
98 | * @throws TorqueException if the query could not be executed |
|
99 | */ |
|
100 | public int count(Criteria c, Connection conn, String columnName) |
|
101 | throws TorqueException |
|
102 | { |
|
103 | /* Clear the select columns. */ |
|
104 | 0 | c.getSelectColumns().clear(); |
105 | 0 | c.getOrderByColumns().clear(); |
106 | 0 | c.getGroupByColumns().clear(); |
107 | ||
108 | UniqueList criteriaSelectModifiers; |
|
109 | 0 | criteriaSelectModifiers = c.getSelectModifiers(); |
110 | ||
111 | 0 | boolean distinct = false; |
112 | 0 | if (criteriaSelectModclass="keyword">ifiers != null |
113 | && criteriaSelectModifiers.size() > 0 |
|
114 | && criteriaSelectModifiers.contains(SqlEnum.DISTINCT.toString())) |
|
115 | { |
|
116 | 0 | criteriaSelectModifiers.remove(SqlEnum.DISTINCT.toString()); |
117 | 0 | distinct = true; |
118 | } |
|
119 | ||
120 | 0 | StringBuffer countStr = new StringBuffer("COUNT("); |
121 | 0 | countStr.append(distinct ? SqlEnum.DISTINCT.toString() : ""); |
122 | 0 | countStr.append(columnName); |
123 | 0 | countStr.append(")"); |
124 | ||
125 | 0 | c.addSelectColumn(countStr.toString()); |
126 | ||
127 | List result; |
|
128 | 0 | if (conn == null) |
129 | { |
|
130 | 0 | result = BasePeer.doSelect(c); |
131 | 0 | } |
132 | else |
|
133 | { |
|
134 | 0 | result = BasePeer.doSelect(c, conn); |
135 | } |
|
136 | ||
137 | 0 | Record record = (Record) result.get(0); |
138 | try |
|
139 | { |
|
140 | 0 | return record.getValue(1).asInt(); |
141 | } |
|
142 | 0 | catch (DataSetException e) |
143 | { |
|
144 | 0 | throw new TorqueException(e); |
145 | } |
|
146 | } |
|
147 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |