1 29 30 package com.caucho.amber.query; 31 32 import com.caucho.amber.AmberQuery; 33 import com.caucho.amber.expr.ArgExpr; 34 import com.caucho.amber.manager.AmberConnection; 35 import com.caucho.amber.type.*; 36 import com.caucho.jdbc.JdbcMetaData; 37 38 import java.lang.reflect.InvocationTargetException ; 39 import java.lang.reflect.Method ; 40 import java.sql.PreparedStatement ; 41 import java.sql.ResultSet ; 42 import java.sql.ResultSetMetaData ; 43 import java.sql.SQLException ; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 51 public class UserQuery implements AmberQuery { 52 private AmberConnection _aConn; 53 private AbstractQuery _query; 54 private ResultSetImpl _rs; 55 56 private QueryCacheKey _cacheKey; 57 58 private Type []_argTypes; 59 private Object []_argValues; 60 private int _argLength = 0; 61 62 private int _firstResult = 0; 63 private int _maxResults = -1; 64 65 private long _cacheMaxAge; 66 67 private boolean _copyOnLoad = true; 68 private boolean _loadOnQuery; 69 70 public UserQuery(AbstractQuery query) 71 { 72 _query = query; 73 74 ArgExpr []argList = query.getArgList(); 75 76 _argTypes = new Type[argList.length]; 77 _argValues = new Object [argList.length]; 78 79 _argLength = argList.length; 80 } 81 82 85 public String getQueryString() 86 { 87 return _query.getQueryString(); 88 } 89 90 public void init(AmberConnection aConn) 91 { 92 setSession(aConn); 93 } 94 95 public void setSession(AmberConnection aConn) 96 { 97 _aConn = aConn; 98 } 99 100 public AmberConnection getSession() 101 { 102 return _aConn; 103 } 104 105 public AmberConnection getConnection() 106 { 107 return _aConn; 108 } 109 110 113 public void setLoadOnQuery(boolean loadOnQuery) 114 { 115 _loadOnQuery = loadOnQuery; 116 } 117 118 121 AbstractQuery getQuery() 122 { 123 return _query; 124 } 125 126 129 Type []getArgTypes() 130 { 131 return _argTypes; 132 } 133 134 137 Object []getArgValues() 138 { 139 return _argValues; 140 } 141 142 145 int getArgLength() 146 { 147 return _argLength; 148 } 149 150 153 public void setString(int index, String v) 154 { 155 _argTypes[index - 1] = StringType.create(); 156 _argValues[index - 1] = v; 157 _argLength = index; 158 } 159 160 163 public void setByte(int index, byte v) 164 { 165 _argTypes[index - 1] = ByteType.create(); 166 _argValues[index - 1] = new Integer (v); 167 _argLength = index; 168 } 169 170 173 public void setShort(int index, short v) 174 { 175 _argTypes[index - 1] = ShortType.create(); 176 _argValues[index - 1] = new Integer (v); 177 _argLength = index; 178 } 179 180 183 public void setInt(int index, int v) 184 { 185 _argTypes[index - 1] = IntegerType.create(); 186 _argValues[index - 1] = new Integer (v); 187 _argLength = index; 188 } 189 190 193 public void setLong(int index, long v) 194 { 195 _argTypes[index - 1] = LongType.create(); 196 _argValues[index - 1] = new Long (v); 197 _argLength = index; 198 } 199 200 203 public void setDouble(int index, double v) 204 { 205 _argTypes[index - 1] = DoubleType.create(); 206 _argValues[index - 1] = new Double (v); 207 _argLength = index; 208 } 209 210 213 public void setFloat(int index, float v) 214 { 215 _argTypes[index - 1] = FloatType.create(); 216 _argValues[index - 1] = new Float (v); 217 _argLength = index; 218 } 219 220 223 public void setTimestamp(int index, java.sql.Timestamp v) 224 { 225 _argTypes[index - 1] = SqlTimestampType.create(); 226 _argValues[index - 1] = v; 227 _argLength = index; 228 } 229 230 233 public void setDate(int index, java.sql.Date v) 234 { 235 _argTypes[index - 1] = SqlDateType.create(); 236 _argValues[index - 1] = v; 237 _argLength = index; 238 } 239 240 243 public void setObject(int index, Object v) 244 { 245 _argTypes[index - 1] = ObjectType.create(); 246 _argValues[index - 1] = v; 247 _argLength = index; 248 } 249 250 253 public void setObject(int index, Object v, Type type) 254 { 255 _argTypes[index - 1] = type; 256 _argValues[index - 1] = v; 257 _argLength = index; 258 } 259 260 263 public void setNull(int index, int v) 264 { 265 _argTypes[index - 1] = StringType.create(); 266 _argValues[index - 1] = null; 267 _argLength = index; 268 } 269 270 273 public void setFirstResult(int index) 274 { 275 _firstResult = index; 276 } 277 278 281 public void setMaxResults(int index) 282 { 283 _maxResults = index; 284 } 285 286 289 public int getMaxResults() 290 { 291 return _maxResults; 292 } 293 294 297 public ResultSet executeQuery() 298 throws SQLException 299 { 300 _aConn.flushNoChecks(); 301 302 if (_rs == null) 303 _rs = new ResultSetImpl(); 304 305 SelectQuery query = (SelectQuery) _query; 306 307 _rs.setQuery(query); 308 _rs.setSession(_aConn); 309 _rs.setFirstResult(_firstResult); 310 _rs.setMaxResults(_maxResults); 311 312 int chunkSize = _aConn.getCacheChunkSize(); 313 boolean isCacheable; 314 315 if (chunkSize <= _firstResult) 316 isCacheable = false; 317 else if (_aConn.isInTransaction() && ! query.isTableReadOnly()) 318 isCacheable = false; 319 else if (! query.isCacheable()) 320 isCacheable = false; 321 else 322 isCacheable = true; 323 324 ResultSetCacheChunk cacheChunk = null; 325 ResultSetMetaData metaData = null; 326 327 if (isCacheable) { 328 int row = 0; 329 330 cacheChunk = _aConn.getQueryCacheChunk(query.getSQL(), _argValues, row); 331 metaData = _aConn.getQueryMetaData(); 332 333 _rs.setCacheChunk(cacheChunk, metaData); 334 _rs.setUserQuery(this); 335 } 336 337 if (cacheChunk == null) { 338 ResultSet rs; 339 340 rs = executeQuery(0, _maxResults); 341 342 metaData = rs.getMetaData(); 343 344 _rs.setResultSet(rs, metaData); 345 346 if (isCacheable) { 347 cacheChunk = new ResultSetCacheChunk(); 348 cacheChunk.setQuery(query); 349 350 _rs.fillCacheChunk(cacheChunk); 351 352 _rs.setCacheChunk(cacheChunk, metaData); 353 354 _aConn.putQueryCacheChunk(query.getSQL(), _argValues, 0, 355 cacheChunk, metaData); 356 } 357 } 358 359 _rs.init(); 360 361 return _rs; 362 } 363 364 367 ResultSet executeQuery(int row, int maxResults) 368 throws SQLException 369 { 370 String sql = _query.getSQL(); 371 372 if (maxResults > 0) { 373 JdbcMetaData metaData = _aConn.getAmberManager().getMetaData(); 374 375 sql = metaData.limit(sql, maxResults); 377 } 378 379 PreparedStatement pstmt = _aConn.prepareStatement(sql); 380 ArgExpr []args = _query.getArgList(); 381 382 if (args.length > 0) 383 pstmt.clearParameters(); 384 385 for (int i = 0; i < args.length; i++) { 386 args[i].setParameter(pstmt, i + 1, _argTypes, _argValues); 387 } 388 389 ResultSet rs = pstmt.executeQuery(); 390 391 for (int i = 0; i < row && rs.next(); i++) { 392 } 393 394 return rs; 395 } 396 397 400 public int executeUpdate() 401 throws SQLException 402 { 403 _aConn.flushNoChecks(); 404 406 String sql = _query.getSQL(); 407 408 PreparedStatement pstmt = _aConn.prepareStatement(sql); 409 ArgExpr []args = _query.getArgList(); 410 411 if (args.length > 0) 412 pstmt.clearParameters(); 413 414 for (int i = 0; i < args.length; i++) { 415 args[i].setParameter(pstmt, i + 1, _argTypes, _argValues); 416 } 417 418 _query.prepare(this, _aConn); 419 420 int count = pstmt.executeUpdate(); 421 422 if (count != 0) 423 _query.complete(this, _aConn); 424 425 return count; 426 } 427 428 431 public void setCacheMaxAge(long ms) 432 { 433 _cacheMaxAge = ms; 434 } 435 436 439 public List <Object > list() 440 throws SQLException 441 { 442 ArrayList <Object > list = new ArrayList <Object >(); 443 444 list(list); 445 446 return list; 447 } 448 449 452 public Object getSingleResult() 453 throws SQLException 454 { 455 SelectQuery query = (SelectQuery) _query; 456 ResultSet rs = null; 457 458 _aConn.pushDepth(); 459 try { 460 rs = executeQuery(); 461 if (rs.next()) 462 return rs.getObject(1); 463 464 return null; 465 } catch (SQLException e) { 466 throw e; 467 } finally { 468 _aConn.popDepth(); 469 470 if (rs != null) 471 rs.close(); 472 } 473 } 474 475 478 public void list(List <Object > list) 479 throws SQLException 480 { 481 SelectQuery query = (SelectQuery) _query; 482 ResultSet rs = null; 483 484 _aConn.pushDepth(); 485 try { 486 rs = executeQuery(); 487 488 int tupleCount = query.getResultCount(); 489 490 while (rs.next()) { 491 if (tupleCount == 1) { 492 Object value = rs.getObject(1); 493 494 list.add(value); 495 } 496 else { 497 Object []values = new Object [tupleCount]; 498 499 for (int i = 0; i < tupleCount; i++) { 500 values[i] = rs.getObject(i + 1); 501 } 502 503 list.add(values); 504 } 505 } 506 } catch (SQLException e) { 507 throw e; 508 } finally { 509 _aConn.popDepth(); 510 511 if (rs != null) 512 rs.close(); 513 } 514 } 515 516 519 public void list(Map <Object ,Object > map, 520 Method methodGetMapKey) 521 throws SQLException , 522 IllegalAccessException , 523 InvocationTargetException 524 { 525 SelectQuery query = (SelectQuery) _query; 526 527 ResultSet rs = null; 528 529 _aConn.pushDepth(); 530 try { 531 rs = executeQuery(); 532 533 int tupleCount = query.getResultCount(); 534 535 while (rs.next()) { 536 if (tupleCount == 1) { 537 Object value = rs.getObject(1); 538 539 com.caucho.amber.entity.Entity entity; 540 entity = (com.caucho.amber.entity.Entity) value; 541 542 Object mapKey; 543 if (methodGetMapKey == null) 544 mapKey = entity.__caucho_getPrimaryKey(); 545 else 546 mapKey = methodGetMapKey.invoke(entity, null); 547 548 map.put(mapKey, value); 549 } 550 else { 551 Object []values = new Object [tupleCount]; 552 553 for (int i = 0; i < tupleCount; i++) { 554 values[i] = rs.getObject(i + 1); 555 } 556 557 Object mapKey = values[0]; 559 map.put(mapKey, values); 560 } 561 } 562 } catch (SQLException e) { 563 throw e; 564 } finally { 565 _aConn.popDepth(); 566 567 if (rs != null) 568 rs.close(); 569 } 570 } 571 572 public String toString() 573 { 574 return "UserQuery[" + _query.getQueryString() + "]"; 575 } 576 } 577 | Popular Tags |