Skip to content

Instantly share code, notes, and snippets.

@duelinmarkers
Created February 14, 2009 13:04
Show Gist options
  • Save duelinmarkers/64357 to your computer and use it in GitHub Desktop.
Save duelinmarkers/64357 to your computer and use it in GitHub Desktop.
; Thoughts on a query API for clj-record.
(require '[clj-record.query :as q)
(manufacturer/find-records {
:founded "1910" ; still just a simple equality
:grade (q/between 85 95)
:name (q/in "Ford" "GM")})
; where between and in return functions that know what they're about ...
; specifically they'd have to return a parameterized SQL condition string
; and one or more value parameters.
; The only challenge is that I have to recognize those guys
; when I go to create sql out of a conditions map.
; My only idea so far is to use (fn? ...) and if true
; let it spit itself out.
; (Note I wouldn't want to use (ifn? ...) there because
; eventually we want to allow serialization of clojure data structures,
; and maps and sets implement IFn.)
; So where currently clj-record.core/to-conditions has something like this:
(if (nil? value)
[(conj parameterized-conditions (format "%s IS NULL" (name attribute))) values]
[(conj parameterized-conditions (format "%s = ?" (name attribute))) (conj values value)]))
; It would change to something like
(cond
(nil? value)
[(conj parameterized-conditions (format "%s IS NULL" (name attribute))) values]
(fn? value)
(let [[new-condition new-values] (value attribute)]
[(conj parameterized-conditions new-condition) (apply conj values new-values)])
:else
[(conj parameterized-conditions (format "%s = ?" (name attribute))) (conj values value)]))
; Can you think of any downside to making this the one-and-only reason
; you can put a function into a conditions map?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment