pyodbc.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # sybase/pyodbc.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """
  8. .. dialect:: sybase+pyodbc
  9. :name: PyODBC
  10. :dbapi: pyodbc
  11. :connectstring: sybase+pyodbc://<username>:<password>@<dsnname>\
  12. [/<database>]
  13. :url: http://pypi.python.org/pypi/pyodbc/
  14. Unicode Support
  15. ---------------
  16. The pyodbc driver currently supports usage of these Sybase types with
  17. Unicode or multibyte strings::
  18. CHAR
  19. NCHAR
  20. NVARCHAR
  21. TEXT
  22. VARCHAR
  23. Currently *not* supported are::
  24. UNICHAR
  25. UNITEXT
  26. UNIVARCHAR
  27. """
  28. from sqlalchemy.dialects.sybase.base import SybaseDialect,\
  29. SybaseExecutionContext
  30. from sqlalchemy.connectors.pyodbc import PyODBCConnector
  31. from sqlalchemy import types as sqltypes, processors
  32. import decimal
  33. class _SybNumeric_pyodbc(sqltypes.Numeric):
  34. """Turns Decimals with adjusted() < -6 into floats.
  35. It's not yet known how to get decimals with many
  36. significant digits or very large adjusted() into Sybase
  37. via pyodbc.
  38. """
  39. def bind_processor(self, dialect):
  40. super_process = super(_SybNumeric_pyodbc, self).\
  41. bind_processor(dialect)
  42. def process(value):
  43. if self.asdecimal and \
  44. isinstance(value, decimal.Decimal):
  45. if value.adjusted() < -6:
  46. return processors.to_float(value)
  47. if super_process:
  48. return super_process(value)
  49. else:
  50. return value
  51. return process
  52. class SybaseExecutionContext_pyodbc(SybaseExecutionContext):
  53. def set_ddl_autocommit(self, connection, value):
  54. if value:
  55. connection.autocommit = True
  56. else:
  57. connection.autocommit = False
  58. class SybaseDialect_pyodbc(PyODBCConnector, SybaseDialect):
  59. execution_ctx_cls = SybaseExecutionContext_pyodbc
  60. colspecs = {
  61. sqltypes.Numeric: _SybNumeric_pyodbc,
  62. }
  63. dialect = SybaseDialect_pyodbc