1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; The CLSQL Object Oriented Data Definitional Language (OODDL)
7 ;;;; and Object Oriented Data Manipulation Language (OODML).
9 ;;;; This file is part of CLSQL.
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; *************************************************************************
16 (in-package #:clsql-sys)
18 (defclass standard-db-object ()
21 :initarg :view-database
23 (:metaclass standard-db-class)
24 (:documentation "Superclass for all CLSQL View Classes."))
26 (defmethod view-database ((self standard-db-object))
27 (slot-value self 'view-database))
29 (defvar *db-deserializing* nil)
30 (defvar *db-initializing* nil)
32 (defmethod slot-value-using-class ((class standard-db-class) instance slot)
33 (declare (optimize (speed 3)))
34 (unless *db-deserializing*
35 (let ((slot-name (%slot-name slot))
36 (slot-object (%slot-object slot class)))
37 (when (and (eql (view-class-slot-db-kind slot-object) :join)
38 (not (slot-boundp instance slot-name)))
39 (let ((*db-deserializing* t))
40 (if (view-database instance)
41 (setf (slot-value instance slot-name)
42 (fault-join-slot class instance slot-object))
43 (setf (slot-value instance slot-name) nil))))))
46 (defmethod (setf slot-value-using-class) (new-value (class standard-db-class)
48 (declare (ignore new-value instance slot))
51 ;; JMM - Can't go around trying to slot-access a symbol! Guess in
52 ;; CMUCL slot-name is the actual slot _object_, while in lispworks it
53 ;; is a lowly symbol (the variable is called slot-name after all) so
54 ;; the object (or in MOP terminology- the "slot definition") has to be
55 ;; retrieved using find-slot-definition
57 (defun %slot-name (slot)
59 #-lispworks (slot-definition-name slot))
61 (defun %slot-object (slot class)
62 (declare (ignorable class))
63 #+lispworks (clos:find-slot-definition slot class)
66 (defmethod initialize-instance :around ((class standard-db-object)
68 &key &allow-other-keys)
69 (declare (ignore all-keys))
70 (let ((*db-deserializing* t))
73 (defun sequence-from-class (view-class-name)
77 (symbol-name (view-table (find-class view-class-name)))
80 (defun create-sequence-from-class (view-class-name
81 &key (database *default-database*))
82 (create-sequence (sequence-from-class view-class-name) :database database))
84 (defun drop-sequence-from-class (view-class-name
85 &key (if-does-not-exist :error)
86 (database *default-database*))
87 (drop-sequence (sequence-from-class view-class-name)
88 :if-does-not-exist if-does-not-exist
92 ;; Build the database tables required to store the given view class
95 (defmethod database-pkey-constraint ((class standard-db-class) database)
96 (let ((keylist (mapcar #'view-class-slot-column (keyslots-for-class class))))
98 (format nil "CONSTRAINT ~APK PRIMARY KEY~A"
99 (database-output-sql (view-table class) database)
100 (database-output-sql keylist database)))))
103 #.(locally-enable-sql-reader-syntax)
105 (defun ensure-schema-version-table (database)
106 (unless (table-exists-p "clsql_object_v" :database database)
107 (create-table [clsql_object_v] '(([name] (string 32))
110 :database database)))
112 (defun update-schema-version-records (view-class-name
113 &key (database *default-database*))
114 (let ((schemadef nil)
115 (tclass (find-class view-class-name)))
116 (dolist (slotdef (class-slots tclass))
117 (let ((res (database-generate-column-definition view-class-name
119 (when res (setf schemadef (cons res schemadef)))))
121 (delete-records :from [clsql_object_v]
122 :where [= [name] (sql-escape (class-name tclass))]
124 (insert-records :into [clsql_object_v]
125 :av-pairs `(([name] ,(sql-escape (class-name tclass)))
126 ([vers] ,(car (object-version tclass)))
127 ([def] ,(prin1-to-string
128 (object-definition tclass))))
129 :database database))))
131 #.(restore-sql-reader-syntax-state)
133 (defun create-view-from-class (view-class-name
134 &key (database *default-database*))
135 "Creates a view in DATABASE based on VIEW-CLASS-NAME which defines
136 the view. The argument DATABASE has a default value of
138 (let ((tclass (find-class view-class-name)))
140 (let ((*default-database* database))
141 (%install-class tclass database)
142 (ensure-schema-version-table database)
143 (update-schema-version-records view-class-name :database database))
144 (error "Class ~s not found." view-class-name)))
147 (defmethod %install-class ((self standard-db-class) database &aux schemadef)
148 (dolist (slotdef (class-slots self))
149 (let ((res (database-generate-column-definition (class-name self)
152 (push res schemadef))))
154 (error "Class ~s has no :base slots" self))
155 (create-table (sql-expression :table (view-table self)) schemadef
157 :constraints (database-pkey-constraint self database))
158 (push self (database-view-classes database))
162 ;; Drop the tables which store the given view class
165 #.(locally-enable-sql-reader-syntax)
167 (defun drop-view-from-class (view-class-name &key (database *default-database*))
168 "Deletes a view or base table from DATABASE based on VIEW-CLASS-NAME
169 which defines that view. The argument DATABASE has a default value of
171 (let ((tclass (find-class view-class-name)))
173 (let ((*default-database* database))
174 (%uninstall-class tclass)
175 (delete-records :from [clsql_object_v]
176 :where [= [name] (sql-escape view-class-name)]))
177 (error "Class ~s not found." view-class-name)))
180 #.(restore-sql-reader-syntax-state)
182 (defun %uninstall-class (self &key (database *default-database*))
183 (drop-table (sql-expression :table (view-table self))
184 :if-does-not-exist :ignore
186 (setf (database-view-classes database)
187 (remove self (database-view-classes database))))
191 ;; List all known view classes
194 (defun list-classes (&key (test #'identity)
195 (root-class 'standard-db-object)
196 (database *default-database*))
197 "Returns a list of View Classes connected to a given DATABASE which
198 defaults to *DEFAULT-DATABASE*."
199 (declare (ignore root-class))
200 (remove-if #'(lambda (c) (not (funcall test c)))
201 (database-view-classes database)))
204 ;; Define a new view class
207 (defmacro def-view-class (class supers slots &rest options)
208 "Extends the syntax of defclass to allow special slots to be mapped
209 onto the attributes of database views. The macro DEF-VIEW-CLASS
210 creates a class called CLASS which maps onto a database view. Such a
211 class is called a View Class. The macro DEF-VIEW-CLASS extends the
212 syntax of DEFCLASS to allow special base slots to be mapped onto the
213 attributes of database views (presently single tables). When a select
214 query that names a View Class is submitted, then the corresponding
215 database view is queried, and the slots in the resulting View Class
216 instances are filled with attribute values from the database. If
217 SUPERS is nil then STANDARD-DB-OBJECT automatically becomes the
218 superclass of the newly-defined View Class."
220 (defclass ,class ,supers ,slots ,@options
221 (:metaclass standard-db-class))
222 (finalize-inheritance (find-class ',class))))
224 (defun keyslots-for-class (class)
225 (slot-value class 'key-slots))
227 (defun key-qualifier-for-instance (obj &key (database *default-database*))
228 (let ((tb (view-table (class-of obj))))
231 (sql-expression :attribute
232 (view-class-slot-column k)
236 (slot-value obj (slot-definition-name k))
238 (let* ((keys (keyslots-for-class (class-of obj)))
239 (keyxprs (mapcar #'qfk (reverse keys))))
241 ((= (length keyxprs) 0) nil)
242 ((= (length keyxprs) 1) (car keyxprs))
243 ((> (length keyxprs) 1) (apply #'sql-operation 'and keyxprs)))))))
246 ;; Function used by 'generate-selection-list'
249 (defun generate-attribute-reference (vclass slotdef)
251 ((eq (view-class-slot-db-kind slotdef) :base)
252 (sql-expression :attribute (view-class-slot-column slotdef)
253 :table (view-table vclass)))
254 ((eq (view-class-slot-db-kind slotdef) :key)
255 (sql-expression :attribute (view-class-slot-column slotdef)
256 :table (view-table vclass)))
260 ;; Function used by 'find-all'
263 (defun generate-selection-list (vclass)
265 (dolist (slotdef (class-slots vclass))
266 (let ((res (generate-attribute-reference vclass slotdef)))
268 (push (cons slotdef res) sels))))
271 (error "No slots of type :base in view-class ~A" (class-name vclass)))))
274 ;; Used by 'create-view-from-class'
278 (defmethod database-generate-column-definition (class slotdef database)
279 (declare (ignore database class))
280 (when (member (view-class-slot-db-kind slotdef) '(:base :key))
282 (list (sql-expression :attribute (view-class-slot-column slotdef))
283 (slot-type slotdef))))
284 (let ((const (view-class-slot-db-constraints slotdef)))
286 (setq cdef (append cdef (list const)))))
290 ;; Called by 'get-slot-values-from-view'
293 (declaim (inline delistify))
294 (defun delistify (list)
299 (defun slot-type (slotdef)
300 (let ((slot-type (slot-definition-type slotdef)))
301 (if (listp slot-type)
302 (cons (find-symbol (symbol-name (car slot-type)) :clsql-sys)
304 (find-symbol (symbol-name slot-type) :clsql-sys))))
306 (defmethod update-slot-from-db ((instance standard-db-object) slotdef value)
307 (declare (optimize (speed 3) #+cmu (extensions:inhibit-warnings 3)))
308 (let ((slot-reader (view-class-slot-db-reader slotdef))
309 (slot-name (slot-definition-name slotdef))
310 (slot-type (slot-type slotdef)))
311 (cond ((and value (null slot-reader))
312 (setf (slot-value instance slot-name)
313 (read-sql-value value (delistify slot-type)
314 (view-database instance))))
316 (update-slot-with-null instance slot-name slotdef))
317 ((typep slot-reader 'string)
318 (setf (slot-value instance slot-name)
319 (format nil slot-reader value)))
320 ((typep slot-reader 'function)
321 (setf (slot-value instance slot-name)
322 (apply slot-reader (list value))))
324 (error "Slot reader is of an unusual type.")))))
326 (defmethod key-value-from-db (slotdef value database)
327 (declare (optimize (speed 3) #+cmu (extensions:inhibit-warnings 3)))
328 (let ((slot-reader (view-class-slot-db-reader slotdef))
329 (slot-type (slot-type slotdef)))
330 (cond ((and value (null slot-reader))
331 (read-sql-value value (delistify slot-type) database))
334 ((typep slot-reader 'string)
335 (format nil slot-reader value))
336 ((typep slot-reader 'function)
337 (apply slot-reader (list value)))
339 (error "Slot reader is of an unusual type.")))))
341 (defun db-value-from-slot (slotdef val database)
342 (let ((dbwriter (view-class-slot-db-writer slotdef))
343 (dbtype (slot-type slotdef)))
345 (string (format nil dbwriter val))
346 (function (apply dbwriter (list val)))
350 (database-output-sql-as-type (car dbtype) val database))
352 (database-output-sql-as-type dbtype val database)))))))
354 (defun check-slot-type (slotdef val)
355 (let* ((slot-type (slot-type slotdef))
356 (basetype (if (listp slot-type) (car slot-type) slot-type)))
357 (when (and slot-type val)
358 (unless (typep val basetype)
359 (error 'clsql-type-error
360 :slotname (slot-definition-name slotdef)
365 ;; Called by find-all
368 (defmethod get-slot-values-from-view (obj slotdeflist values)
369 (flet ((update-slot (slot-def values)
370 (update-slot-from-db obj slot-def values)))
371 (mapc #'update-slot slotdeflist values)
375 (defun synchronize-keys (src srckey dest destkey)
376 (let ((skeys (if (listp srckey) srckey (list srckey)))
377 (dkeys (if (listp destkey) destkey (list destkey))))
378 (mapcar #'(lambda (sk dk)
379 (setf (slot-value dest dk)
386 (defun desynchronize-keys (dest destkey)
387 (let ((dkeys (if (listp destkey) destkey (list destkey))))
388 (mapcar #'(lambda (dk)
389 (setf (slot-value dest dk) nil))
392 (defmethod add-to-relation ((target standard-db-object)
394 (value standard-db-object))
395 (let* ((objclass (class-of target))
396 (sdef (or (slotdef-for-slot-with-class slot-name objclass)
397 (error "~s is not an known slot on ~s" slot-name target)))
398 (dbinfo (view-class-slot-db-info sdef))
399 (join-class (gethash :join-class dbinfo))
400 (homekey (gethash :home-key dbinfo))
401 (foreignkey (gethash :foreign-key dbinfo))
402 (to-many (gethash :set dbinfo)))
403 (unless (equal (type-of value) join-class)
404 (error 'clsql-type-error :slotname slot-name :typespec join-class
406 (when (gethash :target-slot dbinfo)
407 (error "add-to-relation does not work with many-to-many relations yet."))
410 (synchronize-keys target homekey value foreignkey)
411 (if (slot-boundp target slot-name)
412 (unless (member value (slot-value target slot-name))
413 (setf (slot-value target slot-name)
414 (append (slot-value target slot-name) (list value))))
415 (setf (slot-value target slot-name) (list value))))
417 (synchronize-keys value foreignkey target homekey)
418 (setf (slot-value target slot-name) value)))))
420 (defmethod remove-from-relation ((target standard-db-object)
421 slot-name (value standard-db-object))
422 (let* ((objclass (class-of target))
423 (sdef (slotdef-for-slot-with-class slot-name objclass))
424 (dbinfo (view-class-slot-db-info sdef))
425 (homekey (gethash :home-key dbinfo))
426 (foreignkey (gethash :foreign-key dbinfo))
427 (to-many (gethash :set dbinfo)))
428 (when (gethash :target-slot dbinfo)
429 (error "remove-relation does not work with many-to-many relations yet."))
432 (desynchronize-keys value foreignkey)
433 (if (slot-boundp target slot-name)
434 (setf (slot-value target slot-name)
436 (slot-value target slot-name)
439 (desynchronize-keys target homekey)
440 (setf (slot-value target slot-name)
443 (defgeneric update-record-from-slot (object slot &key database)
445 "The generic function UPDATE-RECORD-FROM-SLOT updates an individual
446 data item in the column represented by SLOT. The DATABASE is only used
447 if OBJECT is not yet associated with any database, in which case a
448 record is created in DATABASE. Only SLOT is initialized in this case;
449 other columns in the underlying database receive default values. The
450 argument SLOT is the CLOS slot name; the corresponding column names
451 are derived from the View Class definition."))
453 (defmethod update-record-from-slot ((obj standard-db-object) slot &key
454 (database *default-database*))
455 (let* ((vct (view-table (class-of obj)))
456 (sd (slotdef-for-slot-with-class slot (class-of obj))))
457 (check-slot-type sd (slot-value obj slot))
458 (let* ((att (view-class-slot-column sd))
459 (val (db-value-from-slot sd (slot-value obj slot) database)))
460 (cond ((and vct sd (view-database obj))
461 (update-records (sql-expression :table vct)
462 :attributes (list (sql-expression
465 :where (key-qualifier-for-instance
466 obj :database database)
467 :database (view-database obj)))
468 ((and vct sd (not (view-database obj)))
469 (install-instance obj :database database))
471 (error "Unable to update record.")))))
474 (defgeneric update-record-from-slots (object slots &key database)
476 "The generic function UPDATE-RECORD-FROM-SLOTS updates data in the
477 columns represented by SLOTS. The DATABASE is only used if OBJECT is
478 not yet associated with any database, in which case a record is
479 created in DATABASE. Only slots are initialized in this case; other
480 columns in the underlying database receive default values. The
481 argument SLOTS contains the CLOS slot names; the corresponding column
482 names are derived from the view class definition."))
484 (defmethod update-record-from-slots ((obj standard-db-object) slots &key
485 (database *default-database*))
486 (let* ((vct (view-table (class-of obj)))
487 (sds (slotdefs-for-slots-with-class slots (class-of obj)))
488 (avps (mapcar #'(lambda (s)
489 (let ((val (slot-value
490 obj (slot-definition-name s))))
491 (check-slot-type s val)
492 (list (sql-expression
493 :attribute (view-class-slot-column s))
494 (db-value-from-slot s val database))))
496 (cond ((and avps (view-database obj))
497 (update-records (sql-expression :table vct)
499 :where (key-qualifier-for-instance
500 obj :database database)
501 :database (view-database obj)))
502 ((and avps (not (view-database obj)))
503 (insert-records :into (sql-expression :table vct)
506 (setf (slot-value obj 'view-database) database))
508 (error "Unable to update records"))))
511 (defgeneric update-records-from-instance (object &key database)
513 "Using an instance of a view class, update the database table that
514 stores its instance data. If the instance is already associated with a
515 database, that database is used, and database is ignored. If instance
516 is not yet associated with a database, a record is created for
517 instance in the appropriate table of database and the instance becomes
518 associated with that database."))
520 (defmethod update-records-from-instance ((obj standard-db-object)
521 &key (database *default-database*))
522 (labels ((slot-storedp (slot)
523 (and (member (view-class-slot-db-kind slot) '(:base :key))
524 (slot-boundp obj (slot-definition-name slot))))
525 (slot-value-list (slot)
526 (let ((value (slot-value obj (slot-definition-name slot))))
527 (check-slot-type slot value)
528 (list (sql-expression :attribute (view-class-slot-column slot))
529 (db-value-from-slot slot value database)))))
530 (let* ((view-class (class-of obj))
531 (view-class-table (view-table view-class))
532 (slots (remove-if-not #'slot-storedp (class-slots view-class)))
533 (record-values (mapcar #'slot-value-list slots)))
534 (unless record-values
535 (error "No settable slots."))
536 (if (view-database obj)
537 (update-records (sql-expression :table view-class-table)
538 :av-pairs record-values
539 :where (key-qualifier-for-instance
540 obj :database database)
541 :database (view-database obj))
543 (insert-records :into (sql-expression :table view-class-table)
544 :av-pairs record-values
546 (setf (slot-value obj 'view-database) database)))
549 (defmethod install-instance ((obj standard-db-object)
550 &key (database *default-database*))
551 (labels ((slot-storedp (slot)
552 (and (member (view-class-slot-db-kind slot) '(:base :key))
553 (slot-boundp obj (slot-definition-name slot))))
554 (slot-value-list (slot)
555 (let ((value (slot-value obj (slot-definition-name slot))))
556 (check-slot-type slot value)
557 (list (sql-expression :attribute (view-class-slot-column slot))
558 (db-value-from-slot slot value database)))))
559 (let* ((view-class (class-of obj))
560 (view-class-table (view-table view-class))
561 (slots (remove-if-not #'slot-storedp (class-slots view-class)))
562 (record-values (mapcar #'slot-value-list slots)))
563 (unless record-values
564 (error "No settable slots."))
566 (let ((obj-db (slot-value obj 'view-database)))
568 (equal obj-db database))))
569 (insert-records :into (sql-expression :table view-class-table)
570 :av-pairs record-values
572 (setf (slot-value obj 'view-database) database))
575 ;; Perhaps the slot class is not correct in all CLOS implementations,
576 ;; tho I have not run across a problem yet.
578 (defmethod handle-cascade-delete-rule ((instance standard-db-object)
580 view-class-effective-slot-definition))
581 (let ((val (slot-value instance (slot-definition-name slot))))
584 (if (gethash :target-slot (view-class-slot-db-info slot))
585 ;; For relations with target-slot, we delete just the join instance
586 (mapcar #'(lambda (obj)
587 (delete-instance-records obj))
588 (fault-join-slot-raw (class-of instance) instance slot))
590 (delete-instance-records obj))))
592 (delete-instance-records val)))))
594 (defmethod nullify-join-foreign-keys ((instance standard-db-object) slot)
595 (let* ((dbi (view-class-slot-db-info slot))
596 (fkeys (gethash :foreign-keys dbi)))
597 (mapcar #'(lambda (fk)
598 (if (view-class-slot-nulls-ok slot)
599 (setf (slot-value instance fk) nil)
600 (warn "Nullify delete rule cannot set slot not allowing nulls to nil")))
601 (if (listp fkeys) fkeys (list fkeys)))))
603 (defmethod handle-nullify-delete-rule ((instance standard-db-object)
605 view-class-effective-slot-definition))
606 (let ((dbi (view-class-slot-db-info slot)))
607 (if (gethash :set dbi)
608 (if (gethash :target-slot (view-class-slot-db-info slot))
609 ;;For relations with target-slot, we delete just the join instance
610 (mapcar #'(lambda (obj)
611 (nullify-join-foreign-keys obj slot))
612 (fault-join-slot-raw (class-of instance) instance slot))
613 (dolist (obj (slot-value instance (slot-definition-name slot)))
614 (nullify-join-foreign-keys obj slot)))
615 (nullify-join-foreign-keys
616 (slot-value instance (slot-definition-name slot)) slot))))
618 (defmethod propogate-deletes ((instance standard-db-object))
619 (let* ((view-class (class-of instance))
620 (joins (remove-if #'(lambda (sd)
621 (not (equal (view-class-slot-db-kind sd) :join)))
622 (class-slots view-class))))
624 (let ((delete-rule (gethash :delete-rule (view-class-slot-db-info slot))))
626 ((eql delete-rule :cascade)
627 (handle-cascade-delete-rule instance slot))
628 ((eql delete-rule :deny)
629 (when (slot-value instance (slot-definition-name slot))
631 "Unable to delete slot ~A, because it has a deny delete rule."
633 ((eql delete-rule :nullify)
634 (handle-nullify-delete-rule instance slot))
637 (defgeneric delete-instance-records (instance)
639 "Deletes the records represented by INSTANCE from the database
640 associated with it. If instance has no associated database, an error
643 (defmethod delete-instance-records ((instance standard-db-object))
644 (let ((vt (sql-expression :table (view-table (class-of instance))))
645 (vd (or (view-database instance) *default-database*)))
647 (let ((qualifier (key-qualifier-for-instance instance :database vd)))
648 (with-transaction (:database vd)
649 (propogate-deletes instance)
650 (delete-records :from vt :where qualifier :database vd)
651 (setf (slot-value instance 'view-database) nil)))))
654 (defgeneric update-instance-from-records (instance &key database)
656 "Updates the values in the slots of the View Class instance
657 INSTANCE using the data in the database DATABASE which defaults to the
658 database that INSTANCE is associated with, or the value of
659 *DEFAULT-DATABASE*."))
661 (defmethod update-instance-from-records ((instance standard-db-object)
662 &key (database *default-database*))
663 (let* ((view-class (find-class (class-name (class-of instance))))
664 (view-table (sql-expression :table (view-table view-class)))
665 (vd (or (view-database instance) database))
666 (view-qual (key-qualifier-for-instance instance :database vd))
667 (sels (generate-selection-list view-class))
668 (res (apply #'select (append (mapcar #'cdr sels)
669 (list :from view-table
670 :where view-qual)))))
671 (get-slot-values-from-view instance (mapcar #'car sels) (car res))))
673 (defgeneric update-slot-from-record (instance slot &key database)
675 "Updates the value in the slot SLOT of the View Class instance
676 INSTANCE using the data in the database DATABASE which defaults to the
677 database that INSTANCE is associated with, or the value of
678 *DEFAULT-DATABASE*."))
680 (defmethod update-slot-from-record ((instance standard-db-object)
681 slot &key (database *default-database*))
682 (let* ((view-class (find-class (class-name (class-of instance))))
683 (view-table (sql-expression :table (view-table view-class)))
684 (vd (or (view-database instance) database))
685 (view-qual (key-qualifier-for-instance instance :database vd))
686 (slot-def (slotdef-for-slot-with-class slot view-class))
687 (att-ref (generate-attribute-reference view-class slot-def))
688 (res (select att-ref :from view-table :where view-qual)))
689 (get-slot-values-from-view instance (list slot-def) (car res))))
692 (defgeneric database-null-value (type)
693 (:documentation "Return an expression of type TYPE which SQL NULL values
694 will be converted into."))
696 (defmethod database-null-value ((type t))
698 ((subtypep type 'string) "")
699 ((subtypep type 'integer) 0)
700 ((subtypep type 'float) (float 0.0))
701 ((subtypep type 'list) nil)
702 ((subtypep type 'boolean) nil)
703 ((subtypep type 'symbol) nil)
704 ((subtypep type 'keyword) nil)
705 ((subtypep type 'wall-time) nil)
707 (error "Unable to handle null for type ~A" type))))
709 (defgeneric update-slot-with-null (instance slotname slotdef)
710 (:documentation "Called to update a slot when its column has a NULL
711 value. If nulls are allowed for the column, the slot's value will be
712 nil, otherwise its value will be set to the result of calling
713 DATABASE-NULL-VALUE on the type of the slot."))
715 (defmethod update-slot-with-null ((instance standard-db-object)
718 (let ((st (slot-type slotdef))
719 (allowed (slot-value slotdef 'nulls-ok)))
721 (setf (slot-value instance slotname) nil)
722 (setf (slot-value instance slotname)
723 (database-null-value st)))))
725 (defvar +no-slot-value+ '+no-slot-value+)
727 (defsql sql-slot-value (:symbol "slot-value") (classname slot &optional (value +no-slot-value+) (database *default-database*))
728 (let* ((class (find-class classname))
729 (sld (slotdef-for-slot-with-class slot class)))
731 (if (eq value +no-slot-value+)
732 (sql-expression :attribute (view-class-slot-column sld)
733 :table (view-table class))
738 (error "Unknown slot ~A for class ~A" slot classname))))
740 (defsql sql-view-class (:symbol "view-class") (classname &optional (database *default-database*))
741 (declare (ignore database))
742 (let* ((class (find-class classname)))
743 (unless (view-table class)
744 (error "No view-table for class ~A" classname))
745 (sql-expression :table (view-table class))))
747 (defmethod database-get-type-specifier (type args database)
748 (declare (ignore type args))
749 (if (member (database-type database) '(:postgresql :postgresql-socket))
753 (defmethod database-get-type-specifier ((type (eql 'integer)) args database)
754 (declare (ignore database))
757 (format nil "INT(~A)" (car args))
760 (defmethod database-get-type-specifier ((type (eql 'simple-base-string)) args
763 (format nil "VARCHAR(~A)" (car args))
764 (if (member (database-type database) '(:postgresql :postgresql-socket))
768 (defmethod database-get-type-specifier ((type (eql 'simple-string)) args
771 (format nil "VARCHAR(~A)" (car args))
772 (if (member (database-type database) '(:postgresql :postgresql-socket))
776 (defmethod database-get-type-specifier ((type (eql 'string)) args database)
778 (format nil "VARCHAR(~A)" (car args))
779 (if (member (database-type database) '(:postgresql :postgresql-socket))
783 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database)
784 (declare (ignore args))
785 (case (database-type database)
787 "TIMESTAMP WITHOUT TIME ZONE")
789 "TIMESTAMP WITHOUT TIME ZONE")
794 (defmethod database-get-type-specifier ((type (eql 'duration)) args database)
795 (declare (ignore database args))
798 (deftype raw-string (&optional len)
799 "A string which is not trimmed when retrieved from the database"
802 (defmethod database-get-type-specifier ((type (eql 'raw-string)) args database)
803 (declare (ignore database))
805 (format nil "VARCHAR(~A)" (car args))
808 (defmethod database-get-type-specifier ((type (eql 'float)) args database)
809 (declare (ignore database))
811 (format nil "FLOAT(~A)" (car args))
814 (defmethod database-get-type-specifier ((type (eql 'long-float)) args database)
815 (declare (ignore database))
817 (format nil "FLOAT(~A)" (car args))
820 (defmethod database-get-type-specifier ((type (eql 'boolean)) args database)
821 (declare (ignore args database))
824 (defmethod database-output-sql-as-type (type val database)
825 (declare (ignore type database))
828 (defmethod database-output-sql-as-type ((type (eql 'list)) val database)
829 (declare (ignore database))
830 (progv '(*print-circle* *print-array*) '(t t)
831 (prin1-to-string val)))
833 (defmethod database-output-sql-as-type ((type (eql 'symbol)) val database)
834 (declare (ignore database))
839 (package-name (symbol-package val))
844 (defmethod database-output-sql-as-type ((type (eql 'keyword)) val database)
845 (declare (ignore database))
850 (defmethod database-output-sql-as-type ((type (eql 'vector)) val database)
851 (declare (ignore database))
852 (progv '(*print-circle* *print-array*) '(t t)
853 (prin1-to-string val)))
855 (defmethod database-output-sql-as-type ((type (eql 'array)) val database)
856 (declare (ignore database))
857 (progv '(*print-circle* *print-array*) '(t t)
858 (prin1-to-string val)))
860 (defmethod database-output-sql-as-type ((type (eql 'boolean)) val database)
861 (declare (ignore database))
864 (defmethod database-output-sql-as-type ((type (eql 'string)) val database)
865 (declare (ignore database))
868 (defmethod database-output-sql-as-type ((type (eql 'simple-string))
870 (declare (ignore database))
873 (defmethod database-output-sql-as-type ((type (eql 'simple-base-string))
875 (declare (ignore database))
878 (defmethod read-sql-value (val type database)
879 (declare (ignore type database))
880 (read-from-string val))
882 (defmethod read-sql-value (val (type (eql 'string)) database)
883 (declare (ignore database))
886 (defmethod read-sql-value (val (type (eql 'simple-string)) database)
887 (declare (ignore database))
890 (defmethod read-sql-value (val (type (eql 'simple-base-string)) database)
891 (declare (ignore database))
894 (defmethod read-sql-value (val (type (eql 'raw-string)) database)
895 (declare (ignore database))
898 (defmethod read-sql-value (val (type (eql 'keyword)) database)
899 (declare (ignore database))
900 (when (< 0 (length val))
901 (intern (string-upcase val) "KEYWORD")))
903 (defmethod read-sql-value (val (type (eql 'symbol)) database)
904 (declare (ignore database))
905 (when (< 0 (length val))
907 (read-from-string val)
908 (intern (string-upcase val) "KEYWORD"))))
910 (defmethod read-sql-value (val (type (eql 'integer)) database)
911 (declare (ignore database))
914 (read-from-string val))
917 (defmethod read-sql-value (val (type (eql 'float)) database)
918 (declare (ignore database))
919 ;; writing 1.0 writes 1, so we we *really* want a float, must do (float ...)
920 (float (read-from-string val)))
922 (defmethod read-sql-value (val (type (eql 'boolean)) database)
923 (declare (ignore database))
926 (defmethod read-sql-value (val (type (eql 'wall-time)) database)
927 (declare (ignore database))
928 (unless (eq 'NULL val)
929 (parse-timestring val)))
932 ;; ------------------------------------------------------------
933 ;; Logic for 'faulting in' :join slots
935 (defun fault-join-slot-raw (class instance slot-def)
936 (let* ((dbi (view-class-slot-db-info slot-def))
937 (jc (gethash :join-class dbi)))
938 (let ((jq (join-qualifier class instance slot-def)))
940 (select jc :where jq)))))
942 (defun fault-join-slot (class instance slot-def)
943 (let* ((dbi (view-class-slot-db-info slot-def))
944 (ts (gethash :target-slot dbi))
945 (res (fault-join-slot-raw class instance slot-def)))
948 ((and ts (gethash :set dbi))
949 (mapcar (lambda (obj)
950 (cons obj (slot-value obj ts))) res))
951 ((and ts (not (gethash :set dbi)))
952 (mapcar (lambda (obj) (slot-value obj ts)) res))
953 ((and (not ts) (not (gethash :set dbi)))
955 ((and (not ts) (gethash :set dbi))
958 (defun join-qualifier (class instance slot-def)
959 (declare (ignore class))
960 (let* ((dbi (view-class-slot-db-info slot-def))
961 (jc (find-class (gethash :join-class dbi)))
962 ;;(ts (gethash :target-slot dbi))
963 ;;(tsdef (if ts (slotdef-for-slot-with-class ts jc)))
964 (foreign-keys (gethash :foreign-key dbi))
965 (home-keys (gethash :home-key dbi)))
966 (when (every #'(lambda (slt)
967 (and (slot-boundp instance slt)
968 (not (null (slot-value instance slt)))))
969 (if (listp home-keys) home-keys (list home-keys)))
971 (mapcar #'(lambda (hk fk)
972 (let ((fksd (slotdef-for-slot-with-class fk jc)))
978 (view-class-slot-column fksd)
979 :table (view-table jc)))
983 (slot-value instance hk))
986 (if (listp home-keys)
989 (if (listp foreign-keys)
991 (list foreign-keys)))))
993 (if (> (length jc) 1)
998 (defun find-all (view-classes &rest args &key all set-operation distinct from
999 where group-by having order-by order-by-descending offset limit
1000 (database *default-database*))
1001 "tweeze me apart someone pleeze"
1002 (declare (ignore all set-operation from group-by having offset limit)
1003 (optimize (debug 3) (speed 1)))
1004 (let* ((*db-deserializing* t)
1005 (*default-database* (or database (error 'clsql-nodb-error))))
1006 (flet ((table-sql-expr (table)
1007 (sql-expression :table (view-table table)))
1008 (ref-equal (ref1 ref2)
1011 (tables-equal (table-a table-b)
1012 (string= (string (slot-value table-a 'name))
1013 (string (slot-value table-b 'name)))))
1015 (let* ((sclasses (mapcar #'find-class view-classes))
1016 (sels (mapcar #'generate-selection-list sclasses))
1017 (fullsels (apply #'append sels))
1018 (sel-tables (collect-table-refs where))
1021 (append (mapcar #'table-sql-expr sclasses) sel-tables)
1022 :test #'tables-equal))
1024 (dolist (ob (listify order-by))
1025 (when (and ob (not (member ob (mapcar #'cdr fullsels)
1026 :test #'ref-equal)))
1028 (append fullsels (mapcar #'(lambda (att) (cons nil att))
1030 (dolist (ob (listify order-by-descending))
1031 (when (and ob (not (member ob (mapcar #'cdr fullsels)
1032 :test #'ref-equal)))
1034 (append fullsels (mapcar #'(lambda (att) (cons nil att))
1036 (dolist (ob (listify distinct))
1037 (when (and (typep ob 'sql-ident)
1038 (not (member ob (mapcar #'cdr fullsels)
1039 :test #'ref-equal)))
1041 (append fullsels (mapcar #'(lambda (att) (cons nil att))
1043 ;;(format t "~%fullsels is : ~A" fullsels)
1044 (setq res (apply #'select (append (mapcar #'cdr fullsels)
1045 (cons :from (list tables)) args)))
1046 (flet ((build-instance (vals)
1047 (flet ((%build-instance (vclass selects)
1048 (let ((class-name (class-name vclass))
1049 (db-vals (butlast vals
1050 (- (list-length vals)
1051 (list-length selects))))
1053 (setf vals (nthcdr (list-length selects) vals))
1054 (loop for select in selects
1055 for value in db-vals
1057 (when (eql (slot-value (car select) 'db-kind)
1060 (key-value-from-db (car select) value
1063 (push class-name cache-key)
1064 (%make-fresh-object class-name
1065 (mapcar #'car selects)
1067 (let ((instances (mapcar #'%build-instance sclasses sels)))
1068 (if (= (length sclasses) 1)
1071 (remove-if #'null (mapcar #'build-instance res)))))))
1073 (defun %make-fresh-object (class-name slots values)
1074 (let* ((*db-initializing* t)
1075 (obj (make-instance class-name
1076 :view-database *default-database*)))
1077 (setf obj (get-slot-values-from-view obj slots values))
1078 (postinitialize obj)
1081 (defmethod postinitialize ((self t))
1084 (defun select (&rest select-all-args)
1085 "Selects data from database given the constraints specified. Returns
1086 a list of lists of record values as specified by select-all-args. By
1087 default, the records are each represented as lists of attribute
1088 values. The selections argument may be either db-identifiers, literal
1089 strings or view classes. If the argument consists solely of view
1090 classes, the return value will be instances of objects rather than raw
1092 (flet ((select-objects (target-args)
1094 (every #'(lambda (arg)
1096 (find-class arg nil)))
1098 (multiple-value-bind (target-args qualifier-args)
1099 (query-get-selections select-all-args)
1100 (if (select-objects target-args)
1101 (apply #'find-all target-args qualifier-args)
1102 (let ((expr (apply #'make-query select-all-args)))
1103 (destructuring-bind (&key (flatp nil)
1104 (database *default-database*)
1107 (let ((res (query expr :database database)))
1109 (= (length (slot-value expr 'selections)) 1))