|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# vim:fenc=utf-8 |
| 3 | +# |
| 4 | +# Copyright (c) 2025 Fayaz Yusuf Khan <fayaz.yusuf.khan@gmail.com> |
| 5 | +# Distributed under terms of the MIT license. |
| 6 | +"""Compatibility layer for SQLAlchemy versions.""" |
| 7 | +import sqlalchemy as sa |
| 8 | + |
| 9 | + |
| 10 | +if sa.__version__ < '1.4': |
| 11 | + from sqlalchemy.ext.declarative import declarative_base |
| 12 | +else: |
| 13 | + from sqlalchemy.orm import declarative_base |
| 14 | + |
| 15 | + |
| 16 | +def select(*args, **kwargs): |
| 17 | + """Compatibility function for select.""" |
| 18 | + if sa.__version__ < '1.4': |
| 19 | + return sa.select(args, **kwargs) |
| 20 | + else: |
| 21 | + return sa.select(*args, **kwargs) |
| 22 | + |
| 23 | + |
| 24 | +def case(*args, **kwargs): |
| 25 | + """Compatibility function for case.""" |
| 26 | + if sa.__version__ < '1.4': |
| 27 | + return sa.case(args, **kwargs) |
| 28 | + else: |
| 29 | + return sa.case(*args, **kwargs) |
| 30 | + |
| 31 | + |
| 32 | +def get(session, model, id): |
| 33 | + """Compatibility function for getting an object by ID.""" |
| 34 | + if sa.__version__ < '1.4': |
| 35 | + return session.query(model).get(id) |
| 36 | + else: |
| 37 | + return session.get(model, id) |
| 38 | + |
| 39 | + |
| 40 | +__all__ = ["case", "declarative_base", "select"] |
0 commit comments