ぼざろクリーチャーシリーズ DB 兼 API(自分用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.2 KiB

  1. # pylint: disable = missing-class-docstring
  2. # pylint: disable = missing-function-docstring
  3. """
  4. みてるぞ式魔改造(言ふほどか?)版 Eloquent
  5. """
  6. import eloquent
  7. class DatabaseManager (eloquent.DatabaseManager):
  8. pass
  9. class Model (eloquent.Model):
  10. id: int
  11. def upsert (
  12. self,
  13. *args: str,
  14. ) -> None:
  15. row = self._find_upsert_row (*args)
  16. if row is not None:
  17. self.id = row.id
  18. # pylint: disable = invalid-name
  19. # pylint: disable = attribute-defined-outside-init
  20. self._Model__exists = True
  21. self.save ()
  22. return
  23. try:
  24. self.save ()
  25. except Exception:
  26. row = self._find_upsert_row (*args)
  27. if row is None:
  28. raise
  29. self.id = row.id
  30. # pylint: disable = invalid-name
  31. # pylint: disable = attribute-defined-outside-init
  32. self._Model__exists = True
  33. self.save ()
  34. def _find_upsert_row (
  35. self,
  36. *args: str,
  37. ):
  38. q = self.query ()
  39. for arg in args:
  40. q = q.where (arg, getattr (self, arg))
  41. return q.first ()