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