1 package org.apache.torque.util; 2 3 21 22 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 public class CountHelper 40 { 41 53 public int count(Criteria c) throws TorqueException 54 { 55 return count(c, null, "*"); 56 } 57 58 70 public int count(Criteria c, Connection conn) throws TorqueException 71 { 72 return count(c, conn, "*"); 73 } 74 75 84 public int count(Criteria c, String columnName) 85 throws TorqueException 86 { 87 return count(c, null, columnName); 88 } 89 90 100 public int count(Criteria c, Connection conn, String columnName) 101 throws TorqueException 102 { 103 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 countStr = new StringBuffer ("COUNT("); 121 countStr.append(distinct ? SqlEnum.DISTINCT.toString() : ""); 122 countStr.append(columnName); 123 countStr.append(")"); 124 125 c.addSelectColumn(countStr.toString()); 126 127 List 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 |