Skip to content

Commit

Permalink
update queries to support multiple same ops on single key
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap committed May 29, 2023
1 parent aeb3b36 commit 9dc3b35
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
17 changes: 14 additions & 3 deletions src/route_craft/handlers.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns route-craft.handlers
(:require
[next.jdbc.sql :as sql]
[route-craft.queries :as queries]
[ring.util.http-response :as http-response]))

;; ===================HANDLERS===================
Expand Down Expand Up @@ -71,6 +72,12 @@
{pk-key id})
(http-response/ok [pk-key id])))

(defn query-handler
[{:keys []
:as opts}
request]
#_(queries/rc-query->query-map))

(defmulti generate-handler (fn [_opts handler] handler))

(defn malli-type
Expand Down Expand Up @@ -105,11 +112,11 @@
([opts table ignore-primary-key?] (table->malli-map opts table ignore-primary-key? {:force-optional? false}))
([{:keys [malli-type-mappings]} {:keys [columns column-order]} ignore-primary-key? column-key-opts]
(reduce
(fn [out column-key]
(fn [acc column-key]
(let [{:keys [primary-key?] :as column} (get columns column-key)]
(if (and primary-key? ignore-primary-key?)
out
(conj out (malli-column-key malli-type-mappings column-key column column-key-opts)))))
acc
(conj acc (malli-column-key malli-type-mappings column-key column column-key-opts)))))
[:map]
column-order)))

Expand Down Expand Up @@ -147,6 +154,10 @@
;; - delete by query
;; - generic query

(defmethod generate-handler :get-by-query
[{:keys [] :as opts} _]
)

(defmethod generate-handler :default
[_ handler]
(throw (ex-info "Unsupported handler" {:type ::unsupported-handler
Expand Down
49 changes: 36 additions & 13 deletions src/route_craft/querying.clj → src/route_craft/queries.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns route-craft.querying
(ns route-craft.queries
(:require
[clojure.string :as string]))

Expand Down Expand Up @@ -29,9 +29,9 @@
(defn assoc-joins
[query-map {:keys [base-table columns]} joins]
(reduce-kv
(fn [out column-kw join-type]
(fn [acc 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 [:=
(assoc acc (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
Expand All @@ -53,17 +53,31 @@
:in :in
:nin :not-in})

(defn honeysql-where-cond
[base-table column-key op value]
[(get honeysql-ops op)
(qualified-column-kw base-table column-key)
value])

(defn map->honeysql-where-cond
[base-table column-key acc v]
(reduce-kv
(fn [acc op value]
(conj acc (honeysql-where-cond base-table column-key op value)))
acc v))

(defn rc-where->hsql-where
[{:keys [base-table]} where-map]
(reduce-kv
(fn [out column-key v]
(reduce-kv
(fn [acc op value]
(conj acc
[(get honeysql-ops op)
(qualified-column-kw base-table column-key)
value]))
out v))
(fn [acc column-key v]
(if (map? v)
(map->honeysql-where-cond base-table column-key acc v)
(reduce
(fn [acc entries]
(map->honeysql-where-cond base-table column-key acc entries))
acc
v))
)
[:and]
where-map))

Expand All @@ -72,8 +86,8 @@
(mapcat
(fn [order-map]
(reduce-kv
(fn [out column-kw order-direction]
(conj out [(qualified-column-kw base-table column-kw) (keyword order-direction)]))
(fn [acc column-kw order-direction]
(conj acc [(qualified-column-kw base-table column-kw) (keyword order-direction)]))
[]
order-map))
orders))
Expand All @@ -92,12 +106,21 @@
;; TODO: maybe this ns is candidate for memoization if enabled via config

(comment
(def ctx
{:datasource (jdbc/get-datasource {:jdbcUrl "jdbc:postgresql://127.0.0.1:5432/rc?user=rc&password=rc"})})
(def x (xray-ext/extend-db-xray (dbx/xray (jdbc/get-connection (:datasource ctx)))))

(q/rc-where->hsql-where
{:base-table :companies
:columns (get-in x [:companies :columns])}
{:name {:eq "asdf"}
:address.care_of {:eq "qwer"}})
(q/rc-where->hsql-where
{:base-table :companies
:columns (get-in x [:companies :columns])}
{:name [{:eq "asdf"}
{:eq "qwer"}]
:address.care_of {:eq "qwer"}})
(q/rc-columns->select
{:base-table :companies
:columns (get-in x [:companies :columns])}
Expand Down
10 changes: 5 additions & 5 deletions src/route_craft/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@

(defn get-pk-from-table
[{:keys [columns]}]
(let [pk-cols (reduce-kv (fn [out k {:keys [primary-key?]}]
(let [pk-cols (reduce-kv (fn [acc k {:keys [primary-key?]}]
(if primary-key?
(conj out k)
out))
(conj acc k)
acc))
#{}
columns)]
(if (= 1 (count pk-cols))
Expand All @@ -64,15 +64,15 @@
:pk-key pk-key
:malli-type-mappings malli-type-mappings})]
(->> (reduce
(fn [out handler]
(fn [acc handler]
(let [path (case handler
:insert-one ["" :post]
:get-by-pk [pk-key-path :get]
:update-by-pk [pk-key-path :put]
:delete-by-pk [pk-key-path :delete]
(throw (ex-info "Unsupported handler" {:type ::unsupported-handler
:handler handler})))]
(assoc-in out path (generate-handler-fn handler))))
(assoc-in acc path (generate-handler-fn handler))))
{}
handlers)
(vec)
Expand Down
1 change: 1 addition & 0 deletions src/route_craft/xray_ext.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
columns))

;; TODO: refactor, test
;; TODO: limit rc/permitted-columns
(defn extend-db-xray
[db-xray]
(update db-xray :tables
Expand Down

0 comments on commit 9dc3b35

Please sign in to comment.