1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.List ; 27 28 import org.apache.cayenne.access.OperationObserver; 29 import org.apache.cayenne.access.QueryLogger; 30 import org.apache.cayenne.access.ResultIterator; 31 import org.apache.cayenne.access.trans.SelectTranslator; 32 import org.apache.cayenne.access.util.DistinctResultIterator; 33 import org.apache.cayenne.dba.DbAdapter; 34 import org.apache.cayenne.map.EntityResolver; 35 import org.apache.cayenne.query.PrefetchProcessor; 36 import org.apache.cayenne.query.PrefetchTreeNode; 37 import org.apache.cayenne.query.QueryMetadata; 38 import org.apache.cayenne.query.SelectQuery; 39 40 46 public class SelectAction extends BaseSQLAction { 47 48 protected SelectQuery query; 49 50 public SelectAction(SelectQuery query, DbAdapter adapter, 51 EntityResolver entityResolver) { 52 super(adapter, entityResolver); 53 this.query = query; 54 } 55 56 protected SelectTranslator createTranslator(Connection connection) { 57 SelectTranslator translator = new SelectTranslator(); 58 translator.setQuery(query); 59 translator.setAdapter(adapter); 60 translator.setEntityResolver(getEntityResolver()); 61 translator.setConnection(connection); 62 return translator; 63 } 64 65 public void performAction(Connection connection, OperationObserver observer) 66 throws SQLException , Exception { 67 68 long t1 = System.currentTimeMillis(); 69 70 SelectTranslator translator = createTranslator(connection); 71 PreparedStatement prepStmt = translator.createStatement(); 72 ResultSet rs = prepStmt.executeQuery(); 73 74 RowDescriptor descriptor = new RowDescriptor( 75 translator.getResultColumns(), 76 getAdapter().getExtendedTypes()); 77 JDBCResultIterator workerIterator = new JDBCResultIterator( 78 connection, 79 prepStmt, 80 rs, 81 descriptor, 82 query.getFetchLimit()); 83 84 workerIterator.setPostProcessor(DataRowPostProcessor 85 .createPostProcessor(translator)); 86 87 ResultIterator it = workerIterator; 88 89 if (translator.isSuppressingDistinct()) { 91 92 94 final boolean[] compareFullRows = new boolean[1]; 95 96 QueryMetadata md = query.getMetaData(getEntityResolver()); 97 final PrefetchTreeNode rootPrefetch = md.getPrefetchTree(); 98 99 if (rootPrefetch != null) { 100 rootPrefetch.traverse(new PrefetchProcessor() { 101 102 public void finishPrefetch(PrefetchTreeNode node) { 103 } 104 105 public boolean startDisjointPrefetch(PrefetchTreeNode node) { 106 return rootPrefetch == node; 108 } 109 110 public boolean startUnknownPrefetch(PrefetchTreeNode node) { 111 return rootPrefetch == node; 113 } 114 115 public boolean startJointPrefetch(PrefetchTreeNode node) { 116 if (rootPrefetch != node) { 117 compareFullRows[0] = true; 118 return false; 119 } 120 121 return true; 122 } 123 124 public boolean startPhantomPrefetch(PrefetchTreeNode node) { 125 return true; 126 } 127 }); 128 } 129 130 it = new DistinctResultIterator( 131 workerIterator, 132 translator.getRootDbEntity(), 133 compareFullRows[0]); 134 } 135 136 139 if (!observer.isIteratedResult()) { 140 List resultRows = it.dataRows(true); 143 QueryLogger 144 .logSelectCount(resultRows.size(), System.currentTimeMillis() - t1); 145 146 observer.nextDataRows(query, resultRows); 147 } 148 else { 149 try { 150 workerIterator.setClosingConnection(true); 151 observer.nextDataRows(translator.getQuery(), it); 152 } 153 catch (Exception ex) { 154 it.close(); 155 throw ex; 156 } 157 } 158 } 159 } 160 | Popular Tags |