-
Notifications
You must be signed in to change notification settings - Fork 41
/
debug_exec.go
73 lines (62 loc) · 1.84 KB
/
debug_exec.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
package bob
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"os"
"github.com/stephenafamo/scan"
)
// DebugPrinter is used to print queries and arguments
type DebugPrinter interface {
PrintQuery(query string, args ...any)
}
// an implementtion of the [DebugPrinter]
type writerPrinter struct{ io.Writer }
// implements [DebugPrinter]
func (w writerPrinter) PrintQuery(query string, args ...any) {
fmt.Fprintln(w.Writer, query)
for i, arg := range args {
val := arg
if valuer, ok := val.(driver.Valuer); ok {
val, _ = valuer.Value()
}
fmt.Fprintf(w.Writer, "%d: %T: %v\n", i, arg, val)
}
fmt.Fprintf(w.Writer, "\n")
}
// Debug wraps an [Executor] and prints the queries and args to os.Stdout
func Debug(exec Executor) Executor {
return DebugToWriter(exec, nil)
}
// DebugToWriter wraps an existing [Executor] and writes all
// queries and args to the given [io.Writer]
// if w is nil, it fallsback to [os.Stdout]
func DebugToWriter(exec Executor, w io.Writer) Executor {
if w == nil {
w = os.Stdout
}
return DebugToPrinter(exec, writerPrinter{w})
}
// DebugToPrinter wraps an existing [Executor] and writes all
// queries and args to the given [DebugPrinter]
// if w is nil, it fallsback to writing to [os.Stdout]
func DebugToPrinter(exec Executor, w DebugPrinter) Executor {
if w == nil {
w = writerPrinter{os.Stdout}
}
return debugExecutor{printer: w, exec: exec}
}
type debugExecutor struct {
printer DebugPrinter
exec Executor
}
func (d debugExecutor) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
d.printer.PrintQuery(query, args...)
return d.exec.ExecContext(ctx, query, args...)
}
func (d debugExecutor) QueryContext(ctx context.Context, query string, args ...any) (scan.Rows, error) {
d.printer.PrintQuery(query, args...)
return d.exec.QueryContext(ctx, query, args...)
}