xorm
xormguard implements xorm's contexts.Hook. xorm's AfterProcess hook
exposes the rendered SQL and the measured execution time, which is where
the static rules and the latency check run.
go get github.com/KARTIKrocks/sqlguard/integrations/xormguard
import (
"github.com/KARTIKrocks/sqlguard/integrations/xormguard"
"github.com/KARTIKrocks/sqlguard/middleware"
"xorm.io/xorm"
)
engine, err := xorm.NewEngine("pgx", dsn)
engine.AddHook(xormguard.New(
middleware.WithSlowQueryThreshold(500*time.Millisecond),
middleware.WithN1Detection(10, time.Second),
))
API
| Symbol | Use |
|---|---|
xormguard.New(opts ...middleware.Option) *Hook | Build the hook. Pass to engine.AddHook. |
(*Hook).ResetN1() | Clear N+1 state at a request boundary. |
BeforeProcess, AfterProcess | The contexts.Hook methods; you do not call them. |
hook := xormguard.New(middleware.WithN1Detection(10, time.Second))
engine.AddHook(hook)
func handler(w http.ResponseWriter, r *http.Request) {
defer hook.ResetN1()
// ...
}
Notes
- Static rules run on every statement; latency is reported only on success. Same semantics as every other surface.
- xorm sits on
database/sql, sosqlguard.Registeron the driver you pass toxorm.NewEngineis the alternative. Use this adapter when you wantResetN1(); do not use both on one engine.