/
database.go
74 lines (68 loc) · 2.28 KB
/
database.go
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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neo4j
import (
"fmt"
"github.com/jmcvetta/restclient"
"log"
"net/url"
"strconv"
)
func init() {
fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
fmt.Println()
fmt.Println("IMPORTANT NOTE:")
fmt.Println()
fmt.Println("Package github.com/jmcvetta/neo4j has been renamed github.com/jmcvetta/neoism.")
fmt.Println("All future development, bugfixes, etc. will occur in the new repository.")
fmt.Println("Please update your software. Package neo4j is deprecated.")
fmt.Println()
fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
}
// A Database is a REST client connected to a Neo4j database.
type Database struct {
Rc *restclient.Client
Url string `json:"-"` // Root URL for REST API
HrefNode string `json:"node"`
HrefRefNode string `json:"reference_node"`
HrefNodeIndex string `json:"node_index"`
HrefRelIndex string `json:"relationship_index"`
HrefExtInfo string `json:"extensions_info"`
HrefRelTypes string `json:"relationship_types"`
HrefBatch string `json:"batch"`
HrefCypher string `json:"cypher"`
HrefTransaction string `json:"transaction"`
Version string `json:"neo4j_version"`
Extensions interface{} `json:"extensions"`
}
// Connect establishes a connection to the Neo4j server.
func Connect(uri string) (*Database, error) {
var e NeoError
db := &Database{
Rc: restclient.New(),
}
_, err := url.Parse(uri) // Sanity check
if err != nil {
return nil, err
}
db.Url = uri
req := restclient.RequestResponse{
Url: db.Url,
Method: "GET",
Result: &db,
Error: &e,
}
status, err := db.Rc.Do(&req)
if err != nil {
return nil, err
}
if status != 200 || db.Version == "" {
logPretty(req.RawText)
log.Println("Status " + strconv.Itoa(status) + " trying to connect to " + uri)
return nil, InvalidDatabase
}
return db, nil
}
// A Props is a set of key/value properties.
type Props map[string]interface{}