data.lisp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. (in-package :cl-user)
  2. (defpackage #:assboard.data
  3. (:nicknames :data)
  4. (:use :cl :postmodern))
  5. (in-package :assboard.data)
  6. ;; Install local-time timestamp reader
  7. (local-time:set-local-time-cl-postgres-readers)
  8. (defvar *db-host* "127.0.0.1" "database host name")
  9. (defvar *db-user* "assboard" "database user")
  10. (defvar *db-pass* "assboard" "database pass")
  11. (defvar *db-name* "assboard" "database name")
  12. (defun start-db-connection (&optional (database *db-name*)
  13. (database-user *db-user*)
  14. (database-password *db-pass*)
  15. (host *db-host*))
  16. "Start the database connection. Reconnects if there is an unconnected
  17. database in *database* which matches the database parameter in the function, it will
  18. be reconnected. Returns boolean on whether the global *database* is now connected."
  19. (unless *database*
  20. (setf *database*
  21. (postmodern:connect database database-user database-password
  22. host :pooled-p t))))
  23. (defclass posting ()
  24. ((id :col-type serial :reader posting-id)
  25. (author :col-type string :accessor posting-author :initarg :author)
  26. (status :col-type string :accessor posting-status :initarg :status :col-default "open")
  27. (title :col-type string :accessor posting-title :initarg :title)
  28. (body :col-type string :accessor posting-body :initarg :body)
  29. (maintainer :col-type (or string db-null) :accessor posting-maintainer)
  30. (created-on :col-type timestamptz :reader posting-created-on :col-default (:now))
  31. (updated-on :col-type timestamptz :accessor posting-updated-on :col-default (:now)))
  32. (:metaclass dao-class)
  33. (:keys id))
  34. (deftable posting
  35. (!dao-def)
  36. (!index 'status)
  37. (!index 'author)
  38. (!index 'maintainer))
  39. (defclass raw ()
  40. ((id :col-type serial :reader raw-id)
  41. (crearsigned :col-type string :initarg :clearsigned)
  42. (fingerprint :col-type string :reader raw-fingerprint :initarg :fingerprint)
  43. (timestamp :col-type timestamptz :initarg :timestamp)
  44. (created-on :col-type timestamptz :col-default (:now))
  45. (remote-ip :col-type (or string db-null) :initform (ignore-errors (hunchentoot:real-remote-addr)))
  46. (remote-ua :col-type (or string db-null) :initform (ignore-errors (hunchentoot:user-agent))))
  47. (:metaclass dao-class)
  48. (:keys id))
  49. (deftable raw
  50. (!dao-def))
  51. (defclass posting-edition ()
  52. ((id :col-type serial :reader posting-edition-id)
  53. (posting-id :col-type integer :initarg :posting-id)
  54. (title :col-type string :initarg :title)
  55. (body :col-type string :initarg :body)
  56. (raw-id :col-type integer :initarg :raw-id))
  57. (:metaclass dao-class)
  58. (:keys id))
  59. (deftable posting-edition
  60. (!dao-def)
  61. (!foreign 'posting :posting-id :primary-key)
  62. (!foreign 'raw :raw-id :primary-key))