1package db
2
3import (
4 "context"
5 "database/sql"
6 "strings"
7
8 "github.com/charmbracelet/log/v2"
9 "github.com/jmoiron/sqlx"
10)
11
12func trace(l *log.Logger, query string, args ...interface{}) {
13 if l != nil {
14 // Remove newlines and tabs
15 query = strings.ReplaceAll(query, "\t", "")
16 query = strings.TrimSpace(query)
17 l.Debug("trace", "query", query, "args", args)
18 }
19}
20
21// Select is a wrapper around sqlx.Select that logs the query and arguments.
22func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
23 trace(d.logger, query, args...)
24 return d.DB.Select(dest, query, args...) //nolint:wrapcheck
25}
26
27// Get is a wrapper around sqlx.Get that logs the query and arguments.
28func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
29 trace(d.logger, query, args...)
30 return d.DB.Get(dest, query, args...) //nolint:wrapcheck
31}
32
33// Queryx is a wrapper around sqlx.Queryx that logs the query and arguments.
34func (d *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
35 trace(d.logger, query, args...)
36 return d.DB.Queryx(query, args...) //nolint:wrapcheck
37}
38
39// QueryRowx is a wrapper around sqlx.QueryRowx that logs the query and arguments.
40func (d *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row {
41 trace(d.logger, query, args...)
42 return d.DB.QueryRowx(query, args...)
43}
44
45// Exec is a wrapper around sqlx.Exec that logs the query and arguments.
46func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
47 trace(d.logger, query, args...)
48 return d.DB.Exec(query, args...) //nolint:noctx,wrapcheck
49}
50
51// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.
52func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
53 trace(d.logger, query, args...)
54 return d.DB.SelectContext(ctx, dest, query, args...) //nolint:wrapcheck
55}
56
57// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.
58func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
59 trace(d.logger, query, args...)
60 return d.DB.GetContext(ctx, dest, query, args...) //nolint:wrapcheck
61}
62
63// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.
64func (d *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
65 trace(d.logger, query, args...)
66 return d.DB.QueryxContext(ctx, query, args...) //nolint:wrapcheck
67}
68
69// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.
70func (d *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
71 trace(d.logger, query, args...)
72 return d.DB.QueryRowxContext(ctx, query, args...)
73}
74
75// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.
76func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
77 trace(d.logger, query, args...)
78 return d.DB.ExecContext(ctx, query, args...) //nolint:wrapcheck
79}
80
81// Select is a wrapper around sqlx.Select that logs the query and arguments.
82func (t *Tx) Select(dest interface{}, query string, args ...interface{}) error {
83 trace(t.logger, query, args...)
84 return t.Tx.Select(dest, query, args...) //nolint:wrapcheck
85}
86
87// Get is a wrapper around sqlx.Get that logs the query and arguments.
88func (t *Tx) Get(dest interface{}, query string, args ...interface{}) error {
89 trace(t.logger, query, args...)
90 return t.Tx.Get(dest, query, args...) //nolint:wrapcheck
91}
92
93// Queryx is a wrapper around sqlx.Queryx that logs the query and arguments.
94func (t *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
95 trace(t.logger, query, args...)
96 return t.Tx.Queryx(query, args...) //nolint:wrapcheck
97}
98
99// QueryRowx is a wrapper around sqlx.QueryRowx that logs the query and arguments.
100func (t *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row {
101 trace(t.logger, query, args...)
102 return t.Tx.QueryRowx(query, args...)
103}
104
105// Exec is a wrapper around sqlx.Exec that logs the query and arguments.
106func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
107 trace(t.logger, query, args...)
108 return t.Tx.Exec(query, args...) //nolint:noctx,wrapcheck
109}
110
111// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.
112func (t *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
113 trace(t.logger, query, args...)
114 return t.Tx.SelectContext(ctx, dest, query, args...) //nolint:wrapcheck
115}
116
117// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.
118func (t *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
119 trace(t.logger, query, args...)
120 return t.Tx.GetContext(ctx, dest, query, args...) //nolint:wrapcheck
121}
122
123// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.
124func (t *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
125 trace(t.logger, query, args...)
126 return t.Tx.QueryxContext(ctx, query, args...) //nolint:wrapcheck
127}
128
129// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.
130func (t *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
131 trace(t.logger, query, args...)
132 return t.Tx.QueryRowxContext(ctx, query, args...)
133}
134
135// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.
136func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
137 trace(t.logger, query, args...)
138 return t.Tx.ExecContext(ctx, query, args...) //nolint:wrapcheck
139}