/
statement.go
183 lines (157 loc) · 5.48 KB
/
statement.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
package godb
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
// Stmt defines a new statement
type Stmt interface {
// sql
// Close closes the statement.
Close() error
// Exec executes a prepared statement with the given arguments and
// returns a Result summarizing the effect of the statement.
// Exec uses context.Background internally; to specify the context, use
// ExecContext.
Exec(args ...any) (sql.Result, error)
// ExecContext executes a prepared statement with the given arguments and
// returns a Result summarizing the effect of the statement.
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
// sqlx
// Get using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
Get(dest interface{}, args ...interface{}) error
// GetContext using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
GetContext(ctx context.Context, dest interface{}, args ...interface{}) error
// MustExec (panic) using this statement. Note that the query portion of the error
// output will be blank, as Stmt does not expose its query.
// Any placeholder parameters are replaced with supplied args.
MustExec(args ...interface{}) sql.Result
// MustExecContext (panic) using this statement. Note that the query portion of
// the error output will be blank, as Stmt does not expose its query.
// Any placeholder parameters are replaced with supplied args.
MustExecContext(ctx context.Context, args ...interface{}) sql.Result
// QueryRow using this statement.
// Any placeholder parameters are replaced with supplied args.
QueryRow(args ...interface{}) Row
// QueryRowContext using this statement.
// Any placeholder parameters are replaced with supplied args.
QueryRowContext(ctx context.Context, args ...interface{}) Row
// Query using this statement.
// Any placeholder parameters are replaced with supplied args.
Query(args ...interface{}) (Rows, error)
// QueryContext using this statement.
// Any placeholder parameters are replaced with supplied args.
QueryContext(ctx context.Context, args ...interface{}) (Rows, error)
// Select using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
Select(dest interface{}, args ...interface{}) error
// SelectContext using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error
// Unsafe returns a version of Stmt which will silently succeed to scan when
// columns in the SQL result have no fields in the destination struct.
Unsafe() *sqlx.Stmt
// Safe returns the underlying Stmt
Safe() *sqlx.Stmt
}
// customStmt implements the Stmt interface
type customStmt struct {
stmt *sqlx.Stmt
testErr error
}
// pushTestError
func (c *customStmt) pushTestError(err error) {
c.testErr = err
}
// popTestError
func (c *customStmt) popTestError() error {
err := c.testErr
c.testErr = nil
return err
}
// Close
func (c *customStmt) Close() error {
return c.stmt.Close()
}
// Exec
func (c *customStmt) Exec(args ...any) (sql.Result, error) {
return c.stmt.Exec(args...)
}
// ExecContext
func (c *customStmt) ExecContext(ctx context.Context, args ...any) (sql.Result, error) {
return c.stmt.ExecContext(ctx, args...)
}
// Get
func (c *customStmt) Get(dest interface{}, args ...interface{}) error {
return c.stmt.Get(dest, args...)
}
// GetContext
func (c *customStmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error {
return c.stmt.GetContext(ctx, dest, args...)
}
// MustExec
func (c *customStmt) MustExec(args ...interface{}) sql.Result {
return c.stmt.MustExec(args...)
}
// MustExecContext
func (c *customStmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result {
return c.stmt.MustExecContext(ctx, args...)
}
// QueryRow
func (c *customStmt) QueryRow(args ...interface{}) Row {
return &customRow{row: c.stmt.QueryRowx(args...)}
}
// QueryRowContext
func (c *customStmt) QueryRowContext(ctx context.Context, args ...interface{}) Row {
return &customRow{row: c.stmt.QueryRowxContext(ctx, args...)}
}
// Query
func (c *customStmt) Query(args ...interface{}) (Rows, error) {
if rows, err := c.query(args...); err != nil {
return &customRows{}, err
} else {
return &customRows{rows: rows}, nil
}
}
// query
func (c *customStmt) query(args ...interface{}) (*sqlx.Rows, error) {
if c.testErr != nil {
return &sqlx.Rows{}, c.popTestError()
}
return c.stmt.Queryx(args...)
}
// QueryContext
func (c *customStmt) QueryContext(ctx context.Context, args ...interface{}) (Rows, error) {
if rows, err := c.queryContext(ctx, args...); err != nil {
return &customRows{}, err
} else {
return &customRows{rows: rows}, nil
}
}
// queryContext
func (c *customStmt) queryContext(ctx context.Context, args ...interface{}) (*sqlx.Rows, error) {
if c.testErr != nil {
return &sqlx.Rows{}, c.popTestError()
}
return c.stmt.QueryxContext(ctx, args...)
}
// Select
func (c *customStmt) Select(dest interface{}, args ...interface{}) error {
return c.stmt.Select(dest, args...)
}
// SelectContext
func (c *customStmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error {
return c.stmt.SelectContext(ctx, dest, args...)
}
// Unsafe
func (c *customStmt) Unsafe() *sqlx.Stmt {
return c.stmt.Unsafe()
}
// Safe
func (c *customStmt) Safe() *sqlx.Stmt {
return c.stmt
}