Skip to content

Commit

Permalink
Add do-cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Aug 7, 2024
1 parent 3917d49 commit c0100e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/core/dao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
#:count-dao
#:recreate-table
#:ensure-table-exists
#:deftable))
#:deftable
#:do-cursor))
(in-package #:mito.dao)

(defun foreign-value (obj slot)
Expand Down Expand Up @@ -448,3 +449,17 @@
,@(unless (find :conc-name options :key #'car)
`((: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 ,cursor)
while ,dao
do (progn ,@body)))))
(if (dbi:in-transaction *connection*)
(,main)
(dbi:with-transaction *connection*
(,main))))))
11 changes: 11 additions & 0 deletions t/dao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@
(ok (typep row 'user))
(ok (equal (slot-value row 'name) "Btaro")))
(ok (null (mito.dao:fetch-dao cursor)))))

(let ((records '()))
(do-cursor (dao (mito.dao:select-dao 'user) i)
(push (cons i dao) records)
(when (<= 1 i)
(return)))
(ok (= (length records) 2))
(ok (every (lambda (record)
(typep (cdr record) 'user))
records)))

(when (find-class 'user nil)
(setf (find-class 'user) nil))
(disconnect-toplevel))
Expand Down

0 comments on commit c0100e1

Please sign in to comment.