Yu-Gi-Oh! Deck Building and Card Inventory Management web interface written in Common Lisp, utilizing HTMX.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

100 rindas
4.0KB

  1. #|
  2. src/models/registered-table-mixin.lisp
  3. Model Object Class. Instead of writing new code every time for every
  4. new Model (Model, View, Component), encapsulate the behavior here.
  5. Now using mixins!
  6. |#
  7. (in-package #:cl-deck-builder2.models.registered-table)
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;; Thanks mariari! https://lispcookbook.github.io/cl-cookbook/clos.html#metaclasses
  10. (defclass model-registry ()
  11. ((registry :allocation :class
  12. :accessor model-registry
  13. :initform (make-hash-table))))
  14. (defmethod registered-classes ((registry model-registry) &optional package)
  15. (if package
  16. (gethash (find-package package) (model-registry registry))
  17. ;; If no package was provided return a LIST of all packages tracked by the MODEL-REGISTRY.
  18. (apply #'concatenate 'list
  19. (alexandria:hash-table-values (model-registry registry)))))
  20. (defparameter *registry* (make-instance 'model-registry)
  21. "Global model registry instance.")
  22. (defun registry (&optional (registry *registry*))
  23. "Get the MODEL-REGISTRY associated with REGISTRY. Default is *REGISTRY*."
  24. registry)
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. ;; Inherit from MITO:DAO-TABLE-CLASS because MITO:DAO-TABLE-MIXIN
  27. ;; doesn't have the additional PRIMARY-KEY or TIMESTAMP mechanisms or
  28. ;; anything.
  29. (defclass registered-table-class (mito:dao-table-class) ())
  30. (defmethod closer-mop:validate-superclass ((class registered-table-class)
  31. (superclass mito:dao-table-class))
  32. t)
  33. (defmethod initialize-instance :after ((class registered-table-class) &rest initargs &key &allow-other-keys)
  34. (declare (ignore initargs))
  35. (pushnew (class-name class)
  36. (gethash (symbol-package (class-name class))
  37. (model-registry (registry)))))
  38. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  39. ;; Tables
  40. (defmethod create-tables ((registry model-registry) &optional package)
  41. "Create tables for CLASS-LIST by wrapping DBD:CREATE-TABLE."
  42. (apply #'create-table
  43. (registered-classes registry package)))
  44. (defmethod drop-tables ((registry model-registry) &optional package)
  45. "Drop tables for CLASS-LIST by wrapping DB:DROP-TABLE."
  46. (apply #'drop-table
  47. (registered-classes registry package)))
  48. (defmethod recreate-tables ((registry model-registry) &optional package)
  49. (drop-tables registry package)
  50. (create-tables registry package))
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. (defmethod migration-expressions ((registry model-registry) &optional package)
  53. (with-connection (db)
  54. (with-transaction
  55. (mapcar #'mito:migration-expressions
  56. (registered-classes registry package)))))
  57. (defmethod migrate-table ((registry model-registry) &optional package)
  58. (with-connection (db)
  59. (with-transaction
  60. (mapcar #'mito:migrate-table
  61. (registered-classes registry package)))))
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. (defun ensure-tables-exist (&optional package)
  64. (create-tables (registry) package))
  65. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  66. (defmethod export-registered-classes ((registry model-registry) &key package (stream *standard-output*))
  67. "Export REGISTERED-CLASSES for REGISTRY to STREAM, which by default, is *STANDARD-OUTPUT*."
  68. (with-connection (db)
  69. (with-transaction
  70. (format stream "~{~a~^;~%~};"
  71. (mapcar (lambda (class) (sxql:yield (car (mito:table-definition class))))
  72. (registered-classes registry package))))))
  73. (defun export-registered-classes-to-file (&key (output (relative-pathname "db/schema.sql")) package)
  74. (with-open-file (stream output
  75. :direction :output
  76. :if-exists :supersede
  77. :if-does-not-exist :create)
  78. (export-registered-classes (registry) :package package :stream stream)
  79. (probe-file output)))