-
Notifications
You must be signed in to change notification settings - Fork 32
/
constraints.clj
75 lines (65 loc) · 3.71 KB
/
constraints.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;; Copyright (c) 2011-2015 Michael S. Klishin, Alex Petrov, and The ClojureWerkz
;; Team
;;
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns clojurewerkz.neocons.rest.constraints
"Operations on constraints (Neo4J 2.0+ only)."
(:require [clojurewerkz.neocons.rest :as rest]
[cheshire.core :as json]
[clojurewerkz.neocons.rest.conversion :as conv]
[clojurewerkz.support.http.statuses :refer [missing?]])
(:import clojurewerkz.neocons.rest.Connection)
(:refer-clojure :exclude [rest]))
(defn- get-url
[^Connection connection ^String label]
(str (get-in connection [:endpoint :uri]) "schema/constraint/" (conv/encode-kw-to-string label)))
(defn- get-uniqueness-url
[^Connection connection label]
(str (get-url connection label) "/uniqueness"))
(defn create-unique
"Creates a unique constraint on a given label and property.
See http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-create-uniqueness-constraint"
[^Connection connection label property]
(let [req-body (json/encode {"property_keys" [(conv/kw-to-string property)]})
{:keys [status headers body]} (rest/POST connection (get-uniqueness-url connection label)
:body req-body)]
(when-not (missing? status)
(conv/map-values-to-kw
(json/decode body true)
[:label :property-keys]))))
(defn- get-uniquess-constraints
[^Connection connection label ^String uri]
(let [{:keys [status headers body]} (rest/GET connection (str (get-url connection label)
uri))]
(when-not (missing? status)
(map
#(conv/map-values-to-kw % [:label :property-keys])
(json/decode body true)))))
(defn get-unique
"Gets information about a unique constraint on a given label and a property.
If no property is passed, gets all the various uniqueness constraints for that label.
See http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-get-all-uniqueness-constraints-for-a-label
and http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-get-all-constraints-for-a-label"
([^Connection connection label]
(get-uniquess-constraints connection label "/uniqueness"))
([^Connection connection label property]
(get-uniquess-constraints connection label (str "/uniqueness/" (conv/encode-kw-to-string property)))))
(defn get-all
"Gets information about all the different constraints associated with a label.
See http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-get-all-constraints-for-a-label
If no label is passed, gets information about all the constraints.
http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-get-all-constraints"
([^Connection connection ]
(get-uniquess-constraints connection "" ""))
([^Connection connection label]
(get-uniquess-constraints connection label "")))
(defn drop-unique
"Drops an existing uniquenss constraint on an label and property.
See http://docs.neo4j.org/chunked/milestone/rest-api-schema-constraints.html#rest-api-drop-constraint"
[^Connection connection label property]
(rest/DELETE connection (str (get-uniqueness-url connection label) "/" (conv/encode-kw-to-string property))))