Skip to content

Commit d7f6eb3

Browse files
authored
docs: Add named parameter documentation (#332)
* docs: Add named parameter documentation * Add named parameters to README
1 parent 65fd4a2 commit d7f6eb3

2 files changed

Lines changed: 61 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
> 🚨
2-
>
3-
> sqlc is **new** and under rapid development.
4-
>
5-
> The code it generates is correct and safe for production use, but there is
6-
> currently no guarantee of stability or backwards-compatibility of the command
7-
> line interface, configuration file format or generated code.
8-
>
9-
> 🚨
10-
111
# sqlc: A SQL Compiler
122

133
> And lo, the Great One looked down upon the people and proclaimed:
@@ -232,6 +222,7 @@ Your favorite PostgreSQL / Go features are supported:
232222
- [Query annotations](./docs/annotations.md)
233223
- [Transactions](./docs/transactions.md)
234224
- [Prepared queries](./docs/prepared_query.md)
225+
- [Named parameters](./docs/named_parameters.md)
235226
- [SELECT](./docs/query_one.md)
236227
- [NULL](./docs/null.md)
237228
- [COUNT](./docs/query_count.md)
@@ -420,7 +411,8 @@ Each commit is deployed to the [`devel` channel on Equinox](https://dl.equinox.i
420411
## Other Databases and Languages
421412

422413
sqlc currently only supports PostgreSQL / Go. MySQL support has been merged,
423-
but it's marked as experimental. SQLite and TypeScript support are planned.
414+
but it's marked as experimental. SQLite, TypeScript, and Kotlin support are
415+
planned.
424416

425417
| Language | PostgreSQL | MySQL | SQLite |
426418
| ------------ |:----------------:|:----------------:|:----------------:|

docs/named_parameters.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Named parameters
2+
3+
sqlc tried to generate good names for positional parameters, but sometimes it
4+
lacks enough context. The following SQL generates parameters with less than
5+
ideal names:
6+
7+
```sql
8+
-- name: UpsertAuthorName :one
9+
UPDATE author
10+
SET
11+
name = CASE WHEN $1::bool
12+
THEN $2::text
13+
ELSE name
14+
END
15+
RETURNING *;
16+
```
17+
18+
```go
19+
type UpdateAuthorNameParams struct {
20+
Column1 bool `json:""`
21+
Column2_2 string `json:"_2"`
22+
}
23+
```
24+
25+
In these cases, named parameters give you the control over field names on the
26+
Params struct.
27+
28+
```sql
29+
-- name: UpsertAuthorName :one
30+
UPDATE author
31+
SET
32+
name = CASE WHEN sqlc.arg(set_name)::bool
33+
THEN sqlc.arg(name)::text
34+
ELSE name
35+
END
36+
RETURNING *;
37+
```
38+
39+
```go
40+
type UpdateAuthorNameParams struct {
41+
SetName bool `json:"set_name"`
42+
Name string `json:"name"`
43+
}
44+
```
45+
46+
If the `sqlc.arg()` syntax is too verbose for your taste, you can use the `@`
47+
operator as a shortcut.
48+
49+
```sql
50+
-- name: UpsertAuthorName :one
51+
UPDATE author
52+
SET
53+
name = CASE WHEN @set_name::bool
54+
THEN @name::text
55+
ELSE name
56+
END
57+
RETURNING *;
58+
```

0 commit comments

Comments
 (0)