Skip to content

Instantly share code, notes, and snippets.

@teaforthecat
Created February 14, 2018 19:59
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 teaforthecat/d03b86251b654de37ee5100f462a0c5e to your computer and use it in GitHub Desktop.
Save teaforthecat/d03b86251b654de37ee5100f462a0c5e to your computer and use it in GitHub Desktop.
Hugsql custom parameter type to protect against Oracles maximum in clause items of 1000
(defmethod hugsql.parameters/apply-hugsql-param :in-many
[param data options]
(let [in-clause-split-by (get options :in-clause-split-by 1000) ;; 1000 is Oracle's max
singl-map (get options :singularize-map);; override singularize
values (get-in data (hugsql.parameters/deep-get-vec (:name param)))
singularize (fn [s] (string/replace s #"s$" ""))
column (get singl-map (:name param) (singularize (name (:name param))))
prefix (str column " in ")
join-or (str " or " column " in " )
total-count (count values)
in-groups (partition-all in-clause-split-by values)
q-marks (fn [n] (str "(" (string/join "," (repeat n "?")) ")"))
sql (str prefix
(string/join join-or
(map
(comp q-marks count)
in-groups)))]
(apply vector
sql
values)))
(comment
(hugsql.parameters/apply-hugsql-param {:type :in-many
:name :sources}
{:sources
(take 9 (repeat "x"))}
{})
;; => ["source in (?,?,?,?) or source in (?,?,?,?) or source in (?)" "x" "x" "x" "x" "x" "x" "x" "x" "x"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment