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
20 (defmethod database-query-result-set ((expr %sql-expression) database
21 &key full-set result-types)
22 (database-query-result-set (sql-output expr database) database
23 :full-set full-set :result-types result-types))
25 (defmethod execute-command ((expr %sql-expression)
26 &key (database *default-database*))
27 (execute-command (sql-output expr database) :database database)
31 (defmethod query ((expr %sql-expression) &key (database *default-database*)
32 (result-types nil) (flatp nil))
33 (query (sql-output expr database) :database database :flatp flatp
34 :result-types result-types))
36 (defun truncate-database (database)
37 (unless (typep database 'database)
38 (clsql-base-sys::signal-no-database-error database))
39 (unless (is-database-open database)
40 (database-reconnect database))
41 (when (db-type-has-views? (database-underlying-type database))
42 (dolist (view (list-views :database database))
43 (drop-view view :database database)))
44 (dolist (table (list-tables :database database))
45 (drop-table table :database database))
46 (dolist (index (list-indexes :database database))
47 (drop-index index :database database))
48 (dolist (seq (list-sequences :database database))
49 (drop-sequence seq :database database)))
51 (defun print-query (query-exp &key titles (formats t) (sizes t) (stream t)
52 (database *default-database*))
53 "The PRINT-QUERY function takes a symbolic SQL query expression and
54 formatting information and prints onto STREAM a table containing the
55 results of the query. A list of strings to use as column headings is
56 given by TITLES, which has a default value of NIL. The FORMATS
57 argument is a list of format strings used to print each attribute, and
58 has a default value of T, which means that ~A or ~VA are used if sizes
59 are provided or computed. The field sizes are given by SIZES. It has a
60 default value of T, which specifies that minimum sizes are
61 computed. The output stream is given by STREAM, which has a default
62 value of T. This specifies that *STANDARD-OUTPUT* is used."
63 (flet ((compute-sizes (data)
64 (mapcar #'(lambda (x) (apply #'max (mapcar #'length x)))
65 (apply #'mapcar (cons #'list data))))
66 (format-record (record control sizes)
67 (format stream "~&~?" control
68 (if (null sizes) record
69 (mapcan #'(lambda (s f) (list s f)) sizes record)))))
70 (let* ((query-exp (etypecase query-exp
72 (sql-query (sql-output query-exp))))
73 (data (query query-exp :database database))
74 (sizes (if (or (null sizes) (listp sizes)) sizes
75 (compute-sizes (if titles (cons titles data) data))))
76 (formats (if (or (null formats) (not (listp formats)))
77 (make-list (length (car data)) :initial-element
78 (if (null sizes) "~A " "~VA "))
80 (control-string (format nil "~{~A~}" formats)))
81 (when titles (format-record titles control-string sizes))
82 (dolist (d data (values)) (format-record d control-string sizes)))))
84 (defun insert-records (&key (into nil)
89 (database *default-database*))
90 "Inserts a set of values into a table. The records created contain
91 values for attributes (or av-pairs). The argument VALUES is a list of
92 values. If ATTRIBUTES is supplied then VALUES must be a corresponding
93 list of values for each of the listed attribute names. If AV-PAIRS is
94 non-nil, then both ATTRIBUTES and VALUES must be nil. If QUERY is
95 non-nil, then neither VALUES nor AV-PAIRS should be. QUERY should be a
96 query expression, and the attribute names in it must also exist in the
97 table INTO. The default value of DATABASE is *DEFAULT-DATABASE*."
98 (let ((stmt (make-sql-insert :into into :attrs attributes
99 :vals values :av-pairs av-pairs
101 (execute-command stmt :database database)))
103 (defun make-sql-insert (&key (into nil)
109 (error 'clsql-sql-syntax-error :reason ":into keyword not supplied"))
110 (let ((insert (make-instance 'sql-insert :into into)))
111 (with-slots (attributes values query)
113 (cond ((and vals (not attrs) (not query) (not av-pairs))
115 ((and vals attrs (not subquery) (not av-pairs))
116 (setf attributes attrs)
118 ((and av-pairs (not vals) (not attrs) (not subquery))
119 (setf attributes (mapcar #'car av-pairs))
120 (setf values (mapcar #'cadr av-pairs)))
121 ((and subquery (not vals) (not attrs) (not av-pairs))
122 (setf query subquery))
123 ((and subquery attrs (not vals) (not av-pairs))
124 (setf attributes attrs)
125 (setf query subquery))
127 (error 'clsql-sql-syntax-error
128 :reason "bad or ambiguous keyword combination.")))
131 (defun delete-records (&key (from nil)
133 (database *default-database*))
134 "Deletes rows from a database table specified by FROM in which the
135 WHERE condition is true. The argument DATABASE specifies a database
136 from which the records are to be removed, and defaults to
138 (let ((stmt (make-instance 'sql-delete :from from :where where)))
139 (execute-command stmt :database database)))
141 (defun update-records (table &key (attributes nil)
145 (database *default-database*))
146 "Changes the values of existing fields in TABLE with columns
147 specified by ATTRIBUTES and VALUES (or AV-PAIRS) where the WHERE
150 (setf attributes (mapcar #'car av-pairs)
151 values (mapcar #'cadr av-pairs)))
152 (let ((stmt (make-instance 'sql-update :table table
153 :attributes attributes
156 (execute-command stmt :database database)))
163 (defmethod database-output-sql ((str string) database)
164 (declare (ignore database)
165 (optimize (speed 3) (safety 1) #+cmu (extensions:inhibit-warnings 3))
166 (type (simple-array * (*)) str))
167 (let ((len (length str)))
168 (declare (type fixnum len))
171 ((and (null (position #\' str))
172 (null (position #\\ str)))
173 (concatenate 'string "'" str "'"))
175 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
178 ((= i len) (subseq buf 0 (1+ j)))
179 (declare (type integer i j))
180 (let ((char (aref str i)))
181 (cond ((eql char #\')
182 (setf (aref buf j) #\\)
184 (setf (aref buf j) #\'))
186 (setf (aref buf j) #\\)
188 (setf (aref buf j) #\\))
190 (setf (aref buf j) char))))))))))
192 (let ((keyword-package (symbol-package :foo)))
193 (defmethod database-output-sql ((sym symbol) database)
194 (declare (ignore database))
195 (if (equal (symbol-package sym) keyword-package)
196 (concatenate 'string "'" (string sym) "'")
199 (defmethod database-output-sql ((tee (eql t)) database)
200 (declare (ignore database))
203 (defmethod database-output-sql ((num number) database)
204 (declare (ignore database))
205 (princ-to-string num))
207 (defmethod database-output-sql ((arg list) database)
210 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
211 (sql-output val database))
214 (defmethod database-output-sql ((arg vector) database)
215 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
216 (sql-output val database))
219 (defmethod database-output-sql ((self wall-time) database)
220 (declare (ignore database))
221 (db-timestring self))
223 (defmethod database-output-sql ((self duration) database)
224 (declare (ignore database))
225 (format nil "'~a'" (duration-timestring self)))
227 (defmethod database-output-sql (thing database)
231 (error 'clsql-simple-error
233 "No type conversion to SQL for ~A is defined for DB ~A."
234 :format-arguments (list (type-of thing) (type-of database)))))
237 (defmethod output-sql-hash-key ((arg vector) &optional database)
238 (list 'vector (map 'list (lambda (arg)
239 (or (output-sql-hash-key arg database)
240 (return-from output-sql-hash-key nil)))
243 (defmethod output-sql (expr &optional (database *default-database*))
244 (write-string (database-output-sql expr database) *sql-stream*)
247 (defmethod output-sql ((expr list) &optional (database *default-database*))
249 (write-string +null-string+ *sql-stream*)
251 (write-char #\( *sql-stream*)
252 (do ((item expr (cdr item)))
254 (output-sql (car item) database))
255 (output-sql (car item) database)
256 (write-char #\, *sql-stream*))
257 (write-char #\) *sql-stream*)))
260 (defmethod describe-table ((table sql-create-table)
261 &key (database *default-database*))
262 (database-describe-table
264 (string-downcase (symbol-name (slot-value table 'name)))))
267 (defmethod add-storage-class ((self database) (class symbol) &key (sequence t))
268 (let ((tablename (view-table (find-class class))))
269 (unless (tablep tablename)
270 (create-view-from-class class)
272 (create-sequence-from-class class)))))