1package db23import (4 "context"5 "database/sql"6 "strings"78 "charm.land/log/v2"9 "github.com/jmoiron/sqlx"10)1112func trace(l *log.Logger, query string, args ...interface{}) {13 if l != nil {14 // Remove newlines and tabs15 query = strings.ReplaceAll(query, "\t", "")16 query = strings.TrimSpace(query)17 l.Debug("trace", "query", query, "args", args)18 }19}2021// 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...)25}2627// 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...)31}3233// 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...)37}3839// 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}4445// Exec is a wrapper around sqlx.Exec that logs the query and arguments.46//47// Deprecated: Use [DB.ExecContext] instead.48func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error) {49 trace(d.logger, query, args...)50 return d.DB.Exec(query, args...) //nolint:noctx51}5253// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.54func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {55 trace(d.logger, query, args...)56 return d.DB.SelectContext(ctx, dest, query, args...)57}5859// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.60func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {61 trace(d.logger, query, args...)62 return d.DB.GetContext(ctx, dest, query, args...)63}6465// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.66func (d *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {67 trace(d.logger, query, args...)68 return d.DB.QueryxContext(ctx, query, args...)69}7071// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.72func (d *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {73 trace(d.logger, query, args...)74 return d.DB.QueryRowxContext(ctx, query, args...)75}7677// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.78func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {79 trace(d.logger, query, args...)80 return d.DB.ExecContext(ctx, query, args...)81}8283// Select is a wrapper around sqlx.Select that logs the query and arguments.84func (t *Tx) Select(dest interface{}, query string, args ...interface{}) error {85 trace(t.logger, query, args...)86 return t.Tx.Select(dest, query, args...)87}8889// Get is a wrapper around sqlx.Get that logs the query and arguments.90func (t *Tx) Get(dest interface{}, query string, args ...interface{}) error {91 trace(t.logger, query, args...)92 return t.Tx.Get(dest, query, args...)93}9495// Queryx is a wrapper around sqlx.Queryx that logs the query and arguments.96func (t *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {97 trace(t.logger, query, args...)98 return t.Tx.Queryx(query, args...)99}100101// QueryRowx is a wrapper around sqlx.QueryRowx that logs the query and arguments.102func (t *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row {103 trace(t.logger, query, args...)104 return t.Tx.QueryRowx(query, args...)105}106107// Exec is a wrapper around sqlx.Exec that logs the query and arguments.108//109// Deprecated: Use [Tx.ExecContext] instead.110func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {111 trace(t.logger, query, args...)112 return t.Tx.Exec(query, args...) //nolint:noctx113}114115// SelectContext is a wrapper around sqlx.SelectContext that logs the query and arguments.116func (t *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {117 trace(t.logger, query, args...)118 return t.Tx.SelectContext(ctx, dest, query, args...)119}120121// GetContext is a wrapper around sqlx.GetContext that logs the query and arguments.122func (t *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {123 trace(t.logger, query, args...)124 return t.Tx.GetContext(ctx, dest, query, args...)125}126127// QueryxContext is a wrapper around sqlx.QueryxContext that logs the query and arguments.128func (t *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {129 trace(t.logger, query, args...)130 return t.Tx.QueryxContext(ctx, query, args...)131}132133// QueryRowxContext is a wrapper around sqlx.QueryRowxContext that logs the query and arguments.134func (t *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {135 trace(t.logger, query, args...)136 return t.Tx.QueryRowxContext(ctx, query, args...)137}138139// ExecContext is a wrapper around sqlx.ExecContext that logs the query and arguments.140func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {141 trace(t.logger, query, args...)142 return t.Tx.ExecContext(ctx, query, args...)143}