Skip to content

Commit 1d778a1

Browse files
authored
Reorganzie and conslidate the existing documentation (#885)
* docs: Reorganize and consolidate documentation
2 parents 5c95eea + 7a1ada7 commit 1d778a1

26 files changed

+459
-452
lines changed

docs/guides/privacy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Privacy and Data Collection
1+
# Privacy and data collection
22

33
These days, it feels like every piece of software is tracking you. From your
44
browser, to your phone, to your terminal, programs collect as much data about

docs/howto/alter_table.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/howto/any.md

Lines changed: 0 additions & 79 deletions
This file was deleted.

docs/howto/arrays.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

docs/howto/migrations.md renamed to docs/howto/ddl.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
# Migrations
1+
# Modifying the database schema
22

3-
sqlc will ignore rollback statements when parsing migration SQL files. The following tools are current supported:
3+
sqlc understands `ALTER TABLE` statements when parsing SQL.
44

5+
```sql
6+
CREATE TABLE authors (
7+
id SERIAL PRIMARY KEY,
8+
birth_year int NOT NULL
9+
);
10+
11+
ALTER TABLE authors ADD COLUMN bio text NOT NULL;
12+
ALTER TABLE authors DROP COLUMN birth_year;
13+
ALTER TABLE authors RENAME TO writers;
14+
```
15+
16+
```go
17+
package db
18+
19+
type Writer struct {
20+
ID int
21+
Bio string
22+
}
23+
```
24+
25+
## Handling SQL migrations
26+
27+
sqlc will ignore rollback statements when parsing migration SQL files. The
28+
following tools are current supported:
29+
30+
- [dbmate](https://github.com/amacneil/dbmate)
31+
- [golang-migrate](https://github.com/golang-migrate/migrate)
532
- [goose](https://github.com/pressly/goose)
633
- [sql-migrate](https://github.com/rubenv/sql-migrate)
734
- [tern](https://github.com/jackc/tern)
8-
- [golang-migrate](https://github.com/golang-migrate/migrate)
9-
- [dbmate](https://github.com/amacneil/dbmate)
1035

11-
## goose
36+
### goose
1237

1338
```sql
1439
-- +goose Up
@@ -71,7 +96,7 @@ type Comment struct {
7196
}
7297
```
7398

74-
## golang-migrate
99+
### golang-migrate
75100

76101
In `20060102.up.sql`:
77102

@@ -100,7 +125,7 @@ type Post struct {
100125
}
101126
```
102127

103-
## dbmate
128+
### dbmate
104129

105130
```sql
106131
-- migrate:up

docs/howto/delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Deleting records
1+
# Deleting rows
22

33
```sql
44
CREATE TABLE authors (

docs/howto/enums.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

docs/howto/insert.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Inserting records
1+
# Inserting rows
22

33
```sql
44
CREATE TABLE authors (
@@ -39,3 +39,100 @@ func (q *Queries) CreateAuthor(ctx context.Context, bio string) error {
3939
return err
4040
}
4141
```
42+
43+
## Returning columns from inserted rows
44+
45+
sqlc has full support for the `RETURNING` statement.
46+
47+
```sql
48+
CREATE TABLE authors (
49+
id SERIAL PRIMARY KEY,
50+
bio text NOT NULL
51+
);
52+
53+
-- name: Delete :exec
54+
DELETE FROM authors WHERE id = $1;
55+
56+
-- name: DeleteAffected :execrows
57+
DELETE FROM authors WHERE id = $1;
58+
59+
-- name: DeleteID :one
60+
DELETE FROM authors WHERE id = $1
61+
RETURNING id;
62+
63+
-- name: DeleteAuthor :one
64+
DELETE FROM authors WHERE id = $1
65+
RETURNING *;
66+
```
67+
68+
```go
69+
package db
70+
71+
import (
72+
"context"
73+
"database/sql"
74+
)
75+
76+
type Author struct {
77+
ID int
78+
Bio string
79+
}
80+
81+
type DBTX interface {
82+
ExecContext(context.Context, string, ...interface{}) error
83+
QueryRowContext(context.Context, string, ...interface{}) error
84+
}
85+
86+
func New(db DBTX) *Queries {
87+
return &Queries{db: db}
88+
}
89+
90+
type Queries struct {
91+
db DBTX
92+
}
93+
94+
const delete = `-- name: Delete :exec
95+
DELETE FROM authors WHERE id = $1
96+
`
97+
98+
func (q *Queries) Delete(ctx context.Context, id int) error {
99+
_, err := q.db.ExecContext(ctx, delete, id)
100+
return err
101+
}
102+
103+
const deleteAffected = `-- name: DeleteAffected :exec
104+
DELETE FROM authors WHERE id = $1
105+
`
106+
107+
func (q *Queries) DeleteAffected(ctx context.Context, id int) (int64, error) {
108+
result, err := q.db.ExecContext(ctx, deleteAffected, id)
109+
if err != nil {
110+
return 0, err
111+
}
112+
return result.RowsAffected()
113+
}
114+
115+
const deleteID = `-- name: DeleteID :one
116+
DELETE FROM authors WHERE id = $1
117+
RETURNING id
118+
`
119+
120+
func (q *Queries) DeleteID(ctx context.Context, id int) (int, error) {
121+
row := q.db.QueryRowContext(ctx, deleteID, id)
122+
var i int
123+
err := row.Scan(&i)
124+
return i, err
125+
}
126+
127+
const deleteAuhtor = `-- name: DeleteAuthor :one
128+
DELETE FROM authors WHERE id = $1
129+
RETURNING id, bio
130+
`
131+
132+
func (q *Queries) DeleteAuthor(ctx context.Context, id int) (Author, error) {
133+
row := q.db.QueryRowContext(ctx, deleteAuhtor, id)
134+
var i Author
135+
err := row.Scan(&i.ID, &i.Bio)
136+
return i, err
137+
}
138+
```

docs/howto/json_tags.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

docs/howto/named_parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Named parameters
1+
# Naming parameters
22

33
sqlc tried to generate good names for positional parameters, but sometimes it
44
lacks enough context. The following SQL generates parameters with less than

0 commit comments

Comments
 (0)