-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
56 lines (46 loc) · 1.38 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
package dali
import (
"context"
"database/sql"
)
// Stmt is a prepared statement.
type Stmt struct {
stmt *sql.Stmt
sql string
middleware func(Execer) Execer
}
// BindContext binds args to the prepared statement and returns a Query struct
// ready to be executed. See (*DB).Query method.
func (s *Stmt) BindContext(ctx context.Context, args ...interface{}) *Query {
return &Query{
ctx: ctx,
execer: s.middleware(stmtExecer{s.stmt}),
query: s.sql,
args: args,
}
}
// Bind binds args to the prepared statement and returns a Query struct
// ready to be executed. See (*DB).Query method.
func (s *Stmt) Bind(args ...interface{}) *Query {
return s.BindContext(context.Background(), args...)
}
// Close closes the statement.
func (s *Stmt) Close() error {
return s.stmt.Close()
}
func (s *Stmt) String() string {
return s.sql
}
// stmtExecer is just an adapter for Execer interface.
type stmtExecer struct {
stmt *sql.Stmt
}
func (s stmtExecer) ExecContext(ctx context.Context, _ string, args ...interface{}) (sql.Result, error) {
return s.stmt.ExecContext(ctx, args...)
}
func (s stmtExecer) QueryContext(ctx context.Context, _ string, args ...interface{}) (*sql.Rows, error) {
return s.stmt.QueryContext(ctx, args...)
}
func (s stmtExecer) QueryRowContext(ctx context.Context, _ string, args ...interface{}) *sql.Row {
return s.stmt.QueryRowContext(ctx, args...)
}