|
| 1 | +# Getting started with MySQL |
| 2 | + |
| 3 | +This tutorial assumes that the latest version of sqlc is |
| 4 | +[installed](../overview/install.html) and ready to use. |
| 5 | + |
| 6 | +Create a new directory called `sqlc-tutorial` and open it up. |
| 7 | + |
| 8 | +Initialize a new Go module named `tutorial.sql.dev/app` |
| 9 | + |
| 10 | +```shell |
| 11 | +go mod init tutorial.sqlc.dev/app |
| 12 | +``` |
| 13 | + |
| 14 | +sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current |
| 15 | +directory. In our new directory, create a file named `sqlc.yaml` with the |
| 16 | +following contents: |
| 17 | + |
| 18 | +```yaml |
| 19 | +version: 1 |
| 20 | +packages: |
| 21 | + - path: "tutorial" |
| 22 | + name: "tutorial" |
| 23 | + engine: "mysql" |
| 24 | + schema: "schema.sql" |
| 25 | + queries: "query.sql" |
| 26 | +``` |
| 27 | +
|
| 28 | +sqlc needs to know your database schema and queries. In the same directory, |
| 29 | +create a file named `schema.sql` with the following contents: |
| 30 | + |
| 31 | +```sql |
| 32 | +CREATE TABLE authors ( |
| 33 | + id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 34 | + name text NOT NULL, |
| 35 | + bio text |
| 36 | +); |
| 37 | +``` |
| 38 | + |
| 39 | +Next, create a `query.sql` file with the following four queries: |
| 40 | + |
| 41 | +```sql |
| 42 | +-- name: GetAuthor :one |
| 43 | +SELECT * FROM authors |
| 44 | +WHERE id = ? LIMIT 1; |
| 45 | +
|
| 46 | +-- name: ListAuthors :many |
| 47 | +SELECT * FROM authors |
| 48 | +ORDER BY name; |
| 49 | +
|
| 50 | +-- name: CreateAuthor :execresult |
| 51 | +INSERT INTO authors ( |
| 52 | + name, bio |
| 53 | +) VALUES ( |
| 54 | + ?, ? |
| 55 | +); |
| 56 | +
|
| 57 | +-- name: DeleteAuthor :exec |
| 58 | +DELETE FROM authors |
| 59 | +WHERE id = ?; |
| 60 | +``` |
| 61 | + |
| 62 | +You are now ready to generate code. Run the `generate` command. You shouldn't see any errors or output. |
| 63 | + |
| 64 | +```shell |
| 65 | +sqlc generate |
| 66 | +``` |
| 67 | + |
| 68 | +You should now have a `db` package containing three files. |
| 69 | + |
| 70 | +``` |
| 71 | +├── go.mod |
| 72 | +├── query.sql |
| 73 | +├── schema.sql |
| 74 | +├── sqlc.yaml |
| 75 | +└── tutorial |
| 76 | + ├── db.go |
| 77 | + ├── models.go |
| 78 | + └── query.sql.go |
| 79 | +``` |
| 80 | +
|
| 81 | +You can use your newly generated queries in `app.go`. |
| 82 | +
|
| 83 | +```go |
| 84 | +package main |
| 85 | +
|
| 86 | +import ( |
| 87 | + "context" |
| 88 | + "database/sql" |
| 89 | + "log" |
| 90 | + "reflect" |
| 91 | +
|
| 92 | + "tutorial.sqlc.dev/app/tutorial" |
| 93 | +
|
| 94 | + _ "github.com/go-sql-driver/mysql" |
| 95 | +) |
| 96 | +
|
| 97 | +func run() error { |
| 98 | + ctx := context.Background() |
| 99 | +
|
| 100 | + db, err := sql.Open("mysql", "user:password@/dbname") |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | +
|
| 105 | + queries := tutorial.New(db) |
| 106 | +
|
| 107 | + // list all authors |
| 108 | + authors, err := queries.ListAuthors(ctx) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + log.Println(authors) |
| 113 | +
|
| 114 | + // create an author |
| 115 | + result, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{ |
| 116 | + Name: "Brian Kernighan", |
| 117 | + Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | +
|
| 123 | + insertedAuthorID, err := result.LastInsertId() |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + log.Println(insertedAuthorID) |
| 128 | +
|
| 129 | + // get the author we just inserted |
| 130 | + fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthorID) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | +
|
| 135 | + // prints true |
| 136 | + log.Println(reflect.DeepEqual(insertedAuthorID, fetchedAuthor.ID)) |
| 137 | + return nil |
| 138 | +} |
| 139 | +
|
| 140 | +func main() { |
| 141 | + if err := run(); err != nil { |
| 142 | + log.Fatal(err) |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +Before the code will compile, you'll need to add the Go MySQL driver. |
| 148 | + |
| 149 | +``` |
| 150 | +go get github.com/go-sql-driver/mysql |
| 151 | +go build ./... |
| 152 | +``` |
| 153 | + |
| 154 | +To make that possible, sqlc generates readable, **idiomatic** Go code that you |
| 155 | +otherwise would have had to write yourself. Take a look in `tutorial/query.sql.go`. |
0 commit comments