Created
February 14, 2018 19:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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