diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-02-26 17:50:34 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-02-26 17:50:34 +0000 |
| commit | 2d95ef1f252b2752a3840e84a01d989af9921033 (patch) | |
| tree | f7a9086dc5fed65ef4cc658a5c5d9742bcc58077 /lib/sqlalchemy | |
| parent | 11f996da20cf40692a21f6e836655cc36d1857d7 (diff) | |
| download | sqlalchemy-2d95ef1f252b2752a3840e84a01d989af9921033.tar.gz | |
- the "scale" argument of the Numeric() type is honored when
coercing a returned floating point value into a string
on its way to Decimal - this allows accuracy to function
on SQLite, MySQL. [ticket:1717]
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/processors.py | 32 | ||||
| -rw-r--r-- | lib/sqlalchemy/test/util.py | 11 | ||||
| -rw-r--r-- | lib/sqlalchemy/types.py | 12 |
3 files changed, 44 insertions, 11 deletions
diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index 4cf6831bd..04fa5054a 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -38,9 +38,10 @@ try: return UnicodeResultProcessor(encoding, errors).process else: return UnicodeResultProcessor(encoding).process - - def to_decimal_processor_factory(target_class): - return DecimalResultProcessor(target_class).process + + # TODO: add scale argument + #def to_decimal_processor_factory(target_class): + # return DecimalResultProcessor(target_class).process except ImportError: def to_unicode_processor_factory(encoding, errors=None): @@ -57,13 +58,14 @@ except ImportError: return decoder(value, errors)[0] return process - def to_decimal_processor_factory(target_class): - def process(value): - if value is None: - return None - else: - return target_class(str(value)) - return process + # TODO: add scale argument + #def to_decimal_processor_factory(target_class): + # def process(value): + # if value is None: + # return None + # else: + # return target_class(str(value)) + # return process def to_float(value): if value is None: @@ -92,3 +94,13 @@ except ImportError: str_to_time = str_to_datetime_processor_factory(TIME_RE, datetime.time) str_to_date = str_to_datetime_processor_factory(DATE_RE, datetime.date) + +def to_decimal_processor_factory(target_class, scale=10): + fstring = "%%.%df" % scale + + def process(value): + if value is None: + return None + else: + return target_class(fstring % value) + return process diff --git a/lib/sqlalchemy/test/util.py b/lib/sqlalchemy/test/util.py index 5be00f906..8a3a0e745 100644 --- a/lib/sqlalchemy/test/util.py +++ b/lib/sqlalchemy/test/util.py @@ -39,4 +39,15 @@ def picklers(): for pickle in picklers: for protocol in -1, 0, 1, 2: yield pickle.loads, lambda d:pickle.dumps(d, protocol) + + +def round_decimal(value, prec): + if isinstance(value, float): + return round(value, prec) + + import decimal + + # can also use shift() here but that is 2.6 only + return (value * decimal.Decimal("1" + "0" * prec)).to_integral(decimal.ROUND_FLOOR) / \ + pow(10, prec)
\ No newline at end of file diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index d5f1d9f14..d7b8f9289 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -838,7 +838,7 @@ class Numeric(_DateAffinity, TypeEngine): # try: # from fastdec import mpd as Decimal # except ImportError: - return processors.to_decimal_processor_factory(_python_Decimal) + return processors.to_decimal_processor_factory(_python_Decimal, self.scale) else: return None @@ -877,6 +877,16 @@ class Float(Numeric): def adapt(self, impltype): return impltype(precision=self.precision, asdecimal=self.asdecimal) + def result_processor(self, dialect, coltype): + if self.asdecimal: + #XXX: use decimal from http://www.bytereef.org/libmpdec.html +# try: +# from fastdec import mpd as Decimal +# except ImportError: + return processors.to_decimal_processor_factory(_python_Decimal) + else: + return None + class DateTime(_DateAffinity, TypeEngine): """A type for ``datetime.datetime()`` objects. |
