Skip to content

Commit 527dc9f

Browse files
committed
sqlite: update to meyer v0.1.1, restoring UPDATE/DELETE LIMIT
v0.1.1 adds parser.Options, whose UpdateDeleteLimit field compiles in the grammar SQLITE_ENABLE_UPDATE_DELETE_LIMIT does. The option selects between two forms of SQLite's own rules, so meyer cannot tell which one a caller wants from the SQL alone; sqlc has always accepted ORDER BY and LIMIT on UPDATE and DELETE, so it asks for them. UpdateStmt and DeleteStmt grow OrderBy and Limit fields to match. sqlc's AST has somewhere to put the LIMIT and nowhere to put the ORDER BY, which is what the ANTLR converter did with them too. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RkrYmzwTktB2CA5Bvqg6z5
1 parent e95491e commit 527dc9f

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/spf13/pflag v1.0.10
2222
github.com/sqlc-dev/doubleclick v1.0.0
2323
github.com/sqlc-dev/marino v0.1.0
24-
github.com/sqlc-dev/meyer v0.1.0
24+
github.com/sqlc-dev/meyer v0.1.1
2525
github.com/sqlc-dev/zetajones v0.1.0
2626
github.com/tetratelabs/wazero v1.12.0
2727
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ github.com/sqlc-dev/doubleclick v1.0.0 h1:2/OApfQ2eLgcfa/Fqs8WSMA6atH0G8j9hHbQIg
8383
github.com/sqlc-dev/doubleclick v1.0.0/go.mod h1:ODHRroSrk/rr5neRHlWMSRijqOak8YmNaO3VAZCNl5Y=
8484
github.com/sqlc-dev/marino v0.1.0 h1:8Fn13vFhx7OUcmDFfRZdf3zARAbNl04Lcy74211ZpIw=
8585
github.com/sqlc-dev/marino v0.1.0/go.mod h1:mQxC2dgDE0DWHMb2B5jZNk7KToJuS6wnxnffBfYnq08=
86-
github.com/sqlc-dev/meyer v0.1.0 h1:u1GU0veXPWdtVdmZsMmA1cvWE6xXc9lJDfRznVBuCg4=
87-
github.com/sqlc-dev/meyer v0.1.0/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8=
86+
github.com/sqlc-dev/meyer v0.1.1 h1:BAeZcfgLyTnk9f90DyGEKXPrHxtgvVD/DTM6awq2kUY=
87+
github.com/sqlc-dev/meyer v0.1.1/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8=
8888
github.com/sqlc-dev/zetajones v0.1.0 h1:VeG0atx6lNABr9V2bSI5vL9DvOKTHX0XjMqWUE/rv40=
8989
github.com/sqlc-dev/zetajones v0.1.0/go.mod h1:dU1DxwqC6Cahbpnw16KpH1J2waWRDMdwyDSvovMZR4I=
9090
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

internal/engine/sqlite/convert.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,22 @@ func (c *cc) convertDropStmt(name *meyer.QualifiedName, ifExists bool) ast.Node
375375
}
376376

377377
func (c *cc) convertDeleteStmt(n *meyer.DeleteStmt) ast.Node {
378-
stmt := &ast.DeleteStmt{
378+
return &ast.DeleteStmt{
379379
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
380380
WhereClause: c.convertExpr(n.Where),
381+
LimitCount: c.limitCount(n.Limit),
381382
ReturningList: c.convertReturning(n.Returning),
382383
WithClause: c.convertWith(n.With),
383384
}
384-
return stmt
385+
}
386+
387+
// limitCount converts the LIMIT of an UPDATE or DELETE. sqlc has no place to
388+
// put the OFFSET or the ORDER BY that can accompany it, so they are dropped.
389+
func (c *cc) limitCount(n *meyer.Limit) ast.Node {
390+
if n == nil {
391+
return nil
392+
}
393+
return c.convertExpr(n.Count)
385394
}
386395

387396
func (c *cc) convertInsertStmt(n *meyer.InsertStmt) ast.Node {
@@ -459,6 +468,7 @@ func (c *cc) convertUpdateStmt(n *meyer.UpdateStmt) ast.Node {
459468
TargetList: c.convertSetPairs(n.Set),
460469
FromClause: &ast.List{Items: c.convertFrom(n.From)},
461470
WhereClause: c.convertExpr(n.Where),
471+
LimitCount: c.limitCount(n.Limit),
462472
ReturningList: c.convertReturning(n.Returning),
463473
WithClause: c.convertWith(n.With),
464474
}

internal/engine/sqlite/parse.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ func NewParser() *Parser {
1919
type Parser struct {
2020
}
2121

22+
// parseOptions accepts ORDER BY and LIMIT on UPDATE and DELETE. SQLite has
23+
// them only when built with SQLITE_ENABLE_UPDATE_DELETE_LIMIT, which meyer
24+
// cannot tell from the SQL, so the caller has to ask. sqlc has always
25+
// accepted them.
26+
var parseOptions = parser.Options{UpdateDeleteLimit: true}
27+
2228
func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
2329
blob, err := io.ReadAll(r)
2430
if err != nil {
2531
return nil, err
2632
}
2733
src := string(blob)
28-
parsed, err := parser.ParseString(src)
34+
parsed, err := parseOptions.ParseString(src)
2935
if err != nil {
3036
return nil, normalizeErr(err)
3137
}

0 commit comments

Comments
 (0)