-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.go
More file actions
149 lines (134 loc) · 3.22 KB
/
connection.go
File metadata and controls
149 lines (134 loc) · 3.22 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ladybug
import (
"context"
"fmt"
"time"
"github.com/vkozio/ladybug-go-zero/internal/lbugc"
)
// Connection is a connection to a Ladybug database. Call Close when done.
// Per lbug.h, the underlying C connection is thread-safe. However, Result iteration is not
// safe for concurrent use (consume a Result from one goroutine at a time).
type Connection struct {
c *lbugc.Connection
cfg *Config
}
// Close closes the connection.
func (c *Connection) Close() error {
if c == nil || c.c == nil {
return nil
}
c.c.Close()
c.c = nil
return nil
}
// Query runs a Cypher query and returns a Result. Caller must call Result.Close.
func (c *Connection) Query(ctx context.Context, cypher string) (*Result, error) {
if c == nil || c.c == nil {
return nil, ErrInvalidConn
}
if ctx != nil && ctx.Err() != nil {
return nil, ctx.Err()
}
if err := c.setTimeoutFromContext(ctx); err != nil {
return nil, err
}
done := make(chan struct{})
if ctx != nil {
go func() {
select {
case <-ctx.Done():
c.Interrupt()
case <-done:
}
}()
}
defer close(done)
res, err := c.c.Query(cypher)
if err != nil {
wrapped := fmt.Errorf("ladybug: %w", err)
invokeQueryHook(c.cfg, ctx, cypher, QuerySummary{}, wrapped)
return nil, wrapped
}
r := &Result{c: res}
if ctx != nil && ctx.Err() != nil {
r.Close()
errCtx := ctx.Err()
invokeQueryHook(c.cfg, ctx, cypher, QuerySummary{}, errCtx)
return nil, errCtx
}
var summary QuerySummary
if s, err := r.Summary(); err == nil && s != nil {
summary = *s
}
invokeQueryHook(c.cfg, ctx, cypher, summary, nil)
return r, nil
}
// Prepare prepares a Cypher statement. Caller must call PreparedStatement.Close.
func (c *Connection) Prepare(ctx context.Context, cypher string) (*PreparedStatement, error) {
if c == nil || c.c == nil {
return nil, ErrInvalidConn
}
if ctx != nil && ctx.Err() != nil {
return nil, ctx.Err()
}
if err := c.setTimeoutFromContext(ctx); err != nil {
return nil, err
}
done := make(chan struct{})
if ctx != nil {
go func() {
select {
case <-ctx.Done():
c.Interrupt()
case <-done:
}
}()
}
defer close(done)
ps, err := c.c.Prepare(cypher)
if err != nil {
return nil, fmt.Errorf("ladybug: %w", err)
}
if ctx != nil && ctx.Err() != nil {
ps.Close()
return nil, ctx.Err()
}
return &PreparedStatement{c: ps, conn: c, query: cypher}, nil
}
// SetQueryTimeout sets the query timeout (0 = no timeout).
func (c *Connection) SetQueryTimeout(d time.Duration) {
if c == nil || c.c == nil {
return
}
_ = c.c.SetQueryTimeout(uint64(d.Milliseconds()))
}
// Interrupt interrupts the current query on this connection.
func (c *Connection) Interrupt() {
if c == nil || c.c == nil {
return
}
c.c.Interrupt()
}
func (c *Connection) setTimeoutFromContext(ctx context.Context) error {
if ctx == nil {
return nil
}
deadline, ok := ctx.Deadline()
if !ok {
return nil
}
ms := time.Until(deadline).Milliseconds()
if ms < 0 {
ms = 0
}
return c.c.SetQueryTimeout(uint64(ms))
}
func invokeQueryHook(cfg *Config, ctx context.Context, cypher string, summary QuerySummary, err error) {
if cfg == nil || cfg.OnQueryFinished == nil {
return
}
defer func() {
_ = recover()
}()
cfg.OnQueryFinished(ctx, cypher, summary, err)
}