X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=base%2Fdatabase.lisp;h=1c6fa63ee82fc12911175575a755470b3e8854aa;hb=79dacf4fb7e6707e815c3e8072e5c809acaa9386;hp=e8845ae4346d531a81d536791309025044fec983;hpb=e06ca99080d17083dcfcf3f148de4a8796773e78;p=clsql.git diff --git a/base/database.lisp b/base/database.lisp index e8845ae..1c6fa63 100644 --- a/base/database.lisp +++ b/base/database.lisp @@ -1,7 +1,16 @@ ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;; ************************************************************************* ;;;; -;;;; $Id: $ +;;;; $Id$ +;;;; +;;;; Base database functions +;;;; +;;;; This file is part of CLSQL. +;;;; +;;;; CLSQL users are granted the rights to distribute and use this software +;;;; as governed by the terms of the Lisp Lesser GNU Public License +;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL. +;;;; ************************************************************************* (in-package #:clsql-base-sys) @@ -23,6 +32,9 @@ (defvar *default-database* nil "Specifies the default database to be used.") +(defun is-database-open (database) + (eql (database-state database) :open)) + (defun find-database (database &key (errorp t) (db-type nil)) "The function FIND-DATABASE, given a string DATABASE, searches amongst the connected databases for one matching the name DATABASE. If @@ -72,6 +84,16 @@ connection. If make-default is true, then *default-database* is set to the new connection, otherwise *default-database is not changed. If pool is t the connection will be taken from the general pool, if pool is a conn-pool object the connection will be taken from this pool." + + (when (stringp connection-spec) + (setq connection-spec (string-to-list-connection-spec connection-spec))) + + (unless (member database-type *loaded-database-types*) + (asdf:operate 'asdf:load-op (ensure-keyword + (concatenate 'string + (symbol-name '#:clsql-) + (symbol-name database-type))))) + (if pool (acquire-from-pool connection-spec database-type pool) (let* ((db-name (database-name-from-spec connection-spec database-type)) @@ -103,6 +125,7 @@ is a conn-pool object the connection will be taken from this pool." (setq result (database-connect connection-spec database-type))) (when result + (setf (slot-value result 'state) :open) (pushnew result *connected-databases*) (when make-default (setq *default-database* result)) result)))) @@ -130,7 +153,7 @@ this pool." (setf *connected-databases* (delete database *connected-databases*)) (when (eq database *default-database*) (setf *default-database* (car *connected-databases*))) - (change-class database 'closed-database) + (setf (slot-value database 'state) :closed) t))))) @@ -153,10 +176,10 @@ error should be signaled if the existing database connection cannot be closed. When non-nil (this is the default value) the connection is closed without error checking. When FORCE is nil, an error is signaled if the database connection has been lost." - ;; TODO: just a placeholder + ;; TODO: Support all backends. Perhaps integrate with pools + ;; Handle error and force keywords (declare (ignore database error force))) - (defun status (&optional full) "The function STATUS prints status information to the standard @@ -193,6 +216,21 @@ of full is NIL." (print-separator total-size)))) (values))) +(defun create-database (connection-spec &key database-type) + (when (stringp connection-spec) + (setq connection-spec (string-to-list-connection-spec connection-spec))) + (database-create connection-spec database-type)) + +(defun probe-database (connection-spec &key database-type) + (when (stringp connection-spec) + (setq connection-spec (string-to-list-connection-spec connection-spec))) + (database-probe connection-spec database-type)) + +(defun destroy-database (connection-spec &key database-type) + (when (stringp connection-spec) + (setq connection-spec (string-to-list-connection-spec connection-spec))) + (database-destroy connection-spec database-type)) + (defmacro with-database ((db-var connection-spec &rest connect-args) &body body) "Evaluate the body in an environment, where `db-var' is bound to the @@ -214,3 +252,4 @@ The connection is automatically closed or released to the pool on exit from the `(progv '(*default-database*) (list ,database) ,@body)) +