Skip to content

Commit bd9167e

Browse files
docs: Update getting started guides, use pgx for Postgres guide (#2891)
* docs: Update getting started guides, use pgx for Postgres guide * Update docs/reference/datatypes.md Co-authored-by: Kyle Gray <kyle@sqlc.dev> --------- Co-authored-by: Kyle Gray <kyle@sqlc.dev>
1 parent 3754349 commit bd9167e

File tree

4 files changed

+109
-76
lines changed

4 files changed

+109
-76
lines changed

docs/reference/datatypes.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ type Place struct {
2727
}
2828
```
2929

30-
## Dates and Time
30+
## Dates and times
3131

32-
All PostgreSQL time and date types are returned as `time.Time` structs. For
32+
All date and time types are returned as `time.Time` structs. For
3333
null time or date values, the `NullTime` type from `database/sql` is used.
34+
3435
The `pgx/v5` sql package uses the appropriate pgx types.
3536

37+
For MySQL users relying on `github.com/go-sql-driver/mysql`, ensure that
38+
`parseTime=true` is added to your database connection string.
39+
3640
```sql
3741
CREATE TABLE authors (
3842
id SERIAL PRIMARY KEY,

docs/tutorials/getting-started-mysql.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Getting started with MySQL
22

33
This tutorial assumes that the latest version of sqlc is
4-
[installed](../overview/install.md) and ready to use.
4+
[installed](../overview/install.md) and ready to use. And we'll
5+
be generating Go code, but other
6+
[language plugins](../reference/language-support.rst) are available.
57

68
Create a new directory called `sqlc-tutorial` and open it up.
79

8-
Initialize a new Go module named `tutorial.sql.dev/app`
10+
Initialize a new Go module named `tutorial.sqlc.dev/app`
911

1012
```shell
1113
go mod init tutorial.sqlc.dev/app
@@ -16,7 +18,7 @@ directory. In our new directory, create a file named `sqlc.yaml` with the
1618
following contents:
1719

1820
```yaml
19-
version: 2
21+
version: "2"
2022
sql:
2123
- engine: "mysql"
2224
queries: "query.sql"
@@ -27,8 +29,9 @@ sql:
2729
out: "tutorial"
2830
```
2931
30-
sqlc needs to know your database schema and queries. In the same directory,
31-
create a file named `schema.sql` with the following contents:
32+
sqlc needs to know your database schema and queries in order to generate code.
33+
In the same directory, create a file named `schema.sql` with the following
34+
content:
3235

3336
```sql
3437
CREATE TABLE authors (
@@ -61,13 +64,15 @@ DELETE FROM authors
6164
WHERE id = ?;
6265
```
6366

64-
You are now ready to generate code. Run the `generate` command. You shouldn't see any errors or output.
67+
You are now ready to generate code. You shouldn't see any output when you run
68+
the `generate` subcommand, unless something goes wrong:
6569

6670
```shell
6771
sqlc generate
6872
```
6973

70-
You should now have a `tutorial` package containing three files.
74+
You should now have a `tutorial` subdirectory with three files containing Go
75+
source code. These files comprise a Go package named `tutorial`:
7176

7277
```
7378
├── go.mod
@@ -80,7 +85,8 @@ You should now have a `tutorial` package containing three files.
8085
└── query.sql.go
8186
```
8287

83-
You can use your newly generated queries in `app.go`.
88+
You can use your newly-generated `tutorial` package from any Go program.
89+
Create a file named `tutorial.go` and add the following contents:
8490

8591
```go
8692
package main
@@ -91,9 +97,9 @@ import (
9197
"log"
9298
"reflect"
9399
94-
"tutorial.sqlc.dev/app/tutorial"
95-
96100
_ "github.com/go-sql-driver/mysql"
101+
102+
"tutorial.sqlc.dev/app/tutorial"
97103
)
98104
99105
func run() error {
@@ -146,17 +152,21 @@ func main() {
146152
}
147153
```
148154

149-
Before the code will compile, you'll need to add the Go MySQL driver.
155+
Before this code will compile you'll need to fetch the relevant MySQL driver:
150156

151-
```
157+
```shell
152158
go get github.com/go-sql-driver/mysql
153159
go build ./...
154160
```
155161

156-
To make that possible, sqlc generates readable, **idiomatic** Go code that you
157-
otherwise would have had to write yourself. Take a look in `tutorial/query.sql.go`.
162+
The program should compile without errors. To make that possible, sqlc generates
163+
readable, **idiomatic** Go code that you otherwise would've had to write
164+
yourself. Take a look in `tutorial/query.sql.go`.
165+
166+
Of course for this program to run successfully you'll need
167+
to compile after replacing the database connection parameters in the call to
168+
`sql.Open()` with the correct parameters for your database. And your
169+
database must have the `authors` table as defined in `schema.sql`.
158170

159-
If your tables have columns with date or time types, sqlc expects these values
160-
to scan into `time.Time` structs. If you're using
161-
`github.com/go-sql-driver/mysql`, ensure that `parseTime=true` has been added to
162-
the connection string.
171+
You should now have a working program using sqlc's generated Go source code,
172+
and hopefully can see how you'd use sqlc in your own real-world applications.

docs/tutorials/getting-started-postgresql.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Getting started with PostgreSQL
22

33
This tutorial assumes that the latest version of sqlc is
4-
[installed](../overview/install.md) and ready to use.
4+
[installed](../overview/install.md) and ready to use. And we'll
5+
be generating Go code, but other
6+
[language plugins](../reference/language-support.rst) are available.
57

68
Create a new directory called `sqlc-tutorial` and open it up.
79

8-
Initialize a new Go module named `tutorial.sqlc.dev/app`
10+
Initialize a new Go module named `tutorial.sqlc.dev/app`:
911

1012
```shell
1113
go mod init tutorial.sqlc.dev/app
@@ -25,6 +27,7 @@ sql:
2527
go:
2628
package: "tutorial"
2729
out: "tutorial"
30+
sql_package: "pgx/v5"
2831
```
2932
3033
sqlc needs to know your database schema and queries in order to generate code.
@@ -39,7 +42,7 @@ CREATE TABLE authors (
3942
);
4043
```
4144

42-
Next, create a `query.sql` file with the following four queries:
45+
Next, create a `query.sql` file with the following five queries:
4346

4447
```sql
4548
-- name: GetAuthor :one
@@ -58,23 +61,19 @@ INSERT INTO authors (
5861
)
5962
RETURNING *;
6063
61-
-- name: DeleteAuthor :exec
62-
DELETE FROM authors
63-
WHERE id = $1;
64-
```
65-
66-
If you **do not** want your SQL `UPDATE` queries to return the updated record
67-
to the user, add this to `query.sql`:
68-
69-
```sql
7064
-- name: UpdateAuthor :exec
7165
UPDATE authors
7266
set name = $2,
7367
bio = $3
7468
WHERE id = $1;
69+
70+
-- name: DeleteAuthor :exec
71+
DELETE FROM authors
72+
WHERE id = $1;
7573
```
7674

77-
Otherwise, to return the updated record to the user, add this to `query.sql`:
75+
If you prefer, you can alter the `UpdateAuthor` query to return the updated
76+
record:
7877

7978
```sql
8079
-- name: UpdateAuthor :one
@@ -85,13 +84,15 @@ WHERE id = $1
8584
RETURNING *;
8685
```
8786

88-
You are now ready to generate code. You shouldn't see any errors or output.
87+
You are now ready to generate code. You shouldn't see any output when you run
88+
the `generate` subcommand, unless something goes wrong:
8989

9090
```shell
9191
sqlc generate
9292
```
9393

94-
You should now have a `tutorial` package containing three files.
94+
You should now have a `tutorial` subdirectory with three files containing Go
95+
source code. These files comprise a Go package named `tutorial`:
9596

9697
```
9798
├── go.mod
@@ -104,31 +105,33 @@ You should now have a `tutorial` package containing three files.
104105
└── query.sql.go
105106
```
106107

107-
You can use your newly generated queries in `app.go`.
108+
You can use your newly-generated `tutorial` package from any Go program.
109+
Create a file named `tutorial.go` and add the following contents:
108110

109111
```go
110112
package main
111113
112114
import (
113115
"context"
114-
"database/sql"
115116
"log"
116117
"reflect"
117118
118-
"tutorial.sqlc.dev/app/tutorial"
119+
"github.com/jackc/pgx/v5"
120+
"github.com/jackc/pgx/v5/pgtype"
119121
120-
_ "github.com/lib/pq"
122+
"tutorial.sqlc.dev/app/tutorial"
121123
)
122124
123125
func run() error {
124126
ctx := context.Background()
125127
126-
db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
128+
conn, err := pgx.Connect(ctx, "user=pqgotest dbname=pqgotest sslmode=verify-full")
127129
if err != nil {
128130
return err
129131
}
132+
defer conn.Close(ctx)
130133
131-
queries := tutorial.New(db)
134+
queries := tutorial.New(conn)
132135
133136
// list all authors
134137
authors, err := queries.ListAuthors(ctx)
@@ -140,7 +143,7 @@ func run() error {
140143
// create an author
141144
insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{
142145
Name: "Brian Kernighan",
143-
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
146+
Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
144147
})
145148
if err != nil {
146149
return err
@@ -165,13 +168,23 @@ func main() {
165168
}
166169
```
167170

168-
Before the code will compile, you'll need to add the Go PostgreSQL driver.
171+
Before this code will compile you'll need to fetch the relevant PostgreSQL
172+
driver. You can use `lib/pq` with the standard library's `database/sql`
173+
package, but in this tutorial we've used `pgx/v5`:
169174

170-
```
171-
go get github.com/lib/pq
175+
```shell
176+
go get github.com/jackc/pgx/v5
172177
go build ./...
173178
```
174179

175-
sqlc generates readable, **idiomatic** Go code that you otherwise would have
176-
had to write yourself. Take a look in the `tutorial` package to see what code
177-
sqlc generated.
180+
The program should compile without errors. To make that possible, sqlc generates
181+
readable, **idiomatic** Go code that you otherwise would've had to write
182+
yourself. Take a look in `tutorial/query.sql.go`.
183+
184+
Of course for this program to run successfully you'll need
185+
to compile after replacing the database connection parameters in the call to
186+
`pgx.Connect()` with the correct parameters for your database. And your
187+
database must have the `authors` table as defined in `schema.sql`.
188+
189+
You should now have a working program using sqlc's generated Go source code,
190+
and hopefully can see how you'd use sqlc in your own real-world applications.

0 commit comments

Comments
 (0)