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 ¶
- type WrappedDB
- func (w *WrappedDB) Close() error
- func (w *WrappedDB) DB() *sqlx.DB
- func (w *WrappedDB) Exec(query string, args ...any) (sql.Result, error)
- func (w *WrappedDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (w *WrappedDB) Get(dest any, query string, args ...any) error
- func (w *WrappedDB) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (w *WrappedDB) NamedExec(query string, arg any) (sql.Result, error)
- func (w *WrappedDB) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)
- func (w *WrappedDB) Ping() error
- func (w *WrappedDB) Query(query string, args ...any) (*sql.Rows, error)
- func (w *WrappedDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (w *WrappedDB) Queryx(query string, args ...any) (*sqlx.Rows, error)
- func (w *WrappedDB) ResetN1()
- func (w *WrappedDB) Select(dest any, query string, args ...any) error
- func (w *WrappedDB) SelectContext(ctx context.Context, dest any, query string, args ...any) error
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) ExecContext ¶
ExecContext executes a query with context without returning rows.
func (*WrappedDB) GetContext ¶
GetContext executes a query with context and scans a single row into dest.
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) QueryContext ¶
QueryContext executes a query with context that returns 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.