Skip to content

Commit d7eda89

Browse files
authored
docs: add documentation for sqlc.narg() (#1580)
1 parent a22a7b7 commit d7eda89

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/howto/named_parameters.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,34 @@ SET
5656
END
5757
RETURNING *;
5858
```
59+
60+
## Nullable parameters
61+
62+
sqlc infers the nullability of any specified parameters, and often does exactly
63+
what you want. If you want finer control over the nullability of your
64+
parameters, you may use `sql.narg()` (**n**ullable arg) to override the default
65+
behavior. Using `sql.narg` tells sqlc to ignore whatever nullability it has
66+
inferred and generate a nullable parameter instead. There is no nullable
67+
equivalent of the `@` syntax.
68+
69+
Here is an example that uses a single query to allow updating an author's
70+
name, bio or both.
71+
72+
```sql
73+
-- name: UpdateAuthor :one
74+
UPDATE author
75+
SET
76+
name = coalesce(sqlc.narg('name'), name),
77+
bio = coalesce(sqlc.narg('bio'), bio)
78+
WHERE id = sqlc.arg('id');
79+
```
80+
81+
The following code is generated:
82+
83+
```go
84+
type UpdateAuthorParams struct {
85+
Name sql.NullString
86+
Bio sql.NullString
87+
ID int64
88+
}
89+
```

0 commit comments

Comments
 (0)