From 42d7fded90f0e75c7bdb0c334b2f77986f7cd6b2 Mon Sep 17 00:00:00 2001 From: Steven Kabbes Date: Sat, 30 Apr 2022 17:46:54 -0700 Subject: [PATCH] docs: add documentation for sqlc.narg() --- docs/howto/named_parameters.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/howto/named_parameters.md b/docs/howto/named_parameters.md index f2af4352e9..98d27df283 100644 --- a/docs/howto/named_parameters.md +++ b/docs/howto/named_parameters.md @@ -56,3 +56,34 @@ SET END RETURNING *; ``` + +## Nullable parameters + +sqlc infers the nullability of any specified parameters, and often does exactly +what you want. If you want finer control over the nullability of your +parameters, you may use `sql.narg()` (**n**ullable arg) to override the default +behavior. Using `sql.narg` tells sqlc to ignore whatever nullability it has +inferred and generate a nullable parameter instead. There is no nullable +equivalent of the `@` syntax. + +Here is an example that uses a single query to allow updating an author's +name, bio or both. + +```sql +-- name: UpdateAuthor :one +UPDATE author +SET + name = coalesce(sqlc.narg('name'), name), + bio = coalesce(sqlc.narg('bio'), bio) +WHERE id = sqlc.arg('id'); +``` + +The following code is generated: + +```go +type UpdateAuthorParams struct { + Name sql.NullString + Bio sql.NullString + ID int64 +} +```