Skip to content
Open
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
6 changes: 6 additions & 0 deletions internal/compiler/find_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
if n.LimitCount != nil {
p.limitCount = n.LimitCount
}
if n.LimitOffset != nil {
p.limitOffset = n.LimitOffset
}

case *ast.FuncCall:
p.parent = node
Expand Down Expand Up @@ -132,6 +135,9 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
if n.LimitCount != nil {
p.limitCount = n.LimitCount
}
if n.LimitOffset != nil {
p.limitOffset = n.LimitOffset
}

case *ast.RangeVar:
p.rangeVar = n
Expand Down
24 changes: 24 additions & 0 deletions internal/core/analyzer/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (a *analyzer) analyzeUpdate(s *ast.UpdateStmt) error {
return fmt.Errorf("where: %w", err)
}
}
if err := a.typeOrderLimit(s.SortClause, s.LimitCount, s.LimitOffset); err != nil {
return err
}
return a.projectReturning(s.ReturningList)
}

Expand All @@ -75,6 +78,9 @@ func (a *analyzer) analyzeDelete(s *ast.DeleteStmt) error {
return fmt.Errorf("where: %w", err)
}
}
if err := a.typeOrderLimit(s.SortClause, s.LimitCount, s.LimitOffset); err != nil {
return err
}
return a.projectReturning(s.ReturningList)
}

Expand Down Expand Up @@ -209,3 +215,21 @@ func columnType(rel scopeRel, col core.ClassColumn) exprType {
sourceTableAlias: rel.alias,
}
}

// typeOrderLimit types the ORDER BY and LIMIT ... OFFSET that SQLite accepts
// on an UPDATE or DELETE (MySQL accepts ORDER BY and LIMIT, without OFFSET).
func (a *analyzer) typeOrderLimit(sort *ast.List, count, offset ast.Node) error {
for _, item := range listItems(sort) {
if sb, ok := item.(*ast.SortBy); ok {
if _, err := a.typeExpr(sb.Node); err != nil {
return fmt.Errorf("order by: %w", err)
}
}
}
for _, n := range []ast.Node{count, offset} {
if err := a.typeLimit(n); err != nil {
return fmt.Errorf("limit: %w", err)
}
}
return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- name: DeleteLimitOffset :exec
DELETE FROM t WHERE name = ? ORDER BY score DESC LIMIT ? OFFSET ?;

-- name: DeleteOrderByParam :exec
DELETE FROM t WHERE name = ? ORDER BY abs(score - ?) LIMIT ?;

-- name: UpdateLimitOffset :exec
UPDATE t SET score = ? WHERE name = ? ORDER BY id LIMIT ? OFFSET ?;

-- name: UpdateOrderByParam :exec
UPDATE t SET score = ? WHERE name = ? ORDER BY abs(score - ?) LIMIT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE t (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
score INTEGER NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "sqlite",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# package querytest
query.sql:1:1: sqlite3: SQL logic error: near "ORDER": syntax error
query.sql:5:1: sqlite3: SQL logic error: near "ORDER": syntax error
query.sql:8:1: sqlite3: SQL logic error: near "ORDER": syntax error
query.sql:11:1: sqlite3: SQL logic error: near "ORDER": syntax error
27 changes: 17 additions & 10 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,22 +432,28 @@ func (c *cc) convertDropStmt(name *meyer.QualifiedName, ifExists bool) ast.Node
}

func (c *cc) convertDeleteStmt(n *meyer.DeleteStmt) ast.Node {
return &ast.DeleteStmt{
stmt := &ast.DeleteStmt{
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
WhereClause: c.convertExpr(n.Where),
LimitCount: c.limitCount(n.Limit),
ReturningList: c.convertReturning(n.Returning),
WithClause: c.convertWith(n.With),
}
stmt.SortClause, stmt.LimitCount, stmt.LimitOffset = c.convertOrderLimit(n.OrderBy, n.Limit)
return stmt
}

// limitCount converts the LIMIT of an UPDATE or DELETE. sqlc has no place to
// put the OFFSET or the ORDER BY that can accompany it, so they are dropped.
func (c *cc) limitCount(n *meyer.Limit) ast.Node {
if n == nil {
return nil
// convertOrderLimit converts the ORDER BY and LIMIT ... OFFSET of an UPDATE
// or DELETE, which SQLite accepts when built with
// SQLITE_ENABLE_UPDATE_DELETE_LIMIT.
func (c *cc) convertOrderLimit(orderBy []*meyer.OrderingTerm, limit *meyer.Limit) (sort *ast.List, count, offset ast.Node) {
if len(orderBy) > 0 {
sort = &ast.List{Items: c.convertOrderBy(orderBy)}
}
return c.convertExpr(n.Count)
if limit != nil {
count = c.convertExpr(limit.Count)
offset = c.convertExpr(limit.Offset)
}
return sort, count, offset
}

func (c *cc) convertInsertStmt(n *meyer.InsertStmt) ast.Node {
Expand Down Expand Up @@ -521,15 +527,16 @@ func (c *cc) convertSetPairs(pairs []*meyer.SetPair) *ast.List {
}

func (c *cc) convertUpdateStmt(n *meyer.UpdateStmt) ast.Node {
return &ast.UpdateStmt{
stmt := &ast.UpdateStmt{
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
TargetList: c.convertSetPairs(n.Set),
FromClause: &ast.List{Items: c.convertFrom(n.From)},
WhereClause: c.convertExpr(n.Where),
LimitCount: c.limitCount(n.Limit),
ReturningList: c.convertReturning(n.Returning),
WithClause: c.convertWith(n.With),
}
stmt.SortClause, stmt.LimitCount, stmt.LimitOffset = c.convertOrderLimit(n.OrderBy, n.Limit)
return stmt
}

func (c *cc) convertReturning(cols []*meyer.ResultColumn) *ast.List {
Expand Down
16 changes: 16 additions & 0 deletions internal/sql/ast/delete_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ type DeleteStmt struct {
Relations *List `json:"relations,omitempty"`
UsingClause *List `json:"using_clause,omitempty"`
WhereClause Node `json:"where_clause,omitempty"`
SortClause *List `json:"sort_clause,omitempty"`
LimitCount Node `json:"limit_count,omitempty"`
LimitOffset Node `json:"limit_offset,omitempty"`
ReturningList *List `json:"returning_list,omitempty"`
WithClause *WithClause `json:"with_clause,omitempty"`
// MySQL multi-table DELETE support
Expand Down Expand Up @@ -74,13 +76,27 @@ func (n *DeleteStmt) Format(buf *TrackedBuffer, d format.Dialect) {
buf.condition(n.WhereClause, d)
}

if items(n.SortClause) {
buf.beforeClause(n.SortClause, d)
buf.Line()
buf.WriteString("ORDER BY ")
buf.astFormat(n.SortClause, d)
}

if set(n.LimitCount) {
buf.beforeClause(n.LimitCount, d)
buf.Line()
buf.WriteString("LIMIT ")
buf.astFormat(n.LimitCount, d)
}

if set(n.LimitOffset) {
buf.beforeClause(n.LimitOffset, d)
buf.Line()
buf.WriteString("OFFSET ")
buf.astFormat(n.LimitOffset, d)
}

if items(n.ReturningList) {
buf.beforeClause(n.ReturningList, d)
buf.Line()
Expand Down
16 changes: 16 additions & 0 deletions internal/sql/ast/update_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type UpdateStmt struct {
TargetList *List `json:"target_list,omitempty"`
WhereClause Node `json:"where_clause,omitempty"`
FromClause *List `json:"from_clause,omitempty"`
SortClause *List `json:"sort_clause,omitempty"`
LimitCount Node `json:"limit_count,omitempty"`
LimitOffset Node `json:"limit_offset,omitempty"`
ReturningList *List `json:"returning_list,omitempty"`
WithClause *WithClause `json:"with_clause,omitempty"`
// PostgreSQL 18 RETURNING WITH (OLD AS ..., NEW AS ...) aliases
Expand Down Expand Up @@ -147,13 +149,27 @@ func (n *UpdateStmt) Format(buf *TrackedBuffer, d format.Dialect) {
buf.condition(n.WhereClause, d)
}

if items(n.SortClause) {
buf.beforeClause(n.SortClause, d)
buf.Line()
buf.WriteString("ORDER BY ")
buf.astFormat(n.SortClause, d)
}

if set(n.LimitCount) {
buf.beforeClause(n.LimitCount, d)
buf.Line()
buf.WriteString("LIMIT ")
buf.astFormat(n.LimitCount, d)
}

if set(n.LimitOffset) {
buf.beforeClause(n.LimitOffset, d)
buf.Line()
buf.WriteString("OFFSET ")
buf.astFormat(n.LimitOffset, d)
}

if items(n.ReturningList) {
buf.beforeClause(n.ReturningList, d)
buf.Line()
Expand Down
6 changes: 6 additions & 0 deletions internal/sql/astutils/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
a.apply(n, "Relations", nil, n.Relations)
a.apply(n, "UsingClause", nil, n.UsingClause)
a.apply(n, "WhereClause", nil, n.WhereClause)
a.apply(n, "SortClause", nil, n.SortClause)
a.apply(n, "LimitCount", nil, n.LimitCount)
a.apply(n, "LimitOffset", nil, n.LimitOffset)
a.apply(n, "ReturningList", nil, n.ReturningList)
a.apply(n, "WithClause", nil, n.WithClause)
a.apply(n, "Targets", nil, n.Targets)
Expand Down Expand Up @@ -1174,6 +1177,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
a.apply(n, "TargetList", nil, n.TargetList)
a.apply(n, "WhereClause", nil, n.WhereClause)
a.apply(n, "FromClause", nil, n.FromClause)
a.apply(n, "SortClause", nil, n.SortClause)
a.apply(n, "LimitCount", nil, n.LimitCount)
a.apply(n, "LimitOffset", nil, n.LimitOffset)
a.apply(n, "ReturningList", nil, n.ReturningList)
a.apply(n, "WithClause", nil, n.WithClause)

Expand Down
Loading
Loading