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 1379317 2012-08-31 06:56:48Z tfischer $ 30 */ 31 import java.sql.Connection; 32 import java.util.List; 33 34 import org.apache.torque.Column; 35 import org.apache.torque.ColumnImpl; 36 import org.apache.torque.Torque; 37 import org.apache.torque.TorqueException; 38 import org.apache.torque.criteria.SqlEnum; 39 import org.apache.torque.map.TableMap; 40 import org.apache.torque.om.mapper.IntegerMapper; 41 import org.apache.torque.util.functions.Count; 42 43 /** 44 * Counts entries matching a criteria. 45 */ 46 public class CountHelper 47 { 48 /** 49 * The COUNT function returns the number of rows in a query. 50 * Does not use a connection, hardcode the column to "*" and 51 * set the distinct qualifier to false. 52 * Only use this function if you have added additional constraints to 53 * the criteria, otherwise Torque does not know which table it should 54 * count the datasets in. 55 * 56 * @param c Criteria to get the count for. 57 * @return number of rows matching the query provided 58 * @throws TorqueException if the query could not be executed 59 * 60 * @deprecated use org.apache.toraue.criteria.Criteria instead of 61 * org.apache.toraue.util.Criteria. This method will be removed 62 * in Torque 4.1. 63 */ 64 @Deprecated 65 public int count(Criteria c) throws TorqueException 66 { 67 return count(c, null, "*", null); 68 } 69 70 /** 71 * The COUNT function returns the number of rows in a query. 72 * Does not use a connection, hardcode the column to "*" and 73 * set the distinct qualifier to false. 74 * Only use this function if you have added additional constraints to 75 * the criteria, otherwise Torque does not know which table it should 76 * count the datasets in. 77 * 78 * @param c Criteria to get the count for. 79 * @return number of rows matching the query provided 80 * @throws TorqueException if the query could not be executed 81 */ 82 public int count(org.apache.torque.criteria.Criteria c) 83 throws TorqueException 84 { 85 return count(c, null, "*", null); 86 } 87 88 /** 89 * The COUNT function returns the number of rows in a query. 90 * Hard code the distinct parameter to false and set the column to "*". 91 * Only use this function if you have added additional constraints to 92 * the criteria, otherwise Torque does not know which table it should 93 * count the datasets in. 94 * 95 * @param c Criteria to get the count for. 96 * @param conn Connection to use 97 * @return number of rows matching the query provided 98 * @throws TorqueException if the query could not be executed 99 * 100 * @deprecated use org.apache.toraue.criteria.Criteria instead of 101 * org.apache.toraue.util.Criteria. This method will be removed 102 * in Torque 4.1. 103 */ 104 @Deprecated 105 public int count(Criteria c, Connection conn) throws TorqueException 106 { 107 return count(c, conn, "*", null); 108 } 109 110 /** 111 * The COUNT function returns the number of rows in a query. 112 * Hard code the distinct parameter to false and set the column to "*". 113 * Only use this function if you have added additional constraints to 114 * the criteria, otherwise Torque does not know which table it should 115 * count the datasets in. 116 * 117 * @param c Criteria to get the count for. 118 * @param conn Connection to use 119 * @return number of rows matching the query provided 120 * @throws TorqueException if the query could not be executed 121 */ 122 public int count(org.apache.torque.criteria.Criteria c, Connection conn) 123 throws TorqueException 124 { 125 return count(c, conn, "*", null); 126 } 127 128 /** 129 * Returns the number of rows in a query. 130 * 131 * @param c Criteria to get the count for. 132 * @param columnName Name of database Column which is counted. Preferably, 133 * use the primary key here. 134 * @return number of rows matching the query provided 135 * @throws TorqueException if the query could not be executed 136 * 137 * @deprecated use org.apache.toraue.criteria.Criteria instead of 138 * org.apache.toraue.util.Criteria. This method will be removed 139 * in Torque 4.1. 140 */ 141 @Deprecated 142 public int count(Criteria c, String columnName) 143 throws TorqueException 144 { 145 return count(c, null, columnName, null); 146 } 147 148 /** 149 * Returns the number of rows in a query. 150 * 151 * @param c Criteria to get the count for. 152 * @param columnName Name of database Column which is counted. Preferably, 153 * use the primary key here. 154 * @return number of rows matching the query provided 155 * @throws TorqueException if the query could not be executed 156 */ 157 public int count(org.apache.torque.criteria.Criteria c, String columnName) 158 throws TorqueException 159 { 160 return count(c, null, columnName, null); 161 } 162 163 /** 164 * Returns the number of rows in a query. 165 * 166 * @param c Criteria to get the count for. 167 * @param column the database Column which is counted. Preferably, 168 * use the primary key here. 169 * @return number of rows matching the query provided 170 * @throws TorqueException if the query could not be executed 171 * 172 * @deprecated use org.apache.toraue.criteria.Criteria instead of 173 * org.apache.toraue.util.Criteria. This method will be removed 174 * in Torque 4.1. 175 */ 176 @Deprecated 177 public int count(Criteria c, Column column) 178 throws TorqueException 179 { 180 return count(c, null, column.getSqlExpression(), null); 181 } 182 183 /** 184 * Returns the number of rows in a query. 185 * 186 * @param c Criteria to get the count for. 187 * @param column the database Column which is counted. Preferably, 188 * use the primary key here. 189 * @return number of rows matching the query provided 190 * @throws TorqueException if the query could not be executed 191 */ 192 public int count(org.apache.torque.criteria.Criteria c, Column column) 193 throws TorqueException 194 { 195 return count(c, column.getSqlExpression()); 196 } 197 198 /** 199 * Returns the number of rows in a query. 200 * 201 * @param c Criteria to get the count for. 202 * @param conn Connection to use 203 * @param column The database Column which is counted. Preferably, 204 * use the primary key here. 205 * @return number of rows matching the query provided 206 * @throws TorqueException if the query could not be executed 207 * 208 * @deprecated use org.apache.toraue.criteria.Criteria instead of 209 * org.apache.toraue.util.Criteria. This method will be removed 210 * in Torque 4.1. 211 */ 212 @Deprecated 213 public int count(Criteria c, Connection conn, Column column) 214 throws TorqueException 215 { 216 return count(c, conn, column.getSqlExpression(), null); 217 } 218 219 /** 220 * Returns the number of rows in a query. 221 * 222 * @param c Criteria to get the count for. 223 * @param conn Connection to use 224 * @param column The database Column which is counted. Preferably, 225 * use the primary key here. 226 * @return number of rows matching the query provided 227 * @throws TorqueException if the query could not be executed 228 */ 229 public int count( 230 org.apache.torque.criteria.Criteria c, 231 Connection conn, 232 Column column) 233 throws TorqueException 234 { 235 return count(c, conn, column.getSqlExpression(), null); 236 } 237 238 /** 239 * Counts all rows in a table. 240 * 241 * @param tableMap the table map of the table to count rows in. 242 * 243 * @return the number of rows in the table. 244 * 245 * @throws TorqueException if the query could not be executed 246 */ 247 public int count(TableMap tableMap) 248 throws TorqueException 249 { 250 return count( 251 new org.apache.torque.criteria.Criteria(), 252 null, 253 "*", 254 tableMap); 255 } 256 257 /** 258 * Counts all rows in a table. 259 * 260 * @param tableMap the table map of the table to count rows in. 261 * @param conn the connection to use. 262 * 263 * @return the number of rows in the table. 264 * 265 * @throws TorqueException if the query could not be executed 266 */ 267 public int count(TableMap tableMap, Connection conn) 268 throws TorqueException 269 { 270 return count( 271 new org.apache.torque.criteria.Criteria(), 272 conn, 273 "*", 274 tableMap); 275 } 276 277 /** 278 * Returns the number of rows in a query. 279 * 280 * @param c Criteria to get the count for. 281 * @param conn Connection to use 282 * @param columnName Name of database Column which is counted. Preferably, 283 * use the primary key here. 284 * @param tableMap the table to count the columns in, or null to determine 285 * the table automatically from the criteria. 286 * 287 * @return number of rows matching the query provided. 288 * 289 * @throws TorqueException if the query could not be executed. 290 * 291 * @deprecated use org.apache.toraue.criteria.Criteria instead of 292 * org.apache.toraue.util.Criteria. This method will be removed 293 * in Torque 4.1. 294 */ 295 @Deprecated 296 public int count( 297 Criteria c, 298 Connection conn, 299 String columnName, 300 TableMap tableMap) 301 throws TorqueException 302 { 303 /* Clear the select columns. */ 304 c.getSelectColumns().clear(); 305 c.getOrderByColumns().clear(); 306 c.getGroupByColumns().clear(); 307 308 UniqueList<String> criteriaSelectModifiers 309 = c.getSelectModifiers(); 310 311 boolean distinct = false; 312 if (criteriaSelectModifiers != null 313 && criteriaSelectModifiers.size() > 0 314 && criteriaSelectModifiers.contains(SqlEnum.DISTINCT.toString())) 315 { 316 criteriaSelectModifiers.remove(SqlEnum.DISTINCT.toString()); 317 distinct = true; 318 } 319 320 c.addSelectColumn(new Count(new ColumnImpl(columnName), distinct)); 321 322 String databaseName = (c.getDbName() == null) 323 ? Torque.getDefaultDB() 324 : c.getDbName(); 325 326 BasePeerImpl<Integer> peer = new BasePeerImpl<Integer>( 327 new IntegerMapper(), 328 tableMap, databaseName); 329 330 List<Integer> result = (conn == null) 331 ? peer.doSelect(c) 332 : peer.doSelect(c, conn); 333 334 return result.get(0); 335 } 336 337 /** 338 * Returns the number of rows in a query. 339 * 340 * @param c Criteria to get the count for. 341 * @param conn Connection to use 342 * @param columnName Name of database Column which is counted. Preferably, 343 * use the primary key here. 344 * @param tableMap the table to count the columns in, or null to determine 345 * the table automatically from the criteria. 346 * 347 * @return number of rows matching the query provided. 348 * 349 * @throws TorqueException if the query could not be executed. 350 */ 351 public int count( 352 org.apache.torque.criteria.Criteria c, 353 Connection conn, 354 String columnName, 355 TableMap tableMap) 356 throws TorqueException 357 { 358 /* Clear the select columns. */ 359 c.getSelectColumns().clear(); 360 c.getOrderByColumns().clear(); 361 c.getGroupByColumns().clear(); 362 363 UniqueList<String> criteriaSelectModifiers 364 = c.getSelectModifiers(); 365 366 boolean distinct = false; 367 if (criteriaSelectModifiers != null 368 && criteriaSelectModifiers.size() > 0 369 && criteriaSelectModifiers.contains(SqlEnum.DISTINCT.toString())) 370 { 371 criteriaSelectModifiers.remove(SqlEnum.DISTINCT.toString()); 372 distinct = true; 373 } 374 375 c.addSelectColumn(new Count(new ColumnImpl(columnName), distinct)); 376 377 String databaseName = (c.getDbName() == null) 378 ? Torque.getDefaultDB() 379 : c.getDbName(); 380 381 BasePeerImpl<Integer> peer = new BasePeerImpl<Integer>( 382 new IntegerMapper(), 383 tableMap, databaseName); 384 385 List<Integer> result = (conn == null) 386 ? peer.doSelect(c) 387 : peer.doSelect(c, conn); 388 389 return result.get(0); 390 } 391 }