Code

Ticket #17854: django-1.1.x-bug17854-decimalfield-casts.patch

File django-1.1.x-bug17854-decimalfield-casts.patch, 2.9 KB (added by wdoekes, 15 months ago)

Possible fix against very old django.

  • django/db/models/sql/where.py

    $ diff -pu django/db/models/sql/where.py{.orig,}; diff -pu django/db/backends/__init__.py{.orig,}; diff -pu django/db/backends/mysql/base.py{.orig,}
    old new  
    22Code to manage the creation and SQL rendering of 'where' constraints. 
    33""" 
    44import datetime 
     5import decimal 
     6import re 
    57 
    68from django.utils import tree 
    79from django.db import connection 
    from datastructures import EmptyResultSe 
    1315AND = 'AND' 
    1416OR = 'OR' 
    1517 
     18# Extract precision from decimal field info 
     19DECIMAL_PRECISION_RE = re.compile(r'^\D+(\d+)\D+(\d+)\D+$') 
     20 
    1621class EmptyShortCircuit(Exception): 
    1722    """ 
    1823    Internal exception used to indicate that a "matches nothing" node should be 
    class WhereNode(tree.Node): 
    7075        # here in the future (using Python types is suggested for consistency). 
    7176        if isinstance(value, datetime.datetime): 
    7277            annotation = datetime.datetime 
     78        elif isinstance(value, decimal.Decimal): 
     79            annotation = decimal.Decimal 
    7380        elif hasattr(value, 'value_annotation'): 
    7481            annotation = value.value_annotation 
    7582        else: 
    class WhereNode(tree.Node): 
    154161 
    155162        if value_annot is datetime.datetime: 
    156163            cast_sql = connection.ops.datetime_cast_sql() 
     164        elif value_annot is decimal.Decimal: 
     165            # lvalue[2] holds something like 'numeric(12, 6)' 
     166            precision_match = DECIMAL_PRECISION_RE.match(lvalue[2]) 
     167            max_digits, dec_places = [int(i) for i in precision_match.groups()] 
     168            cast_sql = connection.ops.decimal_cast_sql(max_digits, dec_places) 
     169            del precision_match 
     170            del max_digits 
     171            del dec_places 
    157172        else: 
    158173            cast_sql = '%s' 
    159174 
  • django/db/backends/__init__.py

    old new class BaseDatabaseOperations(object): 
    143143        """ 
    144144        return "%s" 
    145145 
     146    def decimal_cast_sql(self, max_digits, decimal_places): 
     147        """ 
     148        FIXME: documentation 
     149        """ 
     150        return "%s" 
     151 
    146152    def deferrable_sql(self): 
    147153        """ 
    148154        Returns the SQL necessary to make a constraint "initially deferred" 
  • django/db/backends/mysql/base.py

    old new class DatabaseOperations(BaseDatabaseOpe 
    138138            sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
    139139        return sql 
    140140 
     141    def decimal_cast_sql(self, max_digits, decimal_places): 
     142        """ 
     143        FIXME: documentation 
     144        """ 
     145        return "CAST(%%s AS DECIMAL(%d, %d))" % (max_digits, decimal_places) 
     146 
    141147    def drop_foreignkey_sql(self): 
    142148        return "DROP FOREIGN KEY" 
    143149