Stop using SQLAlchemy ORM everywhere

This commit is contained in:
Tulir Asokan
2019-02-14 00:06:45 +02:00
parent 8ef82abe9d
commit 9174186442
5 changed files with 29 additions and 25 deletions
+2 -3
View File
@@ -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
+16 -2
View File
@@ -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))