mariadb.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # dialects/mysql/mariadb.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 .base import MariaDBIdentifierPreparer
  11. from .base import MySQLDialect
  12. from .base import MySQLIdentifierPreparer
  13. from .base import MySQLTypeCompiler
  14. from ...sql import sqltypes
  15. class INET4(sqltypes.TypeEngine[str]):
  16. """INET4 column type for MariaDB
  17. .. versionadded:: 2.0.37
  18. """
  19. __visit_name__ = "INET4"
  20. class INET6(sqltypes.TypeEngine[str]):
  21. """INET6 column type for MariaDB
  22. .. versionadded:: 2.0.37
  23. """
  24. __visit_name__ = "INET6"
  25. class MariaDBTypeCompiler(MySQLTypeCompiler):
  26. def visit_INET4(self, type_: INET4, **kwargs: Any) -> str:
  27. return "INET4"
  28. def visit_INET6(self, type_: INET6, **kwargs: Any) -> str:
  29. return "INET6"
  30. class MariaDBDialect(MySQLDialect):
  31. is_mariadb = True
  32. supports_statement_cache = True
  33. name = "mariadb"
  34. preparer: type[MySQLIdentifierPreparer] = MariaDBIdentifierPreparer
  35. type_compiler_cls = MariaDBTypeCompiler
  36. def loader(driver: str) -> Callable[[], type[MariaDBDialect]]:
  37. dialect_mod = __import__(
  38. "sqlalchemy.dialects.mysql.%s" % driver
  39. ).dialects.mysql
  40. driver_mod = getattr(dialect_mod, driver)
  41. if hasattr(driver_mod, "mariadb_dialect"):
  42. driver_cls = driver_mod.mariadb_dialect
  43. return driver_cls # type: ignore[no-any-return]
  44. else:
  45. driver_cls = driver_mod.dialect
  46. return type(
  47. "MariaDBDialect_%s" % driver,
  48. (
  49. MariaDBDialect,
  50. driver_cls,
  51. ),
  52. {"supports_statement_cache": True},
  53. )