Ticket #19625: django19625-master.mysql-decimalfield-lookup-fail.workaround.patch

File django19625-master.mysql-decimalfield-lookup-fail.workaround.patch, 2.9 KB (added by Walter Doekes, 12 years ago)

Workaround, which raises SQL Warnings instead.

  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 78dbbc6..a9cfc4b 100644
    a b class BaseDatabaseOperations(object):  
    526526        """
    527527        return "%s"
    528528
     529    def decimal_cast_sql(self, max_digits, decimal_places):
     530        """
     531        FIXME: documentation
     532        """
     533        return "%s"
     534
    529535    def deferrable_sql(self):
    530536        """
    531537        Returns the SQL necessary to make a constraint "initially deferred"
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index f24df93..940200b 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    222222        return "(%s %s INTERVAL '%d 0:0:%d:%d' DAY_MICROSECOND)" % (sql, connector,
    223223                timedelta.days, timedelta.seconds, timedelta.microseconds)
    224224
     225    def decimal_cast_sql(self, max_digits, decimal_places):
     226        """
     227        FIXME: documentation
     228        """
     229        return "CAST(%%s AS DECIMAL(%d, %d))" % (max_digits, decimal_places)
     230
    225231    def drop_foreignkey_sql(self):
    226232        return "DROP FOREIGN KEY"
    227233
  • django/db/models/sql/where.py

    diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
    index 3e4b352..eff1a12 100644
    a b from __future__ import absolute_import  
    66
    77import collections
    88import datetime
     9import decimal
     10import re
    911from itertools import repeat
    1012
    1113from django.utils import tree
    from django.utils.six.moves import xrange  
    1820AND = 'AND'
    1921OR = 'OR'
    2022
     23# Extract precision from decimal field info
     24DECIMAL_PRECISION_RE = re.compile(r'^\D+(\d+)\D+(\d+)\D+$')
     25
    2126class EmptyShortCircuit(Exception):
    2227    """
    2328    Internal exception used to indicate that a "matches nothing" node should be
    class WhereNode(tree.Node):  
    6267        # here in the future (using Python types is suggested for consistency).
    6368        if isinstance(value, datetime.datetime):
    6469            value_annotation = datetime.datetime
     70        elif isinstance(value, decimal.Decimal):
     71            value_annotation = decimal.Decimal
    6572        elif hasattr(value, 'value_annotation'):
    6673            value_annotation = value.value_annotation
    6774        else:
    class WhereNode(tree.Node):  
    176183
    177184        if value_annotation is datetime.datetime:
    178185            cast_sql = connection.ops.datetime_cast_sql()
     186        elif value_annotation is decimal.Decimal:
     187            # lvalue[2] holds something like 'numeric(12, 6)'
     188            precision_match = DECIMAL_PRECISION_RE.match(lvalue[2])
     189            max_digits, dec_places = [int(i) for i in precision_match.groups()]
     190            cast_sql = connection.ops.decimal_cast_sql(max_digits, dec_places)
     191            del precision_match
     192            del max_digits
     193            del dec_places
    179194        else:
    180195            cast_sql = '%s'
    181196
Back to Top