sqlxguard

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sqlxguard integrates sqlguard with sqlx.

Every wrapped method routes through the shared sqlguard analysis core (middleware.Guard), so redaction-by-default, stable fingerprints, the pluggable real-grammar parser, slow-query timing and N+1 detection behave identically to the database/sql driver wrapper and to pgxguard. There is no parallel option surface — configure with the standard middleware options:

db := sqlxguard.WrapSqlx(sqlxDB,
    middleware.WithSlowQueryThreshold(50*time.Millisecond),
    middleware.WithN1Detection(10, time.Second),
)

Coverage note: WrappedDB exposes the sqlx-specific extension methods (Select/Get/Queryx/NamedExec and their *Context variants, plus Query/Exec passthrough). For full surface coverage — including QueryRow*, NamedQuery, MustExec and the transaction helpers — layer sqlx on top of the sqlguard driver chain instead:

sqlguard.Register("sqlguard-pgx", pq.Driver{}, opts...)
sqlDB, _ := sql.Open("sqlguard-pgx", dsn)
db := sqlx.NewDb(sqlDB, "postgres")

That path covers every sqlx method automatically because interception happens at the database/sql driver layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WrappedDB

type WrappedDB struct {
	// contains filtered or unexported fields
}

WrappedDB wraps a *sqlx.DB with sqlguard analysis. Every analysis-bearing method drives the shared middleware.Guard, so behavior matches pgxguard and the database/sql driver chain exactly.

func WrapSqlx

func WrapSqlx(db *sqlx.DB, opts ...middleware.Option) *WrappedDB

WrapSqlx creates a new WrappedDB around the given sqlx connection. It accepts the standard sqlguard middleware options (WithAnalyzer, WithReporter, WithSlowQueryThreshold, WithParser, WithN1Detection, …) — the same option set the database/sql driver wrapper and pgxguard use, so there is no parallel configuration surface to drift.

func (*WrappedDB) Close

func (w *WrappedDB) Close() error

Close closes the database connection.

func (*WrappedDB) DB

func (w *WrappedDB) DB() *sqlx.DB

DB returns the underlying *sqlx.DB.

func (*WrappedDB) Exec

func (w *WrappedDB) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows.

func (*WrappedDB) ExecContext

func (w *WrappedDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a query with context without returning rows.

func (*WrappedDB) Get

func (w *WrappedDB) Get(dest any, query string, args ...any) error

Get executes a query and scans a single row into dest.

func (*WrappedDB) GetContext

func (w *WrappedDB) GetContext(ctx context.Context, dest any, query string, args ...any) error

GetContext executes a query with context and scans a single row into dest.

func (*WrappedDB) NamedExec

func (w *WrappedDB) NamedExec(query string, arg any) (sql.Result, error)

NamedExec executes a named query.

func (*WrappedDB) NamedExecContext

func (w *WrappedDB) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)

NamedExecContext executes a named query with context.

func (*WrappedDB) Ping

func (w *WrappedDB) Ping() error

Ping verifies the database connection.

func (*WrappedDB) Query

func (w *WrappedDB) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows.

func (*WrappedDB) QueryContext

func (w *WrappedDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query with context that returns rows.

func (*WrappedDB) Queryx

func (w *WrappedDB) Queryx(query string, args ...any) (*sqlx.Rows, error)

Queryx executes a query that returns sqlx.Rows.

func (*WrappedDB) ResetN1

func (w *WrappedDB) ResetN1()

ResetN1 clears N+1 tracker state. Call it at a per-request boundary (e.g. end of an HTTP handler) to scope N+1 detection to one unit of work. No-op unless WithN1Detection was passed to WrapSqlx.

func (*WrappedDB) Select

func (w *WrappedDB) Select(dest any, query string, args ...any) error

Select executes a query and scans the results into dest.

func (*WrappedDB) SelectContext

func (w *WrappedDB) SelectContext(ctx context.Context, dest any, query string, args ...any) error

SelectContext executes a query with context and scans the results into dest.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL