diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-19 11:17:14 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-19 11:17:14 -0400 |
| commit | 5be0d3133bb3591ca31e2da0a01fb3d3038aa9f8 (patch) | |
| tree | b87ada594b4023536cece6698005571e73758408 /lib/sqlalchemy/connectors | |
| parent | b2c2f58d983147a27031e20b95af4191b2aa8356 (diff) | |
| download | sqlalchemy-5be0d3133bb3591ca31e2da0a01fb3d3038aa9f8.tar.gz | |
switching Decimal treatment in MSSQL to be pyodbc specific, added
to connector to share between sybase/mssql. Going
with turning decimals with very low significant digit to floats,
seems to work so far.
Diffstat (limited to 'lib/sqlalchemy/connectors')
| -rw-r--r-- | lib/sqlalchemy/connectors/pyodbc.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/sqlalchemy/connectors/pyodbc.py b/lib/sqlalchemy/connectors/pyodbc.py index e503135f7..5cfe4a192 100644 --- a/lib/sqlalchemy/connectors/pyodbc.py +++ b/lib/sqlalchemy/connectors/pyodbc.py @@ -4,6 +4,59 @@ from sqlalchemy.util import asbool import sys import re import urllib +import decimal +from sqlalchemy import processors, types as sqltypes + +class PyODBCNumeric(sqltypes.Numeric): + """Turns Decimals with adjusted() < -6 into floats.""" + + def bind_processor(self, dialect): + super_process = super(PyODBCNumeric, self).bind_processor(dialect) + + def process(value): + if self.asdecimal and \ + isinstance(value, decimal.Decimal) and \ + value.adjusted() < -6: + return processors.to_float(value) + elif super_process: + return super_process(value) + else: + return value + return process + + # This method turns the adjusted into a string. + # not sure if this has advantages over the simple float + # approach above. +# def bind_processor(self, dialect): +# def process(value): +# if 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: +# 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 class PyODBCConnector(Connector): driver='pyodbc' |
