1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.CharBuffer; 33 34 import java.sql.SQLException ; 35 import java.util.ArrayList ; 36 import java.util.logging.Logger ; 37 38 class AndExpr extends Expr { 39 private static final Logger log = Log.open(AndExpr.class); 40 41 private ArrayList <Expr> _exprs = new ArrayList <Expr>(); 42 43 AndExpr() 44 { 45 } 46 47 void add(Expr expr) 48 { 49 _exprs.add(expr); 50 } 51 52 Expr getSingleExpr() 53 { 54 if (_exprs.size() == 0) 55 return null; 56 else if (_exprs.size() == 1) 57 return _exprs.get(0); 58 else 59 return this; 60 } 61 62 protected Expr bind(Query query) 63 throws SQLException 64 { 65 for (int i = 0; i < _exprs.size(); i++) { 66 Expr expr = _exprs.get(i); 67 68 expr = expr.bind(query); 69 70 if (! expr.getType().equals(boolean.class)) 71 throw new SQLException (L.l("AND requires boolean operands at {0}", 72 expr)); 73 74 _exprs.set(i, expr); 75 } 76 77 return this; 78 } 79 80 83 public Class getType() 84 { 85 return boolean.class; 86 } 87 88 91 public long subCost(ArrayList <FromItem> fromList) 92 { 93 long cost = 0; 94 95 for (int i = 0; i < _exprs.size(); i++) { 96 cost += _exprs.get(i).subCost(fromList); 97 } 98 99 return cost; 100 } 101 102 105 public void splitAnd(ArrayList <Expr> andProduct) 106 { 107 for (int i = 0; i < _exprs.size(); i++) { 108 _exprs.get(i).splitAnd(andProduct); 109 } 110 } 111 112 115 public boolean isNull(QueryContext context) 116 throws SQLException 117 { 118 boolean isNull = false; 119 for (int i = 0; i < _exprs.size(); i++) { 120 int value = _exprs.get(i).evalBoolean(context); 121 122 if (value == FALSE) 123 return false; 124 else if (value != TRUE) 125 isNull = true; 126 } 127 128 return isNull; 129 } 130 131 134 public int evalBoolean(QueryContext context) 135 throws SQLException 136 { 137 int value = TRUE; 138 139 for (int i = 0; i < _exprs.size(); i++) { 140 int subValue = _exprs.get(i).evalBoolean(context); 141 142 if (subValue == FALSE) 143 return FALSE; 144 else if (subValue == UNKNOWN) 145 value = UNKNOWN; 146 } 147 148 return value; 149 } 150 151 public String evalString(QueryContext context) 152 throws SQLException 153 { 154 switch (evalBoolean(context)) { 155 case TRUE: 156 return "1"; 157 case FALSE: 158 return "0"; 159 default: 160 return null; 161 } 162 } 163 164 public String toString() 165 { 166 CharBuffer cb = CharBuffer.allocate(); 167 cb.append("("); 168 169 for (int i = 0; i < _exprs.size(); i++) { 170 if (i != 0) 171 cb.append(" AND "); 172 173 cb.append(_exprs.get(i)); 174 } 175 176 cb.append(")"); 177 178 return cb.close(); 179 } 180 } 181 | Popular Tags |