prototype of new checks method; tweak config to generate schema itself
This commit is contained in:
parent
a33ee8a3f6
commit
0754d3afe9
|
@ -13,7 +13,8 @@
|
|||
[honeysql "0.4.3"]
|
||||
[edl "0.1.0"]
|
||||
[org.postgresql/postgresql "9.2-1003-jdbc4"]
|
||||
[clj-logging-config "1.9.10"]]
|
||||
[clj-logging-config "1.9.10"]
|
||||
[zip-visit "1.0.2"]]
|
||||
|
||||
:profiles {:test {:dependencies [[org.clojure/tools.nrepl "0.2.3"]
|
||||
[environ "0.4.0"]
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
(ns views.core
|
||||
(:require
|
||||
[views.base-subscribed-views :as bsv]
|
||||
[views.persistence :as vp])
|
||||
[views.persistence :as vp]
|
||||
[edl.schema :refer [denormalized-schema get-schema]])
|
||||
(:import
|
||||
[views.persistence InMemoryPersistence]
|
||||
[views.base_subscribed_views BaseSubscribedViews]))
|
||||
|
||||
(defn config
|
||||
[{:keys [db schema templates persistence] :as opts}]
|
||||
(let [opts (if persistence opts (assoc opts :persistence (InMemoryPersistence.)))]
|
||||
{:db db :schema schema :templates templates :base-subscribed-views (BaseSubscribedViews. opts)}))
|
||||
[{:keys [db templates persistence] :as conf}]
|
||||
(let [schema (denormalized-schema (get-schema db (get conf :schema-name "public")))
|
||||
conf (if persistence conf (assoc conf :persistence (InMemoryPersistence.)))]
|
||||
{:db db :schema schema :templates templates :base-subscribed-views (BaseSubscribedViews. conf)}))
|
||||
|
|
35
src/views/db/checks.clj
Normal file
35
src/views/db/checks.clj
Normal file
|
@ -0,0 +1,35 @@
|
|||
(ns views.db.checks
|
||||
(:require
|
||||
[clojure.zip :as z]
|
||||
[zip.visit :as zv]
|
||||
[honeysql.core :as hsql]))
|
||||
|
||||
(defn replace-param-pred
|
||||
[]
|
||||
(zv/visitor
|
||||
:pre [n s]
|
||||
(if (and (coll? n) (string? (last n)) (= (subs (last n) 0 1) "?"))
|
||||
{:node true
|
||||
:state (conj s n)})))
|
||||
|
||||
(defn swap-wc-preds*
|
||||
[wc]
|
||||
(let [root (z/vector-zip wc)]
|
||||
(zv/visit root nil [(replace-param-pred)])))
|
||||
|
||||
(defn swap-preds
|
||||
[vm]
|
||||
(let [{:keys [node state]} (swap-wc-preds* (:where vm))]
|
||||
{:q (assoc vm :where node) :p state}))
|
||||
|
||||
(defn view-sig->dummy-args
|
||||
[view-sig]
|
||||
(map #(str "?" %) (range 0 (count (rest view-sig)))))
|
||||
|
||||
(defn view-check
|
||||
[action view-fn view-sig]
|
||||
(let [view-map (apply view-fn (view-sig->dummy-args view-sig))
|
||||
{:keys [p q]} (swap-preds view-map)]
|
||||
(-> q
|
||||
(update-in [:where] #(merge % (:where action)))
|
||||
(assoc :select (mapv second p)))))
|
22
test/views/db/checks_test.clj
Normal file
22
test/views/db/checks_test.clj
Normal file
|
@ -0,0 +1,22 @@
|
|||
(ns views.db.checks-test
|
||||
(:require
|
||||
[clojure.test :refer [deftest is run-tests]]
|
||||
[honeysql.core :as hsql]
|
||||
[honeysql.helpers :as hh]
|
||||
[views.fixtures :as vf]
|
||||
[views.db.checks :as vc]))
|
||||
|
||||
(defn view [a b] (hsql/build :select [:c :d :f] :from {:foo :f} :where [:and [:and [:= :a a] [:= :b b]]]))
|
||||
|
||||
(deftest swaps-predicates-and-extracts-clauses
|
||||
(let [{:keys [p q]} (vc/swap-preds (view "?1" "?2"))
|
||||
swapped {:where [:and [:and true true]], :from {:foo :f}, :select [:c :d :f]}]
|
||||
(is (= (set p) #{[:= :a "?1"] [:= :b "?2"]}))
|
||||
(is (= (:where q) (:where swapped)))))
|
||||
|
||||
(deftest constructs-view-check
|
||||
(let [update (hsql/build :update :foo :set {:d "d"} :where [:= :c "c"])
|
||||
check (hsql/build :select [:a :b] :from :foo :where [:and [:and true true] [:= :c "c"]])
|
||||
calcc (vc/view-check update view [:view 1 2])]
|
||||
(is (= (into #{} (:select check)) (into #{} (:select calcc))))
|
||||
(is (= (:where check) (:where calcc)))))
|
Loading…
Reference in a new issue