'Reconstruct the query from this filter'

def query(self):

    op = self.op
    if (op is None):
        return None
    if (self.tablename and self.fieldname):
        l = current.s3db[self.tablename][self.fieldname]
    elif self.l:
        l = self.l.query()
    else:
        l = None
    r = self.r
    if (op in ('AND', 'OR', 'NOT')):
        r = (r.query() if r else True)
    if (op == 'AND'):
        if ((l is not None) and (r is not None)):
            return (l & r)
        elif (r is not None):
            return r
        else:
            return l
    elif (op == 'OR'):
        if ((l is not None) and (r is not None)):
            return (l | r)
        else:
            return None
    elif (op == 'NOT'):
        if (l is not None):
            return (~ l)
        else:
            return None
    elif (l is None):
        return None
    if isinstance(r, S3AxisFilter):
        r = r.query()
    if (r is None):
        return None
    if (op == 'LOWER'):
        return l.lower()
    elif (op == 'UPPER'):
        return l.upper()
    elif (op == 'EQ'):
        return (l == r)
    elif (op == 'NE'):
        return (l != r)
    elif (op == 'LT'):
        return (l < r)
    elif (op == 'LE'):
        return (l <= r)
    elif (op == 'GE'):
        return (l >= r)
    elif (op == 'GT'):
        return (l > r)
    elif (op == 'BELONGS'):
        return l.belongs(r)
    elif (op == 'CONTAINS'):
        return l.contains(r)
    else:
        return None
