Skip to content

Commit 52dd568

Browse files
committed
Rename do-cursor -> do-select, and make it work in all RDBMS (without CURSOR).
1 parent a0fb9dd commit 52dd568

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/core/dao.lisp

+26-14
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#:recreate-table
6363
#:ensure-table-exists
6464
#:deftable
65-
#:do-cursor))
65+
#:do-select))
6666
(in-package #:mito.dao)
6767

6868
(defun foreign-value (obj slot)
@@ -453,16 +453,28 @@
453453
`((:conc-name ,(intern (format nil "~@:(~A-~)" name) (symbol-package name)))))
454454
,@options))
455455

456-
(defmacro do-cursor ((dao select &optional index) &body body)
457-
(with-gensyms (main cursor)
458-
`(flet ((,main ()
459-
(let* ((*want-cursor* t)
460-
(,cursor ,select))
461-
(loop ,@(and index `(for ,index from 0))
462-
for ,dao = (fetch-dao-from-cursor ,cursor)
463-
while ,dao
464-
do (progn ,@body)))))
465-
(if (dbi:in-transaction *connection*)
466-
(,main)
467-
(dbi:with-transaction *connection*
468-
(,main))))))
456+
(defmacro do-select ((dao select &optional index) &body body)
457+
(with-gensyms (main main-body select-fn cursor i)
458+
`(block nil
459+
(labels ((,main-body (,dao ,(or index i))
460+
,@(and (not index)
461+
`((declare (ignore ,i))))
462+
,@body)
463+
(,select-fn () ,select)
464+
(,main ()
465+
(case (dbi:connection-driver-type *connection*)
466+
(:postgres
467+
(let ((,cursor (let ((*want-cursor* t))
468+
(,select-fn))))
469+
(loop ,@(and index `(for ,i from 0))
470+
for ,dao = (fetch-dao-from-cursor ,cursor)
471+
while ,dao
472+
do (,main-body ,dao ,i))))
473+
(otherwise
474+
(loop ,@(and index `(for ,i from 0))
475+
for ,dao in (,select-fn)
476+
do (,main-body ,dao ,i))))))
477+
(if (dbi:in-transaction *connection*)
478+
(,main)
479+
(dbi:with-transaction *connection*
480+
(,main)))))))

t/dao.lisp

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@
270270
(ok (null (mito.dao::fetch-dao-from-cursor cursor)))))
271271

272272
(let ((records '()))
273-
(do-cursor (dao (mito.dao:select-dao 'user) i)
274-
(push (cons i dao) records)
273+
(do-select (user (mito.dao:select-dao 'user) i)
274+
(push (cons i user) records)
275275
(when (<= 1 i)
276276
(return)))
277277
(ok (= (length records) 2))

0 commit comments

Comments
 (0)