Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs: fix parameter syntax inconsistency for MySQL and SQLite
Update howto documentation to include database-specific parameter syntax:
- PostgreSQL: $1, $2, etc.
- MySQL/SQLite: ? placeholders

Changes made to:
- docs/howto/update.md
- docs/howto/insert.md
- docs/howto/delete.md
- docs/howto/select.md

This resolves confusion where the generic examples only showed PostgreSQL
syntax, causing silent failures in MySQL code generation and syntax errors
in SQLite.

Fixes #3697

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
  • Loading branch information
kotahorii and claude committed Aug 2, 2025
commit 4976cf15f208edd185b570d56eb2bae6fcb95a52
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