Inspecting queries
ToSQL
ToSQL returns a query's statement and arguments without running it or needing a database.
text, args, err := db.Users.Where(ant.Gt(&Users.Age, 18)).OrderBy(&Users.Name).Limit(5).ToSQL()
SELECT "users"."id", ... FROM "users" WHERE "users"."age" > $1 ORDER BY "users"."name" LIMIT $2
args is [18 5]. ToSQLContext applies global filters from a context. UpdateSQL and DeleteSQL do the same for writes.
Explain
Explain returns the database's query plan as text.
plan, err := db.Users.Where(ant.Gt(&Users.Age, 18)).Explain(ctx)
Debug
Debug logs one query's SQL and values through slog's default logger. Remove it before committing, since it logs values.
users, err := db.Users.Where(ant.Eq(&Users.Name, "Alice")).Debug().All(ctx)
DryRun
ant.DryRun runs your code in a transaction, returns every statement it ran, and rolls back. Sequences still move on, and anything the function does outside the database still happens.
statements, err := ant.DryRun(ctx, db, func(tx *AppDB) error {
return tx.Users.Insert(ctx, &User{Name: "Temp", Email: "temp@example.com"})
})
for _, s := range statements {
fmt.Println(s.SQL)
}
See it tested: tests/features/developer_tooling