|
| 1 | +"""Backend-specific dialect classes for xml2db. |
| 2 | +
|
| 3 | +This package centralises all database-backend-specific behaviour that was |
| 4 | +previously scattered across the codebase as ``if db_type == "..."`` |
| 5 | +conditionals. Each supported backend has a dedicated subclass of |
| 6 | +:class:`~xml2db.dialect.base.DatabaseDialect`. Unknown backends fall back to |
| 7 | +the base class, which provides safe, generic defaults. |
| 8 | +
|
| 9 | +Usage:: |
| 10 | +
|
| 11 | + from xml2db.dialect import get_dialect |
| 12 | +
|
| 13 | + dialect = get_dialect("postgresql") |
| 14 | + physical_name = dialect.db_identifier("some_very_long_xsd_derived_name") |
| 15 | +
|
| 16 | +The registry is a plain dict so that third-party code (or tests) can register |
| 17 | +custom dialects without subclassing anything in xml2db:: |
| 18 | +
|
| 19 | + from xml2db.dialect import DIALECT_REGISTRY |
| 20 | + from mypackage import OracleDialect |
| 21 | +
|
| 22 | + DIALECT_REGISTRY["oracle"] = OracleDialect |
| 23 | +""" |
| 24 | + |
| 25 | +from .base import DatabaseDialect |
| 26 | +from .duckdb import DuckDBDialect |
| 27 | +from .mssql import MSSQLDialect |
| 28 | +from .mysql import MySQLDialect |
| 29 | +from .postgresql import PostgreSQLDialect |
| 30 | + |
| 31 | +__all__ = [ |
| 32 | + "DatabaseDialect", |
| 33 | + "DuckDBDialect", |
| 34 | + "MSSQLDialect", |
| 35 | + "MySQLDialect", |
| 36 | + "PostgreSQLDialect", |
| 37 | + "DIALECT_REGISTRY", |
| 38 | + "get_dialect", |
| 39 | +] |
| 40 | + |
| 41 | +# Maps the SQLAlchemy dialect name (as returned by engine.dialect.name) to |
| 42 | +# the corresponding DatabaseDialect subclass. |
| 43 | +DIALECT_REGISTRY: dict[str, type[DatabaseDialect]] = { |
| 44 | + "postgresql": PostgreSQLDialect, |
| 45 | + "mssql": MSSQLDialect, |
| 46 | + "mysql": MySQLDialect, |
| 47 | + "mariadb": MySQLDialect, # SQLAlchemy reports MariaDB as "mariadb" |
| 48 | + "duckdb": DuckDBDialect, |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +def get_dialect(db_type: str | None) -> DatabaseDialect: |
| 53 | + """Return a :class:`DatabaseDialect` instance for the given backend name. |
| 54 | +
|
| 55 | + Args: |
| 56 | + db_type: The SQLAlchemy dialect name, e.g. ``"postgresql"``, |
| 57 | + ``"mssql"``, ``"mysql"``, ``"duckdb"``. ``None`` or any |
| 58 | + unrecognised string falls back to the base |
| 59 | + :class:`DatabaseDialect`, which uses safe generic defaults. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + An instantiated :class:`DatabaseDialect` (or subclass) ready for use. |
| 63 | + """ |
| 64 | + cls = DIALECT_REGISTRY.get(db_type, DatabaseDialect) |
| 65 | + return cls() |
0 commit comments