updates for recent views changes. analyzed sql cache now kept global

just a single cache for all view systems that might be in use. i don't
think this should really be a problem
This commit is contained in:
Gered 2016-05-29 19:02:39 -04:00
parent a86d4eef17
commit 9f2bfc1820

View file

@ -6,7 +6,8 @@
(net.sf.jsqlparser.statement.update Update)
(net.sf.jsqlparser.statement.delete Delete)
(net.sf.jsqlparser.statement.insert Insert)
(net.sf.jsqlparser.statement.select Select))
(net.sf.jsqlparser.statement.select Select)
(clojure.lang Atom))
(:require
[clojure.java.jdbc :as jdbc]
[views.core :as views]))
@ -50,12 +51,14 @@
:returning? (sql-stmt-returning? stmt stmt-type)
:tables (get-query-tables-set stmt)})))
(defonce query-info-cache (atom {}))
(defn query-info
[^String sql]
(if-let [info (get-in @views/view-system [:views-sql :cache sql])]
(if-let [info (get @query-info-cache sql)]
info
(let [info (get-query-info sql)]
(swap! views/view-system assoc-in [:views-sql :cache sql] info)
(swap! query-info-cache assoc sql info)
info)))
(defn query-tables
@ -67,7 +70,7 @@
vexec! calls which need to be run in a transaction. Holds all view system hints
generated by any vexec! calls within the transaction until the end, at which point
they are all sent to the view system."
[binding & forms]
[^Atom view-system binding & forms]
(let [tvar (first binding)
db (second binding)
args (drop 2 binding)]
@ -77,7 +80,7 @@
result# (jdbc/with-db-transaction [t# ~db ~@args]
(let [~tvar (assoc ~db :views-sql/hints hints#)]
~@forms))]
(views/put-hints! @hints#)
(views/put-hints! ~view-system @hints#)
result#))))
(defn- execute-sql!
@ -87,14 +90,14 @@
(jdbc/execute! db sqlvec)))
(defn- vexec!*
[db [sql & params :as sqlvec] {:keys [parse? namespace tables returning?] :as options}]
[^Atom view-system db [sql & params :as sqlvec] {:keys [parse? namespace tables returning?] :as options}]
(let [tables (if parse? (query-tables sql) tables)
returning? (if parse? (:returning? (query-info sql)) returning?)
results (execute-sql! db sqlvec returning?)
hint (views/hint namespace tables hint-type)]
(if-let [tx-hints (:views-sql/hints db)]
(swap! tx-hints conj hint)
(views/put-hints! [hint]))
(views/put-hints! view-system [hint]))
results))
(defn vexec!
@ -120,16 +123,18 @@
RETURNING clause, you should specify :returning true in the options given
to vexec!, since automatic detection of this will not work if the SQL cannot
be parsed."
{:arglists '([db sqlvec & options]
[db sqlvec tables & options])}
[db sqlvec & options]
{:arglists '([^Atom view-system db sqlvec & options]
[^Atom view-system db sqlvec tables & options])}
[^Atom view-system db sqlvec & options]
(let [[first-option & [other-options]] options]
(if (or (nil? options)
(map? first-option))
(vexec!* db sqlvec (-> first-option
(assoc :parse? true)
(dissoc :returning?)))
(vexec!* db sqlvec (assoc other-options
:tables (set first-option)
:parse? false)))))
(vexec!* view-system db sqlvec
(-> first-option
(assoc :parse? true)
(dissoc :returning?)))
(vexec!* view-system db sqlvec
(assoc other-options
:tables (set first-option)
:parse? false)))))