KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.torque.TorqueException;
35
36 import com.workingdogs.village.DataSetException;
37 import com.workingdogs.village.Record;
38
39 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         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 JavaDoc conn) throws TorqueException
71     {
72         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 JavaDoc columnName)
85         throws TorqueException
86     {
87         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 JavaDoc conn, String JavaDoc columnName)
101         throws TorqueException
102     {
103         /* Clear the select columns. */
104         c.getSelectColumns().clear();
105         c.getOrderByColumns().clear();
106         c.getGroupByColumns().clear();
107
108         UniqueList criteriaSelectModifiers;
109         criteriaSelectModifiers = c.getSelectModifiers();
110
111         boolean distinct = false;
112         if (criteriaSelectModifiers != null
113             && criteriaSelectModifiers.size() > 0
114             && criteriaSelectModifiers.contains(SqlEnum.DISTINCT.toString()))
115         {
116             criteriaSelectModifiers.remove(SqlEnum.DISTINCT.toString());
117             distinct = true;
118         }
119
120         StringBuffer JavaDoc countStr = new StringBuffer JavaDoc("COUNT(");
121         countStr.append(distinct ? SqlEnum.DISTINCT.toString() : "");
122         countStr.append(columnName);
123         countStr.append(")");
124
125         c.addSelectColumn(countStr.toString());
126
127         List JavaDoc result;
128         if (conn == null)
129         {
130             result = BasePeer.doSelect(c);
131         }
132         else
133         {
134             result = BasePeer.doSelect(c, conn);
135         }
136
137         Record record = (Record) result.get(0);
138         try
139         {
140             return record.getValue(1).asInt();
141         }
142         catch (DataSetException e)
143         {
144             throw new TorqueException(e);
145         }
146     }
147 }
148
Popular Tags