1 19 20 package org.apache.cayenne.dba.sqlserver; 21 22 import java.sql.CallableStatement ; 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.cayenne.access.OperationObserver; 31 import org.apache.cayenne.access.QueryLogger; 32 import org.apache.cayenne.access.ResultIterator; 33 import org.apache.cayenne.access.jdbc.ProcedureAction; 34 import org.apache.cayenne.access.jdbc.RowDescriptor; 35 import org.apache.cayenne.access.trans.ProcedureTranslator; 36 import org.apache.cayenne.dba.DbAdapter; 37 import org.apache.cayenne.map.EntityResolver; 38 import org.apache.cayenne.query.ProcedureQuery; 39 import org.apache.cayenne.query.Query; 40 41 52 public class SQLServerProcedureAction extends ProcedureAction { 53 54 public SQLServerProcedureAction(ProcedureQuery query, DbAdapter adapter, 55 EntityResolver entityResolver) { 56 super(query, adapter, entityResolver); 57 } 58 59 public void performAction(Connection connection, OperationObserver observer) 60 throws SQLException , Exception { 61 62 ProcedureTranslator transl = createTranslator(connection); 63 CallableStatement statement = (CallableStatement ) transl.createStatement(); 64 65 try { 66 boolean hasResultSet = statement.execute(); 70 71 74 Observer localObserver = new Observer(observer); 75 76 78 while (true) { 79 if (hasResultSet) { 80 ResultSet rs = statement.getResultSet(); 81 try { 82 RowDescriptor descriptor = describeResultSet( 83 rs, 84 processedResultSets++); 85 readResultSet(rs, descriptor, query, localObserver); 86 } 87 finally { 88 try { 89 rs.close(); 90 } 91 catch (SQLException ex) { 92 } 93 } 94 } 95 else { 96 int updateCount = statement.getUpdateCount(); 97 if (updateCount == -1) { 98 break; 99 } 100 QueryLogger.logUpdateCount(updateCount); 101 localObserver.nextCount(query, updateCount); 102 } 103 104 hasResultSet = statement.getMoreResults(); 105 } 106 107 readProcedureOutParameters(statement, observer); 112 113 localObserver.flushResults(query); 115 } 116 finally { 117 try { 118 statement.close(); 119 } 120 catch (SQLException ex) { 121 122 } 123 } 124 } 125 126 class Observer implements OperationObserver { 127 128 List results; 129 List counts; 130 OperationObserver observer; 131 132 Observer(OperationObserver observer) { 133 this.observer = observer; 134 } 135 136 void flushResults(Query query) { 137 if (results != null) { 138 Iterator it = results.iterator(); 139 while (it.hasNext()) { 140 observer.nextDataRows(query, (List ) it.next()); 141 } 142 143 results = null; 144 } 145 146 if (counts != null) { 147 Iterator it = counts.iterator(); 148 while (it.hasNext()) { 149 observer.nextCount(query, ((Number ) it.next()).intValue()); 150 } 151 152 counts = null; 153 } 154 } 155 156 public void nextBatchCount(Query query, int[] resultCount) { 157 observer.nextBatchCount(query, resultCount); 158 } 159 160 public void nextCount(Query query, int resultCount) { 161 if (counts == null) { 164 counts = new ArrayList (); 165 } 166 167 counts.add(new Integer (resultCount)); 168 } 169 170 public void nextDataRows(Query query, List dataRows) { 171 if (results == null) { 174 results = new ArrayList (); 175 } 176 177 results.add(dataRows); 178 } 179 180 public void nextDataRows(Query q, ResultIterator it) { 181 observer.nextDataRows(q, it); 182 } 183 184 public void nextGlobalException(Exception ex) { 185 observer.nextGlobalException(ex); 186 } 187 188 public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) { 189 observer.nextGeneratedDataRows(query, keysIterator); 190 } 191 192 public void nextQueryException(Query query, Exception ex) { 193 observer.nextQueryException(query, ex); 194 } 195 196 public boolean isIteratedResult() { 197 return observer.isIteratedResult(); 198 } 199 } 200 } 201 | Popular Tags |