__init__.py 1.8 KB

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