while (procNames.next()) {
procFound++;
}
procNames.close();
if (procFound == 0) {
throw new ResourceException("Cannot find procedure " +
procName +
". Please check catalog, schema and function name.");
}
ResultSet rs =
metadata.getProcedureColumns(catalog,schema,procName,null);
List parameterList = new ArrayList();
boolean function=false;
while(rs.next()) {
if((rs.getShort(5)==DatabaseMetaData.procedureColumnReturn)
&& (!((rs.getString(7)).equals("void")))) {
function= true;
}
if (rs.getString(7).equals("void")) {
continue; // skip extra info from Cloudscape
}
parameterList.add(new Parameter( rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getShort(5),
rs.getShort(6),
rs.getShort(10)));
}
rs.close();
int paramCount = parameterList.size();
if (function) {
paramCount -= 1;
}
//if the procedure is parameterless, paramCount = 0
procName += "(";
for(int i=0;i<paramCount;i++) {
if (i == 0) {
procName += "?";
} else {
procName += ",?";
}
}
procName += ")";
String schemaAddOn = "";
if (schema != null && !schema.equals("")) {
schemaAddOn = schema + ".";
}
if (function) {
procName = "? = call " + schemaAddOn + procName;
} else {
procName = "call " + schemaAddOn + procName;
}
//System.out.println("procName.."+procName);
CallableStatement cstmt =
conn.prepareCall("{"+ procName +"}");
//get all IN parameters and register all OUT parameters
int count = parameterList.size();
int recCount = 0;
IndexedRecord iRec = null;
for (int i=0; i<count; i++) {
Parameter parameter = (Parameter) parameterList.get(i);
if (parameter.isInputColumn()) {
if (iRec == null) {
if (input instanceof IndexedRecord) {
iRec = (IndexedRecord) input;
} else {
throw new ResourceException("Invalid input record");
}
}
//get value from input record
cstmt.setObject(i+1, iRec.get(recCount));
recCount++;
}
}
IndexedRecord oRec = null;
for (int i=0; i<count; i++) {
Parameter parameter = (Parameter) parameterList.get(i);
if (parameter.isOutputColumn()) {
if (oRec == null) {
if (output instanceof IndexedRecord) {
oRec = (IndexedRecord) output;
} else {
throw new ResourceException("Invalid output record");
}
}
if (parameter.isDecimalNumeric()) {
cstmt.registerOutParameter
(i+1, parameter.getDataType(),
parameter.getScale());
} else {
cstmt.registerOutParameter
(i+1, parameter.getDataType());
}
}
}
cstmt.execute();
Class[] parameters = new Class[] {int.class};
//get the right getXXX() from Mapping.java for the output
Mapping map = new Mapping();
for(int i=0; i<count; i++) {
Parameter parameter = (Parameter) parameterList.get(i);
if(parameter.isOutputColumn()) {
String ans =(String)
map.get(new Integer(parameter.getDataType()));
Method method =
cstmt.getClass().getMethod(ans,parameters);
Object[] obj = new Object[] {new Integer(i+1)};
Object o=method.invoke(cstmt,obj);
if(output instanceof IndexedRecord) {
oRec = (IndexedRecord)output;
oRec.add(o);
//System.out.println("output..."+o.toString());
}
}
}
cstmt.close();
return oRec;
// conn.close();
} catch(SQLException ex) {
throw new ResourceException(ex.getMessage());
} catch (NoSuchMethodException ex) {
throw new ResourceException(ex.getMessage());
} catch (IllegalAccessException ex) {
throw new ResourceException(ex.getMessage());
} catch (InvocationTargetException ex) {
throw new ResourceException(ex.getMessage());
}
}