from typing import Optional

from sqlmodel import Field, Session, SQLModel, create_engine, select


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


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

engine = create_engine(sqlite_url, echo=True)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def create_heroes():
    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)
    hero_4 = Hero(name="Tarantula", secret_name="<NAME>", age=32)
    hero_5 = Hero(name="<NAME>", secret_name="<NAME>", age=35)
    hero_6 = Hero(name="<NAME>", secret_name="<NAME>", age=36)
    hero_7 = Hero(name="Captain North America", secret_name="<NAME>", age=93)

    with Session(engine) as session:
        session.add(hero_1)
        session.add(hero_2)
        session.add(hero_3)
        session.add(hero_4)
        session.add(hero_5)
        session.add(hero_6)
        session.add(hero_7)

        session.commit()


def select_heroes():
    with Session(engine) as session:
        statement = select(Hero).where(Hero.id == 1)
        results = session.exec(statement)
        hero = results.first()
        print("Hero:", hero)


def main():
    create_db_and_tables()
    create_heroes()
    select_heroes()


if __name__ == "__main__":
    main()

sqlmodel.SQLModel.metadata.create_all sqlmodel.Session sqlmodel.Field sqlmodel.select sqlmodel.create_engine
[((351, 387), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'echo': '(True)'}), '(sqlite_url, echo=True)\n', (364, 387), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n'), ((158, 195), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (163, 195), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n'), ((422, 458), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (450, 458), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n'), ((944, 959), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (951, 959), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n'), ((1226, 1241), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1233, 1241), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n'), ((1274, 1286), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (1280, 1286), False, 'from sqlmodel import Field, Session, SQLModel, create_engine, select\n')]