r8852: package renaming
[clsql.git] / sql / classes.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; $Id: $
4 ;;;; ======================================================================
5 ;;;;
6 ;;;; Description ==========================================================
7 ;;;; ======================================================================
8 ;;;;
9 ;;;; Classes defining SQL expressions and methods for formatting the
10 ;;;; appropriate SQL commands.
11 ;;;;
12 ;;;; ======================================================================
13
14 (in-package #:clsql-sys)
15
16
17 (defvar +empty-string+ "''")
18
19 (defvar +null-string+ "NULL")
20
21 (defvar *sql-stream* nil
22   "stream which accumulates SQL output")
23
24 (defvar *default-schema* "UNCOMMONSQL")
25
26 (defvar *object-schemas* (make-hash-table :test #'equal)
27   "Hash of schema name to class constituent lists.")
28
29 (defun in-schema (schemaname)
30   (setf *default-schema* schemaname))
31
32 (defun sql-output (sql-expr &optional database)
33   (progv '(*sql-stream*)
34       `(,(make-string-output-stream))
35     (output-sql sql-expr database)
36     (get-output-stream-string *sql-stream*)))
37
38
39 (defclass %sql-expression ()
40   ())
41
42 (defmethod output-sql ((expr %sql-expression) &optional
43                        (database *default-database*))
44   (declare (ignore database))
45   (write-string +null-string+ *sql-stream*))
46
47 (defmethod print-object ((self %sql-expression) stream)
48   (print-unreadable-object
49    (self stream :type t)
50    (write-string (sql-output self) stream)))
51
52 ;; For straight up strings
53
54 (defclass sql (%sql-expression)
55   ((text
56     :initarg :string
57     :initform ""))
58   (:documentation "A literal SQL expression."))
59
60 (defmethod make-load-form ((sql sql) &optional environment)
61   (declare (ignore environment))
62   (with-slots (text)
63     sql
64     `(make-instance 'sql :string ',text)))
65
66 (defmethod output-sql ((expr sql) &optional (database *default-database*))
67   (declare (ignore database))
68   (write-string (slot-value expr 'text) *sql-stream*)
69   t)
70
71 (defmethod print-object ((ident sql) stream)
72   (format stream "#<~S \"~A\">"
73           (type-of ident)
74           (sql-output ident)))
75
76 ;; For SQL Identifiers of generic type
77 (defclass sql-ident (%sql-expression)
78   ((name
79     :initarg :name
80     :initform "NULL"))
81   (:documentation "An SQL identifer."))
82
83 (defmethod make-load-form ((sql sql-ident) &optional environment)
84   (declare (ignore environment))
85   (with-slots (name)
86     sql
87     `(make-instance 'sql-ident :name ',name)))
88
89 (defvar *output-hash* (make-hash-table :test #'equal))
90
91 (defmethod output-sql-hash-key (expr &optional (database *default-database*))
92   (declare (ignore expr database))
93   nil)
94
95 (defmethod output-sql :around ((sql t) &optional (database *default-database*))
96   (declare (ignore database))
97   (let* ((hash-key (output-sql-hash-key sql))
98          (hash-value (when hash-key (gethash hash-key *output-hash*))))
99     (cond ((and hash-key hash-value)
100            (write-string hash-value *sql-stream*))
101           (hash-key
102            (let ((*sql-stream* (make-string-output-stream)))
103              (call-next-method)
104              (setf hash-value (get-output-stream-string *sql-stream*))
105              (setf (gethash hash-key *output-hash*) hash-value))
106            (write-string hash-value *sql-stream*))
107           (t
108            (call-next-method)))))
109
110 (defmethod output-sql ((expr sql-ident) &optional
111                        (database *default-database*))
112   (declare (ignore database))
113   (with-slots (name)
114     expr
115     (etypecase name
116       (string
117        (write-string name *sql-stream*))
118       (symbol
119        (write-string (symbol-name name) *sql-stream*)))
120     t))
121
122 ;; For SQL Identifiers for attributes
123
124 (defclass sql-ident-attribute (sql-ident)
125   ((qualifier
126     :initarg :qualifier
127     :initform "NULL")
128    (type
129     :initarg :type
130     :initform "NULL")
131    (params
132     :initarg :params
133     :initform nil))
134   (:documentation "An SQL Attribute identifier."))
135
136 (defmethod collect-table-refs (sql)
137   (declare (ignore sql))
138   nil)
139
140 (defmethod collect-table-refs ((sql sql-ident-attribute))
141   (let ((qual (slot-value sql 'qualifier)))
142     (if (and qual (symbolp (slot-value sql 'qualifier)))
143         (list (make-instance 'sql-ident-table :name
144                              (slot-value sql 'qualifier))))))
145
146 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
147   (declare (ignore environment))
148   (with-slots (qualifier type name)
149     sql
150     `(make-instance 'sql-ident-attribute :name ',name
151       :qualifier ',qualifier
152       :type ',type)))
153
154 (defmethod output-sql ((expr sql-ident-attribute) &optional
155                        (database *default-database*))
156   (declare (ignore database))
157   (with-slots (qualifier name type params)
158     expr
159     (if (and name (not qualifier) (not type))
160         (write-string (sql-escape (symbol-name name)) *sql-stream*)
161         (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
162                 (if qualifier (sql-escape qualifier) qualifier)
163                 (sql-escape name)
164                 type))
165     t))
166
167 (defmethod output-sql-hash-key ((expr sql-ident-attribute) &optional
168                                 (database *default-database*))
169   (declare (ignore database))
170   (with-slots (qualifier name type params)
171     expr
172     (list 'sql-ident-attribute qualifier name type params)))
173
174 ;; For SQL Identifiers for tables
175 (defclass sql-ident-table (sql-ident)
176   ((alias
177     :initarg :table-alias :initform nil))
178   (:documentation "An SQL table identifier."))
179
180 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
181   (declare (ignore environment))
182   (with-slots (alias name)
183     sql
184     `(make-instance 'sql-ident-table :name name :alias ',alias)))
185
186 (defun generate-sql (expr)
187   (let ((*sql-stream* (make-string-output-stream)))
188     (output-sql expr)
189     (get-output-stream-string *sql-stream*)))
190
191 (defmethod output-sql ((expr sql-ident-table) &optional
192                        (database *default-database*))
193   (declare (ignore database))
194   (with-slots (name alias)
195     expr
196     (if (null alias)
197         (write-string (sql-escape (symbol-name name)) *sql-stream*)
198         (progn
199           (write-string (sql-escape (symbol-name name)) *sql-stream*)
200           (write-char #\Space *sql-stream*)
201           (format *sql-stream* "~s" alias))))
202   t)
203
204 (defmethod output-sql-hash-key ((expr sql-ident-table) &optional
205                                 (database *default-database*))
206   (declare (ignore database))
207   (with-slots (name alias)
208     expr
209     (list 'sql-ident-table name alias)))
210
211 (defclass sql-relational-exp (%sql-expression)
212   ((operator
213     :initarg :operator
214     :initform nil)
215    (sub-expressions
216     :initarg :sub-expressions
217     :initform nil))
218   (:documentation "An SQL relational expression."))
219
220 (defmethod collect-table-refs ((sql sql-relational-exp))
221   (let ((tabs nil))
222     (dolist (exp (slot-value sql 'sub-expressions))
223       (let ((refs (collect-table-refs exp)))
224         (if refs (setf tabs (append refs tabs)))))
225     (remove-duplicates tabs
226                        :test (lambda (tab1 tab2)
227                                (equal (slot-value tab1 'name)
228                                       (slot-value tab2 'name))))))
229
230
231
232
233 ;; Write SQL for relational operators (like 'AND' and 'OR').
234 ;; should do arity checking of subexpressions
235
236 (defmethod output-sql ((expr sql-relational-exp) &optional
237                        (database *default-database*))
238   (with-slots (operator sub-expressions)
239     expr
240     (let ((subs (if (consp (car sub-expressions))
241                     (car sub-expressions)
242                     sub-expressions)))
243       (write-char #\( *sql-stream*)
244       (do ((sub subs (cdr sub)))
245           ((null (cdr sub)) (output-sql (car sub) database))
246         (output-sql (car sub) database)
247         (write-char #\Space *sql-stream*)
248         (output-sql operator database)
249         (write-char #\Space *sql-stream*))
250       (write-char #\) *sql-stream*)))
251   t)
252
253 (defclass sql-upcase-like (sql-relational-exp)
254   ()
255   (:documentation "An SQL 'like' that upcases its arguments."))
256   
257 ;; Write SQL for relational operators (like 'AND' and 'OR').
258 ;; should do arity checking of subexpressions
259   
260 (defmethod output-sql ((expr sql-upcase-like) &optional
261                        (database *default-database*))
262   (flet ((write-term (term)
263            (write-string "upper(" *sql-stream*)
264            (output-sql term database)
265            (write-char #\) *sql-stream*)))
266     (with-slots (sub-expressions)
267       expr
268       (let ((subs (if (consp (car sub-expressions))
269                       (car sub-expressions)
270                       sub-expressions)))
271         (write-char #\( *sql-stream*)
272         (do ((sub subs (cdr sub)))
273             ((null (cdr sub)) (write-term (car sub)))
274           (write-term (car sub))
275           (write-string " LIKE " *sql-stream*))
276         (write-char #\) *sql-stream*))))
277   t)
278
279 (defclass sql-assignment-exp (sql-relational-exp)
280   ()
281   (:documentation "An SQL Assignment expression."))
282
283
284 (defmethod output-sql ((expr sql-assignment-exp) &optional
285                        (database *default-database*))
286   (with-slots (operator sub-expressions)
287     expr
288     (do ((sub sub-expressions (cdr sub)))
289         ((null (cdr sub)) (output-sql (car sub) database))
290       (output-sql (car sub) database)
291       (write-char #\Space *sql-stream*)
292       (output-sql operator database)
293       (write-char #\Space *sql-stream*)))
294   t)
295
296 (defclass sql-value-exp (%sql-expression)
297   ((modifier
298     :initarg :modifier
299     :initform nil)
300    (components
301     :initarg :components
302     :initform nil))
303   (:documentation
304    "An SQL value expression.")
305   )
306
307 (defmethod collect-table-refs ((sql sql-value-exp))
308   (let ((tabs nil))
309     (if (listp (slot-value sql 'components))
310         (progn
311           (dolist (exp (slot-value sql 'components))
312             (let ((refs (collect-table-refs exp)))
313               (if refs (setf tabs (append refs tabs)))))
314           (remove-duplicates tabs
315                              :test (lambda (tab1 tab2)
316                                      (equal (slot-value tab1 'name)
317                                             (slot-value tab2 'name)))))
318         nil)))
319
320
321
322 (defmethod output-sql ((expr sql-value-exp) &optional
323                        (database *default-database*))
324   (with-slots (modifier components)
325     expr
326     (if modifier
327         (progn
328           (write-char #\( *sql-stream*)
329           (output-sql modifier database)
330           (write-char #\Space *sql-stream*)
331           (output-sql components database)
332           (write-char #\) *sql-stream*))
333         (output-sql components database))))
334
335 (defclass sql-typecast-exp (sql-value-exp)
336   ()
337   (:documentation "An SQL typecast expression."))
338
339 (defmethod output-sql ((expr sql-typecast-exp) &optional
340                        (database *default-database*))
341   (database-output-sql expr database))
342
343 (defmethod database-output-sql ((expr sql-typecast-exp) database)
344   (with-slots (components)
345     expr
346     (output-sql components database)))
347
348
349 (defmethod collect-table-refs ((sql sql-typecast-exp))
350   (when (slot-value sql 'components)
351     (collect-table-refs (slot-value sql 'components))))
352
353 (defclass sql-function-exp (%sql-expression)
354   ((name
355     :initarg :name
356     :initform nil)
357    (args
358     :initarg :args
359     :initform nil))
360   (:documentation
361    "An SQL function expression."))
362
363 (defmethod collect-table-refs ((sql sql-function-exp))
364   (let ((tabs nil))
365     (dolist (exp (slot-value sql 'components))
366       (let ((refs (collect-table-refs exp)))
367         (if refs (setf tabs (append refs tabs)))))
368     (remove-duplicates tabs
369                        :test (lambda (tab1 tab2)
370                                (equal (slot-value tab1 'name)
371                                       (slot-value tab2 'name))))))
372
373 (defmethod output-sql ((expr sql-function-exp) &optional
374                        (database *default-database*))
375   (with-slots (name args)
376     expr
377     (output-sql name database)
378     (when args (output-sql args database)))
379   t)
380
381 (defclass sql-query (%sql-expression)
382   ((selections
383     :initarg :selections
384     :initform nil)
385    (all
386     :initarg :all
387     :initform nil)
388    (flatp
389     :initarg :flatp
390     :initform nil)
391    (set-operation
392     :initarg :set-operation
393     :initform nil)
394    (distinct
395     :initarg :distinct
396     :initform nil)
397    (from
398     :initarg :from
399     :initform nil)
400    (where
401     :initarg :where
402     :initform nil)
403    (group-by
404     :initarg :group-by
405     :initform nil)
406    (having
407     :initarg :having
408     :initform nil)
409    (limit
410     :initarg :limit
411     :initform nil)
412    (offset
413     :initarg :offset
414     :initform nil)
415    (order-by
416     :initarg :order-by
417     :initform nil)
418    (order-by-descending
419     :initarg :order-by-descending
420     :initform nil))
421   (:documentation "An SQL SELECT query."))
422
423 (defmethod collect-table-refs ((sql sql-query))
424   (remove-duplicates (collect-table-refs (slot-value sql 'where))
425                      :test (lambda (tab1 tab2)
426                              (equal (slot-value tab1 'name)
427                                     (slot-value tab2 'name)))))
428
429 (defvar *select-arguments*
430   '(:all :database :distinct :flatp :from :group-by :having :order-by
431     :order-by-descending :set-operation :where :offset :limit))
432
433 (defun query-arg-p (sym)
434   (member sym *select-arguments*))
435
436 (defun query-get-selections (select-args)
437   "Return two values: the list of select-args up to the first keyword,
438 uninclusive, and the args from that keyword to the end."
439   (let ((first-key-arg (position-if #'query-arg-p select-args)))
440     (if first-key-arg
441         (values (subseq select-args 0 first-key-arg)
442                 (subseq select-args first-key-arg))
443         select-args)))
444
445 (defmethod make-query (&rest args)
446   (multiple-value-bind (selections arglist)
447       (query-get-selections args)
448     (destructuring-bind (&key all flatp set-operation distinct from where
449                               group-by having order-by order-by-descending
450                               offset limit &allow-other-keys)
451         arglist
452       (if (null selections)
453           (error "No target columns supplied to select statement."))
454       (if (null from)
455           (error "No source tables supplied to select statement."))
456       (make-instance 'sql-query :selections selections
457                      :all all :flatp flatp :set-operation set-operation
458                      :distinct distinct :from from :where where
459                      :limit limit :offset offset
460                      :group-by group-by :having having :order-by order-by
461                      :order-by-descending order-by-descending))))
462
463 (defvar *in-subselect* nil)
464
465 (defmethod output-sql ((query sql-query) &optional
466                        (database *default-database*))
467   (with-slots (distinct selections from where group-by having order-by
468                         order-by-descending limit offset)
469       query
470     (when *in-subselect*
471       (write-string "(" *sql-stream*))
472     (write-string "SELECT " *sql-stream*)
473     (when distinct
474       (write-string "DISTINCT " *sql-stream*)
475       (unless (eql t distinct)
476         (write-string "ON " *sql-stream*)
477         (output-sql distinct database)
478         (write-char #\Space *sql-stream*)))
479     (output-sql (apply #'vector selections) database)
480     (write-string " FROM " *sql-stream*)
481     (if (listp from)
482         (output-sql (apply #'vector from) database)
483         (output-sql from database))
484     (when where
485       (write-string " WHERE " *sql-stream*)
486       (let ((*in-subselect* t))
487         (output-sql where database)))
488     (when group-by
489       (write-string " GROUP BY " *sql-stream*)
490       (output-sql group-by database))
491     (when having
492       (write-string " HAVING " *sql-stream*)
493       (output-sql having database))
494     (when order-by
495       (write-string " ORDER BY " *sql-stream*)
496       (if (listp order-by)
497           (do ((order order-by (cdr order)))
498               ((null order))
499             (output-sql (car order) database)
500             (when (cdr order)
501               (write-char #\, *sql-stream*)))
502           (output-sql order-by database)))
503     (when order-by-descending
504       (write-string " ORDER BY " *sql-stream*)
505       (if (listp order-by-descending)
506           (do ((order order-by-descending (cdr order)))
507               ((null order))
508             (output-sql (car order) database)
509             (when (cdr order)
510               (write-char #\, *sql-stream*)))
511           (output-sql order-by-descending database))
512       (write-string " DESC " *sql-stream*))
513     (when limit
514       (write-string " LIMIT " *sql-stream*)
515       (output-sql limit database))
516     (when offset
517       (write-string " OFFSET " *sql-stream*)
518       (output-sql offset database))
519     (when *in-subselect*
520       (write-string ")" *sql-stream*)))
521   t)
522
523 ;; INSERT
524
525 (defclass sql-insert (%sql-expression)
526   ((into
527     :initarg :into
528     :initform nil)
529    (attributes
530     :initarg :attributes
531     :initform nil)
532    (values
533     :initarg :values
534     :initform nil)
535    (query
536     :initarg :query
537     :initform nil))
538   (:documentation
539    "An SQL INSERT statement."))
540
541 (defmethod output-sql ((ins sql-insert) &optional
542                        (database *default-database*))
543   (with-slots (into attributes values query)
544     ins
545     (write-string "INSERT INTO " *sql-stream*)
546     (output-sql into database)
547     (when attributes
548       (write-char #\Space *sql-stream*)
549       (output-sql attributes database))
550     (when values
551       (write-string " VALUES " *sql-stream*)
552       (output-sql values database))
553     (when query
554       (write-char #\Space *sql-stream*)
555       (output-sql query database)))
556   t)
557
558 ;; DELETE
559
560 (defclass sql-delete (%sql-expression)
561   ((from
562     :initarg :from
563     :initform nil)
564    (where
565     :initarg :where
566     :initform nil))
567   (:documentation
568    "An SQL DELETE statement."))
569
570 (defmethod output-sql ((stmt sql-delete) &optional
571                        (database *default-database*))
572   (with-slots (from where)
573     stmt
574     (write-string "DELETE FROM " *sql-stream*)
575     (typecase from
576       (symbol (write-string (sql-escape from) *sql-stream*))
577       (t  (output-sql from database)))
578     (when where
579       (write-string " WHERE " *sql-stream*)
580       (output-sql where database)))
581   t)
582
583 ;; UPDATE
584
585 (defclass sql-update (%sql-expression)
586   ((table
587     :initarg :table
588     :initform nil)
589    (attributes
590     :initarg :attributes
591     :initform nil)
592    (values
593     :initarg :values
594     :initform nil)
595    (where
596     :initarg :where
597     :initform nil))
598   (:documentation "An SQL UPDATE statement."))
599
600 (defmethod output-sql ((expr sql-update) &optional
601                        (database *default-database*))
602   (with-slots (table where attributes values)
603     expr
604     (flet ((update-assignments ()
605              (mapcar #'(lambda (a b)
606                          (make-instance 'sql-assignment-exp
607                                         :operator '=
608                                         :sub-expressions (list a b)))
609                      attributes values)))
610       (write-string "UPDATE " *sql-stream*)
611       (output-sql table database)
612       (write-string " SET " *sql-stream*)
613       (output-sql (apply #'vector (update-assignments)) database)
614       (when where
615         (write-string " WHERE " *sql-stream*)
616         (output-sql where database))))
617   t)
618
619 ;; CREATE TABLE
620
621 (defclass sql-create-table (%sql-expression)
622   ((name
623     :initarg :name
624     :initform nil)
625    (columns
626     :initarg :columns
627     :initform nil)
628    (modifiers
629     :initarg :modifiers
630     :initform nil))
631   (:documentation
632    "An SQL CREATE TABLE statement."))
633
634 ;; Here's a real warhorse of a function!
635
636 (defun listify (x)
637   (if (atom x)
638       (list x)
639       x))
640
641 (defmethod output-sql ((stmt sql-create-table) &optional
642                        (database *default-database*))
643   (flet ((output-column (column-spec)
644            (destructuring-bind (name type &rest constraints)
645                column-spec
646              (let ((type (listify type)))
647                (output-sql name database)
648                (write-char #\Space *sql-stream*)
649                (write-string
650                 (database-get-type-specifier (car type) (cdr type) database)
651                 *sql-stream*)
652                (let ((constraints
653                       (database-constraint-statement constraints database)))
654                  (when constraints
655                    (write-string " " *sql-stream*)
656                    (write-string constraints *sql-stream*)))))))
657     (with-slots (name columns modifiers)
658       stmt
659       (write-string "CREATE TABLE " *sql-stream*)
660       (output-sql name database)
661       (write-string " (" *sql-stream*)
662       (do ((column columns (cdr column)))
663           ((null (cdr column))
664            (output-column (car column)))
665         (output-column (car column))
666         (write-string ", " *sql-stream*))
667       (when modifiers
668         (do ((modifier (listify modifiers) (cdr modifier)))
669             ((null modifier))
670           (write-string ", " *sql-stream*)
671           (write-string (car modifier) *sql-stream*)))
672       (write-char #\) *sql-stream*)))
673   t)
674
675
676 ;; CREATE VIEW
677
678 (defclass sql-create-view (%sql-expression)
679   ((name :initarg :name :initform nil)
680    (column-list :initarg :column-list :initform nil)
681    (query :initarg :query :initform nil)
682    (with-check-option :initarg :with-check-option :initform nil))
683   (:documentation "An SQL CREATE VIEW statement."))
684
685 (defmethod output-sql ((stmt sql-create-view) &optional database)
686   (with-slots (name column-list query with-check-option) stmt
687     (write-string "CREATE VIEW " *sql-stream*)
688     (output-sql name database)
689     (when column-list (write-string " " *sql-stream*)
690           (output-sql (listify column-list) database))
691     (write-string " AS " *sql-stream*)
692     (output-sql query database)
693     (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
694
695
696 ;;
697 ;; Column constraint types
698 ;;
699 (defparameter *constraint-types*
700   '(("NOT-NULL" . "NOT NULL")
701     ("PRIMARY-KEY" . "PRIMARY KEY")))
702
703 ;;
704 ;; Convert type spec to sql syntax
705 ;;
706
707 (defmethod database-constraint-description (constraint database)
708   (declare (ignore database))
709   (let ((output (assoc (symbol-name constraint) *constraint-types*
710                        :test #'equal)))
711     (if (null output)
712         (error 'clsql-sql-syntax-error
713                :reason (format nil "unsupported column constraint '~a'"
714                                constraint))
715         (cdr output))))
716
717 (defmethod database-constraint-statement (constraint-list database)
718   (declare (ignore database))
719   (make-constraints-description constraint-list))
720   
721 (defun make-constraints-description (constraint-list)
722   (if constraint-list
723       (let ((string ""))
724         (do ((constraint constraint-list (cdr constraint)))
725             ((null constraint) string)
726           (let ((output (assoc (symbol-name (car constraint))
727                                *constraint-types*
728                                :test #'equal)))
729             (if (null output)
730                 (error 'clsql-sql-syntax-error
731                        :reason (format nil "unsupported column constraint '~a'"
732                                        constraint))
733                 (setq string (concatenate 'string string (cdr output))))
734             (if (< 1 (length constraint))
735                 (setq string (concatenate 'string string " "))))))))
736