]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_api/sql/sqlalchemy_types/json.py
1c8f9f7b7ec812f449036cf6a71624247f314a79
[nominatim.git] / src / nominatim_api / sql / sqlalchemy_types / json.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Common json type for different dialects.
9 """
10 from typing import Any
11
12 import sqlalchemy as sa
13 from sqlalchemy.dialects.postgresql import JSONB
14 from sqlalchemy.dialects.sqlite import JSON as sqlite_json
15
16 from ...typing import SaDialect
17
18
19 class Json(sa.types.TypeDecorator[Any]):
20     """ Dialect-independent type for JSON.
21     """
22     impl = sa.types.JSON
23     cache_ok = True
24
25     def load_dialect_impl(self, dialect: SaDialect) -> sa.types.TypeEngine[Any]:
26         if dialect.name == 'postgresql':
27             return JSONB(none_as_null=True)  # type: ignore[no-untyped-call]
28
29         return sqlite_json(none_as_null=True)