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)
30 (defmethod explain ((expr %sql-expression) &key (database *default-database*))
31 (let ((expression (sql-output expr database)))
32 (format *standard-output* "explain: ~S~%" expression)
33 (execute-command (concatenate 'string "explain " expression))))
36 (defmethod query ((expr %sql-expression) &key (database *default-database*)
37 (result-types nil) (flatp nil))
38 (query (sql-output expr database) :database database :flatp flatp
39 :result-types result-types))
41 (defun truncate-database (database)
42 (unless (typep database 'database)
43 (clsql-base-sys::signal-no-database-error database))
44 (unless (is-database-open database)
45 (database-reconnect database))
46 (dolist (table (list-tables database))
47 (drop-table table database))
48 (dolist (index (list-indexes database))
49 (drop-index index database))
50 (dolist (seq (list-sequences database))
51 (drop-sequence seq database)))
53 (defun print-query (query-exp &key titles (formats t) (sizes t) (stream t)
54 (database *default-database*))
55 "The PRINT-QUERY function takes a symbolic SQL query expression and
56 formatting information and prints onto STREAM a table containing the
57 results of the query. A list of strings to use as column headings is
58 given by TITLES, which has a default value of NIL. The FORMATS
59 argument is a list of format strings used to print each attribute, and
60 has a default value of T, which means that ~A or ~VA are used if sizes
61 are provided or computed. The field sizes are given by SIZES. It has a
62 default value of T, which specifies that minimum sizes are
63 computed. The output stream is given by STREAM, which has a default
64 value of T. This specifies that *STANDARD-OUTPUT* is used."
65 (flet ((compute-sizes (data)
66 (mapcar #'(lambda (x) (apply #'max (mapcar #'length x)))
67 (apply #'mapcar (cons #'list data))))
68 (format-record (record control sizes)
69 (format stream "~&~?" control
70 (if (null sizes) record
71 (mapcan #'(lambda (s f) (list s f)) sizes record)))))
72 (let* ((query-exp (etypecase query-exp
74 (sql-query (sql-output query-exp))))
75 (data (query query-exp :database database))
76 (sizes (if (or (null sizes) (listp sizes)) sizes
77 (compute-sizes (if titles (cons titles data) data))))
78 (formats (if (or (null formats) (not (listp formats)))
79 (make-list (length (car data)) :initial-element
80 (if (null sizes) "~A " "~VA "))
82 (control-string (format nil "~{~A~}" formats)))
83 (when titles (format-record titles control-string sizes))
84 (dolist (d data (values)) (format-record d control-string sizes)))))
86 (defun insert-records (&key (into nil)
91 (database *default-database*))
92 "Inserts a set of values into a table. The records created contain
93 values for attributes (or av-pairs). The argument VALUES is a list of
94 values. If ATTRIBUTES is supplied then VALUES must be a corresponding
95 list of values for each of the listed attribute names. If AV-PAIRS is
96 non-nil, then both ATTRIBUTES and VALUES must be nil. If QUERY is
97 non-nil, then neither VALUES nor AV-PAIRS should be. QUERY should be a
98 query expression, and the attribute names in it must also exist in the
99 table INTO. The default value of DATABASE is *DEFAULT-DATABASE*."
100 (let ((stmt (make-sql-insert :into into :attrs attributes
101 :vals values :av-pairs av-pairs
103 (execute-command stmt :database database)))
105 (defun make-sql-insert (&key (into nil)
111 (error 'clsql-sql-syntax-error :reason ":into keyword not supplied"))
112 (let ((insert (make-instance 'sql-insert :into into)))
113 (with-slots (attributes values query)
115 (cond ((and vals (not attrs) (not query) (not av-pairs))
117 ((and vals attrs (not subquery) (not av-pairs))
118 (setf attributes attrs)
120 ((and av-pairs (not vals) (not attrs) (not subquery))
121 (setf attributes (mapcar #'car av-pairs))
122 (setf values (mapcar #'cadr av-pairs)))
123 ((and subquery (not vals) (not attrs) (not av-pairs))
124 (setf query subquery))
125 ((and subquery attrs (not vals) (not av-pairs))
126 (setf attributes attrs)
127 (setf query subquery))
129 (error 'clsql-sql-syntax-error
130 :reason "bad or ambiguous keyword combination.")))
133 (defun delete-records (&key (from nil)
135 (database *default-database*))
136 "Deletes rows from a database table specified by FROM in which the
137 WHERE condition is true. The argument DATABASE specifies a database
138 from which the records are to be removed, and defaults to
140 (let ((stmt (make-instance 'sql-delete :from from :where where)))
141 (execute-command stmt :database database)))
143 (defun update-records (table &key (attributes nil)
147 (database *default-database*))
148 "Changes the values of existing fields in TABLE with columns
149 specified by ATTRIBUTES and VALUES (or AV-PAIRS) where the WHERE
152 (setf attributes (mapcar #'car av-pairs)
153 values (mapcar #'cadr av-pairs)))
154 (let ((stmt (make-instance 'sql-update :table table
155 :attributes attributes
158 (execute-command stmt :database database)))
165 (defmethod database-output-sql ((str string) database)
166 (declare (ignore database)
167 (optimize (speed 3) (safety 1) #+cmu (extensions:inhibit-warnings 3))
168 (type (simple-array * (*)) str))
169 (let ((len (length str)))
170 (declare (type fixnum len))
173 ((and (null (position #\' str))
174 (null (position #\\ str)))
175 (concatenate 'string "'" str "'"))
177 (let ((buf (make-string (+ (* len 2) 2) :initial-element #\')))
180 ((= i len) (subseq buf 0 (1+ j)))
181 (declare (type integer i j))
182 (let ((char (aref str i)))
183 (cond ((eql char #\')
184 (setf (aref buf j) #\\)
186 (setf (aref buf j) #\'))
188 (setf (aref buf j) #\\)
190 (setf (aref buf j) #\\))
192 (setf (aref buf j) char))))))))))
194 (let ((keyword-package (symbol-package :foo)))
195 (defmethod database-output-sql ((sym symbol) database)
196 (declare (ignore database))
197 (if (equal (symbol-package sym) keyword-package)
198 (concatenate 'string "'" (string sym) "'")
201 (defmethod database-output-sql ((tee (eql t)) database)
202 (declare (ignore database))
205 (defmethod database-output-sql ((num number) database)
206 (declare (ignore database))
207 (princ-to-string num))
209 (defmethod database-output-sql ((arg list) database)
212 (format nil "(~{~A~^,~})" (mapcar #'(lambda (val)
213 (sql-output val database))
216 (defmethod database-output-sql ((arg vector) database)
217 (format nil "~{~A~^,~}" (map 'list #'(lambda (val)
218 (sql-output val database))
221 (defmethod database-output-sql ((self wall-time) database)
222 (declare (ignore database))
223 (db-timestring self))
225 (defmethod database-output-sql (thing database)
229 (error 'clsql-simple-error
231 "No type conversion to SQL for ~A is defined for DB ~A."
232 :format-arguments (list (type-of thing) (type-of database)))))
235 (defmethod output-sql-hash-key ((arg vector) &optional database)
236 (list 'vector (map 'list (lambda (arg)
237 (or (output-sql-hash-key arg database)
238 (return-from output-sql-hash-key nil)))
241 (defmethod output-sql (expr &optional (database *default-database*))
242 (write-string (database-output-sql expr database) *sql-stream*)
245 (defmethod output-sql ((expr list) &optional (database *default-database*))
247 (write-string +null-string+ *sql-stream*)
249 (write-char #\( *sql-stream*)
250 (do ((item expr (cdr item)))
252 (output-sql (car item) database))
253 (output-sql (car item) database)
254 (write-char #\, *sql-stream*))
255 (write-char #\) *sql-stream*)))
259 (defmethod add-storage-class ((self database) (class symbol) &key (sequence t))
260 (let ((tablename (view-table (find-class class))))
261 (unless (tablep tablename)
262 (create-view-from-class class)
264 (create-sequence-from-class class)))))