mxodbc.py 4.6 KB

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