Skip to content

Instantly share code, notes, and snippets.

@huangjs
Created October 10, 2012 14:57
Show Gist options
  • Save huangjs/3866151 to your computer and use it in GitHub Desktop.
Save huangjs/3866151 to your computer and use it in GitHub Desktop.
query-prepared and flet-prepared for postmodern
(defmacro query-preparing (query &rest args/format)
"Like query, but make the query prepared (only once)."
(let* ((format :rows)
(args (loop for arg in args/format
if (or (postmodern::dao-spec-for-format arg)
(assoc arg postmodern::*result-styles*))
do (setf format arg)
else
collect arg)))
`(funcall
(load-time-value
(prepare ,query ,format))
,@args)))
(defmacro flet-prepared (&whole whole
(#1=(name (&rest args) (query &rest query-args) &optional (format :row))
&rest rest-definitions)
&body body)
"like defprepared-with-names, but only bound locally"
(declare (ignorable name args query query-args format rest-definitions))
(labels ((generate-flet-clause (spec)
(destructuring-bind #1# spec
`(,name ,args
(funcall
(load-time-value
(prepare ,query ,format))
,@query-args)))))
`(flet ,(mapcar #'generate-flet-clause (second (ldiff whole body)))
(locally ,@body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment