From: Kevin M. Rosenberg Date: Sun, 11 Apr 2004 10:04:35 +0000 (+0000) Subject: r8940: change db closing X-Git-Tag: v3.8.6~677 X-Git-Url: http://git.kpe.io/?a=commitdiff_plain;h=2c14c425a4a203e03b74b868fdf727ea0d48572a;hp=2ae9ee745ff9e17806178e1999b880acc64ab894;p=clsql.git r8940: change db closing --- diff --git a/base/conditions.lisp b/base/conditions.lisp index d5a918a..f6f7e7f 100644 --- a/base/conditions.lisp +++ b/base/conditions.lisp @@ -145,12 +145,11 @@ and signal an clsql-invalid-spec-error if they don't match." (format stream "The database ~A has already been closed." (clsql-closed-error-database c))))) -(define-condition clsql-nodb-error (clsql-error) - ((database :initarg :database :reader clsql-nodb-error-database)) +(define-condition clsql-no-database-error (clsql-error) + ((database :initarg :database :reader clsql-no-database-error-database)) (:report (lambda (c stream) - (format stream "No such database ~S is open." - (clsql-nodb-error-database c))))) - + (format stream "~S is not a CLSQL database." + (clsql-no-database-error-database c))))) ;; Signal conditions @@ -160,14 +159,8 @@ and signal an clsql-invalid-spec-error if they don't match." 'clsql-closed-error :database database)) -(defun signal-nodb-error (database) - (cerror "Ignore this error and return nil." - 'clsql-nodb-error - :database database)) - -(defun signal-no-database-error () - (cerror "Ignore this error and return nil." - 'clsql-nodb-error)) +(defun signal-no-database-error (database) + (error 'clsql-no-database-error :database database)) (define-condition clsql-type-error (clsql-error clsql-condition) ((slotname :initarg :slotname diff --git a/base/database.lisp b/base/database.lisp index 92599bb..5f00b38 100644 --- a/base/database.lisp +++ b/base/database.lisp @@ -32,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 @@ -85,6 +88,12 @@ 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)) @@ -116,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)))) @@ -143,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))))) diff --git a/base/db-interface.lisp b/base/db-interface.lisp index 9c8d363..4f1dc20 100644 --- a/base/db-interface.lisp +++ b/base/db-interface.lisp @@ -34,7 +34,7 @@ database type library loaded successfully.")) (:documentation "Returns database type") (:method (database) - (signal-nodb-error database))) + (signal-no-database-error database))) (defgeneric database-initialize-database-type (database-type) @@ -49,42 +49,35 @@ was called with the connection-spec.")) (defgeneric database-connect (connection-spec database-type) (:documentation "Internal generic implementation of connect.")) +(defgeneric database-reconnect (database) + (:method ((database t)) + (signal-no-database-error database)) + (:documentation "Internal generic implementation of reconnect.")) + (defgeneric database-disconnect (database) - (:method ((database closed-database)) - (signal-closed-database-error database)) (:method ((database t)) - (signal-nodb-error database)) + (signal-no-database-error database)) (:documentation "Internal generic implementation of disconnect.")) (defgeneric database-query (query-expression database result-types) - (:method (query-expression (database closed-database) result-types) - (declare (ignore query-expression result-types)) - (signal-closed-database-error database)) (:method (query-expression (database t) result-types) (declare (ignore query-expression result-types)) - (signal-nodb-error database)) + (signal-no-database-error database)) (:documentation "Internal generic implementation of query.")) (defgeneric database-execute-command (sql-expression database) - (:method (sql-expression (database closed-database)) - (declare (ignore sql-expression)) - (signal-closed-database-error database)) (:method (sql-expression (database t)) (declare (ignore sql-expression)) - (signal-nodb-error database)) + (signal-no-database-error database)) (:documentation "Internal generic implementation of execute-command.")) ;;; Mapping and iteration (defgeneric database-query-result-set (query-expression database &key full-set result-types) - (:method (query-expression (database closed-database) &key full-set result-types) - (declare (ignore query-expression full-set result-types)) - (signal-closed-database-error database) - (values nil nil nil)) (:method (query-expression (database t) &key full-set result-types) (declare (ignore query-expression full-set result-types)) - (signal-nodb-error database) + (signal-no-database-error database) (values nil nil nil)) (:documentation "Internal generic implementation of query mapping. Starts the @@ -102,21 +95,15 @@ returned otherwise. If an error occurs during query execution, the function should signal a clsql-sql-error.")) (defgeneric database-dump-result-set (result-set database) - (:method (result-set (database closed-database)) - (declare (ignore result-set)) - (signal-closed-database-error database)) (:method (result-set (database t)) (declare (ignore result-set)) - (signal-nodb-error database)) + (signal-no-database-error database)) (:documentation "Dumps the received result-set.")) (defgeneric database-store-next-row (result-set database list) - (:method (result-set (database closed-database) list) - (declare (ignore result-set list)) - (signal-closed-database-error database)) (:method (result-set (database t) list) (declare (ignore result-set list)) - (signal-nodb-error database)) + (signal-no-database-error database)) (:documentation "Returns t and stores the next row in the result set in list or returns nil when result-set is finished.")) @@ -130,6 +117,23 @@ returns nil when result-set is finished.")) "Probes for the existence of a database, returns T if database found or NIL if not found. May signal an error if unable to communicate with database server.")) +(defgeneric database-destroy (connection-spec database) + (:method (connection-spec (database t)) + (declare (ignore connection-spec)) + (signal-no-database-error database)) + (:documentation "Destroys (drops) a database.")) + +(defgeneric database-truncate (database) + (:method ((database t)) + (signal-no-database-error database)) + (:documentation "Remove all data from database.")) + +(defgeneric database-describe-table (database table) + (:method ((database t) table) + (declare (ignore table)) + (signal-no-database-error database)) + (:documentation "Return a list of name/type for columns in table")) + (defgeneric database-destory (connection-spec type) (:documentation "Destroys a database, returns T if successfull or signals an error @@ -204,3 +208,49 @@ the given lisp type and parameters.")) (defgeneric database-delete-large-object (object-id database) (:documentation "Deletes the large object in the database")) + + +;; Checks for closed database + +(defmethod database-disconnect :before ((database database)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-query :before (query-expression (database database) + result-set) + (declare (ignore query-expression result-set)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-execute-command :before (sql-expression (database database)) + (declare (ignore sql-expression)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-query-result-set :before (expr (database database) + &key full-set result-types) + (declare (ignore expr full-set result-types)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-dump-result-set :before (result-set (database database)) + (declare (ignore result-set)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-store-next-row :before (result-set (database database) list) + (declare (ignore result-set list)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-commit-transaction :before ((database database)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-start-transaction :before ((database database)) + (unless (is-database-open database) + (signal-closed-database-error database))) + +(defmethod database-abort-transaction :before ((database database)) + (unless (is-database-open database) + (signal-closed-database-error database))) diff --git a/base/utils.lisp b/base/utils.lisp index 57e3e26..98ada92 100644 --- a/base/utils.lisp +++ b/base/utils.lisp @@ -213,3 +213,21 @@ returns (VALUES string-output error-output exit-status)" (error "COMMAND-OUTPUT not implemented for this Lisp") )) + +(eval-when (:compile-toplevel :load-toplevel :execute) + (when (char= #\a (schar (symbol-name '#:a) 0)) + (pushnew :lowercase-reader *features*))) + +(defun string-default-case (str) + #-lowercase-reader + (string-upcase str) + #+lowercase-reader + (string-downcase str)) + +;; From KMRCL +(defun ensure-keyword (name) + "Returns keyword for a name" + (etypecase name + (keyword name) + (string (nth-value 0 (intern (string-default-case name) :keyword))) + (symbol (nth-value 0 (intern (symbol-name name) :keyword))))) diff --git a/classic/package.lisp b/classic/package.lisp index e1253bd..42cd973 100644 --- a/classic/package.lisp +++ b/classic/package.lisp @@ -67,7 +67,8 @@ #:database #:database-name - #:closed-database + #:database-type + #:is-database-open #:database-name-from-spec ;; utils.lisp