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 SubSelectExpr extends Expr { 39 protected static final L10N L = new L10N(SubSelectExpr.class); 40 private static final Logger log = Log.open(SubSelectExpr.class); 41 42 private int _groupIndex; 43 private SelectQuery _subselect; 44 45 private Query _parentQuery; 46 47 SubSelectExpr(SelectQuery query) 48 { 49 _subselect = query; 50 } 51 52 55 public SelectQuery getSubSelect() 56 { 57 return _subselect; 58 } 59 60 63 protected Expr bind(Query query) 64 throws SQLException 65 { 66 if (_parentQuery != null) 67 return this; 68 69 _parentQuery = query; 70 _groupIndex = query.getDataFields(); 71 72 query.setDataFields(_groupIndex + 1); 73 74 _subselect.bind(); 75 76 return this; 77 } 78 79 82 public Class getType() 83 { 84 return _subselect.getType(); 85 } 86 87 ArrayList <SubSelectParamExpr> getParamExprs() 88 { 89 return _subselect.getParamExprs(); 90 } 91 92 95 public long subCost(ArrayList <FromItem> fromList) 96 { 97 ArrayList <SubSelectParamExpr> paramExprs = getParamExprs(); 98 99 long cost = 10; 100 101 for (int i = 0; i < paramExprs.size(); i++) 102 cost += paramExprs.get(i).getExpr().cost(fromList); 103 104 return 2 * cost; 105 } 106 107 110 void evaluate(QueryContext context) 111 throws SQLException 112 { 113 QueryContext subcontext = QueryContext.allocate(); 114 115 ArrayList <SubSelectParamExpr> paramExprs = getParamExprs(); 116 117 for (int i = 0; i < paramExprs.size(); i++) { 118 paramExprs.get(i).eval(context, subcontext); 119 } 120 121 _subselect.execute(subcontext, context.getTransaction()); 122 123 SelectResult result = subcontext.getResult(); 124 125 Data data = context.getGroupData(_groupIndex); 126 127 if (result.next()) { 128 Class type = _subselect.getType(); 129 130 if (long.class.equals(type)) 131 data.setLong(result.getLong(0)); 132 else 133 data.setString(result.getString(0)); 134 } 135 else { 136 data.clear(); 137 } 138 139 result.close(); 140 QueryContext.free(subcontext); 141 } 142 143 150 public String evalString(QueryContext context) 151 throws SQLException 152 { 153 Data data = context.getGroupData(_groupIndex); 154 155 return data.getString(); 156 } 157 158 public String toString() 159 { 160 return "SubSelectExpr[" + _subselect + "]"; 161 } 162 } 163 | Popular Tags |