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