6de8d300 |
1 | (in-package :lisp-on-lines) |
2 | |
3 | |
4 | (defclass db-access-slot-definition () |
5 | ((column-name :initform nil :initarg :db-name :accessor slot-definition-column-name |
6 | :documentation |
7 | "If non-NIL, contains the name of the column this slot is representing.") |
8 | (primary-key :initform nil |
9 | :initarg :primary-key |
10 | :accessor slot-definition-primary-key-p) |
11 | (transient :initform nil :initarg :transient :accessor slot-definition-transient-p |
12 | :documentation |
13 | "If non-NIL, this slot should be treated as transient and |
14 | ignored in all database related operations.") |
15 | (not-null :initform nil :initarg :not-null :accessor slot-definition-not-null-p |
16 | :documentation "If non-NIL, a NON NULL database |
17 | constrained will be introduced.") |
18 | (foreign-type |
19 | :initform nil |
20 | :initarg :foreign-type |
21 | :initarg :references |
22 | :accessor slot-definition-foreign-type) |
23 | (unique :initform nil :initarg :unique :accessor slot-definition-unique) |
24 | |
25 | |
26 | (on-delete :initform :cascade :initarg :on-delete :accessor slot-definition-on-delete |
27 | :documentation "Action to be performed for this slot |
28 | when the refering row in the database ceases to exist. Possible |
29 | values: :CASCADE, :RESTRICT, :SET-NULL, :SET-DEFAULT. If this slot is |
30 | not a foreign key, it does nothing.") |
31 | (delayed-constraint :initform nil :accessor slot-definition-delayed-constraint |
32 | :documentation "Closures adding constraints |
33 | that, for some reason, could not be executed. If there's a slot with |
34 | this attribute not-NIL in a class definition, then there's something |
35 | wrong with its SQL counterpart."))) |
36 | |
37 | (defmethod slot-definition-column-name :around (slotd) |
38 | (or (call-next-method) (slot-definition-name slotd))) |
39 | |
40 | |
41 | (defclass db-access-class (standard-class) |
42 | ((table-name :initarg :table-name :initform nil :accessor class-table-name) |
43 | (indices :initarg :indices :initform () :reader class-indices) |
44 | (unique :initarg :unique :initform () :reader class-unique) |
45 | #+not!(connection-spec :initarg :connection-spec :initform nil :reader db-class-connection-spec) |
46 | |
47 | (unfinished-classes :initform nil :allocation :class :accessor class-unfinished-classes |
48 | :documentation "A class allocated slot |
49 | containing classes for whom not all the constraints could be |
50 | applied.") |
51 | (foreign-keys :initform nil :accessor class-foreign-keys |
52 | :documentation "List of foreign-key slots.") |
53 | (unique-keys :initform nil :accessor class-unique-keys |
54 | :documentation "List of slots whose value should be unique.")) |
55 | (:documentation "Metaclass for PostgreSQL aware classes. It takes |
56 | two additional arguments in DEFTABLE: :INDICES (which slots are used |
57 | as indices) and :CONNECTION-SPEC, which specifies how the class should |
58 | connect to the database (its format is the same as in |
59 | POSTMODERN:CONNECT-TOPLEVEL). If :CONNECTION-SPEC is not provided, |
60 | SUBMARINE assumes it is a class created just for the sake of |
61 | inheritance and does not create any tables for it.")) |
62 | |
63 | (defmethod validate-superclass |
64 | ((class db-access-class) |
65 | (superclass standard-class)) |
66 | t) |
67 | |
68 | |
69 | (defclass db-access-direct-slot-definition (standard-direct-slot-definition |
70 | db-access-slot-definition) |
71 | ()) |
72 | |
73 | (defmethod direct-slot-definition-class |
74 | ((class db-access-class) &key &allow-other-keys) |
75 | (find-class 'db-access-direct-slot-definition)) |
76 | |
77 | (defclass db-access-effective-slot-definition |
78 | (standard-effective-slot-definition |
79 | db-access-slot-definition) |
80 | ()) |
81 | |
82 | (defmethod effective-slot-definition-class |
83 | ((class db-access-class) &key &allow-other-keys) |
84 | (find-class 'db-access-effective-slot-definition)) |
85 | |
86 | (defmethod compute-effective-slot-definition |
87 | ((class db-access-class) name direct-slot-definitions) |
88 | (declare (ignore name)) |
89 | (let ((slotd (call-next-method))) |
90 | (setf (slot-definition-primary-key-p slotd) |
91 | (some #'slot-definition-primary-key-p direct-slot-definitions) |
92 | (slot-definition-transient-p slotd) |
93 | (every #'slot-definition-transient-p direct-slot-definitions) |
94 | (slot-definition-foreign-type slotd) |
95 | (slot-definition-foreign-type (car direct-slot-definitions)) |
96 | (slot-definition-not-null-p slotd) |
97 | (slot-definition-not-null-p (car direct-slot-definitions)) |
98 | (slot-definition-unique slotd) (slot-definition-unique (car direct-slot-definitions)) |
99 | (slot-definition-type slotd) (slot-definition-type (car direct-slot-definitions))) |
100 | slotd)) |
101 | |
102 | (defun class-id-slot-definition (class) |
103 | (find-if #'slot-definition-primary-key-p |
104 | (class-slots class))) |
105 | |
106 | (defmethod class-table-name :around (class) |
107 | (or (call-next-method) |
108 | (class-name class))) |
109 | |
110 | (defclass standard-db-access-class (db-access-class) |
111 | ()) |
112 | |
113 | (defun dao-id-column-name (class) |
114 | (slot-definition-column-name |
115 | (or (class-id-slot-definition class) |
116 | (error "No ID slot (primary key) for ~A" class)))) |
117 | |
118 | (defclass described-db-access-class (standard-db-access-class described-class) |
119 | ()) |
120 | |
121 | (defmethod initialize-instance :around ((class standard-db-access-class) &rest initargs &key (direct-superclasses '())) |
122 | (declare (dynamic-extent initargs)) |
123 | (if (loop for direct-superclass in direct-superclasses |
124 | thereis (ignore-errors (subtypep direct-superclass 'standard-db-access-object))) |
125 | (call-next-method) |
126 | (apply #'call-next-method |
127 | class |
128 | :direct-superclasses |
129 | (append direct-superclasses |
130 | (list (find-class 'standard-db-access-object))) |
131 | initargs))) |
132 | |
133 | (defmethod reinitialize-instance :around ((class standard-db-access-class) &rest initargs &key (direct-superclasses '() direct-superclasses-p)) |
134 | (declare (dynamic-extent initargs)) |
135 | (if (or (not direct-superclasses-p) |
136 | (loop for direct-superclass in direct-superclasses |
137 | thereis (ignore-errors (subtypep direct-superclass 'standard-db-access-object)))) |
138 | (call-next-method) |
139 | (apply #'call-next-method |
140 | class |
141 | :direct-superclasses |
142 | (append direct-superclasses |
143 | (list (find-class 'standard-db-access-object))) |
144 | initargs))) |
145 | |
146 | (defclass standard-db-access-object (standard-object) |
147 | ()) |
148 | |
149 | |
150 | |
151 | (defun find-dao (type id |
152 | &key (table (class-table-name (find-class type))) |
153 | id-column-name) |
154 | |
155 | "Get the dao corresponding to the given primary key, |
156 | or return nil if it does not exist." |
157 | (let ((row (first (query |
158 | (:select '* |
159 | :from table |
160 | :where (:= id (or id-column-name |
161 | (dao-id-column-name |
162 | (find-class type))))))))) |
163 | (make-dao-from-row type row))) |
164 | |
165 | (defmethod shared-initialize :after ((dao standard-db-access-object) |
166 | slots &rest initargs) |
167 | (let ((class (class-of dao))) |
168 | (dolist (slotd (class-slots class)) |
169 | (with-slots (foreign-type) slotd |
170 | (when foreign-type |
171 | (if (slot-boundp-using-class class dao slotd) |
172 | (let ((value (slot-value-using-class class dao slotd))) |
173 | (unless (typep value foreign-type) |
174 | (if (connected-p *database*) |
175 | (setf (slot-value-using-class class dao slotd) |
176 | (find-dao foreign-type value)) |
177 | (let ((obj (make-instance foreign-type))) |
178 | (setf (slot-value-using-class |
179 | (class-of obj) |
180 | obj |
181 | (class-id-slot-definition (class-of obj))) |
182 | value))))))))))) |
183 | |
184 | (defgeneric dao-id (dao) |
185 | (:method ((dao standard-db-access-object)) |
186 | (let ((class (class-of dao))) |
187 | |
188 | (slot-value-using-class class dao (class-id-slot-definition class))))) |
189 | |
190 | (defun make-dao-from-row (type row &key slots) |
191 | (let* ((class (find-class type)) |
192 | (dao (make-instance class)) |
193 | (slotds (class-slots class))) |
194 | (loop |
195 | :for val :in row |
196 | :for slotd |
197 | :in (or |
198 | (loop |
199 | :for slot :in slots |
200 | :collect (find slot slotds |
201 | :key #'slot-definition-name)) |
202 | slotds) |
203 | :do (setf (slot-value-using-class class dao slotd) val) |
204 | :finally (return (reinitialize-instance dao))))) |
205 | |
206 | ;(defgeneric make-dao (type &rest initargs) |
207 | #+nil(defun make-dao (type initargs) |
208 | "Create a DAO of the given `TYPE' and initialize it according |
209 | to the values of the alist `INITARGS'. `Initargs' may contain |
210 | additional values, not used in the initialization proccess." |
211 | (let ((instance (make-instance type))) |
212 | (iter (for slot in (slots-of instance)) |
213 | (setf (slot-value instance (slot-definition-name slot)) |
214 | (let ((the-value (cdr (assoc (intern (symbol-name (slot-definition-name slot)) 'keyword) initargs)))) |
215 | (if (foreign-type-p slot) |
216 | (make-instance (sb-pcl:slot-definition-type slot) :id the-value) |
217 | the-value)))) |
218 | instance)) |
219 | |
220 | |
221 | |
222 | |
223 | |