Stop using SQLAlchemy ORM everywhere
This commit is contained in:
@@ -25,10 +25,9 @@ from .user import User, UserPortal, Contact
|
||||
from .user_profile import UserProfile
|
||||
|
||||
|
||||
def init(db_session, db_engine) -> None:
|
||||
BotChat.query = db_session.query_property()
|
||||
def init(db_engine) -> None:
|
||||
for table in (Portal, Message, User, Contact, UserPortal, Puppet, TelegramFile, UserProfile,
|
||||
RoomState):
|
||||
RoomState, BotChat):
|
||||
table.db = db_engine
|
||||
table.t = table.__table__
|
||||
table.c = table.t.c
|
||||
|
||||
@@ -14,8 +14,9 @@
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from typing import Iterable
|
||||
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from sqlalchemy.orm import Query
|
||||
|
||||
from ..types import TelegramID
|
||||
from .base import Base
|
||||
@@ -23,7 +24,20 @@ from .base import Base
|
||||
|
||||
# Fucking Telegram not telling bots what chats they are in 3:<
|
||||
class BotChat(Base):
|
||||
query = None # type: Query
|
||||
__tablename__ = "bot_chat"
|
||||
id = Column(Integer, primary_key=True) # type: TelegramID
|
||||
type = Column(String, nullable=False)
|
||||
|
||||
@classmethod
|
||||
def delete(cls, id: TelegramID) -> None:
|
||||
cls.db.execute(cls.t.delete().where(cls.c.id == id))
|
||||
|
||||
@classmethod
|
||||
def all(cls) -> Iterable['BotChat']:
|
||||
rows = cls.db.execute(cls.t.select())
|
||||
for row in rows:
|
||||
id, type = row
|
||||
yield cls(id=id, type=type)
|
||||
|
||||
def insert(self) -> None:
|
||||
self.db.execute(self.t.insert().values(id=self.id, type=self.type))
|
||||
|
||||
Reference in New Issue
Block a user