Permalink
Browse files

Reduce impact of patch 9b69c30, add docs

  • Loading branch information...
1 parent e075078 commit 0a537ba7e302eea2811eaf260da71015c0c2b3e5 @marijnh marijnh committed with Oct 14, 2011
Showing with 38 additions and 32 deletions.
  1. +22 −2 cl-postgres/errors.lisp
  2. +1 −0 cl-postgres/package.lisp
  3. +4 −30 cl-postgres/protocol.lisp
  4. +11 −0 doc/cl-postgres.html
View
@@ -33,8 +33,7 @@ if any.")
(query :initform *current-query* :reader database-error-query
:documentation "Query that led to the error, if any.")
(position :initarg :position :initform nil :reader database-error-position)
- (cause :initarg :cause :initform nil :reader database-error-cause)
- (constraint :initarg :constraint :initform nil :reader database-error-constraint))
+ (cause :initarg :cause :initform nil :reader database-error-cause))
(:report (lambda (err stream)
(format stream "Database error~@[ ~A~]: ~A~@[~%~A~]~@[~%Query: ~A~]"
(database-error-code err)
@@ -46,6 +45,27 @@ signal virtually all database-related errors \(though in some cases
socket errors may be raised when a connection fails on the IP
level)."))
+(defun database-error-constraint-name (err)
+ "Given a database-error for an integrity violation, will attempt to
+extract the constraint name."
+ (labels ((extract-quoted-part (string n)
+ "Extracts the Nth quoted substring from STRING."
+ (let* ((start-quote-inst (* 2 n))
+ (start-quote-pos (position-nth #\" string start-quote-inst))
+ (end-quote-pos (position #\" string :start (1+ start-quote-pos))))
+ (subseq string (1+ start-quote-pos) end-quote-pos)))
+ (position-nth (item seq n)
+ "Finds the position of the zero-indexed Nth ITEM in SEQ."
+ (loop :with pos = -1 :repeat (1+ n)
+ :do (setf pos (position item seq :start (1+ pos)))
+ :finally (return pos))))
+ (let ((message (database-error-message err)))
+ (typecase err
+ (cl-postgres-error:not-null-violation (extract-quoted-part message 0))
+ (cl-postgres-error:unique-violation (extract-quoted-part message 0))
+ (cl-postgres-error:foreign-key-violation (extract-quoted-part message 1))
+ (cl-postgres-error:check-violation (extract-quoted-part message 1))))))
+
(define-condition database-connection-error (database-error) ()
(:documentation "Conditions of this type are signalled when an error
occurs that breaks the connection socket. They offer a :reconnect
View
@@ -7,6 +7,7 @@
#:database-error-detail
#:database-error-query
#:database-error-cause
+ #:database-error-contraint-name
#:database-connection
#:database-connection-error
#:database-socket-error
View
@@ -78,43 +78,17 @@ database-error condition."
(let ((data (read-byte-delimited socket)))
(flet ((get-field (char)
(cdr (assoc char data))))
- (let* ((code (get-field #\C))
- (error-type (cl-postgres-error::get-error-type code))
- (message (get-field #\M)))
+ (let ((code (get-field #\C)))
;; These are the errors "ADMIN SHUTDOWN" and "CRASH SHUTDOWN",
;; in which case the server will close the connection right
;; away.
(when (or (string= code "57P01") (string= code "57P02"))
(ensure-socket-is-closed socket))
- (error error-type
+ (error (cl-postgres-error::get-error-type code)
:code code
- :message message
+ :message (get-field #\M)
:detail (or (get-field #\D) (get-field #\H))
- :position (get-field #\p)
- :constraint (extract-constraint-name error-type message))))))
-
-(defun extract-constraint-name (error-type message)
- "Given a PostgreSQL error CODE and MESSAGE for an integrity violation, will attempt to extract the constraint name."
- (case error-type
- (cl-postgres-error:not-null-violation (extract-quoted-part message 0))
- (cl-postgres-error:foreign-key-violation (extract-quoted-part message 1))
- (cl-postgres-error:unique-violation (extract-quoted-part message 0))
- (cl-postgres-error:check-violation (extract-quoted-part message 1))
- (t "")))
-
-(defun extract-quoted-part (string n)
- "Extracts the Nth quoted substring from STRING."
- (let* ((start-quote-inst (* 2 n))
- (start-quote-pos (position-nth #\" string start-quote-inst))
- (end-quote-pos (position #\" string :start (1+ start-quote-pos))))
- (subseq string (1+ start-quote-pos) end-quote-pos)))
-
-(defun position-nth (item seq n)
- "Finds the position of the zero-indexed Nth ITEM in SEQ."
- (loop :with pos = -1
- :repeat (1+ n)
- :do (setf pos (position item seq :start (1+ pos)))
- :finally (return pos)))
+ :position (get-field #\p))))))
(define-condition postgresql-warning (simple-warning)
())
View
@@ -552,6 +552,17 @@
<code>NIL</code> when it was not caused by another condition.</p>
<p class="def">
+ <span>function</span>
+ <a name="database-error-constraint-name"></a>
+ database-error-constraint-name (database-error)
+ <br/>&#8594; string
+ </p>
+
+ <p class="desc">For integrity-violation errors, returns the name
+ of the constraint that was violated (or <code>nil</code> if no
+ constraint was found.)</p>
+
+ <p class="def">
<span>condition</span>
<a name="database-connection-error"></a>
database-connection-error

0 comments on commit 0a537ba

Please sign in to comment.