public JDBCDefinedFinderCommand(JDBCCommandFactory factory,
FinderMetaData f) {
// Constructors --------------------------------------------------
super(factory, f);
typeMapping = jawsEntity.getJawsApplication().getTypeMapping();
// Replace placeholders with ?, but only if query is defined
String query = "";
ArrayList parameters = new ArrayList();
if (f.getQuery() != null) {
StringTokenizer finderQuery = new StringTokenizer(f.getQuery(),"{}", true);
while (finderQuery.hasMoreTokens())
{
String t = finderQuery.nextToken();
if (t.equals("{"))
{
query += "?";
String idx = finderQuery.nextToken(); // Remove number
parameters.add(new Integer(idx));
finderQuery.nextToken(); // Remove }
} else
query += t;
}
}
// Copy index numbers to parameterArray
parameterArray = new int[parameters.size()];
for (int i = 0; i < parameterArray.length; i++)
parameterArray[i] = ((Integer)parameters.get(i)).intValue();
// Since the fields in order clause also will form the select clause together with
// the pk field list, we have to clean the order clause from ASC/DESC's and fields
// that already are within the pk list
// Note that extraOrderColumns will start with a ','
String extraOrderColumns = getExtraOrderColumns(f);
String lcQuery = query.toLowerCase();
// build from clause, including any joins specified by the deployer/assembler
// In case of join query:
// order must explicitly identify tablename.field to order on
// query must start with "INNER JOIN < table to join with > WHERE
// < regular query with fully identified fields >"
if (lcQuery.startsWith(",") || lcQuery.startsWith("inner join")) {
//this is the case of a 'where' that is build to actually join tables:
// ,table2 as foo where foo.col1 = entitytable.col2 AND entitytable.filter = {1}
// or
// inner join table2 on table2.col1 = entitytable.col2 AND entitytable.filter = {1}
String tableList = null;
int whereStart = lcQuery.indexOf("where");
if (whereStart == -1) {
//log this at debug in case someone has made a mistake, but assume that
// they mean a findAll.
if (log.isDebugEnabled()) log.debug("Strange query for finder "+f.getName()+
". Includes join, but no 'where' clause. Is this a findAll?");
tableList = query;
whereClause = "";
} else {
tableList = query.substring(0, whereStart);
whereClause = query.substring(whereStart);
}
fromClause = "FROM "+jawsEntity.getTableName()+tableList;
} else {
fromClause = "FROM "+jawsEntity.getTableName();
if (lcQuery.startsWith("where"))
whereClause = query;
else
whereClause = "where "+query;
}
StringBuffer sqlBuffer = new StringBuffer();
sqlBuffer.append("SELECT ");
//where clauseString primaryKeyList = getPkColumnList();
String tableName = jawsEntity.getTableName();
StringTokenizer stok = new StringTokenizer(getPkColumnList(),",");
while(stok.hasMoreTokens()){
sqlBuffer.append(tableName);
sqlBuffer.append(".");
sqlBuffer.append(stok.nextElement().toString());
sqlBuffer.append(",");
}
// ditch the last ',' at the end...
sqlBuffer.setLength(sqlBuffer.length()-1);
// because it's already on the front of extraOrderColumns
sqlBuffer.append(extraOrderColumns);
sqlBuffer.append(' ");
sqlBuffer.append(fromClause);
sqlBuffer.append(' ");
sqlBuffer.append(whereClause);
if (f.getOrder() != null && !f.getOrder().equals(""))
{
orderClause = " ORDER BY "+f.getOrder();
sqlBuffer.append(orderClause);
}
setSQL(sqlBuffer.toString());
}
|