forked from aws/aws-xray-sdk-go
/
sql_go18.go
183 lines (140 loc) · 5.46 KB
/
sql_go18.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
// +build go1.8
package xray
import (
"context"
"database/sql"
)
// Begin starts a transaction.
func (db *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
tx, err := db.db.BeginTx(ctx, opts)
return &Tx{db, tx}, err
}
// Prepare creates a prepared statement for later queries or executions.
func (db *DB) Prepare(ctx context.Context, query string) (*Stmt, error) {
stmt, err := db.db.PrepareContext(ctx, query)
return &Stmt{db, stmt, query}, err
}
// Ping traces verifying a connection to the database is still alive,
// establishing a connection if necessary and adds corresponding information into subsegment.
func (db *DB) Ping(ctx context.Context) error {
return Capture(ctx, db.dbname, func(ctx context.Context) error {
db.populate(ctx, "PING")
return db.db.PingContext(ctx)
})
}
// Exec captures executing a query without returning any rows and
// adds corresponding information into subsegment.
func (db *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
var res sql.Result
err := Capture(ctx, db.dbname, func(ctx context.Context) error {
db.populate(ctx, query)
var err error
res, err = db.db.ExecContext(ctx, query, args...)
return err
})
return res, err
}
// Query captures executing a query that returns rows and adds corresponding information into subsegment.
func (db *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
var res *sql.Rows
err := Capture(ctx, db.dbname, func(ctx context.Context) error {
db.populate(ctx, query)
var err error
res, err = db.db.QueryContext(ctx, query, args...)
return err
})
return res, err
}
// QueryRow captures executing a query that is expected to return at most one row
// and adds corresponding information into subsegment.
func (db *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
var res *sql.Row
Capture(ctx, db.dbname, func(ctx context.Context) error {
db.populate(ctx, query)
res = db.db.QueryRowContext(ctx, query, args...)
return nil
})
return res
}
// Exec captures executing a query that doesn't return rows and adds
// corresponding information into subsegment.
func (tx *Tx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
var res sql.Result
err := Capture(ctx, tx.db.dbname, func(ctx context.Context) error {
tx.db.populate(ctx, query)
var err error
res, err = tx.tx.ExecContext(ctx, query, args...)
return err
})
return res, err
}
// Query captures executing a query that returns rows and adds corresponding information into subsegment.
func (tx *Tx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
var res *sql.Rows
err := Capture(ctx, tx.db.dbname, func(ctx context.Context) error {
tx.db.populate(ctx, query)
var err error
res, err = tx.tx.QueryContext(ctx, query, args...)
return err
})
return res, err
}
// QueryRow captures executing a query that is expected to return at most one row and adds
// corresponding information into subsegment.
func (tx *Tx) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
var res *sql.Row
Capture(ctx, tx.db.dbname, func(ctx context.Context) error {
tx.db.populate(ctx, query)
res = tx.tx.QueryRowContext(ctx, query, args...)
return nil
})
return res
}
// Stmt returns a transaction-specific prepared statement from an existing statement.
func (tx *Tx) Stmt(ctx context.Context, stmt *Stmt) *Stmt {
return &Stmt{stmt.db, tx.tx.StmtContext(ctx, stmt.stmt), stmt.query}
}
// Exec captures executing a prepared statement with the given arguments and
// returning a Result summarizing the effect of the statement and adds corresponding
// information into subsegment.
func (stmt *Stmt) Exec(ctx context.Context, args ...interface{}) (sql.Result, error) {
var res sql.Result
err := Capture(ctx, stmt.db.dbname, func(ctx context.Context) error {
stmt.populate(ctx, stmt.query)
var err error
res, err = stmt.stmt.ExecContext(ctx, args...)
return err
})
return res, err
}
// Query captures executing a prepared query statement with the given arguments
// and returning the query results as a *Rows and adds corresponding information
// into subsegment.
func (stmt *Stmt) Query(ctx context.Context, args ...interface{}) (*sql.Rows, error) {
var res *sql.Rows
err := Capture(ctx, stmt.db.dbname, func(ctx context.Context) error {
stmt.populate(ctx, stmt.query)
var err error
res, err = stmt.stmt.QueryContext(ctx, args...)
return err
})
return res, err
}
// QueryRow captures executing a prepared query statement with the given arguments and
// adds corresponding information into subsegment.
func (stmt *Stmt) QueryRow(ctx context.Context, args ...interface{}) *sql.Row {
var res *sql.Row
Capture(ctx, stmt.db.dbname, func(ctx context.Context) error {
stmt.populate(ctx, stmt.query)
res = stmt.stmt.QueryRowContext(ctx, args...)
return nil
})
return res
}