Skip to content

Commit

Permalink
Add named graph support to insert! and remove! functions (#15)
Browse files Browse the repository at this point in the history
* provide overrides for the insert! function and the remove! function that can accept a named graph uri
  • Loading branch information
joephayes authored Sep 27, 2020
1 parent 8cbcec1 commit 36b7cb4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(defproject stardog-clj "7.2.0"
(defproject stardog-clj "7.2.1"
:description "Stardog-clj: Clojure bindings for Stardog"
:url "http://stardog.com"
:license {:name "Apache License"
Expand Down
44 changes: 27 additions & 17 deletions src/stardog/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
[db-spec]
(let [{:keys [url user pass db
max-idle min-pool max-pool reasoning]} db-spec
con-config (-> (ConnectionConfiguration/to db )
con-config (-> (ConnectionConfiguration/to db)
(.server url)
(.credentials user pass)
(.reasoning reasoning)
)
(.reasoning reasoning))

pool-config (-> (ConnectionPoolConfig/using con-config)
(.minPool min-pool)
(.maxIdle max-idle)
Expand Down Expand Up @@ -252,23 +252,33 @@

(defn insert!
"Inserts a statement (subject, predicate, object) represented as a 3 item vector"
[^Connection connection triple-list]
(when (< (count triple-list) 3) (throw (IllegalArgumentException. "triple-list must have 3 elements")))
(let [adder (.add connection)
subj (-> (first triple-list) (values/as-uri) (values/convert) )
pred (-> (second triple-list) (values/as-uri) (values/convert))
obj (-> (nth triple-list 2) (values/convert))]
(.statement adder (StatementImpl. subj pred obj Values/DEFAULT_GRAPH))))
([^Connection connection triple-list]
(insert! connection triple-list Values/DEFAULT_GRAPH))
([^Connection connection triple-list graph-uri]
(when (< (count triple-list) 3) (throw (IllegalArgumentException. "triple-list must have 3 elements")))
(let [adder (.add connection)
subj (-> (first triple-list) (values/as-uri) (values/convert))
pred (-> (second triple-list) (values/as-uri) (values/convert))
obj (-> (nth triple-list 2) (values/convert))
context (if (instance? com.stardog.stark.impl.IRIImpl graph-uri)
graph-uri
(values/convert (values/as-uri graph-uri)))]
(.statement adder (StatementImpl. subj pred obj context)))))

(defn remove!
"Removes a statements (subject, predicate, object) represented as a 3 item vector"
[^Connection connection triple-list]
(when (< (count triple-list) 3) (throw (IllegalArgumentException. "triple-list must have 3 elements")))
(let [remover (.remove connection)
subj (-> (first triple-list) (values/as-uri) (values/convert))
pred (-> (second triple-list) (values/as-uri) (values/convert))
obj (-> (nth triple-list 2) (values/convert))]
(.statement remover (StatementImpl. subj pred obj Values/DEFAULT_GRAPH))))
([^Connection connection triple-list]
(remove! connection triple-list Values/DEFAULT_GRAPH))
([^Connection connection triple-list graph-uri]
(when (< (count triple-list) 3) (throw (IllegalArgumentException. "triple-list must have 3 elements")))
(let [remover (.remove connection)
subj (-> (first triple-list) (values/as-uri) (values/convert))
pred (-> (second triple-list) (values/as-uri) (values/convert))
obj (-> (nth triple-list 2) (values/convert))
context (if (instance? com.stardog.stark.impl.IRIImpl graph-uri)
graph-uri
(values/convert (values/as-uri graph-uri)))]
(.statement remover (StatementImpl. subj pred obj context)))))

(defn add-ns!
"Adds a namespace prefix"
Expand Down
25 changes: 20 additions & 5 deletions test/stardog/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
(fact "Insert a vector representing a triple"
(with-open [c (connect test-db-spec)]
(with-transaction [c] (remove! c ["urn:test" "urn:test:clj:prop" "Hello World"]))) => truthy)
(fact "Insert a vector representing a triple into a named graph"
(with-open [c (connect test-db-spec)]
(with-transaction [c] (remove! c ["urn:test" "urn:test:clj:prop" "Hello World"] "urn:test:graph"))) => truthy)
(fact "Attempting to insert a partial statement throws IllegalArgumentException"
(with-open [c (connect test-db-spec)]
(with-transaction [c] (insert! c ["urn:test" "urn:test:clj:prop"]))) => (throws IllegalArgumentException))
Expand All @@ -68,13 +71,25 @@
(with-transaction [c]
(insert! c ["urn:test" "urn:test:clj:prop2" "Hello World"])
(insert! c ["urn:test" "urn:test:clj:prop2" "Hello World2"]))
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop2> ?o } LIMIT 5")) => 2) )
(fact "Multiple inserts in a tx"
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop2> ?o } LIMIT 5")) => 2))
(fact "Multiple inserts into a named graph in a tx"
(with-open [c (connect test-db-spec)]
(with-transaction [c]
(insert! c ["urn:test" "urn:test:clj:prop2" "Hello World"] "urn:test:graph")
(insert! c ["urn:test" "urn:test:clj:prop2" "Hello World2"] "urn:test:graph"))
(count (query c "select ?s ?p ?o WHERE { GRAPH <urn:test:graph> { ?s <urn:test:clj:prop2> ?o }} LIMIT 5")) => 2))
(fact "Multiple removes in a tx"
(with-open [c (connect test-db-spec)]
(with-transaction [c]
(remove! c ["urn:test" "urn:test:clj:prop2" "Hello World"])
(remove! c ["urn:test" "urn:test:clj:prop2" "Hello World2"]))
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop2> ?o } LIMIT 5")) => 0) ))
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop2> ?o } LIMIT 5")) => 0))
(fact "Multiple removes from a named graph in a tx"
(with-open [c (connect test-db-spec)]
(with-transaction [c]
(remove! c ["urn:test" "urn:test:clj:prop2" "Hello World"] "urn:test:graph")
(remove! c ["urn:test" "urn:test:clj:prop2" "Hello World2"] "urn:test:graph"))
(count (query c "select ?s ?p ?o WHERE { GRAPH <urn:test:graph> {?s <urn:test:clj:prop2> ?o }} LIMIT 5")) => 0)))


(facts "About query converter handling"
Expand Down Expand Up @@ -126,7 +141,7 @@
(fn [conn]
(insert! conn ["urn:test" "urn:test:clj:prop3" "Hello World"])))
(with-connection-pool [c ds]
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop3> ?o } LIMIT 5")) => 1) )))
(count (query c "select ?s ?p ?o WHERE { ?s <urn:test:clj:prop3> ?o } LIMIT 5")) => 1))))


(facts "About running a construct query"
Expand All @@ -149,4 +164,4 @@
(remove-ns! c "myns") => truthy))
(fact "list namespaces"
(with-open [c (connect test-db-spec)]
(count (list-namespaces c)) => 98)))
(count (list-namespaces c)) => 96)))

0 comments on commit 36b7cb4

Please sign in to comment.