PreparedStatement ps = null;
try {
Class returnType = method.getReturnType();
SqlStatement sqlStatement = _sqlParser.parse(methodSQL.statement());
ps = sqlStatement.createPreparedStatement(_context, _connection, _cal, method, args);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("PreparedStatement: "
+ sqlStatement.createPreparedStatementString(_context, _connection, method, args));
}
//
// special processing for batch updates
//
if (sqlStatement.isBatchUpdate()) {
return ps.executeBatch();
}
//
// execute the statement
//
boolean hasResults = ps.execute();
//
// callable statement processing
//
if (sqlStatement.isCallableStatement()) {
SQLParameter[] params = (SQLParameter[]) args[0];
for (int i = 0; i < params.length; i++) {
if (params[i].dir != SQLParameter.IN) {
params[i].value = ((CallableStatement) ps).getObject(i + 1);
}
}
return null;
}
//
// process returned data
//
ResultSet rs = null;
int updateCount = ps.getUpdateCount();
if (hasResults) {
rs = ps.getResultSet();
}
if (sqlStatement.getsGeneratedKeys()) {
rs = ps.getGeneratedKeys();
hasResults = true;
}
if (!hasResults && updateCount > -1) {
boolean moreResults = ps.getMoreResults();
int tempUpdateCount = ps.getUpdateCount();
while ((moreResults && rs == null) || tempUpdateCount > -1) {
if (moreResults) {
rs = ps.getResultSet();
hasResults = true;
moreResults = false;
tempUpdateCount = -1;
} else {
moreResults = ps.getMoreResults();
tempUpdateCount = ps.getUpdateCount();
}
}
}
Object returnObject = null;
if (hasResults) {
//
// if a result set mapper was specified in the methods annotation, use it
// otherwise find the mapper for the return type in the hashmap
//
final Class resultSetMapperClass = methodSQL.resultSetMapper();
final ResultSetMapper rsm;
if (!UndefinedResultSetMapper.class.isAssignableFrom(resultSetMapperClass)) {
if (ResultSetMapper.class.isAssignableFrom(resultSetMapperClass)) {
rsm = (ResultSetMapper) resultSetMapperClass.newInstance();
} else {
throw new ControlException("Result set mappers must be subclasses of ResultSetMapper.class!");
}
} else {
if (_resultMappers.containsKey(returnType)) {
rsm = _resultMappers.get(returnType);
} else {
if (_xmlObjectClass != null && _xmlObjectClass.isAssignableFrom(returnType)) {
rsm = _resultMappers.get(_xmlObjectClass);
} else {
rsm = DEFAULT_MAPPER;
}
}
}
returnObject = rsm.mapToResultType(_context, method, rs, _cal);
if (rsm.canCloseResultSet() == false) {
getResources().add(ps);
}
//
// empty ResultSet
//
} else {
if (returnType.equals(Void.TYPE)) {
returnObject = null;
} else if (returnType.equals(Integer.TYPE)) {
returnObject = new Integer(updateCount);
} else if (!sqlStatement.isCallableStatement()) {
throw new ControlException("Method " + method.getName() + "is DML but does not return void or int");
}
}
return returnObject;