'Constructor, recursively introspect the query dict and extract
all relevant subqueries.
@param qdict: the query dict (from Query.as_dict(flat=True))
@param tablenames: the names of the relevant tables'

def __init__(self, qdict, tablenames):

    self.l = None
    self.r = None
    self.op = None
    self.tablename = None
    self.fieldname = None
    if (not qdict):
        return
    l = qdict['first']
    if ('second' in qdict):
        r = qdict['second']
    else:
        r = None
    op = qdict['op']
    if op:
        op = op.upper().strip('_')
    if ('tablename' in l):
        if (l['tablename'] in tablenames):
            self.tablename = l['tablename']
            self.fieldname = l['fieldname']
            if isinstance(r, dict):
                self.op = None
            else:
                self.op = op
                self.r = r
    elif (op == 'AND'):
        self.l = S3AxisFilter(l, tablenames)
        self.r = S3AxisFilter(r, tablenames)
        if (self.l.op or self.r.op):
            self.op = op
    elif (op == 'OR'):
        self.l = S3AxisFilter(l, tablenames)
        self.r = S3AxisFilter(r, tablenames)
        if (self.l.op and self.r.op):
            self.op = op
    elif (op == 'NOT'):
        self.l = S3AxisFilter(l, tablenames)
        self.op = op
    else:
        self.l = S3AxisFilter(l, tablenames)
        if self.l.op:
            self.op = op
