-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_access.py
59 lines (48 loc) · 1.69 KB
/
data_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import create_database, database_exists
from model.entity.base import Base
from sqlalchemy import create_engine,Column,and_, or_
username = "root"
password = "r33115910"
database_name = "online_shop"
connection_string = f"mysql+pymysql://{username}:{password}@localhost:3306/{database_name}"
if not database_exists(connection_string):
create_database(connection_string)
engine = create_engine(connection_string, echo=True)
Session = sessionmaker(bind=engine)
session = Session()
class DataAccess:
def __init__(self, class_name):
self.class_name = class_name
Base.metadata.create_all(bind=engine)
def save(self, entity):
session.add(entity)
session.commit()
session.refresh(entity)
return entity
def edit(self, entity):
session.merge(entity)
session.commit()
return entity
def remove(self, entity):
session.delete(entity)
session.commit()
return entity
def find_all(self):
entity_list = session.query(self.class_name).all()
if entity_list:
return entity_list
else:
raise ValueError("No records found!")
def find_by_id(self, id):
entity = session.get(self.class_name, id)
if entity:
return entity
else:
raise ValueError("No match with this id!")
def find_by(self, find_statement):
entity = session.query(self.class_name).filter(find_statement).all()
if entity:
return entity
else:
raise ValueError("No match with your search!")