r8940: change db closing
authorKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 11 Apr 2004 10:04:35 +0000 (10:04 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 11 Apr 2004 10:04:35 +0000 (10:04 +0000)
base/conditions.lisp
base/database.lisp
base/db-interface.lisp
base/utils.lisp
classic/package.lisp

index d5a918ade3ad10388a10d906cc8d4c0ee04cbd75..f6f7e7f0364c165a926b229adf629529ba9e5a07 100644 (file)
@@ -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
index 92599bb83075be8f92db89df5b42f5d21e9d219a..5f00b3867aee1b08c8b23d2eb7b712e23fbfca79 100644 (file)
@@ -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)))))
 
 
index 9c8d363646aa9604d3b7127d4bae0310f7e45760..4f1dc209ee4eb218d9085cac8854618e4fe3323c 100644 (file)
@@ -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)))
index 57e3e26fb5f0559de0c2a43e3e447adbd0c5b216..98ada92fca4932c6658100063ca9d59d78d2f623 100644 (file)
@@ -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)))))
index e1253bd415a90bf4009aaf54b84a898d6098ab02..42cd973f739aecf7c36ce83962159531e71101f7 100644 (file)
@@ -67,7 +67,8 @@
         
         #:database
         #:database-name
-        #:closed-database
+        #:database-type
+        #:is-database-open
         #:database-name-from-spec
         
         ;; utils.lisp