@SuppressWarnings("unchecked")
@Override
public void initializeCall(XRServiceAdapter xrService, QueryOperation queryOperation,
DatabaseQuery databaseQuery) {
StoredProcedureCall spCall = createCall();
if (getName() != null) {
spCall.setProcedureName(getName());
} else {
spCall.setProcedureName(queryOperation.getName());
}
QName resultType = queryOperation.getResultType();
if ((getInOutArguments().size() + getOutArguments().size()) > 1 &&
!queryOperation.isSimpleXMLFormat()) {
throw DBWSException.multipleOutputArgumentsOnlySupportedForSimpleXML();
}
// find IN and INOUT parameters
for (Parameter p : queryOperation.getParameters()) {
ProcedureArgument arg = findInOutArgument(p.getName());
// default argument name to parameter name
String argName = p.getName();
// override with explicit argument name
if (arg != null) {
argName = arg.getName();
}
if (arg != null && arg instanceof ProcedureOutputArgument) {
if (isCursorType(xrService, p.getType())) {
throw DBWSException.inoutCursorArgumentsNotSupported();
}
spCall.addNamedInOutputArgument(argName, p.getName());
} else {
spCall.addNamedArgument(argName, p.getName());
}
}
// find OUT parameters
if (queryOperation.hasResponse()) {
if (!queryOperation.isSimpleXMLFormat() ||
(spCall.isStoredFunctionCall() && !isCursorType(xrService, resultType))) {
setSingleResult(xrService, spCall, resultType);
// if the JDBC Type code is set on the result, use it
if (queryOperation.getResult().isJdbcTypeSet()) {
ObjectRelationalDatabaseField field = new ObjectRelationalDatabaseField(EMPTY_STRING);
field.setSqlType(queryOperation.getResult().getJdbcType());
field.setSqlTypeName(getTypeNameForJDBCType(queryOperation.getResult().getJdbcType()));
// replace the original field with the new one
((StoredFunctionCall)spCall).getParameters().remove(0);
((StoredFunctionCall)spCall).getParameters().add(0, field);
}
// support stored function with out args
for (ProcedureOutputArgument arg : getOutArguments()) {
// use argument type
if (arg.getResultType() == null || !isCursorType(xrService, arg.getResultType())) {
// if the JDBC Type code is set on the argument, use it
if (arg.isJdbcTypeSet()) {
spCall.addNamedOutputArgument(arg.getName(), arg.getName(), arg.getJdbcType(), getTypeNameForJDBCType(arg.getJdbcType()));
} else {
spCall.addNamedOutputArgument(arg.getName());
}
}
}
} else {
if (spCall.isStoredFunctionCall() && isCursorType(xrService, resultType)) {
spCall.setIsCursorOutputProcedure(true);
// remove the null OUT parameter added by the constructor of SFC
spCall.getParameters().remove(0);
spCall.getParameters().add(0, new DatabaseField(CURSOR_STR));
} else if (getOutArguments().isEmpty()) {
spCall.setReturnsResultSet(true);
} else {
for (ProcedureOutputArgument arg : getOutArguments()) {
// use argument type
if (arg.getResultType() != null && isCursorType(xrService, arg.getResultType())) {
spCall.useNamedCursorOutputAsResultSet(arg.getName());
} else {
// if the JDBC Type code is set on the argument, use it
if (arg.isJdbcTypeSet()) {
spCall.addNamedOutputArgument(arg.getName(), arg.getName(), arg.getJdbcType(), getTypeNameForJDBCType(arg.getJdbcType()));
} else {
spCall.addNamedOutputArgument(arg.getName());
}
}
}
}
}