Subject Re: [IB-Java] Re: Escaped syntax for CallableStatement: need your help/input
Author David Jencks
I found this code in the sun example cci resource adapter (the ra treats
sps as InteractionSpecs). I think it indicates that sun does not expect
you to shuffle in/out parameters. They are determining if the parameter is
in or out from the getProcedureColumns resultset.


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;

--in Parameter --
public boolean isOutputColumn() {
return (parameterType == DatabaseMetaData.procedureColumnOut ||
parameterType == DatabaseMetaData.procedureColumnInOut ||
parameterType == DatabaseMetaData.procedureColumnReturn);
}

public boolean isInputColumn() {
return (parameterType == DatabaseMetaData.procedureColumnIn ||
parameterType == DatabaseMetaData.procedureColumnInOut);
}

david jencks

On 2001.12.13 02:23:12 -0500 rrokytskyy wrote:
>
> > Does it make sense to throw sqlexception for the
> > registerOutParameter but allow getxxx methods to return results
> > from the outxsqlda (??did I remember correctly??) for an execute sp?
>
> no, because the order of parameters will be incorrect. getXXX should
> throw an exception as well... only ResultSet will return parameters.
>
> Thanks,
> Roman
>
>
>
> To unsubscribe from this group, send an email to:
> IB-Java-unsubscribe@egroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>
>