r9250: make :target-slot joins many times more efficient
[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 (defclass sql-query (%sql-expression)
382   ((selections
383     :initarg :selections
384     :initform nil)
385    (all
386     :initarg :all
387     :initform nil)
388    (flatp
389     :initarg :flatp
390     :initform nil)
391    (set-operation
392     :initarg :set-operation
393     :initform nil)
394    (distinct
395     :initarg :distinct
396     :initform nil)
397    (from
398     :initarg :from
399     :initform nil)
400    (where
401     :initarg :where
402     :initform nil)
403    (group-by
404     :initarg :group-by
405     :initform nil)
406    (having
407     :initarg :having
408     :initform nil)
409    (limit
410     :initarg :limit
411     :initform nil)
412    (offset
413     :initarg :offset
414     :initform nil)
415    (order-by
416     :initarg :order-by
417     :initform nil)
418    (order-by-descending
419     :initarg :order-by-descending
420     :initform nil)
421    (inner-join
422     :initarg :inner-join
423     :initform nil)
424    (on
425     :initarg :on
426     :initform nil))
427   (:documentation "An SQL SELECT query."))
428
429 (defmethod collect-table-refs ((sql sql-query))
430   (remove-duplicates (collect-table-refs (slot-value sql 'where))
431                      :test (lambda (tab1 tab2)
432                              (equal (slot-value tab1 'name)
433                                     (slot-value tab2 'name)))))
434
435 (defvar *select-arguments*
436   '(:all :database :distinct :flatp :from :group-by :having :order-by
437     :order-by-descending :set-operation :where :offset :limit
438     :inner-join :on))
439
440 (defun query-arg-p (sym)
441   (member sym *select-arguments*))
442
443 (defun query-get-selections (select-args)
444   "Return two values: the list of select-args up to the first keyword,
445 uninclusive, and the args from that keyword to the end."
446   (let ((first-key-arg (position-if #'query-arg-p select-args)))
447     (if first-key-arg
448         (values (subseq select-args 0 first-key-arg)
449                 (subseq select-args first-key-arg))
450         select-args)))
451
452 (defun make-query (&rest args)
453   (flet ((select-objects (target-args)
454            (and target-args
455                 (every #'(lambda (arg)
456                            (and (symbolp arg)
457                                 (find-class arg nil)))
458                        target-args))))
459     (multiple-value-bind (selections arglist)
460         (query-get-selections args)
461       (if (select-objects selections) 
462           (apply #'select args)
463           (destructuring-bind (&key all flatp set-operation distinct from where
464                                     group-by having order-by order-by-descending
465                                     offset limit inner-join on &allow-other-keys)
466               arglist
467             (if (null selections)
468                 (error "No target columns supplied to select statement."))
469             (if (null from)
470                 (error "No source tables supplied to select statement."))
471             (make-instance 'sql-query :selections selections
472                            :all all :flatp flatp :set-operation set-operation
473                            :distinct distinct :from from :where where
474                            :limit limit :offset offset
475                            :group-by group-by :having having :order-by order-by
476                            :order-by-descending order-by-descending
477                            :inner-join inner-join :on on))))))
478
479 (defvar *in-subselect* nil)
480
481 (defmethod output-sql ((query sql-query) database)
482   (with-slots (distinct selections from where group-by having order-by
483                         order-by-descending limit offset inner-join on)
484       query
485     (when *in-subselect*
486       (write-string "(" *sql-stream*))
487     (write-string "SELECT " *sql-stream*)
488     (when distinct
489       (write-string "DISTINCT " *sql-stream*)
490       (unless (eql t distinct)
491         (write-string "ON " *sql-stream*)
492         (output-sql distinct database)
493         (write-char #\Space *sql-stream*)))
494     (output-sql (apply #'vector selections) database)
495     (when from
496       (write-string " FROM " *sql-stream*)
497       (if (listp from)
498           (output-sql (apply #'vector from) database)
499         (output-sql from database)))
500     (when inner-join
501       (write-string " INNER JOIN " *sql-stream*)
502       (output-sql inner-join database))
503     (when on
504       (write-string " ON " *sql-stream*)
505       (output-sql on database))
506     (when where
507       (write-string " WHERE " *sql-stream*)
508       (let ((*in-subselect* t))
509         (output-sql where database)))
510     (when group-by
511       (write-string " GROUP BY " *sql-stream*)
512       (output-sql group-by database))
513     (when having
514       (write-string " HAVING " *sql-stream*)
515       (output-sql having database))
516     (when order-by
517       (write-string " ORDER BY " *sql-stream*)
518       (if (listp order-by)
519           (do ((order order-by (cdr order)))
520               ((null order))
521             (output-sql (car order) database)
522             (when (cdr order)
523               (write-char #\, *sql-stream*)))
524           (output-sql order-by database)))
525     (when order-by-descending
526       (write-string " ORDER BY " *sql-stream*)
527       (if (listp order-by-descending)
528           (do ((order order-by-descending (cdr order)))
529               ((null order))
530             (output-sql (car order) database)
531             (when (cdr order)
532               (write-char #\, *sql-stream*)))
533           (output-sql order-by-descending database))
534       (write-string " DESC " *sql-stream*))
535     (when limit
536       (write-string " LIMIT " *sql-stream*)
537       (output-sql limit database))
538     (when offset
539       (write-string " OFFSET " *sql-stream*)
540       (output-sql offset database))
541     (when *in-subselect*
542       (write-string ")" *sql-stream*)))
543   t)
544
545 ;; INSERT
546
547 (defclass sql-insert (%sql-expression)
548   ((into
549     :initarg :into
550     :initform nil)
551    (attributes
552     :initarg :attributes
553     :initform nil)
554    (values
555     :initarg :values
556     :initform nil)
557    (query
558     :initarg :query
559     :initform nil))
560   (:documentation
561    "An SQL INSERT statement."))
562
563 (defmethod output-sql ((ins sql-insert) database)
564   (with-slots (into attributes values query)
565     ins
566     (write-string "INSERT INTO " *sql-stream*)
567     (output-sql into database)
568     (when attributes
569       (write-char #\Space *sql-stream*)
570       (output-sql attributes database))
571     (when values
572       (write-string " VALUES " *sql-stream*)
573       (output-sql values database))
574     (when query
575       (write-char #\Space *sql-stream*)
576       (output-sql query database)))
577   t)
578
579 ;; DELETE
580
581 (defclass sql-delete (%sql-expression)
582   ((from
583     :initarg :from
584     :initform nil)
585    (where
586     :initarg :where
587     :initform nil))
588   (:documentation
589    "An SQL DELETE statement."))
590
591 (defmethod output-sql ((stmt sql-delete) database)
592   (with-slots (from where)
593     stmt
594     (write-string "DELETE FROM " *sql-stream*)
595     (typecase from
596       (symbol (write-string (sql-escape from) *sql-stream*))
597       (t  (output-sql from database)))
598     (when where
599       (write-string " WHERE " *sql-stream*)
600       (output-sql where database)))
601   t)
602
603 ;; UPDATE
604
605 (defclass sql-update (%sql-expression)
606   ((table
607     :initarg :table
608     :initform nil)
609    (attributes
610     :initarg :attributes
611     :initform nil)
612    (values
613     :initarg :values
614     :initform nil)
615    (where
616     :initarg :where
617     :initform nil))
618   (:documentation "An SQL UPDATE statement."))
619
620 (defmethod output-sql ((expr sql-update) database)
621   (with-slots (table where attributes values)
622     expr
623     (flet ((update-assignments ()
624              (mapcar #'(lambda (a b)
625                          (make-instance 'sql-assignment-exp
626                                         :operator '=
627                                         :sub-expressions (list a b)))
628                      attributes values)))
629       (write-string "UPDATE " *sql-stream*)
630       (output-sql table database)
631       (write-string " SET " *sql-stream*)
632       (output-sql (apply #'vector (update-assignments)) database)
633       (when where
634         (write-string " WHERE " *sql-stream*)
635         (output-sql where database))))
636   t)
637
638 ;; CREATE TABLE
639
640 (defclass sql-create-table (%sql-expression)
641   ((name
642     :initarg :name
643     :initform nil)
644    (columns
645     :initarg :columns
646     :initform nil)
647    (modifiers
648     :initarg :modifiers
649     :initform nil)
650    (transactions
651     :initarg :transactions
652     :initform nil))
653   (:documentation
654    "An SQL CREATE TABLE statement."))
655
656 ;; Here's a real warhorse of a function!
657
658 (defun listify (x)
659   (if (atom x)
660       (list x)
661       x))
662
663 (defmethod output-sql ((stmt sql-create-table) database)
664   (flet ((output-column (column-spec)
665            (destructuring-bind (name type &optional db-type &rest constraints)
666                column-spec
667              (let ((type (listify type)))
668                (output-sql name database)
669                (write-char #\Space *sql-stream*)
670                (write-string
671                 (if (stringp db-type) db-type ; override definition
672                     (database-get-type-specifier (car type) (cdr type) database))
673                 *sql-stream*)
674                (let ((constraints
675                       (database-constraint-statement constraints database)))
676                  (when constraints
677                    (write-string " " *sql-stream*)
678                    (write-string constraints *sql-stream*)))))))
679     (with-slots (name columns modifiers transactions)
680       stmt
681       (write-string "CREATE TABLE " *sql-stream*)
682       (output-sql name database)
683       (write-string " (" *sql-stream*)
684       (do ((column columns (cdr column)))
685           ((null (cdr column))
686            (output-column (car column)))
687         (output-column (car column))
688         (write-string ", " *sql-stream*))
689       (when modifiers
690         (do ((modifier (listify modifiers) (cdr modifier)))
691             ((null modifier))
692           (write-string ", " *sql-stream*)
693           (write-string (car modifier) *sql-stream*)))
694       (write-char #\) *sql-stream*)
695       (when (and (eq :mysql (database-underlying-type database))
696                  transactions
697                  (db-type-transaction-capable? :mysql database))
698         (write-string " Type=InnoDB" *sql-stream*)))) 
699   t)
700
701
702 ;; CREATE VIEW
703
704 (defclass sql-create-view (%sql-expression)
705   ((name :initarg :name :initform nil)
706    (column-list :initarg :column-list :initform nil)
707    (query :initarg :query :initform nil)
708    (with-check-option :initarg :with-check-option :initform nil))
709   (:documentation "An SQL CREATE VIEW statement."))
710
711 (defmethod output-sql ((stmt sql-create-view) database)
712   (with-slots (name column-list query with-check-option) stmt
713     (write-string "CREATE VIEW " *sql-stream*)
714     (output-sql name database)
715     (when column-list (write-string " " *sql-stream*)
716           (output-sql (listify column-list) database))
717     (write-string " AS " *sql-stream*)
718     (output-sql query database)
719     (when with-check-option (write-string " WITH CHECK OPTION" *sql-stream*))))
720
721
722 ;;
723 ;; Column constraint types
724 ;;
725 (defparameter *constraint-types*
726   (list 
727    (cons (symbol-name-default-case "NOT-NULL") "NOT NULL") 
728    (cons (symbol-name-default-case "PRIMARY-KEY") "PRIMARY KEY")))
729
730 ;;
731 ;; Convert type spec to sql syntax
732 ;;
733
734 (defmethod database-constraint-description (constraint database)
735   (declare (ignore database))
736   (let ((output (assoc (symbol-name constraint) *constraint-types*
737                        :test #'equal)))
738     (if (null output)
739         (error 'clsql-sql-syntax-error
740                :reason (format nil "unsupported column constraint '~a'"
741                                constraint))
742         (cdr output))))
743
744 (defmethod database-constraint-statement (constraint-list database)
745   (declare (ignore database))
746   (make-constraints-description constraint-list))
747   
748 (defun make-constraints-description (constraint-list)
749   (if constraint-list
750       (let ((string ""))
751         (do ((constraint constraint-list (cdr constraint)))
752             ((null constraint) string)
753           (let ((output (assoc (symbol-name (car constraint))
754                                *constraint-types*
755                                :test #'equal)))
756             (if (null output)
757                 (error 'clsql-sql-syntax-error
758                        :reason (format nil "unsupported column constraint '~a'"
759                                        constraint))
760                 (setq string (concatenate 'string string (cdr output))))
761             (if (< 1 (length constraint))
762                 (setq string (concatenate 'string string " "))))))))
763