1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.log.Log; 32 33 import java.sql.SQLException ; 34 import java.util.ArrayList ; 35 import java.util.logging.Logger ; 36 37 class UnaryExpr extends Expr { 38 private static final Logger log = Log.open(UnaryExpr.class); 39 40 private Expr _sub; 41 private int _op; 42 43 UnaryExpr(Expr sub, int op) 44 { 45 _sub = sub; 46 _op = op; 47 } 48 49 52 protected Expr bind(Query query) 53 throws SQLException 54 { 55 Expr newSub = _sub.bind(query); 56 57 switch (_op) { 58 case '-': 59 if (! newSub.isDouble()) 60 throw new SQLException (L.l("unary minus requires a numeric expression at '{0}'", newSub)); 61 break; 62 } 63 64 if (newSub == _sub) 65 return this; 66 else 67 return new UnaryExpr(newSub, _op); 68 } 69 70 73 public Class getType() 74 { 75 switch (_op) { 76 case '-': 77 if (_sub.isLong()) 78 return long.class; 79 else 80 return double.class; 81 82 default: 83 return Object .class; 84 } 85 } 86 87 90 public long subCost(ArrayList <FromItem> fromList) 91 { 92 return _sub.subCost(fromList); 93 } 94 95 98 public boolean isNull(QueryContext context) 99 throws SQLException 100 { 101 return _sub.isNull(context); 102 } 103 104 111 public int evalBoolean(QueryContext context) 112 throws SQLException 113 { 114 switch (_op) { 115 default: 116 throw new IllegalStateException (); 117 } 118 } 119 120 127 public long evalLong(QueryContext context) 128 throws SQLException 129 { 130 switch (_op) { 131 case '-': 132 return - _sub.evalLong(context); 133 134 default: 135 throw new IllegalStateException (); 136 } 137 } 138 139 146 public double evalDouble(QueryContext context) 147 throws SQLException 148 { 149 switch (_op) { 150 case '-': 151 return - _sub.evalDouble(context); 152 153 default: 154 throw new IllegalStateException (); 155 } 156 } 157 158 165 public String evalString(QueryContext context) 166 throws SQLException 167 { 168 switch (_op) { 169 case '-': 170 if (isLong()) 171 return String.valueOf(evalLong(context)); 172 else 173 return String.valueOf(evalDouble(context)); 174 175 default: 176 throw new IllegalStateException (); 177 } 178 } 179 180 185 public void evalGroup(QueryContext context) 186 throws SQLException 187 { 188 _sub.evalGroup(context); 189 } 190 191 public String toString() 192 { 193 switch (_op) { 194 case '-': 195 return "- " + _sub; 196 197 default: 198 throw new IllegalStateException ("can't compare:" + _op); 199 } 200 } 201 } 202 | Popular Tags |