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 BetweenExpr extends Expr { 38 private static final Logger log = Log.open(BetweenExpr.class); 39 40 private Expr _expr; 41 private Expr _min; 42 private Expr _max; 43 private boolean _isNot; 44 45 private boolean _isLong; 46 47 BetweenExpr(Expr expr, Expr min, Expr max, boolean isNot) 48 { 49 _expr = expr; 50 _min = min; 51 _max = max; 52 _isNot = isNot; 53 } 54 55 protected Expr bind(Query query) 56 throws SQLException 57 { 58 _expr = _expr.bind(query); 59 _min = _min.bind(query); 60 _max = _max.bind(query); 61 62 _isLong = _expr.isLong(); 63 64 return this; 65 } 66 67 70 public Class getType() 71 { 72 return boolean.class; 73 } 74 75 78 public long subCost(ArrayList <FromItem> fromList) 79 { 80 return (_expr.subCost(fromList) + 81 _min.subCost(fromList) + 82 _max.subCost(fromList)); 83 } 84 85 88 public int evalBoolean(QueryContext context) 89 throws SQLException 90 { 91 if (_expr.isNull(context)) 92 return UNKNOWN; 93 94 if (_isLong) { 95 long min = _min.evalLong(context); 96 long max = _max.evalLong(context); 97 98 long value = _expr.evalLong(context); 99 100 if (_isNot) 101 return ! (min <= value && value <= max) ? TRUE : FALSE; 102 else 103 return min <= value && value <= max ? TRUE : FALSE; 104 } 105 else { 106 double min = _min.evalDouble(context); 107 double max = _max.evalDouble(context); 108 109 double value = _expr.evalDouble(context); 110 111 if (_isNot) 112 return ! (min <= value && value <= max) ? TRUE : FALSE; 113 else 114 return min <= value && value <= max ? TRUE : FALSE; 115 } 116 } 117 118 public String evalString(QueryContext context) 119 throws SQLException 120 { 121 throw new SQLException ("can't convert string to boolean"); 122 } 123 124 129 public void evalGroup(QueryContext context) 130 throws SQLException 131 { 132 _expr.evalGroup(context); 133 _min.evalGroup(context); 134 _max.evalGroup(context); 135 } 136 137 public String toString() 138 { 139 return "(" + _expr + " BETWEEN " + _min + " AND " + _max + ")"; 140 } 141 } 142 | Popular Tags |