forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Document sqlc.* macros (sqlc-dev#2698)
Add a new section on embedding structs
- Loading branch information
1 parent
9c9b577
commit b6f785f
Showing
4 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#### Embedding structs | ||
|
||
Embedding allows you to reuse existing model structs in more queries, resulting | ||
in less manual serialization work. First, imagine we have the following schema | ||
with students and test scores. | ||
|
||
```sql | ||
CREATE TABLE students ( | ||
id bigserial PRIMARY KEY, | ||
name text NOT NULL, | ||
age integer NOT NULL | ||
); | ||
|
||
CREATE TABLE test_scores ( | ||
student_id bigint NOT NULL, | ||
score integer NOT NULL, | ||
grade text NOT NULL | ||
); | ||
``` | ||
|
||
We want to select the student record and the scores they got on a test. | ||
Here's how we'd usually do that: | ||
|
||
```sql | ||
-- name: ScoreAndTests :many | ||
SELECT students.*, test_scores.* | ||
FROM students | ||
JOIN test_scores ON test_scores.student_id = students.id | ||
WHERE students.id = ?; | ||
``` | ||
|
||
When using Go, sqlc will produce a struct like this: | ||
|
||
```go | ||
type ScoreAndTestsRow struct { | ||
ID int64 | ||
Name string | ||
Age int32 | ||
StudentID int64 | ||
Score int32 | ||
Grade string | ||
} | ||
``` | ||
|
||
With embedding, the struct will contain a model for both tables instead of a | ||
flattened list of columns. | ||
|
||
```sql | ||
-- name: ScoreAndTests :many | ||
SELECT sqlc.embed(students), sqlc.embed(test_scores) | ||
FROM students | ||
JOIN test_scores ON test_scores.student_id = students.id | ||
WHERE students.id = ?; | ||
``` | ||
|
||
``` | ||
type ScoreAndTestsRow struct { | ||
Student Student | ||
TestScore TestScore | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Macros | ||
|
||
## `sqlc.arg` | ||
|
||
Attach a name to a parameter in a SQL query. This macro expands to an | ||
engine-specific parameter placeholder. The name of the parameter is noted and | ||
used during code generation. | ||
|
||
```sql | ||
-- name: GetAuthorByName :one | ||
SELECT * | ||
FROM authors | ||
WHERE lower(name) = sqlc.arg(name); | ||
|
||
-- >>> EXPANDS TO >>> | ||
|
||
-- name: GetAuthorByName :one | ||
SELECT * | ||
FROM authors | ||
WHERE lower(name) = ?; | ||
``` | ||
|
||
See more examples in [Naming parameters](../howto/named_parameters). | ||
|
||
## `sqlc.embed` | ||
|
||
Embedding allows you to reuse existing model structs in more queries, resulting | ||
in less manual serialization work. First, imagine we have the following schema | ||
with students and test scores. | ||
|
||
```sql | ||
CREATE TABLE students ( | ||
id bigserial PRIMARY KEY, | ||
name text, | ||
age integer | ||
); | ||
|
||
CREATE TABLE test_scores ( | ||
student_id bigint, | ||
score integer, | ||
grade text | ||
); | ||
``` | ||
|
||
```sql | ||
-- name: GetStudentAndScore :one | ||
SELECT sqlc.embed(students), sqlc.embed(test_scores) | ||
FROM students | ||
JOIN test_scores ON test_scores.student_id = students.id | ||
WHERE students.id = $1; | ||
|
||
-- >>> EXPANDS TO >>> | ||
|
||
-- name: GetStudentAndScore :one | ||
SELECT students.*, test_scores.* | ||
FROM students | ||
JOIN test_scores ON test_scores.student_id = students.id | ||
WHERE students.id = $1; | ||
``` | ||
|
||
The Go method will return a struct with a field for the `Student` and field for | ||
the test `TestScore` instead of each column existing on the struct. | ||
|
||
```go | ||
type GetStudentAndScoreRow struct { | ||
Student Student | ||
TestScore TestScore | ||
} | ||
|
||
func (q *Queries) GetStudentAndScore(ctx context.Context, id int64) (GetStudentAndScoreRow, error) { | ||
// ... | ||
} | ||
``` | ||
|
||
See a full example in [Embedding structs](../howto/embedding). | ||
|
||
## `sqlc.narg` | ||
|
||
The same as `sqlc.arg`, but always marks the parameter as nullable. | ||
|
||
```sql | ||
-- name: GetAuthorByName :one | ||
SELECT * | ||
FROM authors | ||
WHERE lower(name) = sqlc.narg(name); | ||
|
||
-- >>> EXPANDS TO >>> | ||
|
||
-- name: GetAuthorByName :one | ||
SELECT * | ||
FROM authors | ||
WHERE LOWER(name) = ?; | ||
``` | ||
|
||
See more examples in [Naming parameters](../howto/named_parameters). | ||
|
||
## `sqlc.slice` | ||
|
||
For drivers that do not support passing slices to the IN operator, the | ||
`sqlc.slice` macro generates a dynamic query at runtime with the correct | ||
number of parameters. | ||
|
||
```sql | ||
/* name: SelectStudents :many */ | ||
SELECT * FROM students | ||
WHERE age IN (sqlc.slice("ages")) | ||
|
||
-- >>> EXPANDS TO >>> | ||
|
||
/* name: SelectStudents :many */ | ||
SELECT id, name, age FROM authors | ||
WHERE age IN (/*SLICE:ages*/?) | ||
``` | ||
|
||
Since the `/*SLICE:ages*/` placeholder is dynamically replaced on a per-query | ||
basis, this macro can't be used with prepared statements. | ||
|
||
See a full example in [Passing a slice as a parameter to a | ||
query](../howto/select.html#mysql-and-sqlite). |