|
- # pylint: disable = missing-class-docstring
- # pylint: disable = missing-function-docstring
-
- """
- みてるぞ式魔改造(言ふほどか?)版 Eloquent
- """
-
- import eloquent
-
-
- class DatabaseManager (eloquent.DatabaseManager):
- pass
-
-
- class Model (eloquent.Model):
- id: int
-
- def upsert (
- self,
- *args: str,
- ) -> None:
- row = self._find_upsert_row (*args)
- if row is not None:
- self.id = row.id
- # pylint: disable = invalid-name
- # pylint: disable = attribute-defined-outside-init
- self._Model__exists = True
- self.save ()
- return
-
- try:
- self.save ()
- except Exception:
- row = self._find_upsert_row (*args)
- if row is None:
- raise
- self.id = row.id
- # pylint: disable = invalid-name
- # pylint: disable = attribute-defined-outside-init
- self._Model__exists = True
- self.save ()
-
- def _find_upsert_row (
- self,
- *args: str,
- ):
- q = self.query ()
- for arg in args:
- q = q.where (arg, getattr (self, arg))
- return q.first ()
|