Created
April 17, 2021 17:38
-
-
Save svetlyak40wt/fbb52a8d6ef693bddde0cb9e9e3b9d92 to your computer and use it in GitHub Desktop.
An example of using server-side cursor with fukamachi/cl-dbi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro do-rows ((row sql &key params) &body body) | |
(with-gensyms (prepared cursor-name) | |
(let ((cursor-sql (fmt "DECLARE ~A CURSOR FOR ~A" | |
cursor-name | |
sql)) | |
(fetch-sql (fmt "FETCH FROM ~A" | |
cursor-name))) | |
`(progn | |
(dbi:do-sql *connection* ,cursor-sql ,params) | |
(loop with ,prepared = (dbi:prepare *connection* | |
,fetch-sql) | |
for ,row = (dbi:fetch (dbi:execute ,prepared)) | |
while ,row | |
do (progn ,@body)))))) | |
;;; Example usage: | |
FINANCES/DB> (with-transaction | |
(do-rows (row "SELECT * | |
FROM ods_simplywall_companies | |
WHERE price < price_target_low | |
AND total_score > ?" | |
:params '(19)) | |
(format t "ROW: ~S~%" row))) | |
ROW: (:|ticker| "NBIX" :|company_id| "DB5A2CD9-5845-45EF-8FE1-6176C911FCEA" | |
:|price_target_high| 163 :|price_target_low| 101 :|price_target| 6363/50 | |
:|total_score| 20 :|price| 4829/50 :|etl_updated| 3827657640) | |
ROW: (:|ticker| "NBIX" :|company_id| "DB5A2CD9-5845-45EF-8FE1-6176C911FCEA" | |
:|price_target_high| 163 :|price_target_low| 101 :|price_target| 6363/50 | |
:|total_score| 20 :|price| 4829/50 :|etl_updated| 3827657640) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment