|
| 1 | +# Macros |
| 2 | + |
| 3 | +## `sqlc.arg` |
| 4 | + |
| 5 | +Attach a name to a parameter in a SQL query. This macro expands to an |
| 6 | +engine-specific parameter placeholder. The name of the parameter is noted and |
| 7 | +used during code generation. |
| 8 | + |
| 9 | +```sql |
| 10 | +-- name: GetAuthorByName :one |
| 11 | +SELECT * |
| 12 | +FROM authors |
| 13 | +WHERE lower(name) = sqlc.arg(name); |
| 14 | + |
| 15 | +-- >>> EXPANDS TO >>> |
| 16 | + |
| 17 | +-- name: GetAuthorByName :one |
| 18 | +SELECT * |
| 19 | +FROM authors |
| 20 | +WHERE lower(name) = ?; |
| 21 | +``` |
| 22 | + |
| 23 | +See more examples in [Naming parameters](../howto/named_parameters). |
| 24 | + |
| 25 | +## `sqlc.embed` |
| 26 | + |
| 27 | +Embedding allows you to reuse existing model structs in more queries, resulting |
| 28 | +in less manual serialization work. First, imagine we have the following schema |
| 29 | +with students and test scores. |
| 30 | + |
| 31 | +```sql |
| 32 | +CREATE TABLE students ( |
| 33 | + id bigserial PRIMARY KEY, |
| 34 | + name text, |
| 35 | + age integer |
| 36 | +); |
| 37 | + |
| 38 | +CREATE TABLE test_scores ( |
| 39 | + student_id bigint, |
| 40 | + score integer, |
| 41 | + grade text |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +```sql |
| 46 | +-- name: GetStudentAndScore :one |
| 47 | +SELECT sqlc.embed(students), sqlc.embed(test_scores) |
| 48 | +FROM students |
| 49 | +JOIN test_scores ON test_scores.student_id = students.id |
| 50 | +WHERE students.id = $1; |
| 51 | + |
| 52 | +-- >>> EXPANDS TO >>> |
| 53 | + |
| 54 | +-- name: GetStudentAndScore :one |
| 55 | +SELECT students.*, test_scores.* |
| 56 | +FROM students |
| 57 | +JOIN test_scores ON test_scores.student_id = students.id |
| 58 | +WHERE students.id = $1; |
| 59 | +``` |
| 60 | + |
| 61 | +The Go method will return a struct with a field for the `Student` and field for |
| 62 | +the test `TestScore` instead of each column existing on the struct. |
| 63 | + |
| 64 | +```go |
| 65 | +type GetStudentAndScoreRow struct { |
| 66 | + Student Student |
| 67 | + TestScore TestScore |
| 68 | +} |
| 69 | + |
| 70 | +func (q *Queries) GetStudentAndScore(ctx context.Context, id int64) (GetStudentAndScoreRow, error) { |
| 71 | + // ... |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +See a full example in [Embedding structs](../howto/embedding). |
| 76 | + |
| 77 | +## `sqlc.narg` |
| 78 | + |
| 79 | +The same as `sqlc.arg`, but always marks the parameter as nullable. |
| 80 | + |
| 81 | +```sql |
| 82 | +-- name: GetAuthorByName :one |
| 83 | +SELECT * |
| 84 | +FROM authors |
| 85 | +WHERE lower(name) = sqlc.narg(name); |
| 86 | + |
| 87 | +-- >>> EXPANDS TO >>> |
| 88 | + |
| 89 | +-- name: GetAuthorByName :one |
| 90 | +SELECT * |
| 91 | +FROM authors |
| 92 | +WHERE LOWER(name) = ?; |
| 93 | +``` |
| 94 | + |
| 95 | +See more examples in [Naming parameters](../howto/named_parameters). |
| 96 | + |
| 97 | +## `sqlc.slice` |
| 98 | + |
| 99 | +For drivers that do not support passing slices to the IN operator, the |
| 100 | +`sqlc.slice` macro generates a dynamic query at runtime with the correct |
| 101 | +number of parameters. |
| 102 | + |
| 103 | +```sql |
| 104 | +/* name: SelectStudents :many */ |
| 105 | +SELECT * FROM students |
| 106 | +WHERE age IN (sqlc.slice("ages")) |
| 107 | + |
| 108 | +-- >>> EXPANDS TO >>> |
| 109 | + |
| 110 | +/* name: SelectStudents :many */ |
| 111 | +SELECT id, name, age FROM authors |
| 112 | +WHERE age IN (/*SLICE:ages*/?) |
| 113 | +``` |
| 114 | + |
| 115 | +Since the `/*SLICE:ages*/` placeholder is dynamically replaced on a per-query |
| 116 | +basis, this macro can't be used with prepared statements. |
| 117 | + |
| 118 | +See a full example in [Passing a slice as a parameter to a |
| 119 | +query](../howto/select.html#mysql-and-sqlite). |
0 commit comments