Commit d744c550 authored by Anssi Kääriäinen's avatar Anssi Kääriäinen
Browse files

Fixed #19964 -- Removed relabel_aliases from some structs

Before there was need to have both .relabel_aliases() and .clone() for
many structs. Now there is only relabeled_clone() for those structs
where alias is the only mutable attribute.
parent 679af405
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -344,9 +344,9 @@ class Field(object):
        if hasattr(value, 'get_compiler'):
            value = value.get_compiler(connection=connection)
        if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
            # If the value has a relabel_aliases method, it will need to
            # be invoked before the final SQL is evaluated
            if hasattr(value, 'relabel_aliases'):
            # If the value has a relabeled_clone method it means the
            # value will be handled later on.
            if hasattr(value, 'relabeled_clone'):
                return value
            if hasattr(value, 'as_sql'):
                sql, params = value.as_sql()
+3 −3
Original line number Diff line number Diff line
@@ -153,9 +153,9 @@ class RelatedField(object):
        if hasattr(value, 'get_compiler'):
            value = value.get_compiler(connection=connection)
        if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
            # If the value has a relabel_aliases method, it will need to
            # be invoked before the final SQL is evaluated
            if hasattr(value, 'relabel_aliases'):
            # If the value has a relabeled_clone method it means the
            # value will be handled later on.
            if hasattr(value, 'relabeled_clone'):
                return value
            if hasattr(value, 'as_sql'):
                sql, params = value.as_sql()
+4 −7
Original line number Diff line number Diff line
@@ -63,14 +63,11 @@ class Aggregate(object):

        self.field = tmp

    def clone(self):
        # Different aggregates have different init methods, so use copy here
        # deepcopy is not needed, as self.col is only changing variable.
        return copy.copy(self)

    def relabel_aliases(self, change_map):
    def relabeled_clone(self, change_map):
        clone = copy.copy(self)
        if isinstance(self.col, (list, tuple)):
            self.col = (change_map.get(self.col[0], self.col[0]), self.col[1])
            clone.col = (change_map.get(self.col[0], self.col[0]), self.col[1])
        return clone

    def as_sql(self, qn, connection):
        "Return the aggregate, rendered as SQL with parameters."
+4 −8
Original line number Diff line number Diff line
@@ -32,10 +32,8 @@ class Date(object):
        self.col = col
        self.lookup_type = lookup_type

    def relabel_aliases(self, change_map):
        c = self.col
        if isinstance(c, (list, tuple)):
            self.col = (change_map.get(c[0], c[0]), c[1])
    def relabeled_clone(self, change_map):
        return self.__class__((change_map.get(self.col[0], self.col[0]), self.col[1]))

    def as_sql(self, qn, connection):
        if isinstance(self.col, (list, tuple)):
@@ -53,10 +51,8 @@ class DateTime(object):
        self.lookup_type = lookup_type
        self.tzname = tzname

    def relabel_aliases(self, change_map):
        c = self.col
        if isinstance(c, (list, tuple)):
            self.col = (change_map.get(c[0], c[0]), c[1])
    def relabeled_clone(self, change_map):
        return self.__class__((change_map.get(self.col[0], self.col[0]), self.col[1]))

    def as_sql(self, qn, connection):
        if isinstance(self.col, (list, tuple)):
+12 −11
Original line number Diff line number Diff line
from django.core.exceptions import FieldError
from django.db.models.constants import LOOKUP_SEP
from django.db.models.fields import FieldDoesNotExist
import copy

class SQLEvaluator(object):
    def __init__(self, expression, query, allow_joins=True, reuse=None):
@@ -12,23 +13,23 @@ class SQLEvaluator(object):
        self.reuse = reuse
        self.expression.prepare(self, query, allow_joins)

    def relabeled_clone(self, change_map):
        clone = copy.copy(self)
        clone.cols = []
        for node, col in self.cols[:]:
            if hasattr(col, 'relabeled_clone'):
                clone.cols.append((node, col.relabeled_clone(change_map)))
            else:
                clone.cols.append((node,
                                   (change_map.get(col[0], col[0]), col[1])))
        return clone

    def prepare(self):
        return self

    def as_sql(self, qn, connection):
        return self.expression.evaluate(self, qn, connection)

    def relabel_aliases(self, change_map):
        new_cols = []
        for node, col in self.cols:
            if hasattr(col, "relabel_aliases"):
                col.relabel_aliases(change_map)
                new_cols.append((node, col))
            else:
                new_cols.append((node,
                                (change_map.get(col[0], col[0]), col[1])))
        self.cols = new_cols

    #####################################################
    # Vistor methods for initial expression preparation #
    #####################################################
Loading