'Returns the SQL version of the where clause and the value to be
substituted in. Returns \'\', [] if this node matches everything,
None, [] if this node is empty, and raises EmptyResultSet if this
node can\'t match anything.'

def as_sql(self, qn, connection):

    result = []
    result_params = []
    (everything_childs, nothing_childs) = (0, 0)
    non_empty_childs = len(self.children)
    for child in self.children:
        try:
            if hasattr(child, 'as_sql'):
                (sql, params) = child.as_sql(qn=qn, connection=connection)
            else:
                (sql, params) = self.make_atom(child, qn, connection)
        except EmptyResultSet:
            nothing_childs += 1
        else:
            if sql:
                result.append(sql)
                result_params.extend(params)
            else:
                if (sql is None):
                    non_empty_childs -= 1
                    continue
                everything_childs += 1
        if (self.connector == AND):
            (full_needed, empty_needed) = (non_empty_childs, 1)
        else:
            (full_needed, empty_needed) = (1, non_empty_childs)
        if ((empty_needed - nothing_childs) <= 0):
            if self.negated:
                return ('', [])
            else:
                raise EmptyResultSet
        if ((full_needed - everything_childs) <= 0):
            if self.negated:
                raise EmptyResultSet
            else:
                return ('', [])
    if (non_empty_childs == 0):
        return (None, [])
    conn = ('    %s    ' % self.connector)
    sql_string = conn.join(result)
    if sql_string:
        if self.negated:
            sql_string = ('NOT    (%s)' % sql_string)
        elif (len(result) > 1):
            sql_string = ('(%s)' % sql_string)
    return (sql_string, result_params)
