'Returns the SQL version of the where clause and the value to be
substituted in. Returns None, None if this node is empty.
If \'node\' is provided, that is the root of the SQL generation
(generally not needed except by the internal implementation for
recursion).'

def as_sql(self, qn, connection):

    if (not self.children):
        return (None, [])
    result = []
    result_params = []
    empty = True
    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:
            if ((self.connector == AND) and (not self.negated)):
                raise
            elif self.negated:
                empty = False
            continue
        except FullResultSet:
            if (self.connector == OR):
                if self.negated:
                    empty = True
                    break
                return ('', [])
            if self.negated:
                empty = True
            continue
        empty = False
        if sql:
            result.append(sql)
            result_params.extend(params)
    if empty:
        raise EmptyResultSet
    conn = ('    %s    ' % self.connector)
    sql_string = conn.join(result)
    if sql_string:
        if self.negated:
            sql_string = ('NOT    (%s)' % sql_string)
        elif (len(self.children) != 1):
            sql_string = ('(%s)' % sql_string)
    return (sql_string, result_params)
