r9727: 1 Jul 2004 Kevin Rosenberg <kevin@rosenberg.net>
[clsql.git] / sql / conditions.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     conditions.lisp
6 ;;;; Purpose:  Error conditions for CLSQL
7 ;;;;
8 ;;;; $Id$
9 ;;;;
10 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
16
17 (in-package #:clsql-sys)
18
19 (defvar *backend-warning-behavior* :warn
20   "Action to perform on warning messages from backend. Default is to :warn. May also be
21 set to :error to signal an error or :ignore/nil to silently ignore the warning.")
22
23 ;;; CommonSQL-compatible conditions
24  
25 (define-condition sql-condition ()
26   ())
27
28 (define-condition sql-error (simple-error)
29   ())
30
31 (define-condition sql-database-error (sql-error)
32   ((error-id :initarg :error-id 
33              :initform nil
34              :reader sql-error-error-id)
35    (secondary-error-id :initarg :secondary-error-id
36                        :initform nil
37                        :reader sql-error-secondary-error-id)
38    (database-message :initarg :message
39                      :initform nil
40                      :reader sql-error-database-message)
41    (database :initarg :database
42                      :initform nil
43                      :reader sql-error-database))
44   (:report (lambda (c stream)
45              (format stream "A database error occurred~A: ~A / ~A~%  ~A"
46                      (if (sql-error-database c)
47                          (format nil " on database ~A" (sql-error-database c))
48                          "")
49                      (sql-error-error-id c)
50                      (sql-error-secondary-error-id c)
51                      (sql-error-database-message c))))
52   (:documentation "Used to signal an error in a CLSQL database interface."))
53
54 (define-condition sql-connection-error (sql-database-error)
55   ((database-type :initarg :database-type :initform nil
56                   :reader sql-error-database-type)
57    (connection-spec :initarg :connection-spec :initform nil
58                   :reader sql-error-connection-spec))
59   (:report (lambda (c stream)
60              (format stream "While trying to connect to database ~A~%  using database-type ~A:~%  Error ~D / ~A~%  has occurred."
61                      (when (and (sql-error-connection-spec c)
62                                 (sql-error-database-type c))
63                        (database-name-from-spec
64                         (sql-error-connection-spec c)
65                         (sql-error-database-type c)))
66                      (sql-error-database-type c)
67                      (sql-error-error-id c)
68                      (sql-error-database-message c))))
69   (:documentation "Used to signal an error in connecting to a database."))
70
71 (define-condition sql-database-data-error (sql-database-error)
72   ((expression :initarg :expression :initarg nil 
73                :reader sql-error-expression))
74   (:report (lambda (c stream)
75              (format stream "While accessing database ~A~%  with expression ~S:~%  Error ~D / ~A~%  has occurred."
76                      (sql-error-database c)
77                      (sql-error-expression c)
78                      (sql-error-error-id c)
79                      (sql-error-database-message c))))
80   (:documentation "Used to signal an error with the SQL data
81   passed to a database."))
82
83 (define-condition sql-temporary-error (sql-database-error)
84   ()
85   (:documentation "Used to signal an error when the database
86 cannot currently process a valid interaction because, for
87 example, it is still executing another command possibly issued by
88 another user."))
89
90 (define-condition sql-timeout-error (sql-connection-error)
91   ()
92   (:documentation "Used to signal an error when the database
93 times out while processing some operation."))
94
95 (define-condition sql-fatal-error (sql-connection-error)
96   ()
97   (:documentation "Used to signal an error when the database
98 connection is no longer usable."))
99
100 (define-condition sql-user-error (sql-error)
101   ((message :initarg :message
102             :initform "Unspecified error"
103             :reader sql-user-error-message))
104   (:report (lambda (c stream)
105              (format stream "A CLSQL lisp code error occurred: ~A "
106                      (sql-user-error-message c))))
107   (:documentation  "Used to signal lisp errors inside CLSQL."))
108
109
110
111 ;; Signal conditions
112
113 (defun signal-closed-database-error (database)
114   (error 'sql-fatal-error
115          :database database
116          :connection-spec (when database (connection-spec database))
117          :database-type (when database (database-type database))
118          :message "Database is closed."))
119
120 (defun signal-no-database-error (database)
121   (error 'sql-database-error
122          :database database
123          :message (format nil "~A is not a database." database)))
124
125
126 ;;; CLSQL Extensions
127
128 (define-condition sql-warning (warning sql-condition)
129   ((message :initarg :message :initform nil :reader sql-warning-message))
130   (:report (lambda (c stream)
131              (format stream "~A" (sql-warning-message c)))))
132
133 (define-condition sql-database-warning (sql-warning)
134   ((database :initarg :database :reader sql-warning-database))
135   (:report (lambda (c stream)
136              (format stream 
137                      "While accessing database ~A~%  Warning: ~A~%  has occurred."
138                      (sql-warning-database c)
139                      (sql-warning-message c)))))