1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
4 ;;;; Base database functions
6 ;;;; This file is part of CLSQL.
8 ;;;; CLSQL users are granted the rights to distribute and use this software
9 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
10 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
11 ;;;; *************************************************************************
13 (in-package #:clsql-sys)
16 (defvar *connect-if-exists* :error
17 "Default value for the if-exists keyword argument in calls to
18 CONNECT. Meaningful values are :new, :warn-new, :error, :warn-old
21 ;;TODO: this variable appears to be global, not thread specific and is
22 ;; not protected when modifying the list.
23 (defvar *connected-databases* nil
24 "List of active database objects.")
26 (defun connected-databases ()
27 "Returns the list of active database objects."
28 *connected-databases*)
30 (defvar *default-database* nil
31 "Specifies the default database to be used.")
33 (defun is-database-open (database)
34 (eql (database-state database) :open))
36 (defun find-database (database &key (errorp t) (db-type nil))
37 "Returns the connected databases of type DB-TYPE whose names
38 match the string DATABASE. If DATABASE is a database object, it
39 is returned. If DB-TYPE is nil all databases matching the string
40 DATABASE are considered. If no matching databases are found and
41 ERRORP is nil then nil is returned. If ERRORP is nil and one or
42 more matching databases are found, then the most recently
43 connected database is returned as a first value and the number of
44 matching databases is returned as a second value. If no, or more
45 than one, matching databases are found and ERRORP is true, an
51 (let* ((matches (remove-if
53 (not (and (string= (database-name db) database)
55 (equal (database-type db) db-type)
57 (connected-databases)))
58 (count (length matches)))
59 (if (or (not errorp) (= count 1))
60 (values (car matches) count)
64 (format nil "There exists ~A database called ~A."
65 (if (zerop count) "no" "more than one")
68 (error "A database must be specified rather than NIL."))))
71 (defun connect (connection-spec
72 &key (if-exists *connect-if-exists*)
75 (database-type *default-database-type*)
77 "Connects to a database of the supplied DATABASE-TYPE which
78 defaults to *DEFAULT-DATABASE-TYPE*, using the type-specific
79 connection specification CONNECTION-SPEC. The value of IF-EXISTS,
80 which defaults to *CONNECT-IF-EXISTS*, determines what happens if
81 a connection to the database specified by CONNECTION-SPEC is
82 already established. A value of :new means create a new
83 connection. A value of :warn-new means warn the user and create
84 a new connect. A value of :warn-old means warn the user and use
85 the old connection. A value of :error means fail, notifying the
86 user. A value of :old means return the old connection.
87 MAKE-DEFAULT is t by default which means that *DEFAULT-DATABASE*
88 is set to the new connection, otherwise *DEFAULT-DATABASE* is not
89 changed. If POOL is t the connection will be taken from the
90 general pool, if POOL is a CONN-POOL object the connection will
91 be taken from this pool."
94 (error 'sql-database-error :message "Must specify a database-type."))
96 (when (stringp connection-spec)
97 (setq connection-spec (string-to-list-connection-spec connection-spec)))
99 (unless (member database-type *loaded-database-types*)
100 (asdf:operate 'asdf:load-op (ensure-keyword
102 (symbol-name '#:clsql-)
103 (symbol-name database-type)))
107 (let ((conn (acquire-from-pool connection-spec database-type pool encoding)))
108 (when make-default (setq *default-database* conn))
110 (let* ((db-name (database-name-from-spec connection-spec database-type))
111 (old-db (unless (eq if-exists :new)
112 (find-database db-name :db-type database-type
119 (database-connect connection-spec database-type))
123 "Created new connection ~A to database ~A~%, although there is an existing connection (~A)."
124 result (database-name result) old-db)))
127 (error 'sql-connection-error
129 (format nil "There is an existing connection ~A to database ~A."
131 (database-name old-db)))
133 :report "Create a new connection."
135 (database-connect connection-spec database-type)))
137 :report "Use the existing connection."
138 (setq result old-db))))
144 "Using existing connection ~A to database ~A."
146 (database-name old-db))))
148 (setq result old-db)))
150 (database-connect connection-spec database-type)))
152 (setf (slot-value result 'state) :open)
153 (pushnew result *connected-databases*)
154 (when make-default (setq *default-database* result))
155 (setf (encoding result) encoding)
159 (defun disconnect (&key (database *default-database*) (error nil))
161 "Closes the connection to DATABASE and resets
162 *DEFAULT-DATABASE* if that database was disconnected. If DATABASE
163 is a database instance, this object is closed. If DATABASE is a
164 string, then a connected database whose name matches DATABASE is
165 sought in the list of connected databases. If no matching
166 database is found and ERROR and DATABASE are both non-nil an
167 error is signaled, otherwise nil is returned. If the database is
168 from a pool it will be released to this pool."
169 (let ((database (find-database database :errorp (and database error))))
171 (if (conn-pool database)
172 (with-process-lock ((conn-pool-lock (conn-pool database)) "Delete from pool")
173 (when (release-to-pool database)
174 (setf *connected-databases* (delete database *connected-databases*))
175 (when (eq database *default-database*)
176 (setf *default-database* (car *connected-databases*)))
178 (when (database-disconnect database)
179 ;;TODO: RACE COND: 2 threads disconnecting could stomp on *connected-databases*
180 (setf *connected-databases* (delete database *connected-databases*))
181 (when (eq database *default-database*)
182 (setf *default-database* (car *connected-databases*)))
183 (setf (slot-value database 'state) :closed)
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 (defmacro check-connection-spec (connection-spec database-type template)
191 "Check the connection specification against the provided template,
192 and signal an sql-user-error if they don't match. This function
193 is called by database backends."
195 (destructuring-bind ,template ,connection-spec
196 (declare (ignore ,@(remove '&optional template)))
199 (error 'sql-user-error
202 "The connection specification ~A~%is invalid for database type ~A.~%The connection specification must conform to ~A"
205 (quote ,template))))))
207 (defun reconnect (&key (database *default-database*) (error nil) (force t))
208 "Reconnects DATABASE which defaults to *DEFAULT-DATABASE* to
209 the underlying database management system. On success, t is
210 returned and the variable *DEFAULT-DATABASE* is set to the newly
211 reconnected database. If DATABASE is a database instance, this
212 object is closed. If DATABASE is a string, then a connected
213 database whose name matches DATABASE is sought in the list of
214 connected databases. If no matching database is found and ERROR
215 and DATABASE are both non-nil an error is signaled, otherwise nil
216 is returned. When the current database connection cannot be
217 closed, if FORCE is non-nil, as it is by default, the connection
218 is closed and errors are suppressed. If force is nil and the
219 database connection cannot be closed, an error is signalled."
220 (let ((db (etypecase database
223 (let ((db (find-database database :errorp nil)))
225 (if (and database error)
226 (error 'sql-connection-error
228 (format nil "Unable to find database with connection-spec ~A." database))
229 (return-from reconnect nil)))
232 (when (is-database-open db)
234 (ignore-errors (disconnect :database db))
235 (disconnect :database db :error nil)))
237 (connect (connection-spec db) :encoding (encoding db))))
240 (defun status (&optional full)
241 "Prints information about the currently connected databases to
242 *STANDARD-OUTPUT*. The argument FULL is nil by default and a
243 value of t means that more detailed information about each
244 database is printed."
247 (dolist (db (connected-databases) data)
250 (list (if (equal db *default-database*) "*" "")
252 (string-downcase (string (database-type db)))
253 (cond ((and (command-recording-stream db)
254 (result-recording-stream db))
256 ((command-recording-stream db) "Commands")
257 ((result-recording-stream db) "Results")
261 (if (conn-pool db) "t" "nil")
262 (format nil "~A" (length (database-list-tables db)))
263 (format nil "~A" (length (database-list-views db))))))
265 (compute-sizes (data)
266 (mapcar #'(lambda (x) (apply #'max (mapcar #'length x)))
267 (apply #'mapcar (cons #'list data))))
268 (print-separator (size)
269 (format t "~&~A" (make-string size :initial-element #\-))))
270 (format t "~&CLSQL STATUS: ~A~%" (iso-timestring (get-time)))
271 (let ((data (get-data)))
273 (let* ((titles (if full
274 (list "" "DATABASE" "TYPE" "RECORDING" "POOLED"
276 (list "" "DATABASE" "TYPE" "RECORDING")))
277 (sizes (compute-sizes (cons titles data)))
278 (total-size (+ (apply #'+ sizes) (* 2 (1- (length titles)))))
279 (control-string (format nil "~~&~~{~{~~~AA ~}~~}" sizes)))
280 (print-separator total-size)
281 (format t control-string titles)
282 (print-separator total-size)
283 (dolist (d data) (format t control-string d))
284 (print-separator total-size))))
287 (defun create-database (connection-spec &key (database-type *default-database-type*))
288 "This function creates a database in the database system specified
290 (when (stringp connection-spec)
291 (setq connection-spec (string-to-list-connection-spec connection-spec)))
292 (database-create connection-spec database-type))
294 (defun probe-database (connection-spec &key (database-type *default-database-type*))
295 "This function tests for the existence of a database in the database
296 system specified by DATABASE-TYPE."
297 (when (stringp connection-spec)
298 (setq connection-spec (string-to-list-connection-spec connection-spec)))
299 (database-probe connection-spec database-type))
301 (defun destroy-database (connection-spec &key (database-type *default-database-type*))
302 "This function destroys a database in the database system specified
304 (when (stringp connection-spec)
305 (setq connection-spec (string-to-list-connection-spec connection-spec)))
306 (database-destroy connection-spec database-type))
308 (defun list-databases (connection-spec &key (database-type *default-database-type*))
309 "This function returns a list of databases existing in the database
310 system specified by DATABASE-TYPE."
311 (when (stringp connection-spec)
312 (setq connection-spec (string-to-list-connection-spec connection-spec)))
313 (database-list connection-spec database-type))
316 (when (typep db 'database)
317 (slot-value db 'encoding)))
319 (defun (setf encoding) (encoding db)
320 (when (typep db 'database)
321 (setf (slot-value db 'encoding) encoding)
322 (when (eql (slot-value db 'state) :open)
323 (case (database-type db)
324 ;; FIXME: If database object is open then
325 ;; send command to SQL engine specifying the character
326 ;; encoding for the database
329 ((:postgresql :postgresql-socket)
332 (defmacro with-database ((db-var connection-spec
333 &key make-default pool
334 (if-exists *connect-if-exists*)
335 (database-type *default-database-type*)
338 "Evaluate the body in an environment, where DB-VAR is bound to the
339 database connection given by CONNECTION-SPEC and CONNECT-ARGS. The
340 connection is automatically closed or released to the pool on exit
341 from the body. MAKE-DEFAULT has a default value of NIL."
342 `(let ((,db-var (connect ,connection-spec
343 :database-type ,database-type
344 :if-exists ,if-exists
346 :make-default ,make-default
347 :encoding ,encoding)))
349 (let ((,db-var ,db-var))
351 (disconnect :database ,db-var))))
353 (defmacro with-default-database ((database) &rest body)
354 "Perform BODY with DATABASE bound as *default-database*."
355 `(progv '(*default-database*)