__init__.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # dialects/__init__.py
  2. # Copyright (C) 2005-2024 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. from __future__ import annotations
  8. from typing import Callable
  9. from typing import Optional
  10. from typing import Type
  11. from typing import TYPE_CHECKING
  12. from .. import util
  13. if TYPE_CHECKING:
  14. from ..engine.interfaces import Dialect
  15. __all__ = ("mssql", "mysql", "oracle", "postgresql", "sqlite")
  16. def _auto_fn(name: str) -> Optional[Callable[[], Type[Dialect]]]:
  17. """default dialect importer.
  18. plugs into the :class:`.PluginLoader`
  19. as a first-hit system.
  20. """
  21. if "." in name:
  22. dialect, driver = name.split(".")
  23. else:
  24. dialect = name
  25. driver = "base"
  26. try:
  27. if dialect == "mariadb":
  28. # it's "OK" for us to hardcode here since _auto_fn is already
  29. # hardcoded. if mysql / mariadb etc were third party dialects
  30. # they would just publish all the entrypoints, which would actually
  31. # look much nicer.
  32. module = __import__(
  33. "sqlalchemy.dialects.mysql.mariadb"
  34. ).dialects.mysql.mariadb
  35. return module.loader(driver) # type: ignore
  36. else:
  37. module = __import__("sqlalchemy.dialects.%s" % (dialect,)).dialects
  38. module = getattr(module, dialect)
  39. except ImportError:
  40. return None
  41. if hasattr(module, driver):
  42. module = getattr(module, driver)
  43. return lambda: module.dialect
  44. else:
  45. return None
  46. registry = util.PluginLoader("sqlalchemy.dialects", auto_fn=_auto_fn)
  47. plugins = util.PluginLoader("sqlalchemy.plugins")