Skip to content

Instantly share code, notes, and snippets.

@proprietary
Created July 27, 2017 05:46
Show Gist options
  • Save proprietary/9ad04ee70996875d393cec519e2c2628 to your computer and use it in GitHub Desktop.
Save proprietary/9ad04ee70996875d393cec519e2c2628 to your computer and use it in GitHub Desktop.
rough draft of a clojure macro automating the U in CRUD
(defn get-columns
[_]
#{"part_id" "name" "image_paths"})
(defn update
[_ _ _]
"UPDATED!!")
(defmacro UPDATER
[table]
(let [column-names (vec (get-columns table))
column-symbols (map symbol column-names)
column-names-and-symbols (zipmap column-names column-symbols)]
`(fn [& {:keys [~@column-symbols ~'where]
:or ~(zipmap column-symbols
(repeat (count column-names) nil))}]
(let [#_~@(reduce into [] (for [col column-symbols] [col col]))
provided-columns# (for [col-name# ~column-names
:let [col-sym# (get ~column-names-and-symbols col-name#)]
:when col-sym#]
col-name#)
provided-columns-and-values# (zipmap (map keyword provided-columns#)
(map ~column-names-and-symbols provided-columns#))]
(println (pr-str provided-columns#))
(println (pr-str provided-columns-and-values#))
;; don't forget to wrap `where` with `build-where-clause`
(update ~(eval `(keyword '~table)) provided-columns-and-values# ~'where)
#_(zipmap ~column-symbols provided-columns#)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment