1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import java.sql.SQLException ; 35 import java.util.ArrayList ; 36 import java.util.logging.Logger ; 37 38 public class ExistsExpr extends SubSelectExpr { 39 protected static final L10N L = new L10N(ExistsExpr.class); 40 private static final Logger log = Log.open(ExistsExpr.class); 41 42 private int _groupIndex; 43 private ExistsQuery _exists; 44 45 private Query _parentQuery; 46 47 ExistsExpr(ExistsQuery query) 48 { 49 super(query); 50 51 _exists = query; 52 } 53 54 57 protected Expr bind(Query query) 58 throws SQLException 59 { 60 if (_parentQuery != null) 61 return this; 62 63 _parentQuery = query; 64 65 super.bind(query); 66 67 _groupIndex = query.getDataFields(); 68 69 query.setDataFields(_groupIndex + 1); 70 71 _exists.bind(); 72 73 return this; 74 } 75 76 79 public Class getType() 80 { 81 return boolean.class; 82 } 83 84 ArrayList <SubSelectParamExpr> getParamExprs() 85 { 86 return _exists.getParamExprs(); 87 } 88 89 92 public long subCost(ArrayList <FromItem> fromList) 93 { 94 ArrayList <SubSelectParamExpr> paramExprs = getParamExprs(); 95 96 long cost = 10; 97 98 for (int i = 0; i < paramExprs.size(); i++) 99 cost += paramExprs.get(i).getExpr().cost(fromList); 100 101 return 2 * cost; 102 } 103 104 107 void evaluate(QueryContext context) 108 throws SQLException 109 { 110 QueryContext subcontext = QueryContext.allocate(); 111 112 ArrayList <SubSelectParamExpr> paramExprs = getParamExprs(); 113 114 for (int i = 0; i < paramExprs.size(); i++) { 115 paramExprs.get(i).eval(context, subcontext); 116 } 117 118 boolean exists = _exists.exists(subcontext, context.getTransaction()); 119 Data data = context.getGroupData(_groupIndex); 120 121 data.setBoolean(exists); 122 123 QueryContext.free(subcontext); 124 } 125 126 133 public boolean isNull(QueryContext context) 134 throws SQLException 135 { 136 Data data = context.getGroupData(_groupIndex); 137 138 return data.isNull(); 139 } 140 141 148 public int evalBoolean(QueryContext context) 149 throws SQLException 150 { 151 Data data = context.getGroupData(_groupIndex); 152 153 return data.getBoolean(); 154 } 155 156 public String toString() 157 { 158 return "ExistsExpr[" + _exists + "]"; 159 } 160 } 161 | Popular Tags |