1 28 29 package com.caucho.ejb.ql; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.util.CharBuffer; 33 34 37 class BetweenExpr extends Expr { 38 private Expr _value; 40 private Expr _min; 42 private Expr _max; 44 private boolean _isNot; 46 47 55 BetweenExpr(Expr value, Expr min, Expr max, boolean isNot) 56 throws ConfigException 57 { 58 _value = value; 59 _min = min; 60 _max = max; 61 _isNot = isNot; 62 63 evalTypes(); 64 } 65 66 69 void evalTypes() 70 throws ConfigException 71 { 72 if (getJavaType() != null) 73 return; 74 75 setJavaType(boolean.class); 76 77 if (_value.isDate() && _min.isDate() && _max.isDate()) 78 return; 79 80 else if (_value.isDate()) 81 throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _value)); 82 83 else if (_min.isDate()) 84 throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _min)); 85 86 else if (_max.isDate()) 87 throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _max)); 88 89 if (! _value.isNumeric()) 90 throw error(L.l("BETWEEN expects numeric expression at `{0}'", _value)); 91 92 if (! _min.isNumeric()) 93 throw error(L.l("BETWEEN expects numeric expression at `{0}'", _min)); 94 95 if (! _max.isNumeric()) 96 throw error(L.l("BETWEEN expects numeric expression at `{0}'", _max)); 97 } 98 99 100 105 void generateWhere(CharBuffer cb) 106 { 107 _value.generateWhereSubExpr(cb); 108 109 if (_isNot) 110 cb.append(" NOT BETWEEN "); 111 else 112 cb.append(" BETWEEN "); 113 114 _min.generateWhereSubExpr(cb); 115 116 cb.append(" AND "); 117 118 _max.generateWhereSubExpr(cb); 119 } 120 121 124 void generateWhereSubExpr(CharBuffer cb) 125 { 126 cb.append("("); 127 generateWhere(cb); 128 cb.append(")"); 129 } 130 131 public String toString() 132 { 133 if (_isNot) 134 return "NOT BETWEEN " + _min + " AND " + _max; 135 else 136 return "BETWEEN " + _min + " AND " + _max; 137 } 138 } 139 | Popular Tags |