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.

102 lines
3.4 KiB

  1. ;; Handling databasey whatnots
  2. (in-package :nea/ircd)
  3. (defparameter *local-server-id* 1
  4. "Server ID of the local server.")
  5. (defparameter *default-database-args* '("nea" "nea" "" "localhost" :port 5432)
  6. "Default arguments to pass to POSTMODERN:CONNECT.")
  7. (defmacro execute-one (&rest args)
  8. "Like POSTMODERN:EXECUTE, but raises an error if the amount of rows affected wasn't equal to 1 (and rolls back the transaction)"
  9. `(pomo:with-logical-transaction ()
  10. (let ((rows-modified (pomo:execute ,@args)))
  11. (assert (eql rows-modified 1) () "Modified ~A rows in an EXECUTE-ONE command" rows-modified))))
  12. (cl-postgres-plus-uuid:set-uuid-sql-reader)
  13. (defclass db-system-message ()
  14. ((msgid :col-type uuid
  15. :initarg :msgid
  16. :reader dbm-msgid)
  17. (seq :col-type int
  18. :col-default (:nextval messages_sequence)
  19. :initarg :seq
  20. :reader dbm-seq)
  21. (type :col-type int
  22. :initarg :type
  23. :reader dbm-type)
  24. (user-ref :col-type int
  25. :col-name user_ref
  26. :initarg :user-ref
  27. :reader dbm-user-ref)
  28. (target-user-ref :col-type int
  29. :col-name target_user_ref
  30. :initarg :target-user-ref
  31. :reader dbm-target-user-ref)
  32. (groupchat-ref :col-type uuid
  33. :col-name groupchat_ref
  34. :initarg :groupchat-ref
  35. :reader dbm-groupchat-ref)
  36. (body :col-type varchar
  37. :initarg :body
  38. :reader dbm-body)
  39. (local-ts :col-type timestamp
  40. :col-name local_ts
  41. :initarg :local-ts
  42. :initform (simple-date:universal-time-to-timestamp (get-universal-time))
  43. :reader dbm-local-ts))
  44. (:metaclass pomo:dao-class)
  45. (:table-name system_messages))
  46. (defclass db-message ()
  47. ((msgid :col-type uuid
  48. :initarg :msgid
  49. :reader dbm-msgid)
  50. (seq :col-type int
  51. :col-default (:nextval messages_sequence)
  52. :initarg :seq
  53. :reader dbm-seq)
  54. (user-from :col-type int
  55. :col-name user_from
  56. :initarg :user-from
  57. :reader dbm-user-from)
  58. (user-to :col-type int
  59. :col-name user_to
  60. :initarg :user-to
  61. :reader dbm-user-to)
  62. (groupchat-to :col-type int
  63. :col-name groupchat_to
  64. :initarg :groupchat-to
  65. :reader dbm-groupchat-to)
  66. (tags :col-type varchar
  67. :initarg :tags
  68. :reader dbm-tags)
  69. (body :col-type varchar
  70. :initarg :body
  71. :reader dbm-body)
  72. (origin-ts :col-type timestamp
  73. :col-name origin_ts
  74. :initarg :origin-ts
  75. :initform (simple-date:universal-time-to-timestamp (get-universal-time))
  76. :reader dbm-origin-ts)
  77. (local-ts :col-type timestamp
  78. :col-name local_ts
  79. :initarg :local-ts
  80. :initform (simple-date:universal-time-to-timestamp (get-universal-time))
  81. :reader dbm-local-ts)
  82. (is-notice :col-type boolean
  83. :col-name is_notice
  84. :initarg :is-notice
  85. :initform nil
  86. :reader dbm-is-notice))
  87. (:metaclass pomo:dao-class)
  88. (:table-name messages))
  89. (defun initialize-database (&optional (args *default-database-args*))
  90. "Connect to the PostgreSQL database."
  91. (apply #'postmodern:connect-toplevel args))
  92. (defun notify-new-messages ()
  93. "Notify the message delivery thread of new messages."
  94. (pomo:execute "NOTIFY \"ircd-messages\";"))