Skip to content

Commit

Permalink
Rename do-cursor -> do-for-dao, and make it work in all RDBMS (wi…
Browse files Browse the repository at this point in the history
…thout CURSOR).
  • Loading branch information
fukamachi committed Aug 9, 2024
1 parent a0fb9dd commit e1fc043
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
40 changes: 26 additions & 14 deletions src/core/dao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#:recreate-table
#:ensure-table-exists
#:deftable
#:do-cursor))
#:do-for-dao))
(in-package #:mito.dao)

(defun foreign-value (obj slot)
Expand Down Expand Up @@ -453,16 +453,28 @@
`((:conc-name ,(intern (format nil "~@:(~A-~)" name) (symbol-package name)))))
,@options))

(defmacro do-cursor ((dao select &optional index) &body body)
(with-gensyms (main cursor)
`(flet ((,main ()
(let* ((*want-cursor* t)
(,cursor ,select))
(loop ,@(and index `(for ,index from 0))
for ,dao = (fetch-dao-from-cursor ,cursor)
while ,dao
do (progn ,@body)))))
(if (dbi:in-transaction *connection*)
(,main)
(dbi:with-transaction *connection*
(,main))))))
(defmacro do-for-dao ((dao select &optional index) &body body)
(with-gensyms (main main-body select-fn cursor i)
`(block nil
(labels ((,main-body (,dao ,(or index i))
,@(and (not index)
`((declare (ignore ,i))))
,@body)
(,select-fn () ,select)
(,main ()
(case (dbi:connection-driver-type *connection*)
(:postgres
(let ((,cursor (let ((*want-cursor* t))
(,select-fn))))
(loop ,@(and index `(for ,i from 0))
for ,dao = (fetch-dao-from-cursor ,cursor)
while ,dao
do (,main-body ,dao ,i))))
(otherwise
(loop ,@(and index `(for ,i from 0))
for ,dao in (,select-fn)
do (,main-body ,dao ,i))))))
(if (dbi:in-transaction *connection*)
(,main)
(dbi:with-transaction *connection*
(,main)))))))
4 changes: 2 additions & 2 deletions t/dao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@
(ok (null (mito.dao::fetch-dao-from-cursor cursor)))))

(let ((records '()))
(do-cursor (dao (mito.dao:select-dao 'user) i)
(push (cons i dao) records)
(do-for-dao (user (mito.dao:select-dao 'user) i)
(push (cons i user) records)
(when (<= 1 i)
(return)))
(ok (= (length records) 2))
Expand Down

0 comments on commit e1fc043

Please sign in to comment.