1 29 30 package com.caucho.db.sql; 31 32 import com.caucho.db.Database; 33 import com.caucho.db.store.Transaction; 34 import com.caucho.db.table.TableIterator; 35 import com.caucho.log.Log; 36 import com.caucho.sql.SQLExceptionWrapper; 37 import com.caucho.util.CharBuffer; 38 39 import java.io.IOException ; 40 import java.sql.SQLException ; 41 import java.util.Iterator ; 42 import java.util.logging.Logger ; 43 44 public class SelectQuery extends Query { 45 private static final Logger log = Log.open(SelectQuery.class); 46 47 private Expr []_results; 48 private String []_resultNames; 49 50 private boolean []_groupFields; 51 52 private Order _order; 53 54 SelectQuery(Database db, String sql) 55 throws SQLException 56 { 57 super(db, sql); 58 } 59 60 SelectQuery(Database db, String sql, FromItem []fromItems) 61 throws SQLException 62 { 63 super(db, sql, fromItems); 64 } 65 66 void setResults(Expr []resultExprs) 67 throws SQLException 68 { 69 _results = new Expr[resultExprs.length]; 70 71 for (int i = 0; i < resultExprs.length; i++) { 72 _results[i] = resultExprs[i]; 73 } 74 75 setDataFields(resultExprs.length); 76 } 77 78 Expr []getResults() 79 { 80 return _results; 81 } 82 83 86 public void setGroupResult(int index) 87 { 88 if (_groupFields == null) 89 _groupFields = new boolean[_results.length]; 90 91 _groupFields[index] = true; 92 } 93 94 protected void bind() 95 throws SQLException 96 { 97 super.bind(); 98 99 for (int i = 0; i < _results.length; i++) { 100 _results[i] = _results[i].bind(this); 101 } 102 103 if (isGroup()) { 104 for (int i = 0; i < _results.length; i++) { 105 if (isGroup() && ! (_results[i] instanceof GroupExpr)) 106 _results[i] = new GroupResultExpr(i, _results[i]); 107 } 108 } 109 110 for (int i = 0; i < _results.length; i++) { 111 _results[i] = _results[i].bind(this); 112 } 113 114 _resultNames = new String [_results.length]; 115 116 for (int i = 0; i < _resultNames.length; i++) 117 _resultNames[i] = _results[i].getName(); 118 } 119 120 void setOrder(Order order) 121 { 122 _order = order; 123 } 124 125 128 public boolean isSelect() 129 { 130 return true; 131 } 132 133 136 Class getType() 137 { 138 if (_results.length == 1) 139 return _results[0].getType(); 140 else 141 return Object .class; 142 } 143 144 147 public void execute(QueryContext context, Transaction xa) 148 throws SQLException 149 { 150 SelectResult result = SelectResult.create(_results, _order); 151 FromItem []fromItems = getFromItems(); 152 TableIterator []rows = null; 153 154 try { 155 rows = result.initRows(fromItems); 156 context.init(xa, rows, isReadOnly()); 157 158 if (isGroup()) 159 executeGroup(result, rows, context, xa); 160 else 161 execute(result, rows, context, xa); 162 163 result.initRead(); 164 165 context.setResult(result); 166 } catch (IOException e) { 167 throw new SQLExceptionWrapper(e); 168 } finally { 169 context.unlock(); 172 173 if (rows != null) 174 freeRows(rows, rows.length); 175 } 176 } 177 178 181 private void execute(SelectResult result, 182 TableIterator []rows, 183 QueryContext context, 184 Transaction xa) 185 throws SQLException , IOException 186 { 187 FromItem []fromItems = getFromItems(); 188 int rowLength = fromItems.length; 189 190 if (start(rows, rowLength, context, xa)) { 191 do { 192 result.startRow(); 193 for (int i = 0; i < _results.length; i++) { 194 _results[i].evalToResult(context, result); 195 } 196 } while (nextTuple(rows, rowLength, context, xa)); 197 } 198 } 199 200 private void executeGroup(SelectResult result, 201 TableIterator []rows, 202 QueryContext context, 203 Transaction transaction) 204 throws SQLException , IOException 205 { 206 FromItem []fromItems = getFromItems(); 207 int rowLength = fromItems.length; 208 209 Expr []results = _results; 210 int resultsLength = results.length; 211 212 216 217 if (_groupFields == null) 218 _groupFields = new boolean[0]; 219 220 boolean []groupByFields = _groupFields; 221 int groupByLength = _groupFields.length; 222 223 if (start(rows, rowLength, context, transaction)) { 224 do { 225 context.initGroup(getDataFields(), _groupFields); 226 227 for (int i = 0; i < groupByLength; i++) { 228 if (groupByFields[i]) 229 results[i].evalGroup(context); 230 } 231 232 context.selectGroup(); 233 234 for (int i = 0; i < resultsLength; i++) { 235 if (! (i < groupByLength && groupByFields[i])) 236 results[i].evalGroup(context); 237 } 238 } while (nextTuple(rows, rowLength, context, transaction)); 239 } 240 241 Iterator<GroupItem> groupIter = context.groupResults(); 242 243 while (groupIter.hasNext()) { 244 GroupItem item = groupIter.next(); 245 246 context.setGroupItem(item); 247 248 result.startRow(); 249 for (int i = 0; i < results.length; i++) { 250 results[i].evalToResult(context, result); 251 } 252 } 253 } 254 255 public String toString() 256 { 257 CharBuffer cb = CharBuffer.allocate(); 258 cb.append("SelectQuery["); 259 cb.append("SELECT "); 260 for (int i = 0; i < _results.length; i++) { 261 if (i != 0) 262 cb.append(","); 263 cb.append(_results[i]); 264 } 265 cb.append(" FROM "); 266 267 FromItem []fromItems = getFromItems(); 268 for (int i = 0; i < fromItems.length; i++) { 269 if (i != 0) 270 cb.append(","); 271 cb.append(fromItems[i]); 272 } 273 274 if (_whereExpr != null) { 275 cb.append(" WHERE " + _whereExpr); 276 } 277 cb.append("]"); 278 279 return cb.close(); 280 } 281 } 282 | Popular Tags |