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 MaxExpr extends FunExpr implements GroupExpr { 38 protected static final L10N L = new L10N(MaxExpr.class); 39 private static final Logger log = Log.open(MaxExpr.class); 40 41 private Expr _expr; 42 private int _dataField; 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 _dataField = query.getDataFields(); 57 58 query.setDataFields(_dataField + 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 _expr.getType(); 72 } 73 74 79 public void evalGroup(QueryContext context) 80 throws SQLException 81 { 82 if (_expr.isNull(context)) 83 return; 84 85 double value = _expr.evalDouble(context); 86 double oldValue = context.getGroupDouble(_dataField); 87 88 if (context.isGroupNull(_dataField)) 89 context.setGroupDouble(_dataField, value); 90 else if (oldValue < value) 91 context.setGroupDouble(_dataField, value); 92 } 93 94 99 public boolean isNull(QueryContext context) 100 throws SQLException 101 { 102 return context.isGroupNull(_dataField); 103 } 104 105 112 public double evalDouble(QueryContext context) 113 throws SQLException 114 { 115 if (context.isGroupNull(_dataField)) 116 return 0; 117 else 118 return context.getGroupDouble(_dataField); 119 } 120 121 128 public long evalLong(QueryContext context) 129 throws SQLException 130 { 131 return (long) evalDouble(context); 132 } 133 134 141 public String evalString(QueryContext context) 142 throws SQLException 143 { 144 if (context.isGroupNull(_dataField)) 145 return null; 146 147 double value = context.getGroupDouble(_dataField);; 148 149 return String.valueOf((long) value); 150 } 151 152 public String toString() 153 { 154 return "max(" + _expr + ")"; 155 } 156 } 157 | Popular Tags |