from typing import Optional

from loguru import logger
from sqlmodel import Field, Session, SQLModel, create_engine, or_, select


class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    secret_name: str = Field(max_length=30)
    age: Optional[int] = None


def test_database_with_sqlmodel():
    hero_1 = Hero(name='Deadpond', secret_name='<NAME>')
    hero_2 = Hero(name='Spider-Boy', secret_name='<NAME>')
    hero_3 = Hero(name='Rusty-Man', secret_name='<NAME>', age=48)

    # engine = create_engine('sqlite:///temp.db')
    engine = create_engine('sqlite:///:memory:')

    SQLModel.metadata.create_all(engine)

    with Session(engine) as session:
        for hero in [hero_1, hero_2, hero_3]:
            session.add(hero)
        session.commit()

    with Session(engine) as session:
        statement = select(Hero).where(Hero.name == 'Spider-Boy')
        hero = session.exec(statement).first()
        logger.info(hero)

        # Or statement
        statement = select(Hero).where((Hero.name == 'Spider-Boy') | (Hero.name == 'Rusty-Man'))
        heroes = session.exec(statement)
        for hero in heroes:
            logger.info(hero)

        # Or statement, alternative way
        statement = select(Hero).where(or_(Hero.name == 'Spider-Boy', Hero.name == 'Rusty-Man'))
        heroes = session.exec(statement)
        for hero in heroes:
            logger.info(hero)

        # And statement
        statement = select(Hero).where(Hero.name == 'Spider-Boy', Hero.secret_name == '<NAME>')
        heroes = session.exec(statement)
        for hero in heroes:
            logger.info(hero)

        # And statement, alternative way
        statement = select(Hero).where(Hero.name == 'Spider-Boy').where(Hero.secret_name == '<NAME>')
        heroes = session.exec(statement)
        for hero in heroes:
            logger.info(hero)


if __name__ == '__main__':
    test_database_with_sqlmodel()

sqlmodel.SQLModel.metadata.create_all loguru.logger.info sqlmodel.Session sqlmodel.or_ sqlmodel.Field sqlmodel.select sqlmodel.create_engine
[((189, 226), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (194, 226), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((264, 284), 'sqlmodel.Field', 'Field', ([], {'max_length': '(30)'}), '(max_length=30)\n', (269, 284), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((598, 633), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite:///:memory:"""'], {}), "('sqlite:///:memory:')\n", (611, 633), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((639, 675), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (667, 675), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((686, 701), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (693, 701), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((825, 840), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (832, 840), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((974, 991), 'loguru.logger.info', 'logger.info', (['hero'], {}), '(hero)\n', (985, 991), False, 'from loguru import logger\n'), ((1194, 1211), 'loguru.logger.info', 'logger.info', (['hero'], {}), '(hero)\n', (1205, 1211), False, 'from loguru import logger\n'), ((1292, 1348), 'sqlmodel.or_', 'or_', (["(Hero.name == 'Spider-Boy')", "(Hero.name == 'Rusty-Man')"], {}), "(Hero.name == 'Spider-Boy', Hero.name == 'Rusty-Man')\n", (1295, 1348), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((1431, 1448), 'loguru.logger.info', 'logger.info', (['hero'], {}), '(hero)\n', (1442, 1448), False, 'from loguru import logger\n'), ((1651, 1668), 'loguru.logger.info', 'logger.info', (['hero'], {}), '(hero)\n', (1662, 1668), False, 'from loguru import logger\n'), ((1894, 1911), 'loguru.logger.info', 'logger.info', (['hero'], {}), '(hero)\n', (1905, 1911), False, 'from loguru import logger\n'), ((873, 885), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (879, 885), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((1036, 1048), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (1042, 1048), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((1273, 1285), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (1279, 1285), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((1494, 1506), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (1500, 1506), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n'), ((1731, 1743), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (1737, 1743), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, or_, select\n')]