1 56 package org.objectstyle.cayenne.dba.sqlserver; 57 58 import java.sql.CallableStatement ; 59 import java.sql.Connection ; 60 import java.sql.ResultSet ; 61 import java.sql.SQLException ; 62 import java.util.ArrayList ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 66 import org.apache.log4j.Level; 67 import org.objectstyle.cayenne.access.OperationObserver; 68 import org.objectstyle.cayenne.access.QueryLogger; 69 import org.objectstyle.cayenne.access.ResultIterator; 70 import org.objectstyle.cayenne.access.jdbc.ProcedureAction; 71 import org.objectstyle.cayenne.access.jdbc.RowDescriptor; 72 import org.objectstyle.cayenne.access.trans.ProcedureTranslator; 73 import org.objectstyle.cayenne.dba.DbAdapter; 74 import org.objectstyle.cayenne.map.EntityResolver; 75 import org.objectstyle.cayenne.query.ProcedureQuery; 76 import org.objectstyle.cayenne.query.Query; 77 78 89 public class SQLServerProcedureAction extends ProcedureAction { 90 91 public SQLServerProcedureAction(ProcedureQuery query, DbAdapter adapter, 92 EntityResolver entityResolver) { 93 super(query, adapter, entityResolver); 94 } 95 96 public void performAction(Connection connection, OperationObserver observer) 97 throws SQLException , Exception { 98 99 ProcedureTranslator transl = new ProcedureTranslator(); 100 transl.setAdapter(getAdapter()); 101 transl.setQuery(query); 102 transl.setEntityResolver(this.getEntityResolver()); 103 transl.setConnection(connection); 104 105 CallableStatement statement = (CallableStatement ) transl.createStatement(query 106 .getLoggingLevel()); 107 108 try { 109 boolean hasResultSet = statement.execute(); 113 114 117 Observer localObserver = new Observer(observer); 118 119 121 while (true) { 122 if (hasResultSet) { 123 ResultSet rs = statement.getResultSet(); 124 try { 125 RowDescriptor descriptor = describeResultSet( 126 rs, 127 processedResultSets++); 128 readResultSet(rs, descriptor, query, localObserver); 129 } 130 finally { 131 try { 132 rs.close(); 133 } 134 catch (SQLException ex) { 135 } 136 } 137 } 138 else { 139 int updateCount = statement.getUpdateCount(); 140 if (updateCount == -1) { 141 break; 142 } 143 QueryLogger.logUpdateCount(query.getLoggingLevel(), updateCount); 144 localObserver.nextCount(query, updateCount); 145 } 146 147 hasResultSet = statement.getMoreResults(); 148 } 149 150 readProcedureOutParameters(statement, observer); 154 155 localObserver.flushResults(query); 157 } 158 finally { 159 try { 160 statement.close(); 161 } 162 catch (SQLException ex) { 163 164 } 165 } 166 } 167 168 class Observer implements OperationObserver { 169 170 List results; 171 List counts; 172 OperationObserver observer; 173 174 Observer(OperationObserver observer) { 175 this.observer = observer; 176 } 177 178 void flushResults(Query query) { 179 if (results != null) { 180 Iterator it = results.iterator(); 181 while (it.hasNext()) { 182 observer.nextDataRows(query, (List ) it.next()); 183 } 184 185 results = null; 186 } 187 188 if (counts != null) { 189 Iterator it = counts.iterator(); 190 while (it.hasNext()) { 191 observer.nextCount(query, ((Number ) it.next()).intValue()); 192 } 193 194 counts = null; 195 } 196 } 197 198 public void nextBatchCount(Query query, int[] resultCount) { 199 observer.nextBatchCount(query, resultCount); 200 } 201 202 public void nextCount(Query query, int resultCount) { 203 if (counts == null) { 206 counts = new ArrayList (); 207 } 208 209 counts.add(new Integer (resultCount)); 210 } 211 212 public void nextDataRows(Query query, List dataRows) { 213 if (results == null) { 216 results = new ArrayList (); 217 } 218 219 results.add(dataRows); 220 } 221 222 public void nextDataRows(Query q, ResultIterator it) { 223 observer.nextDataRows(q, it); 224 } 225 226 public void nextGlobalException(Exception ex) { 227 observer.nextGlobalException(ex); 228 } 229 230 public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) { 231 observer.nextGeneratedDataRows(query, keysIterator); 232 } 233 234 public void nextQueryException(Query query, Exception ex) { 235 observer.nextQueryException(query, ex); 236 } 237 238 public Level getLoggingLevel() { 239 return observer.getLoggingLevel(); 240 } 241 242 public boolean isIteratedResult() { 243 return observer.isIteratedResult(); 244 } 245 } 246 } | Popular Tags |