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 CmpExpr extends Expr { 39 private static final Logger log = Log.open(CmpExpr.class); 40 41 private Expr _left; 42 private Expr _right; 43 private int _op; 44 45 CmpExpr(Expr left, Expr right, int op) 46 { 47 _left = left; 48 _right = right; 49 _op = op; 50 } 51 52 protected Expr bind(Query query) 53 throws SQLException 54 { 55 _left = _left.bind(query); 56 _right = _right.bind(query); 57 58 switch (_op) { 59 case Parser.LT: 60 case Parser.LE: 61 case Parser.GT: 62 case Parser.GE: 63 return new DoubleCmpExpr(_op, _left, _right); 64 } 65 66 if (_left.isDouble() || _right.isDouble()) 67 return new DoubleCmpExpr(_op, _left, _right); 68 69 return this; 70 } 71 72 75 public Class getType() 76 { 77 return boolean.class; 78 } 79 80 83 public long subCost(ArrayList <FromItem> fromList) 84 { 85 return _left.subCost(fromList) + _right.subCost(fromList); 86 } 87 88 91 public int evalBoolean(QueryContext context) 92 throws SQLException 93 { 94 if (_left.isNull(context) || _right.isNull(context)) 95 return UNKNOWN; 96 97 switch (_op) { 98 case Parser.NE: 99 { 100 String leftValue = _left.evalString(context); 101 String rightValue = _right.evalString(context); 102 103 if (! (leftValue == rightValue || 104 leftValue != null && leftValue.equals(rightValue))) 105 return TRUE; 106 else 107 return FALSE; 108 } 109 110 case Parser.EQ: 111 { 112 String leftValue = _left.evalString(context); 113 String rightValue = _right.evalString(context); 114 115 if (leftValue == rightValue || 116 leftValue != null && leftValue.equals(rightValue)) 117 return TRUE; 118 else 119 return FALSE; 120 } 121 122 default: 123 throw new SQLException ("can't compare"); 124 } 125 } 126 127 public String evalString(QueryContext context) 128 throws SQLException 129 { 130 throw new SQLException ("can't convert string to boolean"); 131 } 132 133 138 public void evalGroup(QueryContext context) 139 throws SQLException 140 { 141 _left.evalGroup(context); 142 _right.evalGroup(context); 143 } 144 145 public String toString() 146 { 147 switch (_op) { 148 case Parser.EQ: 149 return "(" + _left + " = " + _right + ")"; 150 case Parser.NE: 151 return "(" + _left + " <> " + _right + ")"; 152 case Parser.LT: 153 return "(" + _left + " < " + _right + ")"; 154 case Parser.LE: 155 return "(" + _left + " <= " + _right + ")"; 156 case Parser.GT: 157 return "(" + _left + " > " + _right + ")"; 158 case Parser.GE: 159 return "(" + _left + " >= " + _right + ")"; 160 default: 161 return "(" + _left + " =? " + _right + ")"; 162 } 163 } 164 } 165 | Popular Tags |