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