sb.append(storedProcedure.getName());
}
else {
sb.append(name);
}
QueryOperation qo = new QueryOperation();
qo.setName(sb.toString());
QueryHandler qh;
if (storedProcedure.isFunction()) {
qh = new StoredFunctionQueryHandler();
}
else {
qh = new StoredProcedureQueryHandler();
}
sb = new StringBuilder();
if (isOracle) {
if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
sb.append(storedProcedure.getSchema());
sb.append('.');
}
if (storedProcedure.getCatalog() != null && storedProcedure.getCatalog().length() > 0) {
sb.append(storedProcedure.getCatalog());
sb.append('.');
}
}
else {
if (!isMySQL) {
if (storedProcedure.getCatalog() != null && storedProcedure.getCatalog().length() > 0) {
sb.append(storedProcedure.getCatalog());
sb.append('.');
}
}
if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
sb.append(storedProcedure.getSchema());
sb.append('.');
}
}
sb.append(storedProcedure.getName());
((StoredProcedureQueryHandler)qh).setName(sb.toString());
builder.logMessage(FINEST, "Building QueryOperation for " + sb.toString());
// before assigning queryHandler, check for named query in OR project
List<DatabaseQuery> queries = builder.getOrProject().getQueries();
if (queries.size() > 0) {
for (DatabaseQuery q : queries) {
if (q.getName().equals(qo.getName())) {
qh = new NamedQueryHandler();
((NamedQueryHandler)qh).setName(qo.getName());
}
}
}
qo.setQueryHandler(qh);
SimpleXMLFormat sxf = null;
if (isSimpleXMLFormat() || getReturnType() == null) {
sxf = new SimpleXMLFormat();
}
if (simpleXMLFormatTag != null && simpleXMLFormatTag.length() > 0) {
sxf.setSimpleXMLFormatTag(simpleXMLFormatTag);
}
if (xmlTag != null && xmlTag.length() > 0) {
if (sxf == null) {
sxf = new SimpleXMLFormat();
}
sxf.setXMLTag(xmlTag);
}
Result result = null;
if (!storedProcedure.isFunction() && isOracle && noOutArguments(storedProcedure)) {
result = new Result();
result.setType(new QName(W3C_XML_SCHEMA_NS_URI, "int", "xsd")); // rowcount
}
else {
if (storedProcedure.isFunction()) {
DbStoredFunction storedFunction = (DbStoredFunction)storedProcedure;
DbStoredArgument rarg = storedFunction.getReturnArg();
if (rarg.getJdbcTypeName().contains("CURSOR")) {
result = new CollectionResult();
result.setType(SXF_QNAME_CURSOR);
}
else {
result = new Result();
int rargJdbcType = rarg.getJdbcType();
switch (rargJdbcType) {
case STRUCT:
case ARRAY:
case OTHER:
if (returnType != null) {
result.setType(buildCustomQName(returnType, builder));
}
else {
result.setType(ANY_QNAME);
}
break;
default :
if (isOracle) {
result.setType(OracleHelper.getXMLTypeFromJDBCType(
rarg, builder.getTargetNamespace()));
}
else {
result.setType(getXMLTypeFromJDBCType(rargJdbcType));
}
break;
}
}
}
else if (!isOracle) {
// if user overrides returnType, assume they're right
if (returnType != null) {
result = new Result();
result.setType(buildCustomQName(returnType, builder));
}
else {
if (isCollection) {
result = new CollectionResult();
if (isSimpleXMLFormat()) {
result.setType(SXF_QNAME_CURSOR);
}
}
else {
result = new Result();
result.setType(SXF_QNAME);
}
}
}
// if it is Oracle, then return types are determined by first OUT parameter (below)
}
if (binaryAttachment) {
Attachment attachment = new Attachment();
attachment.setMimeType("application/octet-stream");
result.setAttachment(attachment);
}
for (DbStoredArgument arg : storedProcedure.getArguments()) {
String argName = arg.getName();
if (argName != null) {
ProcedureArgument pa = null;
Parameter parm = null;
InOut direction = arg.getInOut();
QName xmlType = null;
switch (arg.getJdbcType()) {
case STRUCT:
case ARRAY:
case OTHER:
String typeString =
builder.topTransformer.generateSchemaAlias(arg.getJdbcTypeName());
xmlType = buildCustomQName(typeString, builder);
break;
default :
if (isOracle) {
xmlType = OracleHelper.getXMLTypeFromJDBCType(
arg, builder.getTargetNamespace());
}
else {
xmlType = getXMLTypeFromJDBCType(arg.getJdbcType());
}
break;
}
if (direction == IN) {
parm = new Parameter();
parm.setName(argName);
parm.setType(xmlType);
pa = new ProcedureArgument();
pa.setName(argName);
pa.setParameterName(argName);
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler)qh).getInArguments().add(pa);
}
}
else {
// the first OUT/INOUT arg determines singleResult vs. collectionResult
pa = new ProcedureOutputArgument();
ProcedureOutputArgument pao = (ProcedureOutputArgument)pa;
pao.setName(argName);
pao.setParameterName(argName);
if (arg.getJdbcTypeName().contains("CURSOR") &&
returnType == null) { // if user overrides returnType, assume they're right
pao.setResultType(SXF_QNAME_CURSOR);
if (result == null) {
result = new CollectionResult();
result.setType(SXF_QNAME_CURSOR);
}
}
else {
// if user overrides returnType, assume they're right
// Hmm, multiple OUT's gonna be a problem - later!
if (returnType != null && sxf == null) {
xmlType = qNameFromString("{" + builder.getTargetNamespace() + "}" +
returnType, builder.schema);
}
pao.setResultType(xmlType);
if (result == null) {
if (isCollection) {
result = new CollectionResult();
}
else {
result = new Result();
}
result.setType(xmlType);
}
}
if (direction == INOUT) {
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler)qh).getInOutArguments().add(pao);
}
}
else {
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler)qh).getOutArguments().add(pao);
}
}
}
if (arg instanceof PLSQLStoredArgument) {
pa.setComplexTypeName(((PLSQLStoredArgument)arg).getPlSqlTypeName());
}
if (parm != null) {
qo.getParameters().add(parm);
}
}
}
if (sxf != null) {
result.setSimpleXMLFormat(sxf);
// check to see if the O-X project needs descriptor for SimpleXMLFormat
if (builder.oxProject.getDescriptorForAlias(DEFAULT_SIMPLE_XML_FORMAT_TAG) == null) {
SimpleXMLFormatProject sxfProject = new SimpleXMLFormatProject();
builder.oxProject.addDescriptor(sxfProject.buildXRRowSetModelDescriptor());
}
}
qo.setResult(result);
builder.xrServiceModel.getOperations().put(qo.getName(), qo);
}
// check to see if the schema requires sxfType to be added
if (requiresSimpleXMLFormat(builder.xrServiceModel) && builder.schema.getTopLevelElements().
get("simple-xml-format") == null) {