From 103a53263d1aac970157ad356116f4db32451f5a Mon Sep 17 00:00:00 2001 From: Eitaro Fukamachi Date: Thu, 15 Aug 2024 13:48:24 +0000 Subject: [PATCH] Add 'close-cursor' to delete a declared cursor explicitly. --- src/dbd/postgres.lisp | 7 +++++++ src/driver.lisp | 11 +++++++++++ t/dbd/postgres.lisp | 7 +++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/dbd/postgres.lisp b/src/dbd/postgres.lisp index 355027c..bb1f590 100644 --- a/src/dbd/postgres.lisp +++ b/src/dbd/postgres.lisp @@ -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) diff --git a/src/driver.lisp b/src/driver.lisp index edf2f02..2e55c2b 100644 --- a/src/driver.lisp +++ b/src/driver.lisp @@ -32,6 +32,8 @@ #:cursor-formatter #:cursor-declared-p #:make-cursor + #:close-cursor + #:close-cursor-using-connection #:prepare #:prepare-cached #:execute @@ -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) @@ -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.") diff --git a/t/dbd/postgres.lisp b/t/dbd/postgres.lisp index ec7f8b3..e4e2958 100644 --- a/t/dbd/postgres.lisp +++ b/t/dbd/postgres.lisp @@ -7,6 +7,7 @@ #:do-sql #:with-transaction #:make-cursor + #:close-cursor #:execute #:fetch)) (in-package #:dbd-postgres-test) @@ -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)))))