| | 1 | from copy import deepcopy |
|---|
| | 2 | |
|---|
| | 3 | from django.db import connection |
|---|
| | 4 | from django.db.models.fields import FieldDoesNotExist |
|---|
| | 5 | from django.core.exceptions import FieldError |
|---|
| | 6 | from django.utils import tree |
|---|
| | 7 | |
|---|
| | 8 | class Expression(object): |
|---|
| | 9 | """ |
|---|
| | 10 | Base class for all sql expressions, expected by QuerySet.update. |
|---|
| | 11 | """ |
|---|
| | 12 | # Arithmetic connection types |
|---|
| | 13 | ADD = '+' |
|---|
| | 14 | SUB = '-' |
|---|
| | 15 | MUL = '*' |
|---|
| | 16 | DIV = '/' |
|---|
| | 17 | MOD = '%' |
|---|
| | 18 | |
|---|
| | 19 | # Logical connection types |
|---|
| | 20 | AND = 'AND' |
|---|
| | 21 | OR = 'OR' |
|---|
| | 22 | |
|---|
| | 23 | def _combine(self, other, conn, node=None): |
|---|
| | 24 | obj = node or ExpressionNode([self], conn) |
|---|
| | 25 | if isinstance(other, Expression): |
|---|
| | 26 | obj.add(other, conn) |
|---|
| | 27 | else: |
|---|
| | 28 | obj.add(L(other), conn) |
|---|
| | 29 | return obj |
|---|
| | 30 | |
|---|
| | 31 | def __add__(self, other): |
|---|
| | 32 | return self._combine(other, self.ADD) |
|---|
| | 33 | |
|---|
| | 34 | def __sub__(self, other): |
|---|
| | 35 | return self._combine(other, self.SUB) |
|---|
| | 36 | |
|---|
| | 37 | def __mul__(self, other): |
|---|
| | 38 | return self._combine(other, self.MUL) |
|---|
| | 39 | |
|---|
| | 40 | def __div__(self, other): |
|---|
| | 41 | return self._combine(other, self.DIV) |
|---|
| | 42 | |
|---|
| | 43 | def __mod__(self, other): |
|---|
| | 44 | return self._combine(other, self.MOD) |
|---|
| | 45 | |
|---|
| | 46 | def __and__(self, other): |
|---|
| | 47 | return self._combine(other, self.AND) |
|---|
| | 48 | |
|---|
| | 49 | def __or__(self, other): |
|---|
| | 50 | return self._combine(other, self.OR) |
|---|
| | 51 | |
|---|
| | 52 | def __invert__(self, node=None): |
|---|
| | 53 | obj = node or ExpressionNode([self]) |
|---|
| | 54 | obj.negate() |
|---|
| | 55 | return obj |
|---|
| | 56 | |
|---|
| | 57 | def as_sql(self, field, opts, qn=None): |
|---|
| | 58 | raise NotImplementedError |
|---|
| | 59 | |
|---|
| | 60 | class ExpressionNode(Expression, tree.Node): |
|---|
| | 61 | default = None |
|---|
| | 62 | |
|---|
| | 63 | def __init__(self, children=None, connector=None, negated=False): |
|---|
| | 64 | if children is not None and len(children) > 1 and connector is None: |
|---|
| | 65 | raise TypeError('You have to specify a connector.') |
|---|
| | 66 | super(ExpressionNode, self).__init__(children, connector, negated) |
|---|
| | 67 | |
|---|
| | 68 | def _combine(self, *args, **kwargs): |
|---|
| | 69 | return super(ExpressionNode, self)._combine(node=deepcopy(self), *args, **kwargs) |
|---|
| | 70 | |
|---|
| | 71 | def __invert__(self): |
|---|
| | 72 | return super(ExpressionNode, self).__invert__(node=deepcopy(self)) |
|---|
| | 73 | |
|---|
| | 74 | def as_sql(self, field, opts, qn=None, node=None): |
|---|
| | 75 | if node is None: |
|---|
| | 76 | node = self |
|---|
| | 77 | result = [] |
|---|
| | 78 | result_params = [] |
|---|
| | 79 | for child in node.children: |
|---|
| | 80 | if hasattr(child, 'as_sql'): |
|---|
| | 81 | sql, params = child.as_sql(field, opts, qn) |
|---|
| | 82 | format = '%s' |
|---|
| | 83 | else: |
|---|
| | 84 | sql, params = self.as_sql(field, opts, qn, child) |
|---|
| | 85 | if child.negated: |
|---|
| | 86 | format = 'NOT %s' |
|---|
| | 87 | else: |
|---|
| | 88 | format = '%s' |
|---|
| | 89 | if len(child.children) > 1: |
|---|
| | 90 | format %= '(%s)' |
|---|
| | 91 | if sql: |
|---|
| | 92 | result.append(format % sql) |
|---|
| | 93 | result_params.extend(params) |
|---|
| | 94 | conn = ' %s ' % node.connector |
|---|
| | 95 | return conn.join(result), result_params |
|---|
| | 96 | |
|---|
| | 97 | class L(Expression): |
|---|
| | 98 | """ |
|---|
| | 99 | An expression representing the given value. |
|---|
| | 100 | """ |
|---|
| | 101 | def __init__(self, value): |
|---|
| | 102 | self.value = value |
|---|
| | 103 | |
|---|
| | 104 | def as_sql(self, field, opts, qn=None): |
|---|
| | 105 | if self.value is None: |
|---|
| | 106 | return 'NULL', () |
|---|
| | 107 | if hasattr(field, 'get_placeholder'): |
|---|
| | 108 | return field.get_placeholder(self.value), (self.value,) |
|---|
| | 109 | return '%s', (self.value,) |
|---|
| | 110 | |
|---|
| | 111 | class F(Expression): |
|---|
| | 112 | """ |
|---|
| | 113 | An expression representing the value of the given field. |
|---|
| | 114 | """ |
|---|
| | 115 | def __init__(self, name): |
|---|
| | 116 | self.name = name |
|---|
| | 117 | |
|---|
| | 118 | def as_sql(self, field, opts, qn=None): |
|---|
| | 119 | if not qn: |
|---|
| | 120 | qn = connection.ops.quote_name |
|---|
| | 121 | try: |
|---|
| | 122 | column = opts.get_field(self.name).attname |
|---|
| | 123 | except FieldDoesNotExist: |
|---|
| | 124 | names = opts.get_all_field_names() |
|---|
| | 125 | raise FieldError('Cannot resolve keyword %r into field. ' |
|---|
| | 126 | 'Choices are: %s' % (self.name, ', '.join(names))) |
|---|
| | 127 | return '%s.%s' % (qn(opts.db_table), qn(column)), () |