4e697caf0cfb7a51cfd402682d8d35dba733da1e
[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       (typecase from 
583         (list (output-sql (apply #'vector from) database))
584         (string (write-string from *sql-stream*))
585         (t (output-sql from database))))
586     (when inner-join
587       (write-string " INNER JOIN " *sql-stream*)
588       (output-sql inner-join database))
589     (when on
590       (write-string " ON " *sql-stream*)
591       (output-sql on database))
592     (when where
593       (write-string " WHERE " *sql-stream*)
594       (let ((*in-subselect* t))
595         (output-sql where database)))
596     (when group-by
597       (write-string " GROUP BY " *sql-stream*)
598       (output-sql group-by database))
599     (when having
600       (write-string " HAVING " *sql-stream*)
601       (output-sql having database))
602     (when order-by
603       (write-string " ORDER BY " *sql-stream*)
604       (if (listp order-by)
605           (do ((order order-by (cdr order)))
606               ((null order))
607             (output-sql (car order) database)
608             (when (cdr order)
609               (write-char #\, *sql-stream*)))
610           (output-sql order-by database)))
611     (when order-by-descending
612       (write-string " ORDER BY " *sql-stream*)
613       (if (listp order-by-descending)
614           (do ((order order-by-descending (cdr order)))
615               ((null order))
616             (output-sql (car order) database)
617             (when (cdr order)
618               (write-char #\, *sql-stream*)))
619           (output-sql order-by-descending database))
620       (write-string " DESC " *sql-stream*))
621     (when limit
622       (write-string " LIMIT " *sql-stream*)
623       (output-sql limit database))
624     (when offset
625       (write-string " OFFSET " *sql-stream*)
626       (output-sql offset database))
627     (when *in-subselect*
628       (write-string ")" *sql-stream*)))
629   t)
630
631 (defmethod output-sql ((query sql-object-query) database)
632   (declare (ignore database))
633   (with-slots (objects)
634       query
635     (when objects
636       (format *sql-stream* "(~{~A~^ ~})" objects))))
637
638
639 ;; INSERT
640
641 (defclass sql-insert (%sql-expression)
642   ((into
643     :initarg :into
644     :initform nil)
645    (attributes
646     :initarg :attributes
647     :initform nil)
648    (values
649     :initarg :values
650     :initform nil)
651    (query
652     :initarg :query
653     :initform nil))
654   (:documentation
655    "An SQL INSERT statement."))
656
657 (defmethod output-sql ((ins sql-insert) database)
658   (with-slots (into attributes values query)
659     ins
660     (write-string "INSERT INTO " *sql-stream*)
661     (output-sql into database)
662     (when attributes
663       (write-char #\Space *sql-stream*)
664       (output-sql attributes database))
665     (when values
666       (write-string " VALUES " *sql-stream*)
667       (output-sql values database))
668     (when query
669       (write-char #\Space *sql-stream*)
670       (output-sql query database)))
671   t)
672
673 ;; DELETE
674
675 (defclass sql-delete (%sql-expression)
676   ((from
677     :initarg :from
678     :initform nil)
679    (where
680     :initarg :where
681     :initform nil))
682   (:documentation
683    "An SQL DELETE statement."))
684
685 (defmethod output-sql ((stmt sql-delete) database)
686   (with-slots (from where)
687     stmt
688     (write-string "DELETE FROM " *sql-stream*)
689     (typecase from
690       (symbol (write-string (sql-escape from) *sql-stream*))
691       (t  (output-sql from database)))
692     (when where
693       (write-string " WHERE " *sql-stream*)
694       (output-sql where database)))
695   t)
696
697 ;; UPDATE
698
699 (defclass sql-update (%sql-expression)
700   ((table
701     :initarg :table
702     :initform nil)
703    (attributes
704     :initarg :attributes
705     :initform nil)
706    (values
707     :initarg :values
708     :initform nil)
709    (where
710     :initarg :where
711     :initform nil))
712   (:documentation "An SQL UPDATE statement."))
713
714 (defmethod output-sql ((expr sql-update) database)
715   (with-slots (table where attributes values)
716     expr
717     (flet ((update-assignments ()
718              (mapcar #'(lambda (a b)
719                          (make-instance 'sql-assignment-exp
720                                         :operator '=
721                                         :sub-expressions (list a b)))
722                      attributes values)))
723       (write-string "UPDATE " *sql-stream*)
724       (output-sql table database)
725       (write-string " SET " *sql-stream*)
726       (output-sql (apply #'vector (update-assignments)) database)
727       (when where
728         (write-string " WHERE " *sql-stream*)
729         (output-sql where database))))
730   t)
731
732 ;; CREATE TABLE
733
734 (defclass sql-create-table (%sql-expression)
735   ((name
736     :initarg :name
737     :initform nil)
738    (columns
739     :initarg :columns
740     :initform nil)
741    (modifiers
742     :initarg :modifiers
743     :initform nil)
744    (transactions
745     :initarg :transactions
746     :initform nil))
747   (:documentation
748    "An SQL CREATE TABLE statement."))
749
750 ;; Here's a real warhorse of a function!
751
752 (declaim (inline listify))
753 (defun listify (x)
754   (if (atom x)
755       (list x)
756       x))
757
758 (defmethod output-sql ((stmt sql-create-table) database)
759   (flet ((output-column (column-spec)
760            (destructuring-bind (name type &optional db-type &rest constraints)
761                column-spec
762              (let ((type (listify type)))
763                (output-sql name database)
764                (write-char #\Space *sql-stream*)
765                (write-string
766                 (if (stringp db-type) db-type ; override definition
767                     (database-get-type-specifier (car type) (cdr type) database))
768                 *sql-stream*)
769                (let ((constraints
770                       (database-constraint-statement constraints database)))
771                  (when constraints
772                    (write-string " " *sql-stream*)
773                    (write-string constraints *sql-stream*)))))))
774     (with-slots (name columns modifiers transactions)
775       stmt
776       (write-string "CREATE TABLE " *sql-stream*)
777       (output-sql name database)
778       (write-string " (" *sql-stream*)
779       (do ((column columns (cdr column)))
780           ((null (cdr column))
781            (output-column (car column)))
782         (output-column (car column))
783         (write-string ", " *sql-stream*))
784       (when modifiers
785         (do ((modifier (listify modifiers) (cdr modifier)))
786             ((null modifier))
787           (write-string ", " *sql-stream*)
788           (write-string (car modifier) *sql-stream*)))
789       (write-char #\) *sql-stream*)
790       (when (and (eq :mysql (database-underlying-type database))
791                  transactions
792                  (db-type-transaction-capable? :mysql database))
793         (write-string " Type=InnoDB" *sql-stream*)))) 
794   t)
795
796
797 ;; CREATE VIEW
798
799 (defclass sql-create-view (%sql-expression)
800   ((name :initarg :name :initform nil)
801    (column-list :initarg :column-list :initform nil)
802    (query :initarg :query :initform nil)
803    (with-check-option :initarg :with-check-option :initform nil))
804   (:documentation "An SQL CREATE VIEW statement."))
805
806 (defmethod output-sql ((stmt sql-create-view) database)
807   (with-slots (name column-list query with-check-option) stmt
808     (write-string "CREATE VIEW " *sql-stream*)
809     (output-sql name database)
810     (when column-list (write-string " " *sql-stream*)
811           (output-sql (listify column-list) database))
812     (write-string " AS " *sql-stream*)
813     (output-sql query database)
814     (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
815
816
817 ;;
818 ;; Column constraint types
819 ;;
820 (defparameter *constraint-types*
821   (list 
822    (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") 
823    (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")))
824
825 ;;
826 ;; Convert type spec to sql syntax
827 ;;
828
829 (defmethod database-constraint-description (constraint database)
830   (declare (ignore database))
831   (let ((output (assoc (symbol-name constraint) *constraint-types*
832                        :test #'equal)))
833     (if (null output)
834         (error 'clsql-sql-syntax-error
835                :reason (format nil "unsupported column constraint '~a'"
836                                constraint))
837         (cdr output))))
838
839 (defmethod database-constraint-statement (constraint-list database)
840   (declare (ignore database))
841   (make-constraints-description constraint-list))
842   
843 (defun make-constraints-description (constraint-list)
844   (if constraint-list
845       (let ((string ""))
846         (do ((constraint constraint-list (cdr constraint)))
847             ((null constraint) string)
848           (let ((output (assoc (symbol-name (car constraint))
849                                *constraint-types*
850                                :test #'equal)))
851             (if (null output)
852                 (error 'clsql-sql-syntax-error
853                        :reason (format nil "unsupported column constraint '~a'"
854                                        constraint))
855                 (setq string (concatenate 'string string (cdr output))))
856             (if (< 1 (length constraint))
857                 (setq string (concatenate 'string string " "))))))))
858