Skip to content

Commit 466c3e1

Browse files
authored
fix(compiler): Support references to columns in joined tables in UPDATE statements (#1289)
* fix(compiler): Support references to columns in joined tables in UPDATE statements
1 parent 5eb649d commit 466c3e1

File tree

17 files changed

+297
-15
lines changed

17 files changed

+297
-15
lines changed

internal/compiler/find_params.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
117117
if !ok {
118118
continue
119119
}
120-
*p.refs = append(*p.refs, paramRef{parent: target, ref: ref, rv: n.Relation})
120+
for _, relation := range n.Relations.Items {
121+
rv, ok := relation.(*ast.RangeVar)
122+
if !ok {
123+
continue
124+
}
125+
*p.refs = append(*p.refs, paramRef{parent: target, ref: ref, rv: rv})
126+
}
121127
p.seen[ref.Location] = struct{}{}
122128
}
123129

internal/compiler/output_columns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
349349
})
350350
case *ast.UpdateStmt:
351351
list = &ast.List{
352-
Items: append(n.FromClause.Items, n.Relation),
352+
Items: append(n.FromClause.Items, n.Relations.Items...),
353353
}
354354
default:
355355
return nil, fmt.Errorf("sourceTables: unsupported node type: %T", n)

internal/endtoend/testdata/update_join/mysql/db/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/update_join/mysql/db/models.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/update_join/mysql/db/query.sql.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
CREATE TABLE primary_table (
2+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
3+
user_id bigint(20) unsigned NOT NULL,
4+
PRIMARY KEY (id)
5+
);
6+
7+
CREATE TABLE join_table (
8+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
9+
primary_table_id bigint(20) unsigned NOT NULL,
10+
other_table_id bigint(20) unsigned NOT NULL,
11+
is_active tinyint(1) NOT NULL DEFAULT '0',
12+
PRIMARY KEY (id)
13+
);
14+
15+
-- name: UpdateJoin :exec
16+
UPDATE join_table as jt
17+
JOIN primary_table as pt
18+
ON jt.primary_table_id = pt.id
19+
SET jt.is_active = ?
20+
WHERE jt.id = ?
21+
AND pt.user_id = ?;
22+
23+
-- name: UpdateLeftJoin :exec
24+
UPDATE join_table as jt
25+
LEFT JOIN primary_table as pt
26+
ON jt.primary_table_id = pt.id
27+
SET jt.is_active = ?
28+
WHERE jt.id = ?
29+
AND pt.user_id = ?;
30+
31+
-- name: UpdateRightJoin :exec
32+
UPDATE join_table as jt
33+
RIGHT JOIN primary_table as pt
34+
ON jt.primary_table_id = pt.id
35+
SET jt.is_active = ?
36+
WHERE jt.id = ?
37+
AND pt.user_id = ?;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "db",
6+
"engine": "mysql",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

internal/endtoend/testdata/update_join/postgresql/db/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/update_join/postgresql/db/models.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/update_join/postgresql/db/query.sql.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)