Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

151 linhas
4.7KB

  1. # mssql/mxodbc.py
  2. # Copyright (C) 2005-2021 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:: mssql+mxodbc
  9. :name: mxODBC
  10. :dbapi: mxodbc
  11. :connectstring: mssql+mxodbc://<username>:<password>@<dsnname>
  12. :url: http://www.egenix.com/
  13. .. deprecated:: 1.4 The mxODBC DBAPI is deprecated and will be removed
  14. in a future version. Please use one of the supported DBAPIs to
  15. connect to mssql.
  16. Execution Modes
  17. ---------------
  18. mxODBC features two styles of statement execution, using the
  19. ``cursor.execute()`` and ``cursor.executedirect()`` methods (the second being
  20. an extension to the DBAPI specification). The former makes use of a particular
  21. API call specific to the SQL Server Native Client ODBC driver known
  22. SQLDescribeParam, while the latter does not.
  23. mxODBC apparently only makes repeated use of a single prepared statement
  24. when SQLDescribeParam is used. The advantage to prepared statement reuse is
  25. one of performance. The disadvantage is that SQLDescribeParam has a limited
  26. set of scenarios in which bind parameters are understood, including that they
  27. cannot be placed within the argument lists of function calls, anywhere outside
  28. the FROM, or even within subqueries within the FROM clause - making the usage
  29. of bind parameters within SELECT statements impossible for all but the most
  30. simplistic statements.
  31. For this reason, the mxODBC dialect uses the "native" mode by default only for
  32. INSERT, UPDATE, and DELETE statements, and uses the escaped string mode for
  33. all other statements.
  34. This behavior can be controlled via
  35. :meth:`~sqlalchemy.sql.expression.Executable.execution_options` using the
  36. ``native_odbc_execute`` flag with a value of ``True`` or ``False``, where a
  37. value of ``True`` will unconditionally use native bind parameters and a value
  38. of ``False`` will unconditionally use string-escaped parameters.
  39. """
  40. from .base import _MSDate
  41. from .base import _MSDateTime
  42. from .base import _MSTime
  43. from .base import MSDialect
  44. from .base import VARBINARY
  45. from .pyodbc import _MSNumeric_pyodbc
  46. from .pyodbc import MSExecutionContext_pyodbc
  47. from ... import types as sqltypes
  48. from ...connectors.mxodbc import MxODBCConnector
  49. class _MSNumeric_mxodbc(_MSNumeric_pyodbc):
  50. """Include pyodbc's numeric processor."""
  51. class _MSDate_mxodbc(_MSDate):
  52. def bind_processor(self, dialect):
  53. def process(value):
  54. if value is not None:
  55. return "%s-%s-%s" % (value.year, value.month, value.day)
  56. else:
  57. return None
  58. return process
  59. class _MSTime_mxodbc(_MSTime):
  60. def bind_processor(self, dialect):
  61. def process(value):
  62. if value is not None:
  63. return "%s:%s:%s" % (value.hour, value.minute, value.second)
  64. else:
  65. return None
  66. return process
  67. class _VARBINARY_mxodbc(VARBINARY):
  68. """
  69. mxODBC Support for VARBINARY column types.
  70. This handles the special case for null VARBINARY values,
  71. which maps None values to the mx.ODBC.Manager.BinaryNull symbol.
  72. """
  73. def bind_processor(self, dialect):
  74. if dialect.dbapi is None:
  75. return None
  76. DBAPIBinary = dialect.dbapi.Binary
  77. def process(value):
  78. if value is not None:
  79. return DBAPIBinary(value)
  80. else:
  81. # should pull from mx.ODBC.Manager.BinaryNull
  82. return dialect.dbapi.BinaryNull
  83. return process
  84. class MSExecutionContext_mxodbc(MSExecutionContext_pyodbc):
  85. """
  86. The pyodbc execution context is useful for enabling
  87. SELECT SCOPE_IDENTITY in cases where OUTPUT clause
  88. does not work (tables with insert triggers).
  89. """
  90. # todo - investigate whether the pyodbc execution context
  91. # is really only being used in cases where OUTPUT
  92. # won't work.
  93. class MSDialect_mxodbc(MxODBCConnector, MSDialect):
  94. # this is only needed if "native ODBC" mode is used,
  95. # which is now disabled by default.
  96. # statement_compiler = MSSQLStrictCompiler
  97. supports_statement_cache = True
  98. execution_ctx_cls = MSExecutionContext_mxodbc
  99. # flag used by _MSNumeric_mxodbc
  100. _need_decimal_fix = True
  101. colspecs = {
  102. sqltypes.Numeric: _MSNumeric_mxodbc,
  103. sqltypes.DateTime: _MSDateTime,
  104. sqltypes.Date: _MSDate_mxodbc,
  105. sqltypes.Time: _MSTime_mxodbc,
  106. VARBINARY: _VARBINARY_mxodbc,
  107. sqltypes.LargeBinary: _VARBINARY_mxodbc,
  108. }
  109. def __init__(self, description_encoding=None, **params):
  110. super(MSDialect_mxodbc, self).__init__(**params)
  111. self.description_encoding = description_encoding
  112. dialect = MSDialect_mxodbc