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