Skip to main content

Raw SQL

ant.Raw runs your statement and scans the result into a model, any struct, or a single value.

Scan into a model​

users, err := ant.Raw[User](db, `SELECT * FROM users WHERE age > ? ORDER BY name`, 18).All(ctx)
// SELECT * FROM users WHERE age > $1 ORDER BY name

All returns []User. First returns one row or ant.ErrNotFound. Raw results aren't tracked.

Scan into any struct​

Columns match fields by name. Columns with no field are ignored. Fields with no column stay zero.

type Summary struct {
Name string
YearsOn int
}

rows, err := ant.Raw[Summary](db, `SELECT name, age + 1 AS years_on FROM users`).All(ctx)

ant.WithStrict makes a column with no field an error. Two result columns for one field, as SELECT * over a join gives for id, are always an error that names them, where the field used to take the last one.

Scan a single value​

n, err := ant.Raw[int](db, `SELECT count(*) FROM users`).First(ctx) // n is an *int
names, err := ant.Raw[string](db, `SELECT name FROM users ORDER BY name`).All(ctx)
now, err := ant.Raw[time.Time](db, `SELECT now()`).First(ctx)

NULL needs a nullable type: ant.Raw[sql.Null[string]].

Pass arguments​

? works on every database and becomes its placeholder. $1 and the jsonb ? operator still work on PostgreSQL.

a, err := ant.Raw[User](db, `SELECT * FROM users WHERE age > ? AND name <> ?`, 18, "Alice").All(ctx)
// SELECT * FROM users WHERE age > $1 AND name <> $2

A slice fills IN (?), one placeholder per element. Named arguments work too:

ages := []int{17, 30}

b, err := ant.Raw[User](db, `SELECT * FROM users WHERE age IN (?)`, ages).All(ctx)
// SELECT * FROM users WHERE age IN ($1, $2)

c, err := ant.Raw[User](db, `SELECT * FROM users WHERE age IN (@ages)`, sql.Named("ages", ages)).All(ctx)
// SELECT * FROM users WHERE age IN ($1, $2)

An empty slice in IN (?) matches nothing. So does one in NOT IN (?), as in SQL and unlike ant.NotIn:

d, err := ant.Raw[User](db, `SELECT * FROM users WHERE age NOT IN (?)`, []int{}).All(ctx)
// SELECT * FROM users WHERE age NOT IN (NULL)

Outside IN, a slice is one array value, as in age = ANY($1). That includes a slice of a named type, such as []Status for an enum.

Times​

On SQLite and SQL Server, where a time column keeps no time zone, ant binds every time in UTC, including raw query arguments: a time.Time, a *time.Time, sql.NullTime, an sql.Null of a time, and each element of a slice that fills an IN list. A time in another zone stores and compares as the same instant.

since := time.Date(2026, 11, 1, 9, 0, 0, 0, time.FixedZone("EET", 2*3600))

recent, err := ant.Raw[User](db, `SELECT * FROM users WHERE created_at > ?`, since).All(ctx)
// on SQLite and SQL Server, bound as 2026-11-01 07:00:00 UTC

A custom driver.Valuer is passed as is, so have it return UTC. See databases.

Run a statement that returns nothing​

Exec returns the rows affected.

n, err := ant.Raw[any](db, `UPDATE users SET age = age + 1 WHERE age < ?`, 18).Exec(ctx)
// UPDATE users SET age = age + 1 WHERE age < $1

Keep SQL in a file​

ant.RawFile loads a statement from any fs.FS, including go:embed. A missing file is reported at the terminal call.

//go:embed queries/*.sql
var queries embed.FS

func adults(ctx context.Context, db *AppDB) ([]User, error) {
return ant.RawFile[User](db, queries, "queries/adults.sql", sql.Named("min", 18)).All(ctx)
}

Build on raw SQL​

ant.FromRaw starts from your SQL and continues with typed methods. The SQL must return the model's columns. ant wraps it as a derived table.

adults := ant.FromRaw[User](db, `SELECT * FROM users WHERE age > ?`, 18)

page, err := adults.Where(ant.Lt(&Users.Age, 35)).OrderBy(&Users.Name).Limit(10).All(ctx)
// SELECT ... FROM (SELECT * FROM users WHERE age > $1) AS "users"
// WHERE "users"."age" < $2 ORDER BY "users"."name" LIMIT $3

Count counts the wrapped rows. For a fragment inside a typed query, use ant.SQL and ant.SQLCond. Update and Delete on a FromRaw query are refused, because ant can't see which columns your SQL returns. See bulk writes.

Filters do not apply

ant.Raw runs your SQL as written, so global filters and soft deletes don't reach it. Never build the text from input: pass values as arguments.