Skip to content

Commit

Permalink
Use ident keywords in add/retract tuple syntax with attribute-refs? e…
Browse files Browse the repository at this point in the history
…nabled (#698)

* Make it possible to use ident keywords in add/retract tuple syntax even with attribute-refs? enabled

Fixes issue #697

* FIXUP reorder clauses

---------

Co-authored-by: Jonas Östlund <[email protected]>
Co-authored-by: Christian Weilbach <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent 87aed88 commit b249842
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/datahike/db/transaction.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@
entities)))

(defn- transact-add [{:keys [db-after] :as report} [_ e a v tx :as ent]]
(dbu/validate-attr a ent db-after)
(validate-val v ent db-after)
(let [attribute-refs? (:attribute-refs? (dbi/-config db-after))
(let [a (dbu/normalize-and-validate-attr a ent db-after)
_ (validate-val v ent db-after)
attribute-refs? (:attribute-refs? (dbi/-config db-after))
tx (or tx (current-tx report))
db db-after
e (dbu/entid-strict db e)
Expand Down Expand Up @@ -633,7 +633,7 @@
:db/add [(transact-add report op-vec) []]

:db/retract (if-some [e (dbu/entid db e)]
(let [_ (dbu/validate-attr a op-vec db)
(let [a (dbu/normalize-and-validate-attr a op-vec db)
pattern (if (nil? v)
[e a]
(let [v (if (dbu/ref? db a) (dbu/entid-strict db v) v)]
Expand All @@ -644,7 +644,7 @@
[report []])

:db.fn/retractAttribute (if-let [e (dbu/entid db e)]
(let [_ (dbu/validate-attr a op-vec db)
(let [a (dbu/normalize-and-validate-attr a op-vec db)
datoms (vec (dbi/search db [e a]))]
[(reduce transact-retract-datom report datoms)
(retract-components db datoms)])
Expand Down
19 changes: 17 additions & 2 deletions src/datahike/db/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,21 @@
(when eid
(entid-strict db eid)))

(defn attr-has-ref? [db attr]
(and (not (nil? attr))
(not (ds/is-system-keyword? attr))
(:attribute-refs? (dbi/-config db))))

(defn attr-ref-or-ident [db attr]
(if (and (not (number? attr))
(attr-has-ref? db attr))
(dbi/-ref-for db attr)
attr))

(defn attr-info
"Returns identifier name and reference value of an attributes. Both values are identical for non-reference databases."
[db attr]
(if (and (:attribute-refs? (dbi/-config db))
(not (nil? attr)))
(if (attr-has-ref? db attr)
(if (number? attr)
{:ident (dbi/-ident-for db attr) :ref attr}
{:ident attr :ref (dbi/-ref-for db attr)})
Expand Down Expand Up @@ -238,6 +248,11 @@
{:error :transact/schema :attribute attr :context at})))
(validate-attr-ident attr at db)))

(defn normalize-and-validate-attr [attr at db]
(let [attr (attr-ref-or-ident db attr)]
(validate-attr attr at db)
attr))

(defn attr->properties [k v]
(case v
:db.unique/identity [:db/unique :db.unique/identity :db/index]
Expand Down
15 changes: 15 additions & 0 deletions test/datahike/test/attribute_refs/transact_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@
(is (:had-birthday e)))
(d/release conn)))

(deftest transact-with-ident-attrs
(let [conn (setup-new-connection)
e0 (:max-eid @conn)
i (inc e0)]
(d/transact conn [[:db/add i :aka "Devil"]])
(d/transact conn [[:db/add i :aka "Tupen"]])
(is (= #{["Devil"] ["Tupen"]}
(d/q `[:find ?v
:where [~i :aka ?v]] @conn)))
(d/transact conn [[:db/retract i :aka "Tupen"]])
(is (= #{["Devil"]}
(d/q `[:find ?v
:where [~i :aka ?v]] @conn)))
(d/release conn)))

(deftest test-tuples
(let [conn (setup-new-connection)]
(d/transact conn [#:db{:ident :mapping/attributes,
Expand Down

0 comments on commit b249842

Please sign in to comment.