1
1
# Getting started with PostgreSQL
2
2
3
3
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.
5
7
6
8
Create a new directory called ` sqlc-tutorial ` and open it up.
7
9
8
- Initialize a new Go module named ` tutorial.sqlc.dev/app `
10
+ Initialize a new Go module named ` tutorial.sqlc.dev/app ` :
9
11
10
12
``` shell
11
13
go mod init tutorial.sqlc.dev/app
25
27
go :
26
28
package : " tutorial"
27
29
out : " tutorial"
30
+ sql_package : " pgx/v5"
28
31
` ` `
29
32
30
33
sqlc needs to know your database schema and queries in order to generate code.
@@ -39,7 +42,7 @@ CREATE TABLE authors (
39
42
);
40
43
` ` `
41
44
42
- Next, create a `query.sql` file with the following four queries :
45
+ Next, create a `query.sql` file with the following five queries :
43
46
44
47
` ` ` sql
45
48
-- name: GetAuthor :one
@@ -58,23 +61,19 @@ INSERT INTO authors (
58
61
)
59
62
RETURNING *;
60
63
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
70
64
-- name: UpdateAuthor :exec
71
65
UPDATE authors
72
66
set name = $2,
73
67
bio = $3
74
68
WHERE id = $1;
69
+
70
+ -- name: DeleteAuthor :exec
71
+ DELETE FROM authors
72
+ WHERE id = $1;
75
73
` ` `
76
74
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 :
78
77
79
78
` ` ` sql
80
79
-- name: UpdateAuthor :one
@@ -85,13 +84,15 @@ WHERE id = $1
85
84
RETURNING *;
86
85
` ` `
87
86
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 :
89
89
90
90
` ` ` shell
91
91
sqlc generate
92
92
` ` `
93
93
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` :
95
96
96
97
` ` `
97
98
├── go.mod
@@ -104,31 +105,33 @@ You should now have a `tutorial` package containing three files.
104
105
└── query.sql.go
105
106
` ` `
106
107
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 :
108
110
109
111
` ` ` go
110
112
package main
111
113
112
114
import (
113
115
"context"
114
- "database/sql"
115
116
"log"
116
117
"reflect"
117
118
118
- "tutorial.sqlc.dev/app/tutorial"
119
+ "github.com/jackc/pgx/v5"
120
+ "github.com/jackc/pgx/v5/pgtype"
119
121
120
- _ "github.com/lib/pq "
122
+ "tutorial.sqlc.dev/app/tutorial "
121
123
)
122
124
123
125
func run() error {
124
126
ctx := context.Background()
125
127
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")
127
129
if err != nil {
128
130
return err
129
131
}
132
+ defer conn.Close(ctx)
130
133
131
- queries := tutorial.New(db )
134
+ queries := tutorial.New(conn )
132
135
133
136
// list all authors
134
137
authors, err := queries.ListAuthors(ctx)
@@ -140,7 +143,7 @@ func run() error {
140
143
// create an author
141
144
insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{
142
145
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},
144
147
})
145
148
if err != nil {
146
149
return err
@@ -165,13 +168,23 @@ func main() {
165
168
}
166
169
` ` `
167
170
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` :
169
174
170
- ```
171
- go get github.com/lib/pq
175
+ ` ` ` shell
176
+ go get github.com/jackc/pgx/v5
172
177
go build ./...
173
178
` ` `
174
179
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