From c91cf19926524458bf7a2b1a6cbd4080e890a18d Mon Sep 17 00:00:00 2001 From: miteruzo Date: Tue, 15 Oct 2024 00:18:35 +0900 Subject: [PATCH] =?UTF-8?q?Eloquent=20=E3=81=AE=E5=9E=8B=E5=AE=9A=E7=BE=A9?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=A8=E5=9E=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E6=80=A7=E7=A2=BA=E8=AA=8D=EF=BC=9B=E6=9C=AC=E7=95=AA?= =?UTF-8?q?=E7=92=B0=E5=A2=83=E3=81=AB=E5=8F=96=E8=BE=BC=E3=81=BF=E5=8F=AF?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eloquent.pyi | 113 ++++++++++++++++++++ eloquent/orm/relations/dynamic_property.pyi | 4 + update_db.py | 98 ++++++++--------- 3 files changed, 161 insertions(+), 54 deletions(-) create mode 100644 eloquent.pyi create mode 100644 eloquent/orm/relations/dynamic_property.pyi diff --git a/eloquent.pyi b/eloquent.pyi new file mode 100644 index 0000000..ea5b2f0 --- /dev/null +++ b/eloquent.pyi @@ -0,0 +1,113 @@ +from __future__ import annotations + +from typing import Any, Generic, Type, TypeVar, overload + +from eloquent.orm.relations.dynamic_property import DynamicProperty + +_TModel = TypeVar ('_TModel', bound = 'Model') + + +class Connection: + def select (self, query: str, bindings: dict[str, Any] | None = None) -> Any: ... + + def insert (self, query: str, bindings: dict[str, Any] | None = None) -> int: ... + + def update (self, query: str, bindings: dict[str, Any] | None = None) -> int: ... + + def delete (self, query: str, bindings: dict[str, Any] | None = None) -> int: ... + + def transaction (self, callback: Any) -> Any: ... + + def begin_transaction (self) -> None: ... + + def commit (self) -> None: ... + + def rollback (self) -> None: ... + + +class ConnectionResolver: + def connection (self, name: str | None = None) -> Any: ... + + def get_default_connection (self) -> str: ... + + def set_default_connection (self, name: str) -> None: ... + + +class DatabaseManager: + connections: dict[str, Connection] + + def __init__ (self, config: dict[str, Any]) -> None: ... + + def connection (self, name: str | None = None) -> Connection: ... + + def disconnect (self, name: str | None = None) -> None: ... + + def reconnect (self, name: str | None = None) -> Connection: ... + + def get_connections (self) -> dict[str, Connection]: ... + + +class Model: + id: int + + def has_one ( + self, + related_model: Type[Model], + foreign_key: str | None = None, + ) -> DynamicProperty: ... + + def has_many ( + self, + related_model: Type[Model], + foreign_key: str | None = None, + ) -> DynamicProperty: ... + + def belongs_to ( + self, + related_model: Type[Model], + foreign_key: str | None = None, + ) -> DynamicProperty: ... + + def belongs_to_many ( + self, + related_model: Type[Model], + foreign_key: str | None = None, + ) -> DynamicProperty: ... + + def save (self) -> None: ... + + def delete (self) -> None: ... + + @classmethod + def find (cls, id: int) -> Model | None: ... + + @overload + @classmethod + def where (cls, field: str, operator: str, value: Any) -> QueryBuilder: ... + + @overload + @classmethod + def where (cls, field: str, value: Any) -> QueryBuilder: ... + + @classmethod + def where_not_in (cls, column: str, values: list[Any] | tuple) -> QueryBuilder: ... + + @classmethod + def where_not_null (cls, field: str) -> QueryBuilder: ... + + @classmethod + def set_connection_resolver (cls, resolver: DatabaseManager) -> None: ... + + +class QueryBuilder (Generic[_TModel]): + def first (self) -> _TModel | None: ... + + def get (self) -> list[_TModel]: ... + + @overload + def where (self, field: str, operator: str, value: Any) -> QueryBuilder: ... + + @overload + def where (self, field: str, value: Any) -> QueryBuilder: ... + + def where_null (self, field: str) -> QueryBuilder: ... diff --git a/eloquent/orm/relations/dynamic_property.pyi b/eloquent/orm/relations/dynamic_property.pyi new file mode 100644 index 0000000..51d4136 --- /dev/null +++ b/eloquent/orm/relations/dynamic_property.pyi @@ -0,0 +1,4 @@ +from eloquent import Model + + +class DynamicProperty (Model): ... diff --git a/update_db.py b/update_db.py index fd4bdb1..1e55410 100644 --- a/update_db.py +++ b/update_db.py @@ -204,6 +204,14 @@ def search_nico_by_tags ( class Comment (Model): __timestamps__ = False + video_id: int + comment_no: int + user_id: int + content: str + posted_at: datetime + nico_count: int + vpos_ms: int + @property def video ( self, @@ -216,10 +224,22 @@ class Comment (Model): ) -> DynamicProperty: return self.belongs_to (User) + def upsert ( + self, + ) -> None: + row = (Comment.where ('video_id', self.video_id) + .where ('comment_no', self.comment_no) + .first ()) + if row is not None: + self.id = row.id + self.save () + class Tag (Model): __timestamps__ = False + name: str + @property def video_tags ( self, @@ -230,6 +250,8 @@ class Tag (Model): class User (Model): __timestamps__ = False + code: str + @property def comments ( self, @@ -240,6 +262,12 @@ class User (Model): class Video (Model): __timestamps__ = False + code: str + title: str + description: str + uploaded_at: datetime + deleted_at: datetime | None + @property def video_histories ( self, @@ -270,6 +298,10 @@ class Video (Model): class VideoHistory (Model): __timestamps__ = False + video_id: int + fetched_at: date + views_count: int + @property def video ( self, @@ -279,10 +311,9 @@ class VideoHistory (Model): def upsert ( self, ) -> None: - row = (Video - .where ('video_id', self.video_id) - .where ('fetched_at', self.fetched_at) - .first ()) + row = (VideoHistory.where ('video_id', self.video_id) + .where ('fetched_at', self.fetched_at) + .first ()) if row is not None: self.id = row.id self.save () @@ -291,6 +322,11 @@ class VideoHistory (Model): class VideoTag (Model): __timestamps__ = False + video_id: int + tag_id: int + tagged_at: date + untagged_at: date | None + @property def video ( self, @@ -306,10 +342,9 @@ class VideoTag (Model): def upsert ( self, ) -> None: - row = (Video - .where ('video_id', self.video_id) - .where ('tag_id', self.tag_id) - .first ()) + row = (VideoTag.where ('video_id', self.video_id) + .where ('tag_id', self.tag_id) + .first ()) if row is not None: self.id = row.id self.save () @@ -358,53 +393,8 @@ class CommentResult (TypedDict): isMyPost: bool -class CommentRow (TypedDict): - id: int - video_id: int - comment_no: int - user_id: int - content: str - posted_at: datetime - nico_count: int - vpos_ms: int | None - - -class TagRow (TypedDict): - id: int - name: str - - -class UserRow (TypedDict): - id: int - code: str - - -class VideoRow (TypedDict): - id: int - code: str - title: str - description: str - uploaded_at: datetime - deleted_at: datetime | None - - -class VideoHistoryRow (TypedDict): - id: int - video_id: int - fetched_at: date - views_count: int - - -class VideoTagRow (TypedDict): - id: int - video_id: int - tag_id: int - tagged_at: date - untagged_at: date | None - - def normalise ( - s: str + s: str, ) -> str: return unicodedata.normalize ('NFKC', s).lower ()