'Turn a tuple (table_alias, column_name, db_type, lookup_type,
value_annot, params) into valid SQL.
Returns the string for the SQL fragment and the parameters to use for
it.'

def make_atom(self, child, qn, connection):

    (lvalue, lookup_type, value_annot, params_or_value) = child
    if hasattr(lvalue, 'process'):
        try:
            (lvalue, params) = lvalue.process(lookup_type, params_or_value, connection)
        except EmptyShortCircuit:
            raise EmptyResultSet
    else:
        params = Field().get_db_prep_lookup(lookup_type, params_or_value, connection=connection, prepared=True)
    if isinstance(lvalue, tuple):
        field_sql = self.sql_for_columns(lvalue, qn, connection)
    else:
        field_sql = lvalue.as_sql(qn, connection)
    if (value_annot is datetime.datetime):
        cast_sql = connection.ops.datetime_cast_sql()
    else:
        cast_sql = '%s'
    if hasattr(params, 'as_sql'):
        (extra, params) = params.as_sql(qn, connection)
        cast_sql = ''
    else:
        extra = ''
    if ((len(params) == 1) and (params[0] == '') and (lookup_type == 'exact') and connection.features.interprets_empty_strings_as_nulls):
        lookup_type = 'isnull'
        value_annot = True
    if (lookup_type in connection.operators):
        format = ('%s    %%s    %%s' % (connection.ops.lookup_cast(lookup_type),))
        return ((format % (field_sql, (connection.operators[lookup_type] % cast_sql), extra)), params)
    if (lookup_type == 'in'):
        if (not value_annot):
            raise EmptyResultSet
        if extra:
            return (('%s    IN    %s' % (field_sql, extra)), params)
        max_in_list_size = connection.ops.max_in_list_size()
        if (max_in_list_size and (len(params) > max_in_list_size)):
            in_clause_elements = ['(']
            for offset in xrange(0, len(params), max_in_list_size):
                if (offset > 0):
                    in_clause_elements.append('    OR    ')
                in_clause_elements.append(('%s    IN    (' % field_sql))
                group_size = min((len(params) - offset), max_in_list_size)
                param_group = ',    '.join(repeat('%s', group_size))
                in_clause_elements.append(param_group)
                in_clause_elements.append(')')
            in_clause_elements.append(')')
            return (''.join(in_clause_elements), params)
        else:
            return (('%s    IN    (%s)' % (field_sql, ',    '.join(repeat('%s', len(params))))), params)
    elif (lookup_type in ('range', 'year')):
        return (('%s    BETWEEN    %%s    and    %%s' % field_sql), params)
    elif (lookup_type in ('month', 'day', 'week_day')):
        return (('%s    =    %%s' % connection.ops.date_extract_sql(lookup_type, field_sql)), params)
    elif (lookup_type == 'isnull'):
        return (('%s    IS    %sNULL' % (field_sql, (((not value_annot) and 'NOT    ') or ''))), ())
    elif (lookup_type == 'search'):
        return (connection.ops.fulltext_search_sql(field_sql), params)
    elif (lookup_type in ('regex', 'iregex')):
        return ((connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql)), params)
    raise TypeError(('Invalid    lookup_type:    %r' % lookup_type))
