

def as_sql(self):
    qn = self.connection.ops.quote_name
    opts = self.query.get_meta()
    result = [('INSERT INTO %s' % qn(opts.db_table))]
    has_fields = bool(self.query.fields)
    fields = (self.query.fields if has_fields else [opts.pk])
    result.append(('(%s)' % ', '.join([qn(f.column) for f in fields])))
    if has_fields:
        params = values = [[f.get_db_prep_save((getattr(obj, f.attname) if self.query.raw else f.pre_save(obj, True)), connection=self.connection) for f in fields] for obj in self.query.objs]
    else:
        values = [[self.connection.ops.pk_default_value()] for obj in self.query.objs]
        params = [[]]
        fields = [None]
    can_bulk = ((not any((hasattr(field, 'get_placeholder') for field in fields))) and (not self.return_id) and self.connection.features.has_bulk_insert)
    if can_bulk:
        placeholders = [(['%s'] * len(fields))]
    else:
        placeholders = [[self.placeholder(field, v) for (field, v) in zip(fields, val)] for val in values]
        params = self.connection.ops.modify_insert_params(placeholders, params)
    if (self.return_id and self.connection.features.can_return_id_from_insert):
        params = params[0]
        col = ('%s.%s' % (qn(opts.db_table), qn(opts.pk.column)))
        result.append(('VALUES (%s)' % ', '.join(placeholders[0])))
        (r_fmt, r_params) = self.connection.ops.return_insert_id()
        if r_fmt:
            result.append((r_fmt % col))
            params += r_params
        return [(' '.join(result), tuple(params))]
    if can_bulk:
        result.append(self.connection.ops.bulk_insert_sql(fields, len(values)))
        return [(' '.join(result), tuple([v for val in values for v in val]))]
    else:
        return [(' '.join((result + [('VALUES (%s)' % ', '.join(p))])), vals) for (p, vals) in zip(placeholders, params)]

dataset/ETHPy150Open cloudera/hue/desktop/core/ext-py/Django-1.6.10/django/db/models/sql/compiler.py SQLInsertCompiler.as_sql/original