]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Trying one more time to get the decimal handling on mssql right. Closes #1282.
authorMichael Trier <mtrier@gmail.com>
Thu, 22 Jan 2009 01:55:06 +0000 (01:55 +0000)
committerMichael Trier <mtrier@gmail.com>
Thu, 22 Jan 2009 01:55:06 +0000 (01:55 +0000)
CHANGES
lib/sqlalchemy/databases/mssql.py
test/dialect/mssql.py

diff --git a/CHANGES b/CHANGES
index 164d92b0b5ccd18a2e8b24d7be7a15c5e9642abc..81ef239da32fe9fc60705f3ce1c496f0abc01d23 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -25,7 +25,7 @@ CHANGES
     - Fixed bug in delete-orphan cascade whereby two one-to-one
       relations from two different parent classes to the same target 
       class would prematurely expunge the instance.
-      
+
 - sql
     - Further fixes to the "percent signs and spaces in column/table
        names" functionality. [ticket:1284]
@@ -34,6 +34,8 @@ CHANGES
     - Restored convert_unicode handling. Results were being passed
       on through without conversion. [ticket:1291]
 
+    - Really fixing the decimal handling this time. [ticket:1282].
+
 0.5.1
 ========
 
index 1a3c20a071049d6ae24d8cc317016da859981e04..ddd13067943f9d28a0f744754277f8d7cdbcb425 100644 (file)
@@ -328,17 +328,35 @@ class MSNumeric(sqltypes.Numeric):
             if value is None:
                 # Not sure that this exception is needed
                 return value
-            else:
-                if isinstance(value, decimal.Decimal):
-                    sign = (value < 0 and '-' or '') 
-                    if value._exp > -1:
-                        return float(sign + value._int + '0' * value._exp)
-                    else:
-                        s = value._int.zfill(-value._exp+1)
-                        pos = len(s) + value._exp
-                        return sign + s[:pos] + '.' + s[pos:]
+
+            elif isinstance(value, decimal.Decimal):
+                if value.adjusted() < 0:
+                    result = "%s0.%s%s" % (
+                            (value < 0 and '-' or ''),
+                            '0' * (abs(value.adjusted()) - 1),
+                            "".join([str(nint) for nint in value._int]))
+
                 else:
-                    return value
+                    if 'E' in str(value):
+                        result = "%s%s%s" % (
+                                (value < 0 and '-' or ''),
+                                "".join([str(s) for s in value._int]),
+                                "0" * (value.adjusted() - (len(value._int)-1)))
+                    else:
+                        if (len(value._int) - 1) > value.adjusted():
+                            result = "%s%s.%s" % (
+                                    (value < 0 and '-' or ''),
+                                    "".join([str(s) for s in value._int][0:value.adjusted() + 1]),
+                                    "".join([str(s) for s in value._int][value.adjusted() + 1:]))
+                        else:
+                            result = "%s%s" % (
+                                    (value < 0 and '-' or ''),
+                                    "".join([str(s) for s in value._int][0:value.adjusted() + 1]))
+
+                return result
+
+            else:
+                return value
 
         return process
 
index 08ddfd5a1ba837348402e07d682ae4bdc09ee573..3ee3ab9972c1cb4c0b5a5b5081b1ca70d9cd84fc 100755 (executable)
@@ -511,10 +511,15 @@ class TypesTest(TestBase):
         try:
             test_items = [decimal.Decimal(d) for d in '1500000.00000000000000000000',
                           '-1500000.00000000000000000000', '1500000',
-                          '0.0000000000000000002', '0.2', '-0.0000000000000000002',
-                          '156666.458923543', '-156666.458923543', '1', '-1', '1234',
+                          '0.0000000000000000002', '0.2', '-0.0000000000000000002', '-2E-2',
+                          '156666.458923543', '-156666.458923543', '1', '-1', '-1234', '1234',
                           '2E-12', '4E8', '3E-6', '3E-7', '4.1', '1E-1', '1E-2', '1E-3',
-                          '1E-4', '1E-5', '1E-6', '1E-7', '1E-8']
+                          '1E-4', '1E-5', '1E-6', '1E-7', '1E-1', '1E-8', '0.2732E2', '-0.2432E2', '4.35656E2',
+                          '-02452E-2', '45125E-2',
+                          '1234.58965E-2', '1.521E+15', '-1E-25', '1E-25', '1254E-25', '-1203E-25',
+                          '0', '-0.00', '-0', '4585E12', '000000000000000000012', '000000000000.32E12',
+                          '00000000000000.1E+12', '000000000000.2E-32']
+
             for value in test_items:
                 numeric_table.insert().execute(numericcol=value)