# Lettura dati con SQLModel, all, first, one; update; delete
# https://sqlmodel.tiangolo.com/tutorial/select/ e seguito
from typing import Optional
from sqlmodel import Field, SQLModel, Session, create_engine, select


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


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


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_entities():
    tag_offerta = Tag(name="Offerta")
    tag_maionese = Tag(name="Con Maionese")
    tipo_panino = ProductType(name="panino")
    tipo_bibita = ProductType(name="bibita")
    with Session(engine) as session:
        session.add(tag_offerta)
        session.add(tag_maionese)
        session.add(tipo_panino)
        session.add(tipo_bibita)
        session.commit()
        print("After committing the session")
        print("Tag 1:", tag_offerta)
        # No refresh, no print
        print("Product Type 1:", tipo_panino)
        # Refresh automatica
        print("Product Type 1:", tipo_panino.name)
        # Refresh esplicita
        session.refresh(tipo_bibita)
        session.refresh(tag_maionese)
        print("Product Type 2:", tipo_bibita)
    print("After the session closes")
    print("Tag 2:", tag_maionese)


def select_product_types():
    with Session(engine) as session:
        statement = select(ProductType)
        results = session.exec(statement)
        for product_type in results:
            print("product_type:", product_type)


def select_product_type_panino():
    with Session(engine) as session:
        statement = select(ProductType).where(ProductType.name == 'panino')
        results = session.exec(statement)
        for product_type in results:
            print("panino:", product_type)


def select_first_row_tag():
    with Session(engine) as session:
        statement = select(Tag).where(Tag.name == 'Offerta')
        results = session.exec(statement)
        tag = results.first()
        print("first:", tag)


def select_all_tags():
    with Session(engine) as session:
        statement = select(Tag)
        results = session.exec(statement)
        tags = results.all()
        print(tags)


def select_four_tags():
    with Session(engine) as session:
        statement = select(Tag).limit(4)
        results = session.exec(statement)
        tags = results.all()
        print(tags)


def select_next_four_tags():
    with Session(engine) as session:
        statement = select(Tag).offset(4).limit(4)
        results = session.exec(statement)
        tags = results.all()
        print(tags)


def update_tag():
    with Session(engine) as session:
        statement = select(Tag).where(Tag.name == "Con Maionese")
        results = session.exec(statement)
        # mayo = results.one()
        mayo = results.first()
        print("Tag:", mayo)
        mayo.name = "<NAME>"
        session.add(mayo)
        session.commit()
        session.refresh(mayo)
        print(mayo)


def delete_tag():
    with Session(engine) as session:
        statement = select(Tag).where(Tag.name == "<NAME>")
        results = session.exec(statement)
        no_mayo = results.first()
        print("no_mayo: ", no_mayo)
        session.delete(no_mayo)
        session.commit()
        print("Deleted:", no_mayo)
        statement = select(Tag).where(Tag.name == "<NAME>")
        results = session.exec(statement)
        no_mayo = results.first()
        if no_mayo is None:
            print("There's no no_mayo")


def main():
    create_db_and_tables()
    create_entities()
    # select_product_types()
    # select_product_type_panino()
    # select_first_row_tag()
    # select_all_tags()
    # select_four_tags()
    # select_next_four_tags()
    # update_tag()
    delete_tag()


if __name__ == "__main__":
    main()

sqlmodel.create_engine sqlmodel.Session sqlmodel.SQLModel.metadata.create_all sqlmodel.select sqlmodel.Field
[((536, 572), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'echo': '(True)'}), '(sqlite_url, echo=True)\n', (549, 572), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((276, 313), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (281, 313), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((395, 432), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (400, 432), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((607, 643), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (635, 643), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((850, 865), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (857, 865), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((1536, 1551), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1543, 1551), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((1584, 1603), 'sqlmodel.select', 'select', (['ProductType'], {}), '(ProductType)\n', (1590, 1603), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((1777, 1792), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1784, 1792), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2042, 2057), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (2049, 2057), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2266, 2281), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (2273, 2281), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2314, 2325), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (2320, 2325), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2452, 2467), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (2459, 2467), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2652, 2667), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (2659, 2667), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2851, 2866), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (2858, 2866), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((3236, 3251), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (3243, 3251), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((1825, 1844), 'sqlmodel.select', 'select', (['ProductType'], {}), '(ProductType)\n', (1831, 1844), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2090, 2101), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (2096, 2101), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2500, 2511), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (2506, 2511), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2899, 2910), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (2905, 2910), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((3284, 3295), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (3290, 3295), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((3548, 3559), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (3554, 3559), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n'), ((2700, 2711), 'sqlmodel.select', 'select', (['Tag'], {}), '(Tag)\n', (2706, 2711), False, 'from sqlmodel import Field, SQLModel, Session, create_engine, select\n')]