-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
107 lines (80 loc) · 1.86 KB
/
query.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2016 Brandon Buck
package talon
import (
"github.com/bbuck/talon/types"
bolt "github.com/johnnadratowski/golang-neo4j-bolt-driver"
)
var noProperties = make(types.Properties)
// Query reprsents a Talon query before it's been converted in Cypher
type Query struct {
db *DB
rawCypher string
properties types.Properties
}
func (q *Query) ToCypher() string {
if q.rawCypher != "" {
return q.rawCypher
}
return "__INVALID__;"
}
// Query executes a fetch query, expecting rows to be returned.
func (q *Query) Query() (*Rows, error) {
conn, stmt, err := q.getStatement()
if err != nil {
return nil, err
}
rows, err := stmt.QueryNeo(q.propsForQuery())
if err != nil {
conn.Close()
return nil, err
}
r := wrapBoltRows(rows)
return r, nil
}
func (q *Query) Query2() (bolt.Rows, error) {
conn, stmt, err := q.getStatement()
if err != nil {
return nil, err
}
rows, err := stmt.QueryNeo(q.propsForQuery())
if err != nil {
conn.Close()
return nil, err
}
return rows, nil
}
// Exec runs a query that doesn't expect rows to be returned.
func (q *Query) Exec() (*Result, error) {
_, stmt, err := q.getStatement()
result, err := stmt.ExecNeo(q.propsForQuery())
if err != nil {
return nil, err
}
return wrapBoltResult(result), nil
}
func (q *Query) Exec2() (interface{}, error) {
_, stmt, err := q.getStatement()
result, err := stmt.ExecNeo(q.propsForQuery())
if err != nil {
return nil, err
}
return result, nil
}
func (q *Query) getStatement() (bolt.Conn, bolt.Stmt, error) {
conn, err := q.db.conn()
if err != nil {
return nil, nil, err
}
stmt, err := conn.PrepareNeo(q.ToCypher())
if err != nil {
conn.Close()
return nil, nil, err
}
return conn, stmt, nil
}
func (q *Query) propsForQuery() map[string]interface{} {
if len(q.properties) == 0 {
return nil
}
return map[string]interface{}(q.properties)
}