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