forked from Sonek-HoangBui/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
81 lines (72 loc) · 1.98 KB
/
stmt.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
package apmsql
import (
"context"
"database/sql/driver"
"github.com/elastic/apm-agent-go"
)
func newStmt(in driver.Stmt, conn *conn, query string) driver.Stmt {
stmt := &stmt{
Stmt: in,
conn: conn,
signature: conn.driver.querySignature(query),
query: query,
}
stmt.columnConverter, _ = in.(driver.ColumnConverter)
stmt.stmtExecContext, _ = in.(driver.StmtExecContext)
stmt.stmtQueryContext, _ = in.(driver.StmtQueryContext)
stmt.stmtGo19.init(in)
return stmt
}
type stmt struct {
driver.Stmt
stmtGo19
conn *conn
signature string
query string
columnConverter driver.ColumnConverter
stmtExecContext driver.StmtExecContext
stmtQueryContext driver.StmtQueryContext
}
func (s *stmt) startSpan(ctx context.Context, spanType string) (*elasticapm.Span, context.Context) {
return s.conn.startSpan(ctx, s.signature, spanType, s.query)
}
func (s *stmt) ColumnConverter(idx int) driver.ValueConverter {
if s.columnConverter != nil {
return s.columnConverter.ColumnConverter(idx)
}
return driver.DefaultParameterConverter
}
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (_ driver.Result, resultError error) {
span, ctx := s.startSpan(ctx, s.conn.driver.execSpanType)
defer s.conn.finishSpan(ctx, span, resultError)
if s.stmtExecContext != nil {
return s.stmtExecContext.ExecContext(ctx, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return s.Exec(dargs)
}
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, resultError error) {
span, ctx := s.startSpan(ctx, s.conn.driver.querySpanType)
defer s.conn.finishSpan(ctx, span, resultError)
if s.stmtQueryContext != nil {
return s.stmtQueryContext.QueryContext(ctx, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return s.Query(dargs)
}