r9289: Added new operations for the sql syntax.
[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)
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
520 (defun query-arg-p (sym)
521   (member sym *select-arguments*))
522
523 (defun query-get-selections (select-args)
524   "Return two values: the list of select-args up to the first keyword,
525 uninclusive, and the args from that keyword to the end."
526   (let ((first-key-arg (position-if #'query-arg-p select-args)))
527     (if first-key-arg
528         (values (subseq select-args 0 first-key-arg)
529                 (subseq select-args first-key-arg))
530         select-args)))
531
532 (defun make-query (&rest args)
533   (flet ((select-objects (target-args)
534            (and target-args
535                 (every #'(lambda (arg)
536                            (and (symbolp arg)
537                                 (find-class arg nil)))
538                        target-args))))
539     (multiple-value-bind (selections arglist)
540         (query-get-selections args)
541       (if (select-objects selections) 
542           (destructuring-bind (&key flatp refresh &allow-other-keys) arglist
543             (make-instance 'sql-object-query :objects selections
544                            :flatp flatp :refresh refresh
545                            :exp arglist))
546           (destructuring-bind (&key all flatp set-operation distinct from where
547                                     group-by having order-by order-by-descending
548                                     offset limit inner-join on &allow-other-keys)
549               arglist
550             (if (null selections)
551                 (error "No target columns supplied to select statement."))
552             (if (null from)
553                 (error "No source tables supplied to select statement."))
554             (make-instance 'sql-query :selections selections
555                            :all all :flatp flatp :set-operation set-operation
556                            :distinct distinct :from from :where where
557                            :limit limit :offset offset
558                            :group-by group-by :having having :order-by order-by
559                            :order-by-descending order-by-descending
560                            :inner-join inner-join :on on))))))
561
562 (defvar *in-subselect* nil)
563
564 (defmethod output-sql ((query sql-query) database)
565   (with-slots (distinct selections from where group-by having order-by
566                         order-by-descending limit offset inner-join on)
567       query
568     (when *in-subselect*
569       (write-string "(" *sql-stream*))
570     (write-string "SELECT " *sql-stream*)
571     (when distinct
572       (write-string "DISTINCT " *sql-stream*)
573       (unless (eql t distinct)
574         (write-string "ON " *sql-stream*)
575         (output-sql distinct database)
576         (write-char #\Space *sql-stream*)))
577     (output-sql (apply #'vector selections) database)
578     (when from
579       (write-string " FROM " *sql-stream*)
580       (if (listp from)
581           (output-sql (apply #'vector from) database)
582         (output-sql from database)))
583     (when inner-join
584       (write-string " INNER JOIN " *sql-stream*)
585       (output-sql inner-join database))
586     (when on
587       (write-string " ON " *sql-stream*)
588       (output-sql on database))
589     (when where
590       (write-string " WHERE " *sql-stream*)
591       (let ((*in-subselect* t))
592         (output-sql where database)))
593     (when group-by
594       (write-string " GROUP BY " *sql-stream*)
595       (output-sql group-by database))
596     (when having
597       (write-string " HAVING " *sql-stream*)
598       (output-sql having database))
599     (when order-by
600       (write-string " ORDER BY " *sql-stream*)
601       (if (listp order-by)
602           (do ((order order-by (cdr order)))
603               ((null order))
604             (output-sql (car order) database)
605             (when (cdr order)
606               (write-char #\, *sql-stream*)))
607           (output-sql order-by database)))
608     (when order-by-descending
609       (write-string " ORDER BY " *sql-stream*)
610       (if (listp order-by-descending)
611           (do ((order order-by-descending (cdr order)))
612               ((null order))
613             (output-sql (car order) database)
614             (when (cdr order)
615               (write-char #\, *sql-stream*)))
616           (output-sql order-by-descending database))
617       (write-string " DESC " *sql-stream*))
618     (when limit
619       (write-string " LIMIT " *sql-stream*)
620       (output-sql limit database))
621     (when offset
622       (write-string " OFFSET " *sql-stream*)
623       (output-sql offset database))
624     (when *in-subselect*
625       (write-string ")" *sql-stream*)))
626   t)
627
628 (defmethod output-sql ((query sql-object-query) database)
629   (with-slots (objects)
630       query
631     (when objects
632       (format *sql-stream* "(~{~A~^ ~})" objects))))
633
634
635 ;; INSERT
636
637 (defclass sql-insert (%sql-expression)
638   ((into
639     :initarg :into
640     :initform nil)
641    (attributes
642     :initarg :attributes
643     :initform nil)
644    (values
645     :initarg :values
646     :initform nil)
647    (query
648     :initarg :query
649     :initform nil))
650   (:documentation
651    "An SQL INSERT statement."))
652
653 (defmethod output-sql ((ins sql-insert) database)
654   (with-slots (into attributes values query)
655     ins
656     (write-string "INSERT INTO " *sql-stream*)
657     (output-sql into database)
658     (when attributes
659       (write-char #\Space *sql-stream*)
660       (output-sql attributes database))
661     (when values
662       (write-string " VALUES " *sql-stream*)
663       (output-sql values database))
664     (when query
665       (write-char #\Space *sql-stream*)
666       (output-sql query database)))
667   t)
668
669 ;; DELETE
670
671 (defclass sql-delete (%sql-expression)
672   ((from
673     :initarg :from
674     :initform nil)
675    (where
676     :initarg :where
677     :initform nil))
678   (:documentation
679    "An SQL DELETE statement."))
680
681 (defmethod output-sql ((stmt sql-delete) database)
682   (with-slots (from where)
683     stmt
684     (write-string "DELETE FROM " *sql-stream*)
685     (typecase from
686       (symbol (write-string (sql-escape from) *sql-stream*))
687       (t  (output-sql from database)))
688     (when where
689       (write-string " WHERE " *sql-stream*)
690       (output-sql where database)))
691   t)
692
693 ;; UPDATE
694
695 (defclass sql-update (%sql-expression)
696   ((table
697     :initarg :table
698     :initform nil)
699    (attributes
700     :initarg :attributes
701     :initform nil)
702    (values
703     :initarg :values
704     :initform nil)
705    (where
706     :initarg :where
707     :initform nil))
708   (:documentation "An SQL UPDATE statement."))
709
710 (defmethod output-sql ((expr sql-update) database)
711   (with-slots (table where attributes values)
712     expr
713     (flet ((update-assignments ()
714              (mapcar #'(lambda (a b)
715                          (make-instance 'sql-assignment-exp
716                                         :operator '=
717                                         :sub-expressions (list a b)))
718                      attributes values)))
719       (write-string "UPDATE " *sql-stream*)
720       (output-sql table database)
721       (write-string " SET " *sql-stream*)
722       (output-sql (apply #'vector (update-assignments)) database)
723       (when where
724         (write-string " WHERE " *sql-stream*)
725         (output-sql where database))))
726   t)
727
728 ;; CREATE TABLE
729
730 (defclass sql-create-table (%sql-expression)
731   ((name
732     :initarg :name
733     :initform nil)
734    (columns
735     :initarg :columns
736     :initform nil)
737    (modifiers
738     :initarg :modifiers
739     :initform nil)
740    (transactions
741     :initarg :transactions
742     :initform nil))
743   (:documentation
744    "An SQL CREATE TABLE statement."))
745
746 ;; Here's a real warhorse of a function!
747
748 (declaim (inline listify))
749 (defun listify (x)
750   (if (atom x)
751       (list x)
752       x))
753
754 (defmethod output-sql ((stmt sql-create-table) database)
755   (flet ((output-column (column-spec)
756            (destructuring-bind (name type &optional db-type &rest constraints)
757                column-spec
758              (let ((type (listify type)))
759                (output-sql name database)
760                (write-char #\Space *sql-stream*)
761                (write-string
762                 (if (stringp db-type) db-type ; override definition
763                     (database-get-type-specifier (car type) (cdr type) database))
764                 *sql-stream*)
765                (let ((constraints
766                       (database-constraint-statement constraints database)))
767                  (when constraints
768                    (write-string " " *sql-stream*)
769                    (write-string constraints *sql-stream*)))))))
770     (with-slots (name columns modifiers transactions)
771       stmt
772       (write-string "CREATE TABLE " *sql-stream*)
773       (output-sql name database)
774       (write-string " (" *sql-stream*)
775       (do ((column columns (cdr column)))
776           ((null (cdr column))
777            (output-column (car column)))
778         (output-column (car column))
779         (write-string ", " *sql-stream*))
780       (when modifiers
781         (do ((modifier (listify modifiers) (cdr modifier)))
782             ((null modifier))
783           (write-string ", " *sql-stream*)
784           (write-string (car modifier) *sql-stream*)))
785       (write-char #\) *sql-stream*)
786       (when (and (eq :mysql (database-underlying-type database))
787                  transactions
788                  (db-type-transaction-capable? :mysql database))
789         (write-string " Type=InnoDB" *sql-stream*)))) 
790   t)
791
792
793 ;; CREATE VIEW
794
795 (defclass sql-create-view (%sql-expression)
796   ((name :initarg :name :initform nil)
797    (column-list :initarg :column-list :initform nil)
798    (query :initarg :query :initform nil)
799    (with-check-option :initarg :with-check-option :initform nil))
800   (:documentation "An SQL CREATE VIEW statement."))
801
802 (defmethod output-sql ((stmt sql-create-view) database)
803   (with-slots (name column-list query with-check-option) stmt
804     (write-string "CREATE VIEW " *sql-stream*)
805     (output-sql name database)
806     (when column-list (write-string " " *sql-stream*)
807           (output-sql (listify column-list) database))
808     (write-string " AS " *sql-stream*)
809     (output-sql query database)
810     (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
811
812
813 ;;
814 ;; Column constraint types
815 ;;
816 (defparameter *constraint-types*
817   (list 
818    (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") 
819    (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")))
820
821 ;;
822 ;; Convert type spec to sql syntax
823 ;;
824
825 (defmethod database-constraint-description (constraint database)
826   (declare (ignore database))
827   (let ((output (assoc (symbol-name constraint) *constraint-types*
828                        :test #'equal)))
829     (if (null output)
830         (error 'clsql-sql-syntax-error
831                :reason (format nil "unsupported column constraint '~a'"
832                                constraint))
833         (cdr output))))
834
835 (defmethod database-constraint-statement (constraint-list database)
836   (declare (ignore database))
837   (make-constraints-description constraint-list))
838   
839 (defun make-constraints-description (constraint-list)
840   (if constraint-list
841       (let ((string ""))
842         (do ((constraint constraint-list (cdr constraint)))
843             ((null constraint) string)
844           (let ((output (assoc (symbol-name (car constraint))
845                                *constraint-types*
846                                :test #'equal)))
847             (if (null output)
848                 (error 'clsql-sql-syntax-error
849                        :reason (format nil "unsupported column constraint '~a'"
850                                        constraint))
851                 (setq string (concatenate 'string string (cdr output))))
852             (if (< 1 (length constraint))
853                 (setq string (concatenate 'string string " "))))))))
854