Skip to content

Commit

Permalink
Add 'close-cursor' to delete a declared cursor explicitly.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Aug 15, 2024
1 parent f58761b commit 103a532
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/dbd/postgres.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@
:connection conn
:sql sql))

(defmethod close-cursor-using-connection ((conn dbd-postgres-connection) (cursor dbi-cursor))
(when (cursor-declared-p cursor)
(exec-query (connection-handle conn)
(format nil "CLOSE ~A" (cursor-name cursor)))
(setf (cursor-declared-p cursor) nil)
t))

(defmethod execute-using-connection ((conn dbd-postgres-connection) (cursor dbi-cursor) params)
(assert (in-transaction conn))
(with-accessors ((sql query-sql)
Expand Down
11 changes: 11 additions & 0 deletions src/driver.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#:cursor-formatter
#:cursor-declared-p
#:make-cursor
#:close-cursor
#:close-cursor-using-connection
#:prepare
#:prepare-cached
#:execute
Expand Down Expand Up @@ -183,6 +185,10 @@ This method may be overrided by subclasses."
(error 'dbi-unimplemented-error
:method-name 'make-cursor)))

(defgeneric close-cursor (cursor)
(:method ((cursor dbi-cursor))
(close-cursor-using-connection (dbi-connection cursor) cursor)))

(defgeneric execute (query &optional params)
(:documentation "Execute `query` with `params` and return the results.")
(:method (object &optional params)
Expand Down Expand Up @@ -271,6 +277,11 @@ This method must be implemented in each drivers.")
(error 'dbi-unimplemented-error
:method-name 'execute-using-connection)))

(defgeneric close-cursor-using-connection (conn cursor)
(:method (conn cursor)
(declare (ignore conn cursor))
(error 'dbi-unimplemented-error
:method-name 'close-cursor-using-connection)))

(defgeneric begin-transaction (conn)
(:documentation "Start a transaction.")
Expand Down
7 changes: 5 additions & 2 deletions t/dbd/postgres.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#:do-sql
#:with-transaction
#:make-cursor
#:close-cursor
#:execute
#:fetch))
(in-package #:dbd-postgres-test)
Expand Down Expand Up @@ -38,8 +39,10 @@
(make-cursor conn "SELECT * FROM person")))
(execute cursor)
(ok (equal (getf (fetch cursor) :|name|) "Woody"))
(ok (equal (getf (fetch cursor) :|name|) "Buzz"))))
(ok (equal (getf (fetch cursor) :|name|) "Buzz"))
(close-cursor cursor)))
(with-transaction conn
(let ((cursor (make-cursor conn "SELECT * FROM person WHERE name = 'Trixie'")))
(execute cursor)
(ok (null (fetch cursor)))))))
(ok (null (fetch cursor)))
(close-cursor cursor)))))

0 comments on commit 103a532

Please sign in to comment.