001: package net.sourceforge.jaxor.impl;
002:
003: import net.sourceforge.jaxor.*;
004: import net.sourceforge.jaxor.api.*;
005:
006: import java.util.ArrayList;
007: import java.util.List;
008:
009: /**
010: * Created By: Mike
011: * Date: Oct 27, 2003
012: * Time: 10:54:06 PM
013: *
014: * Last Checkin: $Author: mrettig $
015: * Date: $Date: 2004/03/03 03:59:19 $
016: * Revision: $Revision: 1.4 $
017: */
018: public class QueryResultImpl implements QueryResult {
019:
020: private final MetaRow _meta;
021: private final String _sql;
022: private final QueryParams _params;
023: private final JaxorContext _context;
024:
025: public QueryResultImpl(MetaRow clzz, String sql,
026: QueryParams params, JaxorContext context) {
027: _meta = clzz;
028: _sql = sql;
029: _params = params;
030: _context = context;
031: }
032:
033: public QueryParams getParams() {
034: return _params;
035: }
036:
037: public String getSQL() {
038: return _sql;
039: }
040:
041: public List list() {
042: LazyLoader loader = new CachingLazyLoader(_meta.getImplClass(),
043: _sql, _params, _context.getQueryCache(), false) {
044: public Object resolve() {
045: return executeQuery();
046: }
047: };
048: return (List) LazyProxy.create(loader,
049: java.util.ArrayList.class);
050: }
051:
052: public EntityResultSet resultSet() {
053: final JaxorPreparedStatement stmt = preparedStatement();
054: return new EntityResultSetImpl(stmt.execute(), _context, _meta) {
055: /**
056: * override close so that the prepared statement is properly closed.
057: */
058: public void close() {
059: super .close();
060: stmt.close();
061: }
062: };
063: }
064:
065: public JaxorPreparedStatement preparedStatement() {
066: return new JaxorPreparedStatementImpl(_meta, _params, _sql,
067: _context);
068: }
069:
070: private List executeQuery() {
071: EntityResultSet iterator = resultSet();
072: List results = new ArrayList();
073: try {
074: while (iterator.hasNext())
075: results.add(iterator.next());
076: return results;
077: } finally {
078: iterator.close();
079: }
080: }
081:
082: public EntityInterface entity() {
083: LazyLoader loader = new CachingLazyLoader(_meta.getImplClass(),
084: _sql, _params, _context.getQueryCache(), true) {
085: public Object resolve() {
086: return executeQuery();
087: }
088: };
089: return (EntityInterface) loader.getValue();
090: }
091:
092: public EntityInterface entityProxy() {
093: LazyLoader loader = new CachingLazyLoader(_meta.getImplClass(),
094: _sql, _params, _context.getQueryCache(), true) {
095: public Object resolve() {
096: return executeQuery();
097: }
098: };
099: return (EntityInterface) LazyProxy.create(loader, _meta
100: .getImplClass());
101: }
102: }
|