Skip to content

Commit

Permalink
fix functionality for querying
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap committed May 29, 2023
1 parent ad4e14d commit aeb3b36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/route_craft/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[clojure.tools.logging :as log]
[donut.dbxray :as dbx]
[kit.edge.db.postgres]
[next.jdbc :as jdbc]
[route-craft.routes :as routes]
[route-craft.xray-ext :as xray-ext]))

Expand Down
73 changes: 49 additions & 24 deletions src/route_craft/querying.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
(defn qualified-column-kw
[base-table column]
(keyword
(if (string/includes? #"\." column)
(if (string/includes? column "." )
column
(str base-table "." (name column)))))
(str (name base-table) "." (name column)))))

(defn rc-columns->select
[columns]
(mapv qualified-column-kw columns))
[{:keys [base-table]} columns]
(mapv (partial qualified-column-kw base-table) columns))

(def honeysql-joins
{"left" :left-join
Expand All @@ -29,14 +29,13 @@
(defn assoc-joins
[query-map {:keys [base-table columns]} joins]
(reduce-kv
(fn [out column-str join-type]
(let [column-kw (keyword column-str)]
(if-let [[target-table target-column] (get-in columns [column-kw :refers-to])]
(assoc out (get honeysql-joins join-type) [target-table [:=
(keyword (str (name target-table) (name target-column)))
(keyword (str (name base-table) (name column-kw)))]])
(throw (ex-info "No foreign key to join on" {:column column-kw
:type ::unsupported-column-join})))))
(fn [out column-kw join-type]
(if-let [[target-table target-column] (get-in columns [column-kw :refers-to])]
(assoc out (get honeysql-joins join-type) [target-table [:=
(keyword (str (name target-table) "." (name target-column)))
(keyword (str (name base-table) "." (name column-kw)))]])
(throw (ex-info "No foreign key to join on" {:column column-kw
:type ::unsupported-column-join}))))
query-map
joins))

Expand All @@ -58,31 +57,57 @@
[{:keys [base-table]} where-map]
(reduce-kv
(fn [out column-key v]
(let [[op value] v]
(conj out
[(get honeysql-ops op)
(qualified-column-kw base-table column-key)
value])))
(reduce-kv
(fn [acc op value]
(conj acc
[(get honeysql-ops op)
(qualified-column-kw base-table column-key)
value]))
out v))
[:and]
where-map))

(defn rc-order->hsql-order-by
[{:keys [base-table]} order-map]
(reduce-kv
(fn [out column-kw order-direction]
(conj out [(qualified-column-kw base-table column-kw) (keyword order-direction)]))
[]
order-map))
[{:keys [base-table]} orders]
(mapcat
(fn [order-map]
(reduce-kv
(fn [out column-kw order-direction]
(conj out [(qualified-column-kw base-table column-kw) (keyword order-direction)]))
[]
order-map))
orders))

(defn rc-query->query-map
"Given a validated route-craft query, convert to honeysql query map"
[{:keys [where joins limit offset columns order]} opts]
(cond-> {}
columns (assoc :select (rc-columns->select columns))
columns (assoc :select (rc-columns->select opts columns))
joins (assoc-joins opts joins)
where (assoc :where (rc-where->hsql-where opts where))
limit (assoc :limit limit)
offset (assoc :offset offset)
order (assoc :order-by (rc-order->hsql-order-by opts order))))

;; TODO: maybe this ns is candidate for memoization if enabled via config

(comment

(q/rc-where->hsql-where
{:base-table :companies
:columns (get-in x [:companies :columns])}
{:name {:eq "asdf"}
:address.care_of {:eq "qwer"}})
(q/rc-columns->select
{:base-table :companies
:columns (get-in x [:companies :columns])}
[:name :address.care_of])
(q/assoc-joins
{}
{:base-table :companies
:columns (get-in x [:tables :companies :columns])}
{:address_id "left"})
(q/rc-order->hsql-order-by
{:base-table :companies
:columns (get-in x [:tables :companies :columns])}
[{:id "asc"}]))

0 comments on commit aeb3b36

Please sign in to comment.