Search in sources :

Example 1 with ObjectCountVisitor

use of com.servoy.j2db.util.visitor.ObjectCountVisitor in project servoy-client by Servoy.

the class QuerySelect method removeUnusedJoins.

/**
 * Remove joins that whose foreign table is not referred to in this query.
 */
public void removeUnusedJoins(boolean keepInnerjoins) {
    boolean updated = true;
    while (joins != null && updated) {
        updated = false;
        int njoins = joins.size();
        for (int i = 0; i < njoins && !updated; i++) {
            ISQLJoin join = joins.get(i);
            if (!(join instanceof ISQLTableJoin) || (keepInnerjoins && ((ISQLTableJoin) join).hasInnerJoin()) || ((ISQLTableJoin) join).isPermanent()) {
                // count may depend on related records or is marked as permanent
                continue;
            }
            if (!(((ISQLTableJoin) join).getForeignTableReference() instanceof TableExpression)) {
                // derived table
                continue;
            }
            BaseQueryTable joinTable = ((ISQLTableJoin) join).getForeignTable();
            ObjectCountVisitor selectCounter = new ObjectCountVisitor(joinTable, true);
            ObjectCountVisitor joinCounter = new ObjectCountVisitor(joinTable, true);
            acceptVisitor(selectCounter);
            join.acceptVisitor(joinCounter);
            if (selectCounter.getCount() == joinCounter.getCount()) {
                // the table is not referenced outside the join; it may be removed
                if (njoins == 1) {
                    joins = null;
                } else {
                    joins.remove(i);
                }
                updated = true;
            }
        }
    }
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) ObjectCountVisitor(com.servoy.j2db.util.visitor.ObjectCountVisitor)

Aggregations

BaseQueryTable (com.servoy.base.query.BaseQueryTable)1 ObjectCountVisitor (com.servoy.j2db.util.visitor.ObjectCountVisitor)1