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 SumExpr extends FunExpr implements GroupExpr { 38 protected static final L10N L = new L10N(SumExpr.class); 39 private static final Logger log = Log.open(SumExpr.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("sum 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 public Class getType() 67 { 68 if (_expr.isLong()) 69 return long.class; 70 else 71 return double.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 value = _expr.evalDouble(context); 97 double oldValue = context.getGroupDouble(_groupField); 98 99 context.setGroupDouble(_groupField, value + oldValue); 100 } 101 102 109 public boolean isNull(QueryContext context) 110 throws SQLException  111 { 112 return context.isGroupNull(_groupField); 113 } 114 115 122 public double evalDouble(QueryContext context) 123 throws SQLException  124 { 125 return context.getGroupDouble(_groupField); 126 } 127 128 135 public long evalLong(QueryContext context) 136 throws SQLException  137 { 138 return (long) evalDouble(context); 139 } 140 141 148 public String evalString(QueryContext context) 149 throws SQLException  150 { 151 return String.valueOf(evalLong(context)); 152 } 153 } 154 | Popular Tags |