zxjdbc.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # mssql/zxjdbc.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+zxjdbc
  9. :name: zxJDBC for Jython
  10. :dbapi: zxjdbc
  11. :connectstring: mssql+zxjdbc://user:pass@host:port/dbname\
  12. [?key=value&key=value...]
  13. :driverurl: http://jtds.sourceforge.net/
  14. .. note:: Jython is not supported by current versions of SQLAlchemy. The
  15. zxjdbc dialect should be considered as experimental.
  16. """
  17. from ...connectors.zxJDBC import ZxJDBCConnector
  18. from .base import MSDialect, MSExecutionContext
  19. from ... import engine
  20. class MSExecutionContext_zxjdbc(MSExecutionContext):
  21. _embedded_scope_identity = False
  22. def pre_exec(self):
  23. super(MSExecutionContext_zxjdbc, self).pre_exec()
  24. # scope_identity after the fact returns null in jTDS so we must
  25. # embed it
  26. if self._select_lastrowid and self.dialect.use_scope_identity:
  27. self._embedded_scope_identity = True
  28. self.statement += "; SELECT scope_identity()"
  29. def post_exec(self):
  30. if self._embedded_scope_identity:
  31. while True:
  32. try:
  33. row = self.cursor.fetchall()[0]
  34. break
  35. except self.dialect.dbapi.Error:
  36. self.cursor.nextset()
  37. self._lastrowid = int(row[0])
  38. if (self.isinsert or self.isupdate or self.isdelete) and \
  39. self.compiled.returning:
  40. self._result_proxy = engine.FullyBufferedResultProxy(self)
  41. if self._enable_identity_insert:
  42. table = self.dialect.identifier_preparer.format_table(
  43. self.compiled.statement.table)
  44. self.cursor.execute("SET IDENTITY_INSERT %s OFF" % table)
  45. class MSDialect_zxjdbc(ZxJDBCConnector, MSDialect):
  46. jdbc_db_name = 'jtds:sqlserver'
  47. jdbc_driver_name = 'net.sourceforge.jtds.jdbc.Driver'
  48. execution_ctx_cls = MSExecutionContext_zxjdbc
  49. def _get_server_version_info(self, connection):
  50. return tuple(
  51. int(x)
  52. for x in connection.connection.dbversion.split('.')
  53. )
  54. dialect = MSDialect_zxjdbc