Skip to content

docs: fix parameter syntax inconsistency for MySQL and SQLite #4036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/howto/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ CREATE TABLE authors (
id SERIAL PRIMARY KEY,
bio text NOT NULL
);
```

The parameter syntax varies by database engine:

**PostgreSQL:**
```sql
-- name: DeleteAuthor :exec
DELETE FROM authors WHERE id = $1;
```

**MySQL and SQLite:**
```sql
-- name: DeleteAuthor :exec
DELETE FROM authors WHERE id = ?;
```

```go
package db

Expand Down
35 changes: 35 additions & 0 deletions docs/howto/insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ CREATE TABLE authors (
id SERIAL PRIMARY KEY,
bio text NOT NULL
);
```

The parameter syntax varies by database engine:

**PostgreSQL:**
```sql
-- name: CreateAuthor :exec
INSERT INTO authors (bio) VALUES ($1);
```

**MySQL and SQLite:**
```sql
-- name: CreateAuthor :exec
INSERT INTO authors (bio) VALUES (?);
```

```go
package db

Expand Down Expand Up @@ -51,7 +62,10 @@ CREATE TABLE authors (
name text NOT NULL,
bio text
);
```

**PostgreSQL:**
```sql
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
Expand All @@ -69,6 +83,27 @@ INSERT INTO authors (
RETURNING id;
```

**SQLite (with RETURNING support):**
```sql
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)
RETURNING *;

-- name: CreateAuthorAndReturnId :one
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)
RETURNING id;
```

Note: MySQL does not support the `RETURNING` clause. Use `:execresult` instead to get the last insert ID.

```go
package db

Expand Down
15 changes: 15 additions & 0 deletions docs/howto/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ CREATE TABLE authors (
bio text NOT NULL,
birth_year int NOT NULL
);
```

The parameter syntax varies by database engine:

**PostgreSQL:**
```sql
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1;
Expand All @@ -19,6 +23,17 @@ SELECT * FROM authors
ORDER BY id;
```

**MySQL and SQLite:**
```sql
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = ?;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY id;
```

A few new pieces of code are generated beyond the `Author` struct. An interface
for the underlying database is generated. The `*sql.DB` and `*sql.Tx` types
satisfy this interface.
Expand Down
19 changes: 19 additions & 0 deletions docs/howto/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ CREATE TABLE authors (
If your query has a single parameter, your Go method will also have a single
parameter.

The parameter syntax varies by database engine:

**PostgreSQL:**
```sql
-- name: UpdateAuthorBios :exec
UPDATE authors SET bio = $1;
```

**MySQL and SQLite:**
```sql
-- name: UpdateAuthorBios :exec
UPDATE authors SET bio = ?;
```

```go
package db

Expand Down Expand Up @@ -52,12 +61,22 @@ func (q *Queries) UpdateAuthorBios(ctx context.Context, bio string) error {
If your query has more than one parameter, your Go method will accept a
`Params` struct.

**PostgreSQL:**
```sql
-- name: UpdateAuthor :exec
UPDATE authors SET bio = $2
WHERE id = $1;
```

**MySQL and SQLite:**
```sql
-- name: UpdateAuthor :exec
UPDATE authors SET bio = ?
WHERE id = ?;
```

Note: For MySQL and SQLite, parameters are bound in the order they appear in the query, regardless of the order in the function signature.

```go
package db

Expand Down
Loading