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 MinExpr extends FunExpr implements GroupExpr { 38 protected static final L10N L = new L10N(MinExpr.class); 39 private static final Logger log = Log.open(MinExpr.class); 40 41 private Expr _expr; 42 private int _groupField; 43 private Class _type; 44 45 protected void addArg(Expr expr) 46 throws SQLException 47 { 48 if (_expr != null) 49 throw new SQLException (L.l("min requires a single argument")); 50 51 _expr = expr; 52 } 53 54 protected Expr bind(Query query) 55 throws SQLException 56 { 57 _groupField = query.getDataFields(); 58 59 query.setDataFields(_groupField + 1); 60 query.setGroup(true); 61 62 _expr = _expr.bind(query); 63 64 _type = _expr.getType(); 65 66 return this; 67 } 68 69 72 public Class getType() 73 { 74 return _type; 75 } 76 77 82 public void initGroup(QueryContext context) 83 throws SQLException 84 { 85 } 86 87 92 public void evalGroup(QueryContext context) 93 throws SQLException 94 { 95 if (_expr.isNull(context)) 96 return; 97 98 if (_expr.isLong()) { 99 long value = _expr.evalLong(context); 100 long oldValue = context.getGroupLong(_groupField); 101 102 if (context.isGroupNull(_groupField)) 103 context.setGroupLong(_groupField, value); 104 else if (value < oldValue) 105 context.setGroupLong(_groupField, value); 106 } 107 else { 108 double value = _expr.evalDouble(context); 109 double oldValue = context.getGroupDouble(_groupField); 110 111 if (context.isGroupNull(_groupField)) 112 context.setGroupDouble(_groupField, value); 113 else if (value < oldValue) 114 context.setGroupDouble(_groupField, value); 115 } 116 } 117 118 123 public boolean isNull(QueryContext context) 124 throws SQLException 125 { 126 return context.isGroupNull(_groupField); 127 } 128 129 136 public double evalDouble(QueryContext context) 137 throws SQLException 138 { 139 return context.getGroupDouble(_groupField); 140 } 141 142 149 public long evalLong(QueryContext context) 150 throws SQLException 151 { 152 return context.getGroupLong(_groupField); 153 } 154 155 162 public String evalString(QueryContext context) 163 throws SQLException 164 { 165 return context.getGroupString(_groupField); 166 } 167 } 168 | Popular Tags |