I recently figured out how the sequence generators can be helpful in my program and I was putting together a function to generate all the rows of the database in my app, but I realized I have a problem.<br><br>I am using Jay McCarthy's SQLite module and using prepare, load-params, step and finalize to generate all the matching rows. I see that I could use the exec function, but I was interested in using sequences for stylistic reasons and to learn sequences better.<br>
<br>My problem is how to call finalize correctly. In the ideal case, where the entire sequence is iterated on, I can clean up in the functions the determine whether or not to continue the sequence, but it seems that if the sequence is short circuited as in a for/and or for/or then my statement cannot be finalized and the database will stay locked. <br>
<br>From reading the docs I couldn't think of a way to ensure that I was releasing the resources correctly, but I was wondering if anyone had an idea.<br><br>Here is the code that I wrote:<br><br>(define (in-database-file-indexes (where #f) . rest)<br>
(let ([stmt (prepare <br> (database) <br> (if where<br> (format "SELECT file_index FROM media WHERE ~A" where)<br> "SELECT file_index FROM media"))])<br>
(make-do-sequence<br> (lambda ()<br> (values<br> (lambda (p) <br> (and-let* ([r (step p)])<br> (vector-ref r 0)))<br> (lambda (p) p)<br> (begin<br> (when (and where<br>
(not (null? rest)))<br> (apply load-params stmt rest))<br> stmt)<br> (lambda (p) #t)<br> (lambda (v) <br> (if v <br> #t<br> (begin<br>
(finalize stmt)<br> #f)))<br> (lambda (p v) #t))))))<br><br>-- <br>Christopher W. Bowron <<a href="mailto:chris@bowron.us">chris@bowron.us</a>><br>[ Nothing is exciting if you know what the outcome will be ]<br>
- Joseph Campbell<br>