fdb.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # firebird/fdb.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:: firebird+fdb
  9. :name: fdb
  10. :dbapi: pyodbc
  11. :connectstring: firebird+fdb://user:password@host:port/path/to/db[?key=value&key=value...]
  12. :url: https://pypi.org/project/fdb/
  13. fdb is a kinterbasdb compatible DBAPI for Firebird.
  14. .. versionchanged:: 0.9 - The fdb dialect is now the default dialect
  15. under the ``firebird://`` URL space, as ``fdb`` is now the official
  16. Python driver for Firebird.
  17. Arguments
  18. ----------
  19. The ``fdb`` dialect is based on the
  20. :mod:`sqlalchemy.dialects.firebird.kinterbasdb` dialect, however does not
  21. accept every argument that Kinterbasdb does.
  22. * ``enable_rowcount`` - True by default, setting this to False disables
  23. the usage of "cursor.rowcount" with the
  24. Kinterbasdb dialect, which SQLAlchemy ordinarily calls upon automatically
  25. after any UPDATE or DELETE statement. When disabled, SQLAlchemy's
  26. CursorResult will return -1 for result.rowcount. The rationale here is
  27. that Kinterbasdb requires a second round trip to the database when
  28. .rowcount is called - since SQLA's resultproxy automatically closes
  29. the cursor after a non-result-returning statement, rowcount must be
  30. called, if at all, before the result object is returned. Additionally,
  31. cursor.rowcount may not return correct results with older versions
  32. of Firebird, and setting this flag to False will also cause the
  33. SQLAlchemy ORM to ignore its usage. The behavior can also be controlled on a
  34. per-execution basis using the ``enable_rowcount`` option with
  35. :meth:`_engine.Connection.execution_options`::
  36. conn = engine.connect().execution_options(enable_rowcount=True)
  37. r = conn.execute(stmt)
  38. print(r.rowcount)
  39. * ``retaining`` - False by default. Setting this to True will pass the
  40. ``retaining=True`` keyword argument to the ``.commit()`` and ``.rollback()``
  41. methods of the DBAPI connection, which can improve performance in some
  42. situations, but apparently with significant caveats.
  43. Please read the fdb and/or kinterbasdb DBAPI documentation in order to
  44. understand the implications of this flag.
  45. .. versionchanged:: 0.9.0 - the ``retaining`` flag defaults to ``False``.
  46. In 0.8 it defaulted to ``True``.
  47. .. seealso::
  48. https://pythonhosted.org/fdb/usage-guide.html#retaining-transactions
  49. - information on the "retaining" flag.
  50. """ # noqa
  51. from .kinterbasdb import FBDialect_kinterbasdb
  52. from ... import util
  53. class FBDialect_fdb(FBDialect_kinterbasdb):
  54. supports_statement_cache = True
  55. def __init__(self, enable_rowcount=True, retaining=False, **kwargs):
  56. super(FBDialect_fdb, self).__init__(
  57. enable_rowcount=enable_rowcount, retaining=retaining, **kwargs
  58. )
  59. @classmethod
  60. def dbapi(cls):
  61. return __import__("fdb")
  62. def create_connect_args(self, url):
  63. opts = url.translate_connect_args(username="user")
  64. if opts.get("port"):
  65. opts["host"] = "%s/%s" % (opts["host"], opts["port"])
  66. del opts["port"]
  67. opts.update(url.query)
  68. util.coerce_kw_type(opts, "type_conv", int)
  69. return ([], opts)
  70. def _get_server_version_info(self, connection):
  71. """Get the version of the Firebird server used by a connection.
  72. Returns a tuple of (`major`, `minor`, `build`), three integers
  73. representing the version of the attached server.
  74. """
  75. # This is the simpler approach (the other uses the services api),
  76. # that for backward compatibility reasons returns a string like
  77. # LI-V6.3.3.12981 Firebird 2.0
  78. # where the first version is a fake one resembling the old
  79. # Interbase signature.
  80. isc_info_firebird_version = 103
  81. fbconn = connection.connection
  82. version = fbconn.db_info(isc_info_firebird_version)
  83. return self._parse_version_info(version)
  84. dialect = FBDialect_fdb