Skip to content

Commit 17ace55

Browse files
kotahoriiclaude
andauthored
docs: fix parameter syntax inconsistency for MySQL and SQLite (#4036)
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 <[email protected]>
1 parent 6bc3e48 commit 17ace55

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

docs/howto/delete.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ CREATE TABLE authors (
55
id SERIAL PRIMARY KEY,
66
bio text NOT NULL
77
);
8+
```
9+
10+
The parameter syntax varies by database engine:
811

12+
**PostgreSQL:**
13+
```sql
914
-- name: DeleteAuthor :exec
1015
DELETE FROM authors WHERE id = $1;
1116
```
1217

18+
**MySQL and SQLite:**
19+
```sql
20+
-- name: DeleteAuthor :exec
21+
DELETE FROM authors WHERE id = ?;
22+
```
23+
1324
```go
1425
package db
1526

docs/howto/insert.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ CREATE TABLE authors (
55
id SERIAL PRIMARY KEY,
66
bio text NOT NULL
77
);
8+
```
9+
10+
The parameter syntax varies by database engine:
811

12+
**PostgreSQL:**
13+
```sql
914
-- name: CreateAuthor :exec
1015
INSERT INTO authors (bio) VALUES ($1);
1116
```
1217

18+
**MySQL and SQLite:**
19+
```sql
20+
-- name: CreateAuthor :exec
21+
INSERT INTO authors (bio) VALUES (?);
22+
```
23+
1324
```go
1425
package db
1526

@@ -51,7 +62,10 @@ CREATE TABLE authors (
5162
name text NOT NULL,
5263
bio text
5364
);
65+
```
5466

67+
**PostgreSQL:**
68+
```sql
5569
-- name: CreateAuthor :one
5670
INSERT INTO authors (
5771
name, bio
@@ -69,6 +83,27 @@ INSERT INTO authors (
6983
RETURNING id;
7084
```
7185

86+
**SQLite (with RETURNING support):**
87+
```sql
88+
-- name: CreateAuthor :one
89+
INSERT INTO authors (
90+
name, bio
91+
) VALUES (
92+
?, ?
93+
)
94+
RETURNING *;
95+
96+
-- name: CreateAuthorAndReturnId :one
97+
INSERT INTO authors (
98+
name, bio
99+
) VALUES (
100+
?, ?
101+
)
102+
RETURNING id;
103+
```
104+
105+
Note: MySQL does not support the `RETURNING` clause. Use `:execresult` instead to get the last insert ID.
106+
72107
```go
73108
package db
74109

docs/howto/select.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ CREATE TABLE authors (
88
bio text NOT NULL,
99
birth_year int NOT NULL
1010
);
11+
```
1112

13+
The parameter syntax varies by database engine:
1214

15+
**PostgreSQL:**
16+
```sql
1317
-- name: GetAuthor :one
1418
SELECT * FROM authors
1519
WHERE id = $1;
@@ -19,6 +23,17 @@ SELECT * FROM authors
1923
ORDER BY id;
2024
```
2125

26+
**MySQL and SQLite:**
27+
```sql
28+
-- name: GetAuthor :one
29+
SELECT * FROM authors
30+
WHERE id = ?;
31+
32+
-- name: ListAuthors :many
33+
SELECT * FROM authors
34+
ORDER BY id;
35+
```
36+
2237
A few new pieces of code are generated beyond the `Author` struct. An interface
2338
for the underlying database is generated. The `*sql.DB` and `*sql.Tx` types
2439
satisfy this interface.

docs/howto/update.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@ CREATE TABLE authors (
1212
If your query has a single parameter, your Go method will also have a single
1313
parameter.
1414

15+
The parameter syntax varies by database engine:
16+
17+
**PostgreSQL:**
1518
```sql
1619
-- name: UpdateAuthorBios :exec
1720
UPDATE authors SET bio = $1;
1821
```
1922

23+
**MySQL and SQLite:**
24+
```sql
25+
-- name: UpdateAuthorBios :exec
26+
UPDATE authors SET bio = ?;
27+
```
28+
2029
```go
2130
package db
2231

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

64+
**PostgreSQL:**
5565
```sql
5666
-- name: UpdateAuthor :exec
5767
UPDATE authors SET bio = $2
5868
WHERE id = $1;
5969
```
6070

71+
**MySQL and SQLite:**
72+
```sql
73+
-- name: UpdateAuthor :exec
74+
UPDATE authors SET bio = ?
75+
WHERE id = ?;
76+
```
77+
78+
Note: For MySQL and SQLite, parameters are bound in the order they appear in the query, regardless of the order in the function signature.
79+
6180
```go
6281
package db
6382

0 commit comments

Comments
 (0)