Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Created April 17, 2021 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svetlyak40wt/fbb52a8d6ef693bddde0cb9e9e3b9d92 to your computer and use it in GitHub Desktop.
Save svetlyak40wt/fbb52a8d6ef693bddde0cb9e9e3b9d92 to your computer and use it in GitHub Desktop.
An example of using server-side cursor with fukamachi/cl-dbi
(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