-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
109 lines (91 loc) · 2.91 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
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
package pqxd
import (
"context"
"database/sql/driver"
"go.uber.org/atomic"
)
// compatibility check
var (
_ driver.Stmt = (*statement)(nil)
_ driver.StmtQueryContext = (*statement)(nil)
_ driver.StmtExecContext = (*statement)(nil)
)
// queryWithPrepare executes the prepared statement
type queryWithPrepare func(ctx context.Context, query string, selectedList []string, args []driver.NamedValue) (driver.Rows, error)
// execContext executes the prepared statement
type execContext func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
// statement is an implementation of driver.Stmt
type statement struct {
// query is a string of prepared statement
query string
// selectedList is a list of selected items
selectedList []string
// numInput is the number of placeholders in the statement
numInput int
// queryWithPrepare executes the statement
queryWithPrepare queryWithPrepare
// execContext executes the statement
execContext execContext
// closed is a flag that indicates whether the statement is closed
closed atomic.Bool
// connCloseCheckClosure checks if the connection is closed
connCloseCheckClosure func() error
}
// Close See: driver.Stmt
func (s statement) Close() error {
if err := s.connCloseCheckClosure(); err != nil {
s.closed.Store(true)
return driver.ErrBadConn
}
if s.closed.Load() {
return ErrStatementClosed
}
s.closed.Store(true)
return nil
}
// NumInput See: driver.Stmt
func (s statement) NumInput() int {
return s.numInput
}
// Exec See: driver.Stmt
func (s statement) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), toNamedValue(args))
}
// Query See: driver.Stmt
func (s statement) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), toNamedValue(args))
}
// QueryContext See: driver.StmtQueryContext
func (s statement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.queryWithPrepare(ctx, s.query, s.selectedList, args)
}
// ExecContext See: driver.StmtExecContext
func (s statement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if err := s.connCloseCheckClosure(); err != nil {
return nil, driver.ErrBadConn
}
if s.closed.Load() {
s.closed.Store(true)
return nil, ErrStatementClosed
}
return s.execContext(ctx, s.query, args)
}
// newStatement returns a new statement
func newStatement(
query string,
selectedList []string,
numInput int,
queryWithPrepare queryWithPrepare,
execContext execContext,
connCloseCheckClosure func() error,
) *statement {
return &statement{
query: query,
selectedList: selectedList,
numInput: numInput,
queryWithPrepare: queryWithPrepare,
execContext: execContext,
closed: *atomic.NewBool(false),
connCloseCheckClosure: connCloseCheckClosure,
}
}