Skip to content

Commit 963aae5

Browse files
avidaldlo
authored andcommitted
various compiler improvements consolidated from multiple commits
Commit information is from https://github.com/avidal/django-pyodbc 2012-06-28 13:44:33 -0500: 6f2dfa3 - Fix bug when generating insert queries on django < 1.4. 2012-06-20 15:13:39 -0500: 6b9ca0a - Use SCOPE_IDENTITY in INSERT queries to get the inserted id. 2012-02-09 11:21:00 -0600: c2bc6de - Fix bug when inserting with a specific primary key. 2012-02-07 10:46:54 -0600: b1ecdc4 - Fixed bug in regards to bulk inserting. 2012-02-03 15:46:11 -0600: c7ca41d - Make it backwards compatible. 2012-02-03 12:48:13 -0600: 0dd232b - Deal with bulk_insert related changes.
1 parent 95b7433 commit 963aae5

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

django_pyodbc/compiler.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,54 @@ def as_sql(self):
280280
qn = self.connection.ops.quote_name
281281
opts = self.query.model._meta
282282
result = ['INSERT INTO %s' % qn(opts.db_table)]
283-
result.append('(%s)' % ', '.join([qn(c) for c in self.query.columns]))
283+
284+
has_fields = bool(self.query.fields)
285+
fields = self.query.fields if has_fields else [opts.pk]
286+
columns = [f.column for f in fields]
287+
288+
result.append('(%s)' % ', '.join([qn(c) for c in columns]))
289+
290+
if has_fields:
291+
params = values = [
292+
[
293+
f.get_db_prep_save(getattr(obj, f.attname) if self.query.raw else f.pre_save(obj, True), connection=self.connection)
294+
for f in fields
295+
]
296+
for obj in self.query.objs
297+
]
298+
else:
299+
values = [[self.connection.ops.pk_default_value()] for obj in self.query.objs]
300+
params = [[]]
301+
fields = [None]
302+
303+
placeholders = [
304+
[self.placeholder(field, v) for field, v in zip(fields, val)]
305+
for val in values
306+
]
307+
284308
if self.return_id and self.connection.features.can_return_id_from_insert:
309+
params = params[0]
285310
output = 'OUTPUT inserted.%s' % qn(opts.pk.column)
286311
result.append(output)
287-
values = [self.placeholder(*v) for v in self.query.values]
288-
result.append('VALUES (%s)' % ', '.join(values))
289-
params = self.query.params
290-
sql = ' '.join(result)
312+
result.append("VALUES (%s)" % ", ".join(placeholders[0]))
313+
return [(" ".join(result), tuple(params))]
314+
315+
sql, params = [
316+
(" ".join(result + ["VALUES (%s)" % ", ".join(p)]), vals)
317+
for p, vals in zip(placeholders, params)
318+
]
291319

320+
# This section deals with specifically setting the primary key,
321+
# or using default values if necessary
292322
meta = self.query.get_meta()
293323
if meta.has_auto_field:
294324
# db_column is None if not explicitly specified by model field
295325
auto_field_column = meta.auto_field.db_column or meta.auto_field.column
296326

297-
if auto_field_column in self.query.columns:
327+
if auto_field_column in columns:
298328
quoted_table = self.connection.ops.quote_name(meta.db_table)
299-
if len(self.query.columns) == 1 and not params:
329+
# If there are no fields specified in the insert..
330+
if not has_fields:
300331
sql = "INSERT INTO %s DEFAULT VALUES" % quoted_table
301332
else:
302333
sql = "SET IDENTITY_INSERT %s ON;\n%s;\nSET IDENTITY_INSERT %s OFF" % \

0 commit comments

Comments
 (0)