String arg2Alias = (String) state.getFieldToAlias(arg2) .get(arg1Desc.getReverseReferenceDescriptor().getName()); queryClassToString(state.getWhereBuffer(), arg1.getQueryClass(), q, schema, ID_ONLY, state); state.addToWhere((c.getOp() == ConstraintOp.CONTAINS ? " = " : " != ") + arg2Alias + "." + DatabaseUtil.getColumnName(arg1Desc.getReverseReferenceDescriptor()));
if (arg2 == null) { ReferenceDescriptor reverse = arg1Desc.getReverseReferenceDescriptor(); String indirectTableAlias = state.getIndirectAlias(); ClassDescriptor tableMaster = schema.getTableMaster(reverse .getClassDescriptor()); state.addToFrom(DatabaseUtil.getTableName(tableMaster) + " AS " + indirectTableAlias); if (schema.isTruncated(tableMaster)) { state.addToWhere(indirectTableAlias + ".class = '" + reverse.getClassDescriptor().getType().getName() + "' AND "); } state.addToWhere("("); queryClassToString(state.getWhereBuffer(), arg1.getQueryClass(), q, schema, ID_ONLY, state); state.addToWhere((c.getOp() == ConstraintOp.CONTAINS ? " = " : " != ") + indirectTableAlias + "." + DatabaseUtil.getColumnName(reverse) + " AND " + indirectTableAlias + ".id = " + arg2Obj.getId() + ")"); } else { String arg2Alias = (String) state.getFieldToAlias(arg2) .get(arg1Desc.getReverseReferenceDescriptor().getName()); queryClassToString(state.getWhereBuffer(), arg1.getQueryClass(), q, schema, ID_ONLY, state); state.addToWhere((c.getOp() == ConstraintOp.CONTAINS ? " = " : " != ") + arg2Alias + "." + DatabaseUtil.getColumnName(arg1Desc.getReverseReferenceDescriptor())); }
    protected static void containsConstraintToString(State state, ContainsConstraint c,            Query q, DatabaseSchema schema) throws ObjectStoreException {        QueryReference arg1 = c.getReference();        QueryClass arg2 = c.getQueryClass();        InterMineObject arg2Obj = c.getObject();        Map fieldNameToFieldDescriptor = schema.getModel().getFieldDescriptorsForClass(arg1                .getQueryClass().getType());        ReferenceDescriptor arg1Desc = (ReferenceDescriptor)            fieldNameToFieldDescriptor.get(arg1.getFieldName());        if (arg1Desc == null) {            throw new ObjectStoreException(arg1.getQueryClass().getType().toString()                    + " is not in the model");        }        if (arg1 instanceof QueryObjectReference) {            String arg1Alias = (String) state.getFieldToAlias(arg1.getQueryClass()).get(arg1Desc                    .getName());            if (c.getOp().equals(ConstraintOp.IS_NULL) || c.getOp().equals(ConstraintOp                        .IS_NOT_NULL)) {                state.addToWhere(arg1Alias + "." + DatabaseUtil.getColumnName(arg1Desc)                        + " " + c.getOp().toString());            } else {                state.addToWhere(arg1Alias + "." + DatabaseUtil.getColumnName(arg1Desc)                        + (c.getOp() == ConstraintOp.CONTAINS ? " = " : " != "));                if (arg2 == null) {                    objectToString(state.getWhereBuffer(), arg2Obj);                } else {                    queryClassToString(state.getWhereBuffer(), arg2, q, schema, ID_ONLY, state);                }            }        } else if (arg1 instanceof QueryCollectionReference) {            if (arg1Desc.relationType() == FieldDescriptor.ONE_N_RELATION) {                String arg2Alias = (String) state.getFieldToAlias(arg2)                    .get(arg1Desc.getReverseReferenceDescriptor().getName());                queryClassToString(state.getWhereBuffer(), arg1.getQueryClass(), q, schema, ID_ONLY,                        state);                state.addToWhere((c.getOp() == ConstraintOp.CONTAINS ? " = " : " != ") + arg2Alias                        + "."                        + DatabaseUtil.getColumnName(arg1Desc.getReverseReferenceDescriptor()));            } else {                CollectionDescriptor arg1ColDesc = (CollectionDescriptor) arg1Desc;                String indirectTableAlias = state.getIndirectAlias();                state.addToFrom(DatabaseUtil.getIndirectionTableName(arg1ColDesc) + " AS "                        + indirectTableAlias);                state.addToWhere(c.getOp().equals(ConstraintOp.CONTAINS) ? "(" : "( NOT (");                queryClassToString(state.getWhereBuffer(), arg1.getQueryClass(), q, schema, ID_ONLY,                        state);                state.addToWhere(" = " + indirectTableAlias + "."                        + DatabaseUtil.getInwardIndirectionColumnName(arg1ColDesc) + " AND "                        + indirectTableAlias + "."                        + DatabaseUtil.getOutwardIndirectionColumnName(arg1ColDesc) + " = ");                queryClassToString(state.getWhereBuffer(), arg2, q, schema, ID_ONLY, state);                state.addToWhere(c.getOp().equals(ConstraintOp.CONTAINS) ? ")" : "))");            }        }    }