1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: conditions.lisp
6 ;;;; Purpose: Error conditions for high-level SQL interface
7 ;;;; Programmers: Kevin M. Rosenberg based on
8 ;;;; Original code by Pierre R. Mai
9 ;;;; Date Started: Feb 2002
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
16 ;;;; CLSQL users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;; *************************************************************************
21 (in-package #:clsql-base-sys)
24 (define-condition clsql-condition ()
27 (define-condition clsql-error (error clsql-condition)
30 (define-condition clsql-simple-error (simple-condition clsql-error)
33 (define-condition clsql-warning (warning clsql-condition)
36 (define-condition clsql-simple-warning (simple-condition clsql-warning)
39 (define-condition clsql-invalid-spec-error (clsql-error)
40 ((connection-spec :initarg :connection-spec
41 :reader clsql-invalid-spec-error-connection-spec)
42 (database-type :initarg :database-type
43 :reader clsql-invalid-spec-error-database-type)
44 (template :initarg :template
45 :reader clsql-invalid-spec-error-template))
46 (:report (lambda (c stream)
47 (format stream "The connection specification ~A~%is invalid for database type ~A.~%The connection specification must conform to ~A"
48 (clsql-invalid-spec-error-connection-spec c)
49 (clsql-invalid-spec-error-database-type c)
50 (clsql-invalid-spec-error-template c)))))
52 (defmacro check-connection-spec (connection-spec database-type template)
53 "Check the connection specification against the provided template,
54 and signal an clsql-invalid-spec-error if they don't match."
56 (destructuring-bind ,template ,connection-spec
57 (declare (ignore ,@(remove '&optional template)))
59 (error () (error 'clsql-invalid-spec-error
60 :connection-spec ,connection-spec
61 :database-type ,database-type
62 :template (quote ,template)))))
64 (define-condition clsql-access-error (clsql-error)
65 ((database-type :initarg :database-type
66 :reader clsql-access-error-database-type)
67 (connection-spec :initarg :connection-spec
68 :reader clsql-access-error-connection-spec)
69 (error :initarg :error :reader clsql-access-error-error))
70 (:report (lambda (c stream)
71 (format stream "While trying to access database ~A~% using database-type ~A:~% Error ~A~% has occurred."
72 (database-name-from-spec
73 (clsql-access-error-connection-spec c)
74 (clsql-access-error-database-type c))
75 (clsql-access-error-database-type c)
76 (clsql-access-error-error c)))))
78 (define-condition clsql-connect-error (clsql-access-error)
79 ((errno :initarg :errno :reader clsql-connect-error-errno))
80 (:report (lambda (c stream)
81 (format stream "While trying to connect to database ~A~% using database-type ~A:~% Error ~D / ~A~% has occurred."
82 (database-name-from-spec
83 (clsql-access-error-connection-spec c)
84 (clsql-access-error-database-type c))
85 (clsql-access-error-database-type c)
86 (clsql-connect-error-errno c)
87 (clsql-access-error-error c)))))
89 (define-condition clsql-sql-error (clsql-error)
90 ((database :initarg :database :reader clsql-sql-error-database)
91 (expression :initarg :expression :reader clsql-sql-error-expression)
92 (errno :initarg :errno :reader clsql-sql-error-errno)
93 (error :initarg :error :reader clsql-sql-error-error))
94 (:report (lambda (c stream)
95 (format stream "While accessing database ~A~% with expression ~S:~% Error ~D / ~A~% has occurred."
96 (clsql-sql-error-database c)
97 (clsql-sql-error-expression c)
98 (clsql-sql-error-errno c)
99 (clsql-sql-error-error c)))))
101 (define-condition clsql-database-warning (clsql-warning)
102 ((database :initarg :database :reader clsql-database-warning-database)
103 (message :initarg :message :reader clsql-database-warning-message))
104 (:report (lambda (c stream)
105 (format stream "While accessing database ~A~% Warning: ~A~% has occurred."
106 (clsql-database-warning-database c)
107 (clsql-database-warning-message c)))))
109 (define-condition clsql-exists-condition (clsql-condition)
110 ((old-db :initarg :old-db :reader clsql-exists-condition-old-db)
111 (new-db :initarg :new-db :reader clsql-exists-condition-new-db
113 (:report (lambda (c stream)
114 (format stream "In call to ~S:~%" 'connect)
116 ((null (clsql-exists-condition-new-db c))
118 " There is an existing connection ~A to database ~A."
119 (clsql-exists-condition-old-db c)
120 (database-name (clsql-exists-condition-old-db c))))
121 ((eq (clsql-exists-condition-new-db c)
122 (clsql-exists-condition-old-db c))
124 " Using existing connection ~A to database ~A."
125 (clsql-exists-condition-old-db c)
126 (database-name (clsql-exists-condition-old-db c))))
129 " Created new connection ~A to database ~A~%, although there is an existing connection (~A)."
130 (clsql-exists-condition-new-db c)
131 (database-name (clsql-exists-condition-new-db c))
132 (clsql-exists-condition-old-db c)))))))
134 (define-condition clsql-exists-warning (clsql-exists-condition
138 (define-condition clsql-exists-error (clsql-exists-condition
142 (define-condition clsql-closed-error (clsql-error)
143 ((database :initarg :database :reader clsql-closed-error-database))
144 (:report (lambda (c stream)
145 (format stream "The database ~A has already been closed."
146 (clsql-closed-error-database c)))))
148 (define-condition clsql-no-database-error (clsql-error)
149 ((database :initarg :database :reader clsql-no-database-error-database))
150 (:report (lambda (c stream)
151 (format stream "~S is not a CLSQL database."
152 (clsql-no-database-error-database c)))))
157 (defun signal-closed-database-error (database)
158 (cerror "Ignore this error and return nil."
162 (defun signal-no-database-error (database)
163 (error 'clsql-no-database-error :database database))
165 (define-condition clsql-type-error (clsql-error clsql-condition)
166 ((slotname :initarg :slotname
167 :reader clsql-type-error-slotname)
168 (typespec :initarg :typespec
169 :reader clsql-type-error-typespec)
170 (value :initarg :value
171 :reader clsql-type-error-value))
172 (:report (lambda (c stream)
174 "Invalid value ~A in slot ~A, not of type ~A."
175 (clsql-type-error-value c)
176 (clsql-type-error-slotname c)
177 (clsql-type-error-typespec c)))))
179 (define-condition clsql-sql-syntax-error (clsql-error)
180 ((reason :initarg :reason
181 :reader clsql-sql-syntax-error-reason))
182 (:report (lambda (c stream)
183 (format stream "Invalid SQL syntax: ~A"
184 (clsql-sql-syntax-error-reason c)))))