-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_options.go
62 lines (54 loc) · 1.21 KB
/
connect_options.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
// Copyright (c) 2016 Brandon Buck
package talon
import (
"bytes"
"fmt"
bolt "github.com/johnnadratowski/golang-neo4j-bolt-driver"
)
// ConnectOptions allows customiztaino of how to connect to a Neo4j database
// with talon.
type ConnectOptions struct {
User string
Pass string
Host string
Port uint16
Pool uint16
}
// URL takes the options set for connection and generates a bolt connection
// string.
func (co *ConnectOptions) URL() string {
buf := new(bytes.Buffer)
buf.WriteString("bolt://")
if co.User != "" {
buf.WriteString(co.User)
if co.Pass != "" {
buf.WriteRune(':')
buf.WriteString(co.Pass)
}
buf.WriteRune('@')
}
buf.WriteString(co.Host)
if co.Port > 0 {
buf.WriteRune(':')
buf.WriteString(fmt.Sprintf("%d", co.Port))
}
return buf.String()
}
// Connect will take the provided connection options and attempt to establish
// a connection to a Neo4j database.
func (co ConnectOptions) Connect() (db *DB, err error) {
db = new(DB)
if co.Pool == 0 {
db.driver = &driver{
Driver: bolt.NewDriver(),
connectOptions: co,
}
} else {
var pool bolt.DriverPool
pool, err = bolt.NewDriverPool(co.URL(), int(co.Pool))
db.driver = &driverPool{
pool: pool,
}
}
return
}