1 29 30 package com.caucho.db.sql; 31 32 import com.caucho.log.Log; 33 34 import java.sql.SQLException ; 35 import java.util.ArrayList ; 36 import java.util.logging.Logger ; 37 38 class EqExpr extends Expr { 39 private static final Logger log = Log.open(EqExpr.class); 40 41 private Expr _left; 42 private Expr _right; 43 44 EqExpr(Expr left, Expr right) 45 { 46 _left = left; 47 _right = right; 48 49 if (left == null || right == null) 50 throw new NullPointerException (); 51 52 if (right instanceof UnboundIdentifierExpr && 53 ! (left instanceof UnboundIdentifierExpr)) { 54 Expr temp = _right; 55 _right = _left; 56 _left = temp; 57 } 58 } 59 60 protected Expr bind(Query query) 61 throws SQLException 62 { 63 Expr newLeft = _left.bind(query); 64 Expr newRight = _right.bind(query); 65 66 if (newLeft instanceof ColumnExpr && 67 newLeft.getType().equals(String .class)) { 68 return new StringEqExpr((ColumnExpr) newLeft, newRight); 69 } 70 else if (newRight instanceof ColumnExpr && 71 newRight.getType().equals(String .class)) { 72 return new StringEqExpr((ColumnExpr) newRight, newLeft); 73 } 74 75 if (_left == newLeft && _right == newRight) 76 return this; 77 else 78 return new EqExpr(newLeft, newRight); 79 } 80 81 84 public IndexExpr getIndexExpr(FromItem item) 85 { 86 if (_left instanceof IdExpr) { 87 IdExpr expr = (IdExpr) _left; 88 89 if (expr.getColumn().getIndex() != null && 90 item == expr.getFromItem()) { 91 return new IndexExpr(expr, _right); 92 } 93 } 94 95 if (_right instanceof IdExpr) { 96 IdExpr expr = (IdExpr) _right; 97 98 if (expr.getColumn().getIndex() != null && 99 item == expr.getFromItem()) { 100 return new IndexExpr(expr, _left); 101 } 102 } 103 104 return null; 105 } 106 107 110 public Class getType() 111 { 112 return boolean.class; 113 } 114 115 118 public long cost(ArrayList <FromItem> fromList) 119 { 120 if (_left instanceof UnboundIdentifierExpr && 121 _right.cost(fromList) == 0) { 122 UnboundIdentifierExpr id = (UnboundIdentifierExpr) _left; 123 124 return id.lookupCost(fromList); 125 } 126 else if (_right instanceof UnboundIdentifierExpr && 127 _left.cost(fromList) == 0) { 128 UnboundIdentifierExpr id = (UnboundIdentifierExpr) _right; 129 130 return id.lookupCost(fromList); 131 } 132 else { 133 return subCost(fromList); 134 } 135 } 136 137 140 public long subCost(ArrayList <FromItem> fromList) 141 { 142 return _left.subCost(fromList) + _right.subCost(fromList); 143 } 144 145 148 public boolean isNull(QueryContext context) 149 throws SQLException 150 { 151 return (_left.isNull(context) || _right.isNull(context)); 152 } 153 154 157 public int evalBoolean(QueryContext context) 158 throws SQLException 159 { 160 if (_left.isNull(context) || _right.isNull(context)) 161 return UNKNOWN; 162 163 if (_left.isLong() && _right.isLong()) { 164 if (_left.evalLong(context) == _right.evalLong(context)) 165 return TRUE; 166 else 167 return FALSE; 168 } 169 else if (_left.isDouble() && _right.isDouble()) { 170 if (_left.evalDouble(context) == _right.evalDouble(context)) 171 return TRUE; 172 else 173 return FALSE; 174 } 175 else { 176 String leftValue = _left.evalString(context); 177 String rightValue = _right.evalString(context); 178 179 if (leftValue == rightValue || leftValue.equals(rightValue)) 180 return TRUE; 181 else 182 return FALSE; 183 } 184 } 185 186 public String evalString(QueryContext context) 187 throws SQLException 188 { 189 throw new SQLException ("can't convert string to boolean"); 190 } 191 192 197 public void evalGroup(QueryContext context) 198 throws SQLException 199 { 200 _left.evalGroup(context); 201 _right.evalGroup(context); 202 } 203 204 public String toString() 205 { 206 return "(" + _left + " = " + _right + ")"; 207 } 208 } 209 | Popular Tags |