fdb.py 4.3 KB

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