r9434: 22 May 2004 Kevin Rosenberg
[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) database)
36   (declare (ignore database))
37   (write-string +null-string+ *sql-stream*))
38
39 (defmethod print-object ((self %sql-expression) stream)
40   (print-unreadable-object
41    (self stream :type t)
42    (write-string (sql-output self) stream)))
43
44 ;; For straight up strings
45
46 (defclass sql (%sql-expression)
47   ((text
48     :initarg :string
49     :initform ""))
50   (:documentation "A literal SQL expression."))
51
52 (defmethod make-load-form ((sql sql) &optional environment)
53   (declare (ignore environment))
54   (with-slots (text)
55     sql
56     `(make-instance 'sql :string ',text)))
57
58 (defmethod output-sql ((expr sql) database)
59   (declare (ignore database))
60   (write-string (slot-value expr 'text) *sql-stream*)
61   t)
62
63 (defmethod print-object ((ident sql) stream)
64   (format stream "#<~S \"~A\">"
65           (type-of ident)
66           (sql-output ident nil)))
67
68 ;; For SQL Identifiers of generic type
69 (defclass sql-ident (%sql-expression)
70   ((name
71     :initarg :name
72     :initform "NULL"))
73   (:documentation "An SQL identifer."))
74
75 (defmethod make-load-form ((sql sql-ident) &optional environment)
76   (declare (ignore environment))
77   (with-slots (name)
78     sql
79     `(make-instance 'sql-ident :name ',name)))
80
81 (defvar *output-hash* (make-hash-table :test #'equal))
82
83 (defmethod output-sql-hash-key (expr database)
84   (declare (ignore expr database))
85   nil)
86
87 #+ignore
88 (defmethod output-sql :around ((sql t) database)
89   (let* ((hash-key (output-sql-hash-key sql database))
90          (hash-value (when hash-key (gethash hash-key *output-hash*))))
91     (cond ((and hash-key hash-value)
92            (write-string hash-value *sql-stream*))
93           (hash-key
94            (let ((*sql-stream* (make-string-output-stream)))
95              (call-next-method)
96              (setf hash-value (get-output-stream-string *sql-stream*))
97              (setf (gethash hash-key *output-hash*) hash-value))
98            (write-string hash-value *sql-stream*))
99           (t
100            (call-next-method)))))
101
102 (defmethod output-sql ((expr sql-ident) database)
103   (with-slots (name)
104       expr
105     (write-string
106      (convert-to-db-default-case 
107       (etypecase name
108         (string name)
109         (symbol (symbol-name name)))
110       database)
111      *sql-stream*))
112   t)
113
114 ;; For SQL Identifiers for attributes
115
116 (defclass sql-ident-attribute (sql-ident)
117   ((qualifier
118     :initarg :qualifier
119     :initform "NULL")
120    (type
121     :initarg :type
122     :initform "NULL"))
123   (:documentation "An SQL Attribute identifier."))
124
125 (defmethod collect-table-refs (sql)
126   (declare (ignore sql))
127   nil)
128
129 (defmethod collect-table-refs ((sql sql-ident-attribute))
130   (let ((qual (slot-value sql 'qualifier)))
131     (if (and qual (symbolp (slot-value sql 'qualifier)))
132         (list (make-instance 'sql-ident-table :name
133                              (slot-value sql 'qualifier))))))
134
135 (defmethod make-load-form ((sql sql-ident-attribute) &optional environment)
136   (declare (ignore environment))
137   (with-slots (qualifier type name)
138     sql
139     `(make-instance 'sql-ident-attribute :name ',name
140       :qualifier ',qualifier
141       :type ',type)))
142
143 (defmethod output-sql ((expr sql-ident-attribute) database)
144   (with-slots (qualifier name type) expr
145     (if (and (not qualifier) (not type))
146         (etypecase name
147           ;; Honor care of name
148           (string
149            (write-string name *sql-stream*))
150           (symbol
151            (write-string (sql-escape (convert-to-db-default-case 
152                                       (symbol-name name) database)) *sql-stream*)))
153       
154         ;;; KMR: The TYPE field is used by CommonSQL for type conversion -- it
155       ;;; should not be output in SQL statements
156       #+ignore
157       (format *sql-stream* "~@[~A.~]~A~@[ ~A~]"
158               (when qualifier
159                 (convert-to-db-default-case (sql-escape qualifier) database))
160               (sql-escape (convert-to-db-default-case name database))
161               (when type
162                 (convert-to-db-default-case (symbol-name type) database)))
163       (format *sql-stream* "~@[~A.~]~A"
164               (when qualifier
165                 (typecase qualifier 
166                   (string (format nil "~s" qualifier))
167                   (t (convert-to-db-default-case (sql-escape qualifier) 
168                                                  database))))
169               (sql-escape (convert-to-db-default-case name database))))
170     t))
171
172 (defmethod output-sql-hash-key ((expr sql-ident-attribute) database)
173   (declare (ignore database))
174   (with-slots (qualifier name type)
175     expr
176     (list 'sql-ident-attribute qualifier name type)))
177
178 ;; For SQL Identifiers for tables
179 (defclass sql-ident-table (sql-ident)
180   ((alias
181     :initarg :table-alias :initform nil))
182   (:documentation "An SQL table identifier."))
183
184 (defmethod make-load-form ((sql sql-ident-table) &optional environment)
185   (declare (ignore environment))
186   (with-slots (alias name)
187     sql
188     `(make-instance 'sql-ident-table :name ',name :table-alias ',alias)))
189
190 (defun generate-sql (expr database)
191   (let ((*sql-stream* (make-string-output-stream)))
192     (output-sql expr database)
193     (get-output-stream-string *sql-stream*)))
194
195 (defmethod output-sql ((expr sql-ident-table) database)
196   (with-slots (name alias)
197     expr
198     (if (null alias)
199         (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
200         (progn
201           (write-string (sql-escape (convert-to-db-default-case (symbol-name name) database)) *sql-stream*)
202           (write-char #\Space *sql-stream*)
203           (format *sql-stream* "~s" alias))))
204   t)
205
206 #|
207 (defmethod database-output-sql ((self duration) database)
208   (declare (ignore database))
209   (format nil "'~a'" (duration-timestring self)))
210
211 (defmethod database-output-sql ((self money) database)
212   (database-output-sql (slot-value self 'odcl::units) database))
213 |#
214
215
216 (defmethod output-sql-hash-key ((expr sql-ident-table) database)
217   (declare (ignore database))
218   (with-slots (name alias)
219     expr
220     (list 'sql-ident-table name alias)))
221
222 (defclass sql-relational-exp (%sql-expression)
223   ((operator
224     :initarg :operator
225     :initform nil)
226    (sub-expressions
227     :initarg :sub-expressions
228     :initform nil))
229   (:documentation "An SQL relational expression."))
230
231 (defmethod collect-table-refs ((sql sql-relational-exp))
232   (let ((tabs nil))
233     (dolist (exp (slot-value sql 'sub-expressions))
234       (let ((refs (collect-table-refs exp)))
235         (if refs (setf tabs (append refs tabs)))))
236     (remove-duplicates tabs
237                        :test (lambda (tab1 tab2)
238                                (equal (slot-value tab1 'name)
239                                       (slot-value tab2 'name))))))
240
241
242
243
244 ;; Write SQL for relational operators (like 'AND' and 'OR').
245 ;; should do arity checking of subexpressions
246
247 (defmethod output-sql ((expr sql-relational-exp) database)
248   (with-slots (operator sub-expressions)
249     expr
250     (let ((subs (if (consp (car sub-expressions))
251                     (car sub-expressions)
252                     sub-expressions)))
253       (write-char #\( *sql-stream*)
254       (do ((sub subs (cdr sub)))
255           ((null (cdr sub)) (output-sql (car sub) database))
256         (output-sql (car sub) database)
257         (write-char #\Space *sql-stream*)
258         (output-sql operator database)
259         (write-char #\Space *sql-stream*))
260       (write-char #\) *sql-stream*)))
261   t)
262
263 (defclass sql-upcase-like (sql-relational-exp)
264   ()
265   (:documentation "An SQL 'like' that upcases its arguments."))
266   
267 ;; Write SQL for relational operators (like 'AND' and 'OR').
268 ;; should do arity checking of subexpressions
269   
270 (defmethod output-sql ((expr sql-upcase-like) database)
271   (flet ((write-term (term)
272            (write-string "upper(" *sql-stream*)
273            (output-sql term database)
274            (write-char #\) *sql-stream*)))
275     (with-slots (sub-expressions)
276       expr
277       (let ((subs (if (consp (car sub-expressions))
278                       (car sub-expressions)
279                       sub-expressions)))
280         (write-char #\( *sql-stream*)
281         (do ((sub subs (cdr sub)))
282             ((null (cdr sub)) (write-term (car sub)))
283           (write-term (car sub))
284           (write-string " LIKE " *sql-stream*))
285         (write-char #\) *sql-stream*))))
286   t)
287
288 (defclass sql-assignment-exp (sql-relational-exp)
289   ()
290   (:documentation "An SQL Assignment expression."))
291
292
293 (defmethod output-sql ((expr sql-assignment-exp) database)
294   (with-slots (operator sub-expressions)
295     expr
296     (do ((sub sub-expressions (cdr sub)))
297         ((null (cdr sub)) (output-sql (car sub) database))
298       (output-sql (car sub) database)
299       (write-char #\Space *sql-stream*)
300       (output-sql operator database)
301       (write-char #\Space *sql-stream*)))
302   t)
303
304 (defclass sql-value-exp (%sql-expression)
305   ((modifier
306     :initarg :modifier
307     :initform nil)
308    (components
309     :initarg :components
310     :initform nil))
311   (:documentation
312    "An SQL value expression.")
313   )
314
315 (defmethod collect-table-refs ((sql sql-value-exp))
316   (let ((tabs nil))
317     (if (listp (slot-value sql 'components))
318         (progn
319           (dolist (exp (slot-value sql 'components))
320             (let ((refs (collect-table-refs exp)))
321               (if refs (setf tabs (append refs tabs)))))
322           (remove-duplicates tabs
323                              :test (lambda (tab1 tab2)
324                                      (equal (slot-value tab1 'name)
325                                             (slot-value tab2 'name)))))
326         nil)))
327
328
329
330 (defmethod output-sql ((expr sql-value-exp) database)
331   (with-slots (modifier components)
332     expr
333     (if modifier
334         (progn
335           (write-char #\( *sql-stream*)
336           (output-sql modifier database)
337           (write-char #\Space *sql-stream*)
338           (output-sql components database)
339           (write-char #\) *sql-stream*))
340         (output-sql components database))))
341
342 (defclass sql-typecast-exp (sql-value-exp)
343   ()
344   (:documentation "An SQL typecast expression."))
345
346 (defmethod output-sql ((expr sql-typecast-exp) database)
347   (database-output-sql expr database))
348
349 (defmethod database-output-sql ((expr sql-typecast-exp) database)
350   (with-slots (components)
351     expr
352     (output-sql components database)))
353
354
355 (defmethod collect-table-refs ((sql sql-typecast-exp))
356   (when (slot-value sql 'components)
357     (collect-table-refs (slot-value sql 'components))))
358
359 (defclass sql-function-exp (%sql-expression)
360   ((name
361     :initarg :name
362     :initform nil)
363    (args
364     :initarg :args
365     :initform nil))
366   (:documentation
367    "An SQL function expression."))
368
369 (defmethod collect-table-refs ((sql sql-function-exp))
370   (let ((tabs nil))
371     (dolist (exp (slot-value sql 'components))
372       (let ((refs (collect-table-refs exp)))
373         (if refs (setf tabs (append refs tabs)))))
374     (remove-duplicates tabs
375                        :test (lambda (tab1 tab2)
376                                (equal (slot-value tab1 'name)
377                                       (slot-value tab2 'name))))))
378
379 (defmethod output-sql ((expr sql-function-exp) database)
380   (with-slots (name args)
381     expr
382     (output-sql name database)
383     (when args (output-sql args database)))
384   t)
385
386
387 (defclass sql-between-exp (sql-function-exp)
388   () 
389   (:documentation "An SQL between expression."))
390
391 (defmethod output-sql ((expr sql-between-exp) database)
392   (with-slots (name args)
393       expr 
394     (output-sql (first args) database)
395     (write-string " BETWEEN " *sql-stream*)
396     (output-sql (second args) database)
397     (write-string " AND " *sql-stream*)
398     (output-sql (third args) database))
399   t)
400
401 (defclass sql-query-modifier-exp (%sql-expression) 
402   ((modifier :initarg :modifier :initform nil)
403    (components :initarg :components :initform nil))
404   (:documentation "An SQL query modifier expression."))
405
406 (defmethod output-sql ((expr sql-query-modifier-exp) database)
407   (with-slots (modifier components)
408       expr
409     (output-sql modifier database)
410     (write-string " " *sql-stream*)
411     (output-sql (car components) database)
412     (when components 
413       (mapc #'(lambda (comp) 
414                 (write-string ", " *sql-stream*)
415                 (output-sql comp database))
416             (cdr components))))
417   t)
418
419 (defclass sql-set-exp (%sql-expression)
420   ((operator
421     :initarg :operator
422     :initform nil)
423    (sub-expressions
424     :initarg :sub-expressions
425     :initform nil))
426   (:documentation "An SQL set expression."))
427
428 (defmethod collect-table-refs ((sql sql-set-exp))
429   (let ((tabs nil))
430     (dolist (exp (slot-value sql 'sub-expressions))
431       (let ((refs (collect-table-refs exp)))
432         (if refs (setf tabs (append refs tabs)))))
433     (remove-duplicates tabs
434                        :test (lambda (tab1 tab2)
435                                (equal (slot-value tab1 'name)
436                                       (slot-value tab2 'name))))))
437
438 (defmethod output-sql ((expr sql-set-exp) database)
439   (with-slots (operator sub-expressions)
440       expr
441     (let ((subs (if (consp (car sub-expressions))
442                     (car sub-expressions)
443                     sub-expressions)))
444       (when (= (length subs) 1)
445         (output-sql operator database)
446         (write-char #\Space *sql-stream*))
447       (do ((sub subs (cdr sub)))
448           ((null (cdr sub)) (output-sql (car sub) database))
449         (output-sql (car sub) database)
450         (write-char #\Space *sql-stream*)
451         (output-sql operator database)
452         (write-char #\Space *sql-stream*))))
453   t)
454
455 (defclass sql-query (%sql-expression)
456   ((selections
457     :initarg :selections
458     :initform nil)
459    (all
460     :initarg :all
461     :initform nil)
462    (flatp
463     :initarg :flatp
464     :initform nil)
465    (set-operation
466     :initarg :set-operation
467     :initform nil)
468    (distinct
469     :initarg :distinct
470     :initform nil)
471    (from
472     :initarg :from
473     :initform nil)
474    (where
475     :initarg :where
476     :initform nil)
477    (group-by
478     :initarg :group-by
479     :initform nil)
480    (having
481     :initarg :having
482     :initform nil)
483    (limit
484     :initarg :limit
485     :initform nil)
486    (offset
487     :initarg :offset
488     :initform nil)
489    (order-by
490     :initarg :order-by
491     :initform nil)
492    (inner-join
493     :initarg :inner-join
494     :initform nil)
495    (on
496     :initarg :on
497     :initform nil))
498   (:documentation "An SQL SELECT query."))
499
500 (defclass sql-object-query (%sql-expression)
501   ((objects
502     :initarg :objects
503     :initform nil)
504    (flatp
505     :initarg :flatp
506     :initform nil)
507    (exp
508     :initarg :exp
509     :initform nil)
510    (refresh
511     :initarg :refresh
512     :initform nil)))
513
514 (defmethod collect-table-refs ((sql sql-query))
515   (remove-duplicates (collect-table-refs (slot-value sql 'where))
516                      :test (lambda (tab1 tab2)
517                              (equal (slot-value tab1 'name)
518                                     (slot-value tab2 'name)))))
519
520 (defvar *select-arguments*
521   '(:all :database :distinct :flatp :from :group-by :having :order-by
522     :set-operation :where :offset :limit :inner-join :on
523     ;; below keywords are not a SQL argument, but these keywords may terminate select
524     :caching :refresh))
525
526 (defun query-arg-p (sym)
527   (member sym *select-arguments*))
528
529 (defun query-get-selections (select-args)
530   "Return two values: the list of select-args up to the first keyword,
531 uninclusive, and the args from that keyword to the end."
532   (let ((first-key-arg (position-if #'query-arg-p select-args)))
533     (if first-key-arg
534         (values (subseq select-args 0 first-key-arg)
535                 (subseq select-args first-key-arg))
536         select-args)))
537
538 (defun make-query (&rest args)
539   (flet ((select-objects (target-args)
540            (and target-args
541                 (every #'(lambda (arg)
542                            (and (symbolp arg)
543                                 (find-class arg nil)))
544                        target-args))))
545     (multiple-value-bind (selections arglist)
546         (query-get-selections args)
547       (if (select-objects selections) 
548           (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
549             (make-instance 'sql-object-query :objects selections
550                            :flatp flatp :refresh refresh
551                            :exp arglist))
552           (destructuring-bind (&key all flatp set-operation distinct from where
553                                     group-by having order-by 
554                                     offset limit inner-join on &allow-other-keys)
555               arglist
556             (if (null selections)
557                 (error "No target columns supplied to select statement."))
558             (if (null from)
559                 (error "No source tables supplied to select statement."))
560             (make-instance 'sql-query :selections selections
561                            :all all :flatp flatp :set-operation set-operation
562                            :distinct distinct :from from :where where
563                            :limit limit :offset offset
564                            :group-by group-by :having having :order-by order-by
565                            :inner-join inner-join :on on))))))
566
567 (defvar *in-subselect* nil)
568
569 (defmethod output-sql ((query sql-query) database)
570   (with-slots (distinct selections from where group-by having order-by
571                         limit offset inner-join on all set-operation) 
572       query
573     (when *in-subselect*
574       (write-string "(" *sql-stream*))
575     (write-string "SELECT " *sql-stream*)
576     (when all 
577       (write-string "ALL " *sql-stream*))
578     (when (and distinct (not all))
579       (write-string "DISTINCT " *sql-stream*)
580       (unless (eql t distinct)
581         (write-string "ON " *sql-stream*)
582         (output-sql distinct database)
583         (write-char #\Space *sql-stream*)))
584     (output-sql (apply #'vector selections) database)
585     (when from
586       (write-string " FROM " *sql-stream*)
587       (typecase from 
588         (list (output-sql (apply #'vector from) database))
589         (string (write-string 
590                  (sql-escape 
591                   (convert-to-db-default-case from database)) *sql-stream*))
592         (t (output-sql from database))))
593     (when inner-join
594       (write-string " INNER JOIN " *sql-stream*)
595       (output-sql inner-join database))
596     (when on
597       (write-string " ON " *sql-stream*)
598       (output-sql on database))
599     (when where
600       (write-string " WHERE " *sql-stream*)
601       (let ((*in-subselect* t))
602         (output-sql where database)))
603     (when group-by
604       (write-string " GROUP BY " *sql-stream*)
605       (output-sql group-by database))
606     (when having
607       (write-string " HAVING " *sql-stream*)
608       (output-sql having database))
609     (when order-by
610       (write-string " ORDER BY " *sql-stream*)
611       (if (listp order-by)
612           (do ((order order-by (cdr order)))
613               ((null order))
614             (let ((item (car order)))
615               (typecase item 
616                 (cons 
617                  (output-sql (car item) database)
618                  (format *sql-stream* " ~A" (cadr item)))
619                 (t 
620                  (output-sql item database)))
621               (when (cdr order)
622                 (write-char #\, *sql-stream*))))
623           (output-sql order-by database)))
624     (when limit
625       (write-string " LIMIT " *sql-stream*)
626       (output-sql limit database))
627     (when offset
628       (write-string " OFFSET " *sql-stream*)
629       (output-sql offset database))
630     (when *in-subselect*
631       (write-string ")" *sql-stream*))
632     (when set-operation 
633       (write-char #\Space *sql-stream*)
634       (output-sql set-operation database)))
635   t)
636
637 (defmethod output-sql ((query sql-object-query) database)
638   (declare (ignore database))
639   (with-slots (objects)
640       query
641     (when objects
642       (format *sql-stream* "(~{~A~^ ~})" objects))))
643
644
645 ;; INSERT
646
647 (defclass sql-insert (%sql-expression)
648   ((into
649     :initarg :into
650     :initform nil)
651    (attributes
652     :initarg :attributes
653     :initform nil)
654    (values
655     :initarg :values
656     :initform nil)
657    (query
658     :initarg :query
659     :initform nil))
660   (:documentation
661    "An SQL INSERT statement."))
662
663 (defmethod output-sql ((ins sql-insert) database)
664   (with-slots (into attributes values query)
665     ins
666     (write-string "INSERT INTO " *sql-stream*)
667     (output-sql into database)
668     (when attributes
669       (write-char #\Space *sql-stream*)
670       (output-sql attributes database))
671     (when values
672       (write-string " VALUES " *sql-stream*)
673       (output-sql values database))
674     (when query
675       (write-char #\Space *sql-stream*)
676       (output-sql query database)))
677   t)
678
679 ;; DELETE
680
681 (defclass sql-delete (%sql-expression)
682   ((from
683     :initarg :from
684     :initform nil)
685    (where
686     :initarg :where
687     :initform nil))
688   (:documentation
689    "An SQL DELETE statement."))
690
691 (defmethod output-sql ((stmt sql-delete) database)
692   (with-slots (from where)
693     stmt
694     (write-string "DELETE FROM " *sql-stream*)
695     (typecase from
696       (symbol (write-string (sql-escape from) *sql-stream*))
697       (t  (output-sql from database)))
698     (when where
699       (write-string " WHERE " *sql-stream*)
700       (output-sql where database)))
701   t)
702
703 ;; UPDATE
704
705 (defclass sql-update (%sql-expression)
706   ((table
707     :initarg :table
708     :initform nil)
709    (attributes
710     :initarg :attributes
711     :initform nil)
712    (values
713     :initarg :values
714     :initform nil)
715    (where
716     :initarg :where
717     :initform nil))
718   (:documentation "An SQL UPDATE statement."))
719
720 (defmethod output-sql ((expr sql-update) database)
721   (with-slots (table where attributes values)
722     expr
723     (flet ((update-assignments ()
724              (mapcar #'(lambda (a b)
725                          (make-instance 'sql-assignment-exp
726                                         :operator '=
727                                         :sub-expressions (list a b)))
728                      attributes values)))
729       (write-string "UPDATE " *sql-stream*)
730       (output-sql table database)
731       (write-string " SET " *sql-stream*)
732       (output-sql (apply #'vector (update-assignments)) database)
733       (when where
734         (write-string " WHERE " *sql-stream*)
735         (output-sql where database))))
736   t)
737
738 ;; CREATE TABLE
739
740 (defclass sql-create-table (%sql-expression)
741   ((name
742     :initarg :name
743     :initform nil)
744    (columns
745     :initarg :columns
746     :initform nil)
747    (modifiers
748     :initarg :modifiers
749     :initform nil)
750    (transactions
751     :initarg :transactions
752     :initform nil))
753   (:documentation
754    "An SQL CREATE TABLE statement."))
755
756 ;; Here's a real warhorse of a function!
757
758 (declaim (inline listify))
759 (defun listify (x)
760   (if (atom x)
761       (list x)
762       x))
763
764 (defmethod output-sql ((stmt sql-create-table) database)
765   (flet ((output-column (column-spec)
766            (destructuring-bind (name type &optional db-type &rest constraints)
767                column-spec
768              (let ((type (listify type)))
769                (output-sql name database)
770                (write-char #\Space *sql-stream*)
771                (write-string
772                 (if (stringp db-type) db-type ; override definition
773                     (database-get-type-specifier (car type) (cdr type) database))
774                 *sql-stream*)
775                (let ((constraints (database-constraint-statement  
776                                    (if (and db-type (symbolp db-type))
777                                        (cons db-type constraints)
778                                        constraints)
779                                    database)))
780                  (when constraints
781                    (write-string " " *sql-stream*)
782                    (write-string constraints *sql-stream*)))))))
783     (with-slots (name columns modifiers transactions)
784       stmt
785       (write-string "CREATE TABLE " *sql-stream*)
786       (output-sql name database)
787       (write-string " (" *sql-stream*)
788       (do ((column columns (cdr column)))
789           ((null (cdr column))
790            (output-column (car column)))
791         (output-column (car column))
792         (write-string ", " *sql-stream*))
793       (when modifiers
794         (do ((modifier (listify modifiers) (cdr modifier)))
795             ((null modifier))
796           (write-string ", " *sql-stream*)
797           (write-string (car modifier) *sql-stream*)))
798       (write-char #\) *sql-stream*)
799       (when (and (eq :mysql (database-underlying-type database))
800                  transactions
801                  (db-type-transaction-capable? :mysql database))
802         (write-string " Type=InnoDB" *sql-stream*)))) 
803   t)
804
805
806 ;; CREATE VIEW
807
808 (defclass sql-create-view (%sql-expression)
809   ((name :initarg :name :initform nil)
810    (column-list :initarg :column-list :initform nil)
811    (query :initarg :query :initform nil)
812    (with-check-option :initarg :with-check-option :initform nil))
813   (:documentation "An SQL CREATE VIEW statement."))
814
815 (defmethod output-sql ((stmt sql-create-view) database)
816   (with-slots (name column-list query with-check-option) stmt
817     (write-string "CREATE VIEW " *sql-stream*)
818     (output-sql name database)
819     (when column-list (write-string " " *sql-stream*)
820           (output-sql (listify column-list) database))
821     (write-string " AS " *sql-stream*)
822     (output-sql query database)
823     (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
824
825
826 ;;
827 ;; Column constraint types
828 ;;
829 (defparameter *constraint-types*
830   (list 
831    (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") 
832    (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")
833    (cons (symbol-name-default-case "NOT") "NOT") 
834    (cons (symbol-name-default-case "NULL") "NULL") 
835    (cons (symbol-name-default-case "PRIMARY") "PRIMARY") 
836    (cons (symbol-name-default-case "KEY") "KEY")))
837
838 ;;
839 ;; Convert type spec to sql syntax
840 ;;
841
842 (defmethod database-constraint-description (constraint database)
843   (declare (ignore database))
844   (let ((output (assoc (symbol-name constraint) *constraint-types*
845                        :test #'equal)))
846     (if (null output)
847         (error 'sql-user-error
848                :message (format nil "unsupported column constraint '~A'"
849                                 constraint))
850         (cdr output))))
851
852 (defmethod database-constraint-statement (constraint-list database)
853   (declare (ignore database))
854   (make-constraints-description constraint-list))
855   
856 (defun make-constraints-description (constraint-list)
857   (if constraint-list
858       (let ((string ""))
859         (do ((constraint constraint-list (cdr constraint)))
860             ((null constraint) string)
861           (let ((output (assoc (symbol-name (car constraint))
862                                *constraint-types*
863                                :test #'equal)))
864             (if (null output)
865                 (error 'sql-user-error
866                        :message (format nil "unsupported column constraint '~A'"
867                                         constraint))
868                 (setq string (concatenate 'string string (cdr output))))
869             (if (< 1 (length constraint))
870                 (setq string (concatenate 'string string " "))))))))
871