1 56 package org.objectstyle.cayenne.access.jdbc; 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.Collections ; 63 import java.util.HashMap ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import org.objectstyle.cayenne.CayenneRuntimeException; 68 import org.objectstyle.cayenne.access.OperationObserver; 69 import org.objectstyle.cayenne.access.QueryLogger; 70 import org.objectstyle.cayenne.access.trans.ProcedureTranslator; 71 import org.objectstyle.cayenne.access.types.ExtendedType; 72 import org.objectstyle.cayenne.dba.DbAdapter; 73 import org.objectstyle.cayenne.map.EntityResolver; 74 import org.objectstyle.cayenne.map.Procedure; 75 import org.objectstyle.cayenne.map.ProcedureParameter; 76 import org.objectstyle.cayenne.query.ProcedureQuery; 77 78 85 public class ProcedureAction extends BaseSQLAction { 86 87 protected ProcedureQuery query; 88 89 93 protected int processedResultSets; 94 95 public ProcedureAction(ProcedureQuery query, DbAdapter adapter, 96 EntityResolver entityResolver) { 97 super(adapter, entityResolver); 98 this.query = query; 99 } 100 101 public void performAction(Connection connection, OperationObserver observer) 102 throws SQLException , Exception { 103 104 processedResultSets = 0; 105 106 ProcedureTranslator transl = new ProcedureTranslator(); 107 transl.setAdapter(getAdapter()); 108 transl.setQuery(query); 109 transl.setEntityResolver(getEntityResolver()); 110 transl.setConnection(connection); 111 112 CallableStatement statement = (CallableStatement ) transl.createStatement(query 113 .getLoggingLevel()); 114 115 try { 116 statement.execute(); 120 121 readProcedureOutParameters(statement, observer); 123 124 while (true) { 126 if (statement.getMoreResults()) { 127 ResultSet rs = statement.getResultSet(); 128 129 try { 130 RowDescriptor descriptor = describeResultSet( 131 rs, 132 processedResultSets++); 133 readResultSet(rs, descriptor, query, observer); 134 } 135 finally { 136 try { 137 rs.close(); 138 } 139 catch (SQLException ex) { 140 } 141 } 142 } 143 else { 144 int updateCount = statement.getUpdateCount(); 145 if (updateCount == -1) { 146 break; 147 } 148 QueryLogger.logUpdateCount(query.getLoggingLevel(), updateCount); 149 observer.nextCount(query, updateCount); 150 } 151 } 152 } 153 finally { 154 try { 155 statement.close(); 156 } 157 catch (SQLException ex) { 158 159 } 160 } 161 } 162 163 169 protected RowDescriptor describeResultSet(ResultSet resultSet, int setIndex) { 170 if (setIndex < 0) { 171 throw new IllegalArgumentException ( 172 "Expected a non-negative result set index. Got: " + setIndex); 173 } 174 175 List descriptors = query.getResultDescriptors(); 176 177 if (descriptors.isEmpty()) { 178 return new RowDescriptor(resultSet, getAdapter().getExtendedTypes()); 180 } 181 182 if (setIndex >= descriptors.size() || descriptors.get(setIndex) == null) { 184 throw new CayenneRuntimeException("No descriptor for result set at index '" 185 + setIndex 186 + "' configured."); 187 } 188 189 ColumnDescriptor[] columns = (ColumnDescriptor[]) descriptors.get(setIndex); 190 return new RowDescriptor(columns, getAdapter().getExtendedTypes()); 191 } 192 193 196 protected Procedure getProcedure() { 197 return getEntityResolver().lookupProcedure(query); 198 } 199 200 203 protected void readProcedureOutParameters( 204 CallableStatement statement, 205 OperationObserver delegate) throws SQLException , Exception { 206 207 long t1 = System.currentTimeMillis(); 208 209 Map result = null; 211 List parameters = getProcedure().getCallParameters(); 212 for (int i = 0; i < parameters.size(); i++) { 213 ProcedureParameter parameter = (ProcedureParameter) parameters.get(i); 214 215 if (!parameter.isOutParam()) { 216 continue; 217 } 218 219 if (result == null) { 220 result = new HashMap (); 221 } 222 223 ColumnDescriptor descriptor = new ColumnDescriptor(parameter); 224 ExtendedType type = getAdapter().getExtendedTypes().getRegisteredType( 225 descriptor.getJavaClass()); 226 Object val = type.materializeObject(statement, i + 1, descriptor 227 .getJdbcType()); 228 229 result.put(descriptor.getLabel(), val); 230 } 231 232 if (result != null && !result.isEmpty()) { 233 QueryLogger.logSelectCount(query.getLoggingLevel(), 1, System 235 .currentTimeMillis() 236 - t1); 237 delegate.nextDataRows(query, Collections.singletonList(result)); 238 } 239 } 240 } | Popular Tags |