Expressions
An ant.Expr[V] computes a value in SQL and has a Go type. You can compare it, order by it, select it, or write it to a column.
Use an expression in a query
lower := ant.Lower(&Users.Name)
users, err := db.Users.
Where(lower.Eq("bob")).
Order(lower.Desc()).
All(ctx)
// SELECT ... FROM "users" WHERE LOWER("users"."name") = $1 ORDER BY LOWER("users"."name") DESC
Columns and arithmetic
ant.Col turns a field pointer into an expression. Arithmetic stays typed.
nextYear := ant.Col(&Users.Age).Plus(1)
users, err := db.Users.Where(nextYear.Eq(18)).All(ctx)
// SELECT ... FROM "users" WHERE ("users"."age" + $1) = $2
| Method | Does |
|---|---|
.Plus, .Minus, .Times, .Div | arithmetic with a value |
.Eq, .Ne, .Gt, .Gte, .Lt, .Lte | compare with a value, giving a Cond |
.EqExpr, .GtExpr, .LtExpr | compare with another expression |
.IsNull, .IsNotNull | check for NULL |
.Asc, .Desc | an ordering term |
.Filter(conds...) | narrow an aggregate |
.Over(window) | run as a window function |
Built-in functions
lower := ant.Lower(&Users.Name)
upper := ant.Upper(&Users.Name)
length := ant.Length(&Users.Name)
fallback := ant.Coalesce(&Users.Bio, new("none"))
sorted := ant.Collate(&Users.Name, "C")
users, err := db.Users.Where(length.Gte(5)).Order(sorted.Asc()).All(ctx)
// SELECT ... FROM "users" WHERE LENGTH("users"."name") >= $1 ORDER BY "users"."name" COLLATE "C"
Coalesce takes the first value that isn't NULL. Ordering by it sorts rows with no bio as if they held a default:
users, err := db.Users.Order(ant.Coalesce(&Users.Bio, new("none")).Asc()).All(ctx)
// SELECT ... FROM "users" ORDER BY COALESCE("users"."bio", $1)
CASE
ant.Case picks a value by condition.
kind := ant.Case[string]().
When(ant.Lt(&Users.Age, 18), "minor").
When(ant.Gt(&Users.Age, 65), "senior").
Else("adult")
// CASE WHEN "users"."age" < $1 THEN $2 WHEN "users"."age" > $3 THEN $4 ELSE $5 END
Else returns an Expr[string]. End returns Expr[sql.Null[string]], since a row that matches nothing is NULL. WhenExpr and ElseExpr take columns or other expressions as results. Assigned with ant.SetExpr to a column declared with .Enum(...), the CASE is cast to that enum type, which PostgreSQL would otherwise refuse as text.
To read the label, map it into a struct with Project:
type Labeled struct {
Name string
Kind string
}
var Labeleds = ant.Fields[Labeled]()
kind := ant.Case[string]().
When(ant.Lt(&Users.Age, 18), "minor").
When(ant.Gt(&Users.Age, 65), "senior").
Else("adult")
rows, err := ant.Project[Labeled](
db.Users.Where(),
ant.Into(&Labeleds.Name, &Users.Name),
ant.IntoExpr(&Labeleds.Kind, kind),
).All(ctx)
// SELECT "users"."name" AS "name",
// CASE WHEN "users"."age" < $1 THEN $2 WHEN "users"."age" > $3 THEN $4 ELSE $5 END AS "kind"
// FROM "users"
Call any database function
ant.Func calls a function by name with the result type you state. Columns go in as expressions. Other values are bound.
fixed := ant.Func[string]("REPLACE", ant.Col(&Users.Name), "o", "0")
// REPLACE("users"."name", $1, $2)
A SQL fragment
ant.SQL takes your own expression. Each ? takes a column or a bound value, so values never enter the statement text. ant.SQLCond is the same, returning a condition.
label := ant.SQL[string]("? || ?", ant.Col(&Users.Name), "/x")
// "users"."name" || $1
even := ant.SQLCond("? % ? = 0", ant.Col(&Users.Age), 2)
users, err := db.Users.Where(even).All(ctx)
// SELECT ... FROM "users" WHERE ("users"."age" % $1 = 0)
With arguments, their number must match the ? marks, or it is an error that counts both. With none, each ? is written as it is, so jsonb's ? operator works.
The string is SQL. Pass values as arguments and keep the text constant.
See it tested: tests/features/querying/expressions_test.go