Review comments: lift (schema db) in a few places.
This commit is contained in:
parent
5ece60e3e9
commit
c344de665e
1 changed files with 21 additions and 18 deletions
|
@ -120,12 +120,12 @@
|
||||||
;; TODO: use q for searching? Have q use this for searching for a single pattern?
|
;; TODO: use q for searching? Have q use this for searching for a single pattern?
|
||||||
(<eavt [db pattern]
|
(<eavt [db pattern]
|
||||||
(let [[e a v] pattern
|
(let [[e a v] pattern
|
||||||
v (and v (ds/->SQLite schema a v))] ;; We assume e and a are always given.
|
v (and v (ds/->SQLite schema a v))] ;; We assume e and a are always given.
|
||||||
(go-pair
|
(go-pair
|
||||||
(->>
|
(->>
|
||||||
{:select [:e :a :v :tx [1 :added]] ;; TODO: generalize columns.
|
{:select [:e :a :v :tx [1 :added]] ;; TODO: generalize columns.
|
||||||
:from [:all_datoms]
|
:from [:all_datoms]
|
||||||
:where (cons :and (map #(vector := %1 %2) [:e :a :v] (take-while (comp not nil?) [e a v])))} ;; Must drop nils.
|
:where (cons :and (map #(vector := %1 %2) [:e :a :v] (take-while (comp not nil?) [e a v])))} ;; Must drop nils.
|
||||||
(sql/format)
|
(sql/format)
|
||||||
|
|
||||||
(s/all-rows (:sqlite-connection db))
|
(s/all-rows (:sqlite-connection db))
|
||||||
|
@ -135,12 +135,12 @@
|
||||||
|
|
||||||
(<avet [db pattern]
|
(<avet [db pattern]
|
||||||
(let [[a v] pattern
|
(let [[a v] pattern
|
||||||
v (ds/->SQLite schema a v)]
|
v (ds/->SQLite schema a v)]
|
||||||
(go-pair
|
(go-pair
|
||||||
(->>
|
(->>
|
||||||
{:select [:e :a :v :tx [1 :added]] ;; TODO: generalize columns.
|
{:select [:e :a :v :tx [1 :added]] ;; TODO: generalize columns.
|
||||||
:from [:all_datoms]
|
:from [:all_datoms]
|
||||||
:where [:and [:= :a a] [:= :v v] [:= :index_avet 1]]}
|
:where [:and [:= :a a] [:= :v v] [:= :index_avet 1]]}
|
||||||
(sql/format)
|
(sql/format)
|
||||||
|
|
||||||
(s/all-rows (:sqlite-connection db))
|
(s/all-rows (:sqlite-connection db))
|
||||||
|
@ -150,13 +150,13 @@
|
||||||
|
|
||||||
(<apply-datoms [db datoms]
|
(<apply-datoms [db datoms]
|
||||||
(go-pair
|
(go-pair
|
||||||
(let [exec (partial s/execute! (:sqlite-connection db))]
|
(let [exec (partial s/execute! (:sqlite-connection db))
|
||||||
|
schema (.-schema db)] ;; TODO: understand why (schema db) fails.
|
||||||
;; TODO: batch insert, batch delete.
|
;; TODO: batch insert, batch delete.
|
||||||
(doseq [datom datoms]
|
(doseq [datom datoms]
|
||||||
(let [[e a v tx added] datom
|
(let [[e a v tx added] datom
|
||||||
schema (.-schema db) ;; TODO: understand why (schema db) fails.
|
v (ds/->SQLite schema a v)
|
||||||
v (ds/->SQLite schema a v)
|
fulltext? (ds/fulltext? schema a)]
|
||||||
fulltext? (ds/fulltext? schema a)]
|
|
||||||
;; Append to transaction log.
|
;; Append to transaction log.
|
||||||
(<? (exec
|
(<? (exec
|
||||||
["INSERT INTO transactions VALUES (?, ?, ?, ?, ?)" e a v tx (if added 1 0)]))
|
["INSERT INTO transactions VALUES (?, ?, ?, ?, ?)" e a v tx (if added 1 0)]))
|
||||||
|
@ -798,8 +798,9 @@
|
||||||
;; TODO: constrain entities; constrain attributes.
|
;; TODO: constrain entities; constrain attributes.
|
||||||
|
|
||||||
(go-pair
|
(go-pair
|
||||||
(doseq [[op e a v] (:entities report)]
|
(let [schema (schema db)]
|
||||||
(ds/ensure-valid-value (schema db) a v))
|
(doseq [[op e a v] (:entities report)]
|
||||||
|
(ds/ensure-valid-value schema a v)))
|
||||||
report))
|
report))
|
||||||
|
|
||||||
(defn- <ensure-unique-constraints
|
(defn- <ensure-unique-constraints
|
||||||
|
@ -813,12 +814,13 @@
|
||||||
|
|
||||||
(go-pair
|
(go-pair
|
||||||
;; TODO: comment on applying datoms that violate uniqueness.
|
;; TODO: comment on applying datoms that violate uniqueness.
|
||||||
(let [unique-datoms (transient {})] ;; map (nil, a, v)|(e, a, nil)|(e, a, v) -> datom.
|
(let [schema (schema db)
|
||||||
|
unique-datoms (transient {})] ;; map (nil, a, v)|(e, a, nil)|(e, a, v) -> datom.
|
||||||
(doseq [[e a v tx added :as datom] (:tx-data report)]
|
(doseq [[e a v tx added :as datom] (:tx-data report)]
|
||||||
|
|
||||||
(when added
|
(when added
|
||||||
;; Check for violated :db/unique constraint between datom and existing store.
|
;; Check for violated :db/unique constraint between datom and existing store.
|
||||||
(when (ds/unique? (schema db) a)
|
(when (ds/unique? schema a)
|
||||||
(when-let [found (first (<? (<avet db [a v])))]
|
(when-let [found (first (<? (<avet db [a v])))]
|
||||||
(raise "Cannot add " datom " because of unique constraint: " found
|
(raise "Cannot add " datom " because of unique constraint: " found
|
||||||
{:error :transact/unique
|
{:error :transact/unique
|
||||||
|
@ -826,7 +828,7 @@
|
||||||
:entity datom})))
|
:entity datom})))
|
||||||
|
|
||||||
;; Check for violated :db/unique constraint between datoms.
|
;; Check for violated :db/unique constraint between datoms.
|
||||||
(when (ds/unique? (schema db) a)
|
(when (ds/unique? schema a)
|
||||||
(let [key [nil a v]]
|
(let [key [nil a v]]
|
||||||
(when-let [other (get unique-datoms key)]
|
(when-let [other (get unique-datoms key)]
|
||||||
(raise "Cannot add " datom " and " other " because together they violate unique constraint"
|
(raise "Cannot add " datom " and " other " because together they violate unique constraint"
|
||||||
|
@ -836,7 +838,7 @@
|
||||||
(assoc! unique-datoms key datom)))
|
(assoc! unique-datoms key datom)))
|
||||||
|
|
||||||
;; Check for violated :db/cardinality :db.cardinality/one constraint between datoms.
|
;; Check for violated :db/cardinality :db.cardinality/one constraint between datoms.
|
||||||
(when-not (ds/multival? (schema db) a)
|
(when-not (ds/multival? schema a)
|
||||||
(let [key [e a nil]]
|
(let [key [e a nil]]
|
||||||
(when-let [other (get unique-datoms key)]
|
(when-let [other (get unique-datoms key)]
|
||||||
(raise "Cannot add " datom " and " other " because together they violate cardinality constraint"
|
(raise "Cannot add " datom " and " other " because together they violate cardinality constraint"
|
||||||
|
@ -858,7 +860,8 @@
|
||||||
{:pre [(db? db) (report? report)]}
|
{:pre [(db? db) (report? report)]}
|
||||||
(go-pair
|
(go-pair
|
||||||
(let [initial-report report
|
(let [initial-report report
|
||||||
{tx :tx} report]
|
{tx :tx} report
|
||||||
|
schema (schema db)]
|
||||||
(loop [report initial-report
|
(loop [report initial-report
|
||||||
es (:entities initial-report)]
|
es (:entities initial-report)]
|
||||||
(let [[[op e a v :as entity] & entities] es]
|
(let [[[op e a v :as entity] & entities] es]
|
||||||
|
@ -867,7 +870,7 @@
|
||||||
report
|
report
|
||||||
|
|
||||||
(= op :db/add)
|
(= op :db/add)
|
||||||
(if (ds/multival? (schema db) a)
|
(if (ds/multival? schema a)
|
||||||
(if (empty? (<? (<eavt db [e a v])))
|
(if (empty? (<? (<eavt db [e a v])))
|
||||||
(recur (transact-report report (datom e a v tx true)) entities)
|
(recur (transact-report report (datom e a v tx true)) entities)
|
||||||
(recur report entities))
|
(recur report entities))
|
||||||
|
|
Loading…
Reference in a new issue