feat: scraper usage example, fixed session handling
This commit is contained in:
@@ -4,6 +4,7 @@ from functools import wraps
|
||||
from sqlalchemy import ForeignKey, select, insert, update
|
||||
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, Session, relationship
|
||||
from typing import List
|
||||
import time
|
||||
import toml
|
||||
|
||||
global db
|
||||
@@ -39,18 +40,18 @@ class baza():
|
||||
nazwisko: Mapped[ str] = mapped_column()
|
||||
data_urodzenia: Mapped[ str] = mapped_column()
|
||||
czy_aktywny: Mapped[ bool] = mapped_column()
|
||||
klub_id: Mapped[ List[str]] = mapped_column(ForeignKey(f"{tnp}kluby.id_klubu"))
|
||||
klub_id: Mapped[ List[str]] = mapped_column(ForeignKey(f"{tnp}kluby.id_klubu"), nullable=True)
|
||||
klub: Mapped[ List["kluby"]] = relationship(back_populates="sportowcy_w_klubie", foreign_keys=[klub_id])
|
||||
narodowosc: Mapped[ str] = mapped_column()
|
||||
ilosc_trofeow: Mapped[ int] = mapped_column()
|
||||
ostatnie_trofeum_id: Mapped[ int] = mapped_column(ForeignKey(f"{tnp}trofea.id_trofeum"), nullable=True)
|
||||
ostatnie_trofeum: Mapped[ "trofea"] = relationship(back_populates="zawodnik", foreign_keys=[ostatnie_trofeum_id])
|
||||
pierwszy_mecz_id: Mapped[ int] = mapped_column(ForeignKey(f"{tnp}mecze.id_meczu"))
|
||||
pierwszy_mecz_id: Mapped[ int] = mapped_column(ForeignKey(f"{tnp}mecze.id_meczu"), nullable=True)
|
||||
pierwszy_mecz: Mapped[ "mecze"] = relationship()
|
||||
wycena: Mapped[ int] = mapped_column()
|
||||
ostatni_gol_dla_id: Mapped[ str] = mapped_column(ForeignKey(f"{tnp}kluby.id_klubu"))
|
||||
ostatni_gol_dla_id: Mapped[ str] = mapped_column(ForeignKey(f"{tnp}kluby.id_klubu"), nullable=True)
|
||||
ostatni_gol_dla: Mapped[ "kluby"] = relationship(back_populates="sportowcy_ostatni_gol", foreign_keys=[ostatni_gol_dla_id])
|
||||
statystyki_id: Mapped[ List[int]] = mapped_column(ForeignKey(f"{tnp}statystyki_sportowcow.id_statystyki"))
|
||||
statystyki_id: Mapped[ List[int]] = mapped_column(ForeignKey(f"{tnp}statystyki_sportowcow.id_statystyki"), nullable=True)
|
||||
statystyki: Mapped[List["statystyki_sportowcow"]] = relationship(back_populates="sportowiec")
|
||||
trofea: Mapped[ List["trofea"]] = relationship(back_populates="zawodnik", foreign_keys="[trofea.id_zawodnika]")
|
||||
|
||||
@@ -168,7 +169,7 @@ class baza():
|
||||
except:
|
||||
self.session.rollback()
|
||||
self.session.close()
|
||||
self.refresh_session()
|
||||
self.refresh_session()
|
||||
return return_val
|
||||
return wrapper
|
||||
|
||||
@@ -186,6 +187,8 @@ class baza():
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
print(f"[{round(time.time())}] SELECT")
|
||||
|
||||
results = (
|
||||
self.session.
|
||||
query(self.entities[entity_type]).
|
||||
@@ -193,10 +196,12 @@ class baza():
|
||||
all()
|
||||
)
|
||||
|
||||
print(f"[{round(time.time())}] SELECT RESULTS: {results}")
|
||||
|
||||
return results
|
||||
|
||||
@exit_gracefully
|
||||
def simple_insert_one(self, type, **kwargs):
|
||||
def simple_insert_one(self, entity_type, **kwargs):
|
||||
"""
|
||||
Użycie:
|
||||
simple_insert_one(ldb.kluby, id_klubu="polska", pelna_nazwa="Reprezentacja Polski", skrocona_nazwa="PL")
|
||||
@@ -204,12 +209,18 @@ class baza():
|
||||
https://docs.sqlalchemy.org/en/20/tutorial/data_insert.html
|
||||
https://docs.sqlalchemy.org/en/20/orm/session_basics.html
|
||||
"""
|
||||
obj = type(**kwargs)
|
||||
with Session(self.db.engine) as session:
|
||||
session.add(obj)
|
||||
session.commit()
|
||||
return 0
|
||||
return 1
|
||||
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
print(f"[{round(time.time())}] INSERT")
|
||||
|
||||
obj = self.entities[entity_type](**kwargs)
|
||||
#with Session(self.db.engine) as session:
|
||||
self.session.add(obj)
|
||||
self.session.commit()
|
||||
return 0
|
||||
#return 1
|
||||
|
||||
@exit_gracefully
|
||||
def simple_insert_many(self, objs_list):
|
||||
@@ -220,11 +231,11 @@ class baza():
|
||||
https://docs.sqlalchemy.org/en/20/tutorial/data_insert.html
|
||||
https://docs.sqlalchemy.org/en/20/orm/session_basics.html
|
||||
"""
|
||||
with Session(self.db.engine) as session:
|
||||
session.add_all(objs_list)
|
||||
session.commit()
|
||||
return 0
|
||||
return 1
|
||||
#with Session(self.db.engine) as session:
|
||||
self.session.add_all(objs_list)
|
||||
self.session.commit()
|
||||
return 0
|
||||
#return 1
|
||||
|
||||
@exit_gracefully
|
||||
def sample_data_init(self, override_safety_check=False):
|
||||
|
||||
Reference in New Issue
Block a user