Skip to content

Commit

Permalink
Added basic bolt API
Browse files Browse the repository at this point in the history
  • Loading branch information
ducky427 committed Jul 29, 2016
1 parent 592214b commit 4cec63c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(defproject clojurewerkz/neocons "3.2.0-SNAPSHOT"
:description "Neocons is a feature rich idiomatic Clojure client for the Neo4J REST API"
:description "Neocons is a feature rich idiomatic Clojure client for the Neo4J REST API. It also supports Bolt protocol."
:url "http://clojureneo4j.info"
:license {:name "Eclipse Public License"}
:min-lein-version "2.5.1"
:dependencies [[org.clojure/clojure "1.7.0"]
[cheshire "5.5.0"]
[clj-http "2.0.0" :exclusions [org.clojure/clojure]]
[clojurewerkz/support "1.1.0" :exclusions [com.google.guava/guava]]]
[clojurewerkz/support "1.1.0" :exclusions [com.google.guava/guava]]
[org.neo4j.driver/neo4j-java-driver "1.0.4"]]
:test-selectors {:default (fn [m] (and (not (:time-consuming m))
(not (:http-auth m))
(not (:edge-features m))
Expand Down
45 changes: 45 additions & 0 deletions src/clojure/clojurewerkz/neocons/bolt.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
;; 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.bolt
(:require [clojure.string :as string])
(:import (java.util Map)
(org.neo4j.driver.v1 AuthTokens Config Driver
GraphDatabase Record Session
StatementResult Values)))

(defn- env-var
[^String s]
(get (System/getenv) s))

(defn connect
(^Driver [^String url]
(let [login (env-var "NEO4J_LOGIN")
password (env-var "NEO4J_PASSWORD")]
(if (or (string/blank? login)
(string/blank? password))
(GraphDatabase/driver url)
(connect url login password))))
(^Driver [^String url ^String username ^String password]
(GraphDatabase/driver url (AuthTokens/basic username password)))
(^Driver [^String url ^String username ^String password ^Config config]
(GraphDatabase/driver url (AuthTokens/basic username password) config)))

(defn create-session
^Session [^Driver driver]
(.session driver))

(defn query
([^Session session ^String qry]
(query session qry {}))
([^Session session ^String qry ^Map params]
(map (fn [^Record r]
(into {} (.asMap r)))
(iterator-seq (.run session qry params)))))
30 changes: 30 additions & 0 deletions test/clojurewerkz/neocons/bolt/test/bolt_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;; 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.bolt.test.bolt-test
(:require [clojurewerkz.neocons.bolt :as neobolt]
[clojure.test :refer :all])
(:import (java.util Map)
(org.neo4j.driver.v1 Driver Session)))

(deftest test-connection
(with-open [driver (neobolt/connect "bolt://localhost")]
(is (instance? Driver driver))))

(deftest test-session
(with-open [driver (neobolt/connect "bolt://localhost")]
(with-open [session (neobolt/create-session driver)]
(is (instance? Session session)))))

(deftest test-simple-query
(with-open [driver (neobolt/connect "bolt://localhost")]
(with-open [session (neobolt/create-session driver)]
(let [res (neobolt/query session "CREATE (n {name: {name}}) RETURN n.name AS name;" {"name" "Arthur"})]
(is (= res [{"name" "Arthur"}]))))))

0 comments on commit 4cec63c

Please sign in to comment.