1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.sql.Connection ; 59 import java.sql.PreparedStatement ; 60 import java.sql.ResultSet ; 61 import java.sql.SQLException ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.access.OperationObserver; 65 import org.objectstyle.cayenne.access.QueryLogger; 66 import org.objectstyle.cayenne.access.ResultIterator; 67 import org.objectstyle.cayenne.access.trans.SelectTranslator; 68 import org.objectstyle.cayenne.access.util.DistinctResultIterator; 69 import org.objectstyle.cayenne.dba.DbAdapter; 70 import org.objectstyle.cayenne.map.EntityResolver; 71 import org.objectstyle.cayenne.query.SelectQuery; 72 73 79 public class SelectAction extends BaseSQLAction { 80 81 protected SelectQuery query; 82 83 public SelectAction(SelectQuery query, DbAdapter adapter, 84 EntityResolver entityResolver) { 85 super(adapter, entityResolver); 86 this.query = query; 87 } 88 89 protected SelectTranslator createTranslator(Connection connection) { 90 SelectTranslator translator = new SelectTranslator(); 91 translator.setQuery(query); 92 translator.setAdapter(adapter); 93 translator.setEntityResolver(getEntityResolver()); 94 translator.setConnection(connection); 95 return translator; 96 } 97 98 public void performAction(Connection connection, OperationObserver observer) 99 throws SQLException , Exception { 100 101 long t1 = System.currentTimeMillis(); 102 103 SelectTranslator translator = createTranslator(connection); 104 PreparedStatement prepStmt = translator.createStatement(query.getLoggingLevel()); 105 ResultSet rs = prepStmt.executeQuery(); 106 107 RowDescriptor descriptor = new RowDescriptor( 108 translator.getResultColumns(), 109 getAdapter().getExtendedTypes()); 110 JDBCResultIterator workerIterator = new JDBCResultIterator( 111 connection, 112 prepStmt, 113 rs, 114 descriptor, 115 query.getFetchLimit()); 116 117 ResultIterator it = workerIterator; 118 119 if (translator.isSuppressingDistinct()) { 121 it = new DistinctResultIterator(workerIterator, translator.getRootDbEntity()); 122 } 123 124 127 if (!observer.isIteratedResult()) { 128 List resultRows = it.dataRows(true); 131 QueryLogger.logSelectCount(query.getLoggingLevel(), resultRows.size(), System 132 .currentTimeMillis() 133 - t1); 134 135 observer.nextDataRows(query, resultRows); 136 } 137 else { 138 try { 139 workerIterator.setClosingConnection(true); 140 observer.nextDataRows(translator.getQuery(), it); 141 } 142 catch (Exception ex) { 143 it.close(); 144 throw ex; 145 } 146 } 147 } 148 } | Popular Tags |