String catalog = rs.getString("PROCEDURE_CAT");
String schema = rs.getString("PROCEDURE_SCHEM");
short type = rs.getShort("PROCEDURE_TYPE");
Procedure procedure = new Procedure(name);
procedure.setCatalog(catalog);
procedure.setSchema(schema);
switch (type) {
case DatabaseMetaData.procedureNoResult:
case DatabaseMetaData.procedureResultUnknown:
procedure.setReturningValue(false);
break;
case DatabaseMetaData.procedureReturnsResult:
procedure.setReturningValue(true);
break;
}
if (procedures == null) {
procedures = new HashMap<String, Procedure>();
}
procedures.put(procedure.getFullyQualifiedName(), procedure);
}
}
finally {
rs.close();
}
// if nothing found, return
if (procedures == null) {
return;
}
// get columns
ResultSet columnsRS = getMetaData().getProcedureColumns(
null,
schemaPattern,
namePattern,
null);
try {
while (columnsRS.next()) {
String schema = columnsRS.getString("PROCEDURE_SCHEM");
String name = columnsRS.getString("PROCEDURE_NAME");
// TODO: this will be moved to Delegate...
if (EXCLUDED_PROCEDURES.contains(name)) {
continue;
}
String columnName = columnsRS.getString("COLUMN_NAME");
short type = columnsRS.getShort("COLUMN_TYPE");
String key = (schema != null) ? schema + '.' + name : name;
// skip ResultSet columns, as they are not described in Cayenne procedures
// yet...
if (type == DatabaseMetaData.procedureColumnResult) {
logObj.debug("skipping ResultSet column: " + key + "." + columnName);
}
Procedure procedure = procedures.get(key);
if (procedure == null) {
logObj.info("invalid procedure column, no procedure found: "
+ key
+ "."
+ columnName);
continue;
}
ProcedureParameter column = new ProcedureParameter(columnName);
if (columnName == null) {
if (type == DatabaseMetaData.procedureColumnReturn) {
logObj.debug("null column name, assuming result column: " + key);
column.setName("_return_value");
}
else {
logObj.info("invalid null column name, skipping column : " + key);
continue;
}
}
int columnType = columnsRS.getInt("DATA_TYPE");
int columnSize = columnsRS.getInt("LENGTH");
// ignore precision of non-decimal columns
int decimalDigits = -1;
if (TypesMapping.isDecimal(columnType)) {
decimalDigits = columnsRS.getShort("SCALE");
if (columnsRS.wasNull()) {
decimalDigits = -1;
}
}
switch (type) {
case DatabaseMetaData.procedureColumnIn:
column.setDirection(ProcedureParameter.IN_PARAMETER);
break;
case DatabaseMetaData.procedureColumnInOut:
column.setDirection(ProcedureParameter.IN_OUT_PARAMETER);
break;
case DatabaseMetaData.procedureColumnOut:
column.setDirection(ProcedureParameter.OUT_PARAMETER);
break;
case DatabaseMetaData.procedureColumnReturn:
procedure.setReturningValue(true);
break;
}
column.setMaxLength(columnSize);
column.setPrecision(decimalDigits);
column.setProcedure(procedure);
column.setType(columnType);
procedure.addCallParameter(column);
}
}
finally {
columnsRS.close();
}