Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
202 lines
4.1 KiB
Markdown
202 lines
4.1 KiB
Markdown
# Getting started with SQLite
|
|
|
|
This tutorial assumes that the latest version of sqlc is
|
|
[installed](../overview/install.md) and ready to use.
|
|
|
|
We'll generate Go code here, but other
|
|
[language plugins](../reference/language-support.md) are available. You'll
|
|
naturally need the Go toolchain if you want to build and run a program with the
|
|
code sqlc generates, but sqlc itself has no dependencies.
|
|
|
|
## Setting up
|
|
|
|
Create a new directory called `sqlc-tutorial` and open it up.
|
|
|
|
Initialize a new Go module named `tutorial.sqlc.dev/app`
|
|
|
|
```shell
|
|
go mod init tutorial.sqlc.dev/app
|
|
```
|
|
|
|
sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current
|
|
directory. In our new directory, create a file named `sqlc.yaml` with the
|
|
following contents:
|
|
|
|
```yaml
|
|
version: "2"
|
|
sql:
|
|
- engine: "sqlite"
|
|
queries: "query.sql"
|
|
schema: "schema.sql"
|
|
gen:
|
|
go:
|
|
package: "tutorial"
|
|
out: "tutorial"
|
|
```
|
|
|
|
## Schema and queries
|
|
|
|
sqlc needs to know your database schema and queries in order to generate code.
|
|
In the same directory, create a file named `schema.sql` with the following
|
|
content:
|
|
|
|
```sql
|
|
CREATE TABLE authors (
|
|
id INTEGER PRIMARY KEY,
|
|
name text NOT NULL,
|
|
bio text
|
|
);
|
|
```
|
|
|
|
Next, create a `query.sql` file with the following five queries:
|
|
|
|
```sql
|
|
-- name: GetAuthor :one
|
|
SELECT * FROM authors
|
|
WHERE id = ? LIMIT 1;
|
|
|
|
-- name: ListAuthors :many
|
|
SELECT * FROM authors
|
|
ORDER BY name;
|
|
|
|
-- name: CreateAuthor :one
|
|
INSERT INTO authors (
|
|
name, bio
|
|
) VALUES (
|
|
?, ?
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: UpdateAuthor :exec
|
|
UPDATE authors
|
|
set name = ?,
|
|
bio = ?
|
|
WHERE id = ?;
|
|
|
|
-- name: DeleteAuthor :exec
|
|
DELETE FROM authors
|
|
WHERE id = ?;
|
|
```
|
|
|
|
If you prefer, you can alter the `UpdateAuthor` query to return the updated
|
|
record:
|
|
|
|
```sql
|
|
-- name: UpdateAuthor :one
|
|
UPDATE authors
|
|
set name = ?,
|
|
bio = ?
|
|
WHERE id = ?
|
|
RETURNING *;
|
|
```
|
|
|
|
## Generating code
|
|
|
|
You are now ready to generate code. You shouldn't see any output when you run
|
|
the `generate` subcommand, unless something goes wrong:
|
|
|
|
```shell
|
|
sqlc generate
|
|
```
|
|
|
|
You should now have a `tutorial` subdirectory with three files containing Go
|
|
source code. These files comprise a Go package named `tutorial`:
|
|
|
|
```
|
|
├── go.mod
|
|
├── query.sql
|
|
├── schema.sql
|
|
├── sqlc.yaml
|
|
└── tutorial
|
|
├── db.go
|
|
├── models.go
|
|
└── query.sql.go
|
|
```
|
|
|
|
## Using generated code
|
|
|
|
You can use your newly-generated `tutorial` package from any Go program.
|
|
Create a file named `tutorial.go` and add the following contents:
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
_ "embed"
|
|
"log"
|
|
"reflect"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
"tutorial.sqlc.dev/app/tutorial"
|
|
)
|
|
|
|
//go:embed schema.sql
|
|
var ddl string
|
|
|
|
func run() error {
|
|
ctx := context.Background()
|
|
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// create tables
|
|
if _, err := db.ExecContext(ctx, ddl); err != nil {
|
|
return err
|
|
}
|
|
|
|
queries := tutorial.New(db)
|
|
|
|
// list all authors
|
|
authors, err := queries.ListAuthors(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Println(authors)
|
|
|
|
// create an author
|
|
insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{
|
|
Name: "Brian Kernighan",
|
|
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Println(insertedAuthor)
|
|
|
|
// get the author we just inserted
|
|
fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthor.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// prints true
|
|
log.Println(reflect.DeepEqual(insertedAuthor, fetchedAuthor))
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
Before this code will compile you'll need to fetch the relevant SQLite driver:
|
|
|
|
```shell
|
|
go get modernc.org/sqlite
|
|
go build .
|
|
```
|
|
|
|
The program should compile without errors, and run successfully. To make that
|
|
possible, sqlc generates readable, **idiomatic** Go code that you
|
|
otherwise would've had to write yourself. Take a look in `tutorial/query.sql.go`.
|
|
|
|
You should now have a working program using sqlc's generated Go source code,
|
|
and hopefully can see how you'd use sqlc in your own real-world applications.
|