Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

queryset-refactor: Added a few modifications to the select column ali…

…ases from

[7457], based on a patch from Ian Kelly. Fixed #7057.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7466 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 67bfd15c47d16cd0b63c66f421854e3353fe9351 1 parent d7ac133
Malcolm Tredinnick authored April 26, 2008

Showing 1 changed file with 29 additions and 24 deletions. Show diff stats Hide diff stats

  1. 53  django/db/models/sql/query.py
53  django/db/models/sql/query.py
@@ -366,9 +366,9 @@ def get_columns(self, with_aliases=False):
366 366
         """
367 367
         qn = self.quote_name_unless_alias
368 368
         result = ['(%s) AS %s' % (col, alias) for alias, col in self.extra_select.iteritems()]
369  
-        aliases = self.extra_select.keys()
  369
+        aliases = set(self.extra_select.keys())
370 370
         if with_aliases:
371  
-            col_aliases = set(aliases)
  371
+            col_aliases = aliases.copy()
372 372
         else:
373 373
             col_aliases = set()
374 374
         if self.select:
@@ -378,43 +378,44 @@ def get_columns(self, with_aliases=False):
378 378
                     if with_aliases and col[1] in col_aliases:
379 379
                         c_alias = 'Col%d' % len(col_aliases)
380 380
                         result.append('%s AS %s' % (r, c_alias))
381  
-                        aliases.append(c_alias)
  381
+                        aliases.add(c_alias)
382 382
                         col_aliases.add(c_alias)
383 383
                     else:
384 384
                         result.append(r)
385  
-                        aliases.append(r)
  385
+                        aliases.add(r)
386 386
                         col_aliases.add(col[1])
387 387
                 else:
388 388
                     result.append(col.as_sql(quote_func=qn))
389 389
                     if hasattr(col, 'alias'):
390  
-                        aliases.append(col.alias)
  390
+                        aliases.add(col.alias)
391 391
                         col_aliases.add(col.alias)
392 392
         elif self.default_cols:
393  
-            cols = self.get_default_columns(True, with_aliases, col_aliases)
  393
+            cols, new_aliases = self.get_default_columns(with_aliases,
  394
+                    col_aliases)
394 395
             result.extend(cols)
395  
-            aliases.extend(cols)
  396
+            aliases.update(new_aliases)
396 397
         for table, col in self.related_select_cols:
397 398
             r = '%s.%s' % (qn(table), qn(col))
398 399
             if with_aliases and col in col_aliases:
399 400
                 c_alias = 'Col%d' % len(col_aliases)
400 401
                 result.append('%s AS %s' % (r, c_alias))
401  
-                aliases.append(c_alias)
  402
+                aliases.add(c_alias)
402 403
                 col_aliases.add(c_alias)
403 404
             else:
404 405
                 result.append(r)
405  
-                aliases.append(r)
  406
+                aliases.add(r)
406 407
                 col_aliases.add(col)
407 408
 
408  
-        self._select_aliases = set(aliases)
  409
+        self._select_aliases = aliases
409 410
         return result
410 411
 
411  
-    def get_default_columns(self, as_str=False, with_aliases=False,
412  
-            col_aliases=None):
  412
+    def get_default_columns(self, with_aliases=False, col_aliases=None):
413 413
         """
414 414
         Computes the default columns for selecting every field in the base
415  
-        model. Returns a list of default (alias, column) pairs suitable for
416  
-        inclusion as the select columns. If 'as_str' is True, returns a list of
417  
-        strings, quoted appropriately for use in SQL directly.
  415
+        model.
  416
+
  417
+        Returns a list of strings, quoted appropriately for use in SQL
  418
+        directly, as well as a set of aliases used in the select statement.
418 419
         """
419 420
         result = []
420 421
         table_alias = self.tables[0]
@@ -422,6 +423,7 @@ def get_default_columns(self, as_str=False, with_aliases=False,
422 423
         seen = {None: table_alias}
423 424
         qn = self.quote_name_unless_alias
424 425
         qn2 = self.connection.ops.quote_name
  426
+        aliases = set()
425 427
         for field, model in self.model._meta.get_fields_with_model():
426 428
             try:
427 429
                 alias = seen[model]
@@ -429,16 +431,19 @@ def get_default_columns(self, as_str=False, with_aliases=False,
429 431
                 alias = self.join((table_alias, model._meta.db_table,
430 432
                         root_pk, model._meta.pk.column))
431 433
                 seen[model] = alias
432  
-            if as_str:
433  
-                if with_aliases and field.column in col_aliases:
434  
-                    c_alias = 'Col%d' % len(col_aliases)
435  
-                    result.append('%s.%s AS %s' % (qn(alias),
436  
-                        qn2(field.column), c_alias))
437  
-                else:
438  
-                    result.append('%s.%s' % (qn(alias), qn2(field.column)))
  434
+            if with_aliases and field.column in col_aliases:
  435
+                c_alias = 'Col%d' % len(col_aliases)
  436
+                result.append('%s.%s AS %s' % (qn(alias),
  437
+                    qn2(field.column), c_alias))
  438
+                col_aliases.add(c_alias)
  439
+                aliases.add(c_alias)
439 440
             else:
440  
-                result.append((alias, field.column))
441  
-        return result
  441
+                r = '%s.%s' % (qn(alias), qn2(field.column))
  442
+                result.append(r)
  443
+                aliases.add(r)
  444
+                if with_aliases:
  445
+                    col_aliases.add(field.column)
  446
+        return result, aliases
442 447
 
443 448
     def get_from_clause(self):
444 449
         """

0 notes on commit 67bfd15

Please sign in to comment.
Something went wrong with that request. Please try again.