Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cardinality :many in MS SQL: replace array_agg with Clojure fn #17

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/com/fulcrologic/rad/database_adapters/sql/query.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
(boolean? v) v
:else (str "'" v "'")))

(defn group-join-results
"Takes result of join query in format [{:c0 a, :c1 b}] and transforms it into
format [{:c0 a, :c1 [b ...]}]"
[result]
(->> result
(reduce (fn [acc {:keys [c0 c1]}]
(update acc c0 #(conj! (or % (transient [])) c1)))
{})
(reduce-kv (fn [acc c0 c1ts]
(conj! acc {:c0 c0, :c1 (persistent! c1ts)}))
(transient []))
persistent!))

(>defn to-many-join-column-query
[{::attr/keys [key->attribute] :as env} {::attr/keys [target cardinality identities qualified-key] :as attr} ids]
[any? ::attr/attribute coll? => (? (s/tuple string? ::attr/attributes))]
Expand All @@ -46,7 +59,7 @@
table (table-name key->attribute target-attr) ; address
target-id-column (column-name target-attr) ; id
id-list (str/join "," (map q ids))]
[(format "SELECT %1$s.%2$s AS c0, array_agg(%3$s.%4$s) AS c1 FROM %1$s LEFT JOIN %3$s ON %1$s.%2$s = %3$s.%5$s WHERE %1$s.%2$s IN (%6$s) GROUP BY %1$s.%2$s"
[(format "SELECT %1$s.%2$s AS c0, %3$s.%4$s AS c1 FROM %1$s JOIN %3$s ON %1$s.%2$s = %3$s.%5$s WHERE %1$s.%2$s IN (%6$s)"
rev-target-table rev-target-column table target-id-column column id-list)
[reverse-target-attr attr]]
(throw (ex-info "Cannot create to-many reference column." {:k qualified-key}))))))
Expand Down Expand Up @@ -153,7 +166,8 @@
results-by-id (reduce
(fn [result [join-query join-attributes]]
(let [join-rows (log/spy :debug (sql/query datasource [join-query] {:builder-fn row-builder}))
join-eql-results (log/spy :debug (sql-results->edn-results join-rows join-attributes))
join-rows-groups (log/spy :debug (group-join-results join-rows))
join-eql-results (log/spy :debug (sql-results->edn-results join-rows-groups join-attributes))
join-result-by-id (log/spy :debug (enc/keys-by id-key join-eql-results))]
(deep-merge result join-result-by-id)))
base-result-map-by-id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
[1 2 3])]
(assertions
"Generates the correct SQL"
actual => "SELECT accounts.id AS c0, array_agg(addresses.id) AS c1 FROM accounts LEFT JOIN addresses ON accounts.id = addresses.accounts_addresses_accounts_id WHERE accounts.id IN (1,2,3) GROUP BY accounts.id"
actual => "SELECT accounts.id AS c0, addresses.id AS c1 FROM accounts JOIN addresses ON accounts.id = addresses.accounts_addresses_accounts_id WHERE accounts.id IN (1,2,3)"
"Returns the attributes in the order they appear in the query"
attrs => [attrs/account-id attrs/account-addresses])))

Expand Down