1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; The CLSQL Functional Data Manipulation Language (FDML).
8 ;;;; This file is part of CLSQL.
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
15 (in-package #:clsql-sys)
17 ;;; Basic operations on databases
19 (defmethod database-query-result-set ((expr %sql-expression) database
20 &key full-set result-types)
21 (database-query-result-set (sql-output expr database) database
22 :full-set full-set :result-types result-types))
24 (defmethod execute-command ((sql-expression string)
25 &key (database *default-database*))
26 (record-sql-command sql-expression database)
27 (let ((res (database-execute-command sql-expression database)))
28 (record-sql-result res database))
31 (defmethod execute-command ((expr %sql-expression)
32 &key (database *default-database*))
33 (execute-command (sql-output expr database) :database database)
36 (defmethod query ((query-expression string) &key (database *default-database*)
37 (result-types :auto) (flatp nil) (field-names t))
38 (record-sql-command query-expression database)
39 (multiple-value-bind (rows names)
40 (database-query query-expression database result-types field-names)
41 (let ((result (if (and flatp (= 1 (length (car rows))))
44 (record-sql-result result database)
49 (defmethod query ((expr %sql-expression) &key (database *default-database*)
50 (result-types :auto) (flatp nil) (field-names t))
51 (query (sql-output expr database) :database database :flatp flatp
52 :result-types result-types :field-names field-names))
54 (defmethod query ((expr sql-object-query) &key (database *default-database*)
55 (result-types :auto) (flatp nil) (field-names t))
56 (declare (ignore result-types field-names))
57 (apply #'select (append (slot-value expr 'objects)
58 (slot-value expr 'exp)
59 (when (slot-value expr 'refresh)
60 (list :refresh (sql-output expr database)))
61 (when (or flatp (slot-value expr 'flatp) )
63 (list :database database))))
66 (defun print-query (query-exp &key titles (formats t) (sizes t) (stream t)
67 (database *default-database*))
68 "Prints a tabular report of the results returned by the SQL
69 query QUERY-EXP, which may be a symbolic SQL expression or a
70 string, in DATABASE which defaults to *DEFAULT-DATABASE*. The
71 report is printed onto STREAM which has a default value of t
72 which means that *STANDARD-OUTPUT* is used. The TITLE argument,
73 which defaults to nil, allows the specification of a list of
74 strings to use as column titles in the tabular output. SIZES
75 accepts a list of column sizes, one for each column selected by
76 QUERY-EXP, to use in formatting the tabular report. The default
77 value of t means that minimum sizes are computed. FORMATS is a
78 list of format strings to be used for printing each column
79 selected by QUERY-EXP. The default value of FORMATS is t meaning
80 that ~A is used to format all columns or ~VA if column sizes are
82 (flet ((compute-sizes (data)
84 (apply #'max (mapcar #'(lambda (y)
85 (if (null y) 3 (length y)))
87 (apply #'mapcar (cons #'list data))))
88 (format-record (record control sizes)
89 (format stream "~&~?" control
90 (if (null sizes) record
91 (mapcan #'(lambda (s f) (list s f)) sizes record)))))
92 (let* ((query-exp (etypecase query-exp
94 (sql-query (sql-output query-exp database))))
95 (data (query query-exp :database database :result-types nil
97 (sizes (if (or (null sizes) (listp sizes)) sizes
98 (compute-sizes (if titles (cons titles data) data))))
99 (formats (if (or (null formats) (not (listp formats)))
100 (make-list (length (car data)) :initial-element
101 (if (null sizes) "~A " "~VA "))
103 (control-string (format nil "~{~A~}" formats)))
104 (when titles (format-record titles control-string sizes))
105 (dolist (d data (values)) (format-record d control-string sizes)))))
107 (defun insert-records (&key (into nil)
112 (database *default-database*))
113 "Inserts records into the table specified by INTO in DATABASE
114 which defaults to *DEFAULT-DATABASE*. There are five ways of
115 specifying the values inserted into each row. In the first VALUES
116 contains a list of values to insert and ATTRIBUTES, AV-PAIRS and
117 QUERY are nil. This can be used when values are supplied for all
118 attributes in INTO. In the second, ATTRIBUTES is a list of column
119 names, VALUES is a corresponding list of values and AV-PAIRS and
120 QUERY are nil. In the third, ATTRIBUTES, VALUES and QUERY are nil
121 and AV-PAIRS is an alist of (attribute value) pairs. In the
122 fourth, VALUES, AV-PAIRS and ATTRIBUTES are nil and QUERY is a
123 symbolic SQL query expression in which the selected columns also
124 exist in INTO. In the fifth method, VALUES and AV-PAIRS are nil
125 and ATTRIBUTES is a list of column names and QUERY is a symbolic
126 SQL query expression which returns values for the specified
128 (let ((stmt (make-sql-insert :into into :attrs attributes
129 :vals values :av-pairs av-pairs
131 (execute-command stmt :database database)))
133 (defun make-sql-insert (&key (into nil)
139 (error 'sql-user-error :message ":into keyword not supplied"))
140 (let ((insert (make-instance 'sql-insert :into into)))
141 (with-slots (attributes values query)
143 (cond ((and vals (not attrs) (not query) (not av-pairs))
145 ((and vals attrs (not subquery) (not av-pairs))
146 (setf attributes attrs)
148 ((and av-pairs (not vals) (not attrs) (not subquery))
149 (setf attributes (mapcar #'car av-pairs))
150 (setf values (mapcar #'cadr av-pairs)))
151 ((and subquery (not vals) (not attrs) (not av-pairs))
152 (setf query subquery))
153 ((and subquery attrs (not vals) (not av-pairs))
154 (setf attributes attrs)
155 (setf query subquery))
157 (error 'sql-user-error
158 :message "bad or ambiguous keyword combination.")))
161 (defun delete-records (&key (from nil)
163 (database *default-database*))
164 "Deletes records satisfying the SQL expression WHERE from the
165 table specified by FROM in DATABASE specifies a database which
166 defaults to *DEFAULT-DATABASE*."
167 (let ((stmt (make-instance 'sql-delete :from from :where where)))
168 (execute-command stmt :database database)))
170 (defun update-records (table &key (attributes nil)
174 (database *default-database*))
175 "Updates the attribute values of existing records satsifying
176 the SQL expression WHERE in the table specified by TABLE in
177 DATABASE which defaults to *DEFAULT-DATABASE*. There are three
178 ways of specifying the values to update for each row. In the
179 first, VALUES contains a list of values to use in the update and
180 ATTRIBUTES and AV-PAIRS are nil. This can be used when values are
181 supplied for all attributes in TABLE. In the second, ATTRIBUTES
182 is a list of column names, VALUES is a corresponding list of
183 values and AV-PAIRS is nil. In the third, ATTRIBUTES and VALUES
184 are nil and AV-PAIRS is an alist of (attribute value) pairs."
186 (setf attributes (mapcar #'car av-pairs)
187 values (mapcar #'cadr av-pairs)))
188 (let ((stmt (make-instance 'sql-update :table table
189 :attributes attributes
192 (execute-command stmt :database database)))
197 (defmethod database-output-sql ((str string) database)
198 (declare (ignore database)
199 (optimize (speed 3) (safety 1) #+cmu (extensions:inhibit-warnings 3))
200 (type (simple-array * (*)) str))
201 (let ((len (length str)))
202 (declare (type fixnum len))
205 ((and (null (position #\' str))
206 (null (position #\\ str)))
207 (concatenate 'string "'" str "'"))
209 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
212 ((= i len) (subseq buf 0 (1+ j)))
213 (declare (type fixnum i j))
214 (let ((char (aref str i)))
215 (declare (character char))
216 (cond ((char= char #\')
217 (setf (aref buf j) #\')
219 (setf (aref buf j) #\'))
221 (setf (aref buf j) #\\)
223 (setf (aref buf j) #\\))
225 (setf (aref buf j) char))))))))))
227 (let ((keyword-package (symbol-package :foo)))
228 (defmethod database-output-sql ((sym symbol) database)
229 (convert-to-db-default-case
230 (if (equal (symbol-package sym) keyword-package)
231 (concatenate 'string "'" (string sym) "'")
235 (defmethod database-output-sql ((tee (eql t)) database)
236 (declare (ignore database))
239 (defmethod database-output-sql ((num number) database)
240 (declare (ignore database))
241 (princ-to-string num))
243 (defmethod database-output-sql ((arg list) database)
246 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
247 (sql-output val database))
250 (defmethod database-output-sql ((arg vector) database)
251 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
252 (sql-output val database))
255 (defmethod database-output-sql ((self wall-time) database)
256 (declare (ignore database))
257 (db-timestring self))
259 (defmethod database-output-sql ((self duration) database)
260 (declare (ignore database))
261 (format nil "'~a'" (duration-timestring self)))
263 (defmethod database-output-sql (thing database)
267 (error 'sql-user-error
270 "No type conversion to SQL for ~A is defined for DB ~A."
271 (type-of thing) (type-of database)))))
274 (defmethod output-sql-hash-key ((arg vector) database)
275 (list 'vector (map 'list (lambda (arg)
276 (or (output-sql-hash-key arg database)
277 (return-from output-sql-hash-key nil)))
280 (defmethod output-sql (expr database)
281 (write-string (database-output-sql expr database) *sql-stream*)
284 (defmethod output-sql ((expr list) database)
286 (write-string +null-string+ *sql-stream*)
288 (write-char #\( *sql-stream*)
289 (do ((item expr (cdr item)))
291 (output-sql (car item) database))
292 (output-sql (car item) database)
293 (write-char #\, *sql-stream*))
294 (write-char #\) *sql-stream*)))
298 (defmethod add-storage-class ((self database) (class symbol) &key (sequence t))
299 (let ((tablename (view-table (find-class class))))
300 (unless (tablep tablename)
301 (create-view-from-class class)
303 (create-sequence-from-class class)))))
308 (defmacro do-query (((&rest args) query-expression
309 &key (database '*default-database*) (result-types :auto))
311 "Repeatedly executes BODY within a binding of ARGS on the
312 fields of each row selected by the SQL query QUERY-EXPRESSION,
313 which may be a string or a symbolic SQL expression, in DATABASE
314 which defaults to *DEFAULT-DATABASE*. The values returned by the
315 execution of BODY are returned. RESULT-TYPES is a list of symbols
316 which specifies the lisp type for each field returned by
317 QUERY-EXPRESSION. If RESULT-TYPES is nil all results are returned
318 as strings whereas the default value of :auto means that the lisp
319 types are automatically computed for each field."
320 (let ((result-set (gensym "RESULT-SET-"))
321 (qe (gensym "QUERY-EXPRESSION-"))
322 (columns (gensym "COLUMNS-"))
323 (row (gensym "ROW-"))
325 `(let ((,qe ,query-expression))
328 (dolist (,row (query ,qe))
329 (destructuring-bind ,args
334 (let ((,db ,database))
335 (multiple-value-bind (,result-set ,columns)
336 (database-query-result-set ,qe ,db
338 :result-types ,result-types)
341 (do ((,row (make-list ,columns)))
342 ((not (database-store-next-row ,result-set ,db ,row))
344 (destructuring-bind ,args ,row
346 (database-dump-result-set ,result-set ,db))))))))))
348 (defun map-query (output-type-spec function query-expression
349 &key (database *default-database*)
350 (result-types :auto))
351 "Map the function FUNCTION over the attribute values of each
352 row selected by the SQL query QUERY-EXPRESSION, which may be a
353 string or a symbolic SQL expression, in DATABASE which defaults
354 to *DEFAULT-DATABASE*. The results of the function are collected
355 as specified in OUTPUT-TYPE-SPEC and returned like in
356 MAP. RESULT-TYPES is a list of symbols which specifies the lisp
357 type for each field returned by QUERY-EXPRESSION. If RESULT-TYPES
358 is nil all results are returned as strings whereas the default
359 value of :auto means that the lisp types are automatically
360 computed for each field."
361 (typecase query-expression
363 (map output-type-spec #'(lambda (x) (apply function x))
364 (query query-expression)))
367 (macrolet ((type-specifier-atom (type)
368 `(if (atom ,type) ,type (car ,type))))
369 (case (type-specifier-atom output-type-spec)
371 (map-query-for-effect function query-expression database
374 (map-query-to-list function query-expression database result-types))
375 ((simple-vector simple-string vector string array simple-array
376 bit-vector simple-bit-vector base-string
378 (map-query-to-simple output-type-spec function query-expression
379 database result-types))
382 (cmucl-compat:result-type-or-lose output-type-spec t)
383 function query-expression :database database
384 :result-types result-types)))))))
386 (defun map-query-for-effect (function query-expression database result-types)
387 (multiple-value-bind (result-set columns)
388 (database-query-result-set query-expression database :full-set nil
389 :result-types result-types)
390 (let ((flatp (and (= columns 1)
391 (typep query-expression 'sql-query)
392 (slot-value query-expression 'flatp))))
395 (do ((row (make-list columns)))
396 ((not (database-store-next-row result-set database row))
400 (funcall function row)))
401 (database-dump-result-set result-set database))))))
403 (defun map-query-to-list (function query-expression database result-types)
404 (multiple-value-bind (result-set columns)
405 (database-query-result-set query-expression database :full-set nil
406 :result-types result-types)
407 (let ((flatp (and (= columns 1)
408 (typep query-expression 'sql-query)
409 (slot-value query-expression 'flatp))))
412 (let ((result (list nil)))
413 (do ((row (make-list columns))
414 (current-cons result (cdr current-cons)))
415 ((not (database-store-next-row result-set database row))
420 (funcall function (copy-list row)))))))
421 (database-dump-result-set result-set database))))))
423 (defun map-query-to-simple (output-type-spec function query-expression database result-types)
424 (multiple-value-bind (result-set columns rows)
425 (database-query-result-set query-expression database :full-set t
426 :result-types result-types)
427 (let ((flatp (and (= columns 1)
428 (typep query-expression 'sql-query)
429 (slot-value query-expression 'flatp))))
433 ;; We know the row count in advance, so we allocate once
435 (cmucl-compat:make-sequence-of-type output-type-spec rows))
436 (row (make-list columns))
437 (index 0 (1+ index)))
438 ((not (database-store-next-row result-set database row))
440 (declare (fixnum index))
441 (setf (aref result index)
444 (funcall function (copy-list row)))))
445 ;; Database can't report row count in advance, so we have
446 ;; to grow and shrink our vector dynamically
448 (cmucl-compat:make-sequence-of-type output-type-spec 100))
449 (allocated-length 100)
450 (row (make-list columns))
451 (index 0 (1+ index)))
452 ((not (database-store-next-row result-set database row))
453 (cmucl-compat:shrink-vector result index))
454 (declare (fixnum allocated-length index))
455 (when (>= index allocated-length)
456 (setq allocated-length (* allocated-length 2)
457 result (adjust-array result allocated-length)))
458 (setf (aref result index)
461 (funcall function (copy-list row))))))
462 (database-dump-result-set result-set database))))))
464 ;;; Row processing macro from CLSQL
466 (defmacro for-each-row (((&rest fields) &key from order-by where distinct limit)
468 (let ((d (gensym "DISTINCT-"))
469 (bind-fields (loop for f in fields collect (car f)))
470 (w (gensym "WHERE-"))
471 (o (gensym "ORDER-BY-"))
472 (frm (gensym "FROM-"))
473 (l (gensym "LIMIT-"))
474 (q (gensym "QUERY-")))
480 (let ((,q (query-string ',fields ,frm ,w ,d ,o ,l)))
481 (loop for tuple in (query ,q)
482 collect (destructuring-bind ,bind-fields tuple
485 (defun query-string (fields from where distinct order-by limit)
488 (format nil "select ~A~{~A~^,~} from ~{~A~^ and ~}"
489 (if distinct "distinct " "") (field-names fields)
491 (if where (format nil " where ~{~A~^ ~}"
492 (where-strings where)) "")
493 (if order-by (format nil " order by ~{~A~^, ~}"
494 (order-by-strings order-by)))
495 (if limit (format nil " limit ~D" limit) "")))
497 (defun lisp->sql-name (field)
500 (symbol (string-upcase (symbol-name field)))
502 (t (format nil "~A" field))))
504 (defun field-names (field-forms)
505 "Return a list of field name strings from a fields form"
506 (loop for field-form in field-forms
509 (if (cadr field-form)
513 (defun from-names (from)
514 "Return a list of field name strings from a fields form"
515 (loop for table in (if (atom from) (list from) from)
516 collect (lisp->sql-name table)))
519 (defun where-strings (where)
520 (loop for w in (if (atom (car where)) (list where) where)
523 (format nil "~A ~A ~A" (second w) (first w) (third w))
524 (format nil "~A" w))))
526 (defun order-by-strings (order-by)
527 (loop for o in order-by
531 (format nil "~A ~A" (lisp->sql-name (car o))
532 (lisp->sql-name (cadr o))))))
535 ;;; Large objects support
537 (defun create-large-object (&key (database *default-database*))
538 "Creates a new large object in the database and returns the object identifier"
539 (database-create-large-object database))
541 (defun write-large-object (object-id data &key (database *default-database*))
542 "Writes data to the large object"
543 (database-write-large-object object-id data database))
545 (defun read-large-object (object-id &key (database *default-database*))
546 "Reads the large object content"
547 (database-read-large-object object-id database))
549 (defun delete-large-object (object-id &key (database *default-database*))
550 "Deletes the large object in the database"
551 (database-delete-large-object object-id database))
554 ;;; Prepared statements
556 (defun prepare-sql (sql-stmt types &key (database *default-database*) (result-types :auto) field-names)
557 "Prepares a SQL statement for execution. TYPES contains a
558 list of types corresponding to the input parameters. Returns a
559 prepared-statement object.
567 (unless (db-type-has-prepared-stmt? (database-type database))
568 (error 'sql-user-error
571 "Database backend type ~:@(~A~) does not support prepared statements."
572 (database-type database))))
574 (database-prepare sql-stmt types database result-types field-names))
576 (defun bind-parameter (prepared-stmt position value)
577 "Sets the value of a parameter is in prepared statement."
578 (database-bind-parameter prepared-stmt position value)
581 (defun run-prepared-sql (prepared-stmt)
582 "Execute the prepared sql statment. All input parameters must be bound."
583 (database-run-prepared prepared-stmt))
585 (defun free-prepared-sql (prepared-stmt)
586 "Delete the objects associated with a prepared statement."
587 (database-free-prepared prepared-stmt))