'Returns a list of q_objects which need to go into the having clause
instead of the where clause. Removes the splitted out nodes from the
given q_object. Note that the q_object is altered, so cloning it is
needed.'

def split_having_parts(self, q_object, negated=False):

    having_parts = []
    for c in q_object.children[:]:
        if isinstance(c, Node):
            in_negated = (c.negated ^ negated)
            if ((c.connector == OR) and self.need_having(c)):
                q_object.children.remove(c)
                having_parts.append(c)
                c.negated = in_negated
            else:
                having_parts.extend(self.split_having_parts(c, in_negated)[1])
        elif self.need_having(c):
            q_object.children.remove(c)
            new_q = self.where_class(children=[c], negated=negated)
            having_parts.append(new_q)
    return (q_object, having_parts)
