-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
59 lines (54 loc) · 2.1 KB
/
exec.go
File metadata and controls
59 lines (54 loc) · 2.1 KB
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
package sqldb
import "context"
// Exec executes a query with optional args.
func Exec(ctx context.Context, conn Executor, fmtr QueryFormatter, query string, args ...any) error {
err := conn.Exec(ctx, query, args...)
if err != nil {
return WrapErrorWithQuery(err, query, args, fmtr)
}
return nil
}
// ExecRowsAffected executes a query with optional args
// and returns the number of rows affected by an
// update, insert, or delete. Not every database or database
// driver may support this.
func ExecRowsAffected(ctx context.Context, conn Executor, fmtr QueryFormatter, query string, args ...any) (int64, error) {
n, err := conn.ExecRowsAffected(ctx, query, args...)
if err != nil {
return 0, WrapErrorWithQuery(err, query, args, fmtr)
}
return n, nil
}
// ExecStmt returns a function that can be used to execute a prepared statement
// with optional args.
func ExecStmt(ctx context.Context, conn Preparer, fmtr QueryFormatter, query string) (execFunc func(ctx context.Context, args ...any) error, closeStmt func() error, err error) {
stmt, err := conn.Prepare(ctx, query)
if err != nil {
return nil, nil, WrapErrorWithQuery(err, query, nil, fmtr)
}
execFunc = func(ctx context.Context, args ...any) error {
err := stmt.Exec(ctx, args...)
if err != nil {
return WrapErrorWithQuery(err, query, args, fmtr)
}
return nil
}
return execFunc, stmt.Close, nil
}
// ExecRowsAffectedStmt returns a function that can be used to execute
// a prepared statement with optional args and returns the number of
// rows affected by an update, insert, or delete.
func ExecRowsAffectedStmt(ctx context.Context, conn Preparer, fmtr QueryFormatter, query string) (execFunc func(ctx context.Context, args ...any) (int64, error), closeStmt func() error, err error) {
stmt, err := conn.Prepare(ctx, query)
if err != nil {
return nil, nil, WrapErrorWithQuery(err, query, nil, fmtr)
}
execFunc = func(ctx context.Context, args ...any) (int64, error) {
n, err := stmt.ExecRowsAffected(ctx, args...)
if err != nil {
return 0, WrapErrorWithQuery(err, query, args, fmtr)
}
return n, nil
}
return execFunc, stmt.Close, nil
}