columnName = buildNested((NestedExpression) expression, sel, values);
NestedExpression nestedExp = (NestedExpression) expression;  StringBuffer sel = new StringBuffer(); StringBuffer columns = new StringBuffer(); StringBuffer leftJoin = new StringBuffer(); StringBuffer where = new StringBuffer(); StringBuffer whereComp = new StringBuffer(); StringBuffer orderBy = new StringBuffer(); List values = new LinkedList(); List queries = new LinkedList();  String columnName; String selectString;   if (checkIfORExists(nestedExp)) {                columnName = buildNested(nestedExp, sel, values); selectString = sel.toString(); } else { columns.append("SELECT DISTINCT ("); columns.append("a1" + '.' + stepEntryId); columnName = "retrieved"; columns.append(") AS " + columnName); columns.append(" FROM "); where.append("WHERE ");  doNestedNaturalJoin(e, nestedExp, columns, where, whereComp, values, queries, orderBy); selectString = columns.toString() + ' ' + leftJoin.toString() + ' ' + where.toString() + " AND ( " + whereComp.toString() + " ) " + ' ' + orderBy.toString();   }         return doExpressionQuery(selectString, columnName, values);
    public List query(WorkflowExpressionQuery query) throws StoreException {        Expression expression = query.getExpression();        StringBuffer sel = new StringBuffer();        List values = new ArrayList();        String columnName = null;        if (expression.isNested()) {            columnName = buildNested((NestedExpression) expression, sel, values);        } else {            columnName = buildSimple((FieldExpression) expression, sel, values);        }        if (query.getSortOrder() != WorkflowExpressionQuery.SORT_NONE) {            sel.append(" ORDER BY ");            if (query.getOrderBy() != 0) {                // Had the unexpected result of doing a "ORDER BY 1" w/o this in place                String fName = fieldName(query.getOrderBy());                if (log.isDebugEnabled()) {                    log.debug("Found fieldName as: " + fName);                }                sel.append(fName);                // In MySQL and Informix, you have to select any field you order by -- others?                String current = sel.toString();                // Remember that it will be surrounded by "DISTINCT (XXXX)"                // Should already be formatted as:                //      SELECT DISTINCT(columnName) FROM table WHERE ......                // Want to be in the format:                //      SELECT DISTINCT(columnName), fName FROM table WHERE .......                String entry = current.substring(0, current.indexOf(columnName)) + columnName + ") , " + fName + " ";                entry += current.substring(current.indexOf(columnName) + columnName.length() + 1);                sel = new StringBuffer(entry);                if (query.getSortOrder() == WorkflowExpressionQuery.SORT_ASC) {                    sel.append(" ASC");                } else {                    sel.append(" DESC");                }                if (log.isDebugEnabled()) {                    log.debug("Finalized query: " + sel.toString());                }            } else {                // Order by is empty, so order by the columnName                sel.append(columnName);            }        }        if (log.isDebugEnabled()) {            log.debug("Finished query is: " + sel.toString());        }        List results = doExpressionQuery(sel.toString(), columnName, values);        return results;    }