You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
778 B
Python

6 months ago
from config.database import SessionLocal
from sqlalchemy import text
class BaseRepository:
def __init__(self):
self.session = SessionLocal()
def add(self, obj):
self.session.add(obj)
self.session.commit()
self.session.refresh(obj)
return obj
def delete(self, obj):
self.session.delete(obj)
self.session.commit()
def get_by_id(self, entity, id_):
return self.session.query(entity).get(id_)
def list_all(self, entity):
return self.session.query(entity).all()
def execute_sql(self, sql, params=None):
"""执行原生 SQL"""
result = self.session.execute(text(sql), params or {})
return result.fetchall()
def close(self):
self.session.close()