I'm dabbling in Clojure and HugSQL, coming from a Java + MyBatis 
background, and I'm working through approaches for avoiding the ol' 1+N 
SELECTs when retrieving nested data. Being a beginner, I'm looking for a 
general sanity check, whether in the small or the large.

In Java + MyBatis, I've used two approaches:
* Run a single query with eager joins, leaning on MyBatis to sort out my 
data graph. Very nice (but also easy to break off a little too much).
* First run a query for a driver result and then query again for an 
aggregate of sub-details, e.g. first query for order headers, then again 
for order lines WHERE ORDER_NUM IN ({{orderNumbers}}). Finally, merge the 
results to build up the object graph.

It's the latter approach that I'm working through in Clojure at the moment, 
which has led me to assoc'ing groups, as follows:

;; assoc groups of ys into corresponding xs with assoc-key, matching xs to 
ys on match-fn
(defn assoc-groups [xs ys match-fn assoc-key]
  (let [groups (group-by match-fn ys)]
    (map
      #(assoc % assoc-key (get groups (match-fn %)))
      xs)))

(def orders
  [{:order-number 1 :city "Arris Dome"}
   {:order-number 2 :city "Narshe"}
   {:order-number 3 :city "Etria"}])

(def lines
  [{:order-number 1 :item-number "BASEMENT-KEY"    :qty-ordered 1}
   {:order-number 1 :item-number "SEEDLING"        :qty-ordered 11}
   {:order-number 2 :item-number "MAGITEK-BATTERY" :qty-ordered 2}
   {:order-number 2 :item-number "WOOL-GLOVES"     :qty-ordered 22}
   {:order-number 3 :item-number "ARIADNE-THREAD"  :qty-ordered 3}])

;; apply order lines to headers, matching on :order-number
(def result (assoc-groups orders lines :order-number :lines))

;; output
=> result
({:order-number 1,
  :city "Arris Dome",
  :lines
  [{:order-number 1,
    :item-number "BASEMENT-KEY",
    :qty-ordered 1}
   {:order-number 1,
    :item-number "SEEDLING",
    :qty-ordered 11}]}
 {:order-number 2,
  :city "Narshe",
  :lines
  [{:order-number 2,
    :item-number "MAGITEK-BATTERY",
    :qty-ordered 2}
   {:order-number 2,
    :item-number "WOOL-GLOVES",
    :qty-ordered 22}]}
 {:order-number 3,
  :city "Etria",
  :lines
  [{:order-number 3,
    :item-number "ARIADNE-THREAD",
    :qty-ordered 3}]})

To my naive way of thinking, this "assoc-groups" concept seems like it 
would be fairly common, which makes me wonder if I've reinvented a wheel 
and my limited grasp of functional terminology is preventing me from 
finding it. Or, maybe it's not very common because a different approach is 
more widespread. 

I'd appreciate any quick mentoring. Thanks!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to