ParamInfo info;
StringBuffer sb;
Integer paramNo;
if (!oql.startsWith("CALL ")) {
throw new QueryException("Stored procedure call must start with CALL");
}
// Fix for bug #995
// as = oql.indexOf( " AS " );
as = oql.lastIndexOf(" AS ");
if (as < 0) {
throw new QueryException("Stored procedure call must end with \"AS <class-name>\"");
}
leftParen = oql.indexOf("(");
rightParen = oql.indexOf(")");
sql = new StringBuffer();
paramCnt = 0;
_paramInfo = new Hashtable();
if (oql.startsWith("CALL SQL")) {
int startOff = oql.toUpperCase().indexOf("WHERE "); // parameters begin here!
if (!(startOff < 0)) {
startOff += 6;
sql.append(oql.substring(5, startOff));
int i = startOff;
while (i < as) {
if (oql.charAt(i) == '$') {
// get parameter number if given
sb = new StringBuffer();
for (int j = i + 1; j < as; j++) {
char c = oql.charAt(j);
if (!Character.isDigit(c)) {
break;
}
sb.append(c);
}
sql.append('?'); // replace "$" with "?"
if (sb.length() > 0) {
sql.append(sb); // and add parameter number to it
paramNo = Integer.valueOf(sb.toString());
} else {
paramNo = new Integer(paramCnt + 1);
}
info = (ParamInfo) _paramInfo.get(paramNo);
if (info == null) {
info = new ParamInfo("", "java.lang.Object", null,
_database.getClassLoader());
}
//info.mapToSQLParam( paramCnt + 1 );
_paramInfo.put(paramNo , info);
paramCnt++;
i += sb.length() + 1;
} else {
sql.append(oql.charAt(i));
i++;
}
}
} else {
sql.append(oql.substring(5, as));
}
} else if ((leftParen < 0) && (rightParen < 0)) {
sql.append(oql.substring(5, as));
} else {
if (((leftParen < 0) && (rightParen >= 0))
|| (leftParen > rightParen)) {
throw new QueryException("Syntax error: parenthesis");
}
sql.append(oql.substring(5, leftParen));
sql.append('(');
for (int i = leftParen + 1; i < rightParen; i++) {
if (oql.charAt(i) == '$') {
// get parameter number if given
sb = new StringBuffer();
for (int j = i + 1; j < rightParen; j++) {
char c = oql.charAt(j);
if (!Character.isDigit(c)) {
break;
}
sb.append(c);
}
if (sb.length() > 0) {
paramNo = Integer.valueOf(sb.toString());
} else {
paramNo = new Integer(paramCnt + 1);
}
info = (ParamInfo) _paramInfo.get(paramNo);
if (info == null) {
info = new ParamInfo("", "java.lang.Object", null,
_database.getClassLoader());
}
//info.mapToSQLParam( paramCnt + 1 );
_paramInfo.put(paramNo , info);
paramCnt++;
}
}
for (int i = 0; i < paramCnt; i++) {
sql.append('?');
if (i < paramCnt - 1) {
sql.append(',');
}
}
sql.append(')');
}
_spCall = sql.toString();
_projectionType = ParseTreeWalker.PARENT_OBJECT;
_bindTypes = new Class[ paramCnt ];
for (int i = 0; i < paramCnt; i++) {
_bindTypes[i] = Object.class;
}
objType = oql.substring(as + 4).trim();
if (objType.length() == 0) {
throw new QueryException("Missing object name");
}
try {
_objClass = ClassLoadingUtils.loadClass(_database.getClassLoader(), objType);
} catch (ClassNotFoundException except) {
throw new QueryException("Could not find class " + objType);
}
_dbEngine = ((AbstractDatabaseImpl) _database).getLockEngine();
if ((_dbEngine == null) || (_dbEngine.getPersistence(_objClass) == null)) {
throw new QueryException("Could not find an engine supporting class " + objType);
}
}