1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import java.sql.SQLException ; 35 import java.util.logging.Logger ; 36 37 public class CountExpr extends FunExpr implements GroupExpr { 38 protected static final L10N L = new L10N(CountExpr.class); 39 private static final Logger log = Log.open(CountExpr.class); 40 41 private Expr _expr; 42 private int _groupField; 43 44 protected void addArg(Expr expr) 45 throws SQLException 46 { 47 if (_expr != null) 48 throw new SQLException (L.l("max requires a single argument")); 49 50 _expr = expr; 51 } 52 53 protected Expr bind(Query query) 54 throws SQLException 55 { 56 _groupField = query.getDataFields(); 57 58 query.setDataFields(_groupField + 1); 59 query.setGroup(true); 60 61 _expr = _expr.bind(query); 62 63 return this; 64 } 65 66 69 public Class getType() 70 { 71 return long.class; 72 } 73 74 79 public void initGroup(QueryContext context) 80 throws SQLException 81 { 82 context.setGroupDouble(_groupField, 0); 83 } 84 85 90 public void evalGroup(QueryContext context) 91 throws SQLException 92 { 93 if (_expr.isNull(context)) 94 return; 95 96 double oldValue = context.getGroupDouble(_groupField); 97 98 context.setGroupDouble(_groupField, oldValue + 1); 99 } 100 101 108 public boolean isNull(QueryContext context) 109 throws SQLException 110 { 111 return context.isGroupNull(_groupField); 112 } 113 114 121 public double evalDouble(QueryContext context) 122 throws SQLException 123 { 124 return context.getGroupDouble(_groupField); 125 } 126 127 134 public long evalLong(QueryContext context) 135 throws SQLException 136 { 137 return (long) evalDouble(context); 138 } 139 140 147 public String evalString(QueryContext context) 148 throws SQLException 149 { 150 return String.valueOf(evalLong(context)); 151 } 152 } 153 | Popular Tags |