pyodbc.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # sybase/pyodbc.py
  2. # Copyright (C) 2005-2022 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: https://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>[/<database>]
  12. :url: https://pypi.org/project/pyodbc/
  13. Unicode Support
  14. ---------------
  15. The pyodbc driver currently supports usage of these Sybase types with
  16. Unicode or multibyte strings::
  17. CHAR
  18. NCHAR
  19. NVARCHAR
  20. TEXT
  21. VARCHAR
  22. Currently *not* supported are::
  23. UNICHAR
  24. UNITEXT
  25. UNIVARCHAR
  26. """ # noqa
  27. import decimal
  28. from sqlalchemy import processors
  29. from sqlalchemy import types as sqltypes
  30. from sqlalchemy.connectors.pyodbc import PyODBCConnector
  31. from sqlalchemy.dialects.sybase.base import SybaseDialect
  32. from sqlalchemy.dialects.sybase.base import SybaseExecutionContext
  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).bind_processor(dialect)
  41. def process(value):
  42. if self.asdecimal and isinstance(value, decimal.Decimal):
  43. if value.adjusted() < -6:
  44. return processors.to_float(value)
  45. if super_process:
  46. return super_process(value)
  47. else:
  48. return value
  49. return process
  50. class SybaseExecutionContext_pyodbc(SybaseExecutionContext):
  51. def set_ddl_autocommit(self, connection, value):
  52. if value:
  53. connection.autocommit = True
  54. else:
  55. connection.autocommit = False
  56. class SybaseDialect_pyodbc(PyODBCConnector, SybaseDialect):
  57. execution_ctx_cls = SybaseExecutionContext_pyodbc
  58. supports_statement_cache = True
  59. colspecs = {sqltypes.Numeric: _SybNumeric_pyodbc}
  60. @classmethod
  61. def dbapi(cls):
  62. return PyODBCConnector.dbapi()
  63. dialect = SybaseDialect_pyodbc