Skip to content

Commit fd3ed9d

Browse files
authored
fix: mysql delete join panic (#2197)
* fix:mysql delete join panic * fix: test
1 parent 3e55012 commit fd3ed9d

File tree

17 files changed

+325
-32
lines changed

17 files changed

+325
-32
lines changed

internal/compiler/output_columns.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
404404
var list *ast.List
405405
switch n := node.(type) {
406406
case *ast.DeleteStmt:
407-
list = &ast.List{
408-
Items: []ast.Node{n.Relation},
409-
}
407+
list = n.Relations
410408
case *ast.InsertStmt:
411409
list = &ast.List{
412410
Items: []ast.Node{n.Relation},

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

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

internal/endtoend/testdata/delete_inner_join/mysql/go/models.go

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

internal/endtoend/testdata/delete_inner_join/mysql/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CREATE TABLE author (
2+
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL
4+
);
5+
6+
CREATE TABLE book (
7+
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
8+
title VARCHAR(255) NOT NULL
9+
);
10+
11+
CREATE TABLE author_book (
12+
author_id INT UNSIGNED NOT NULL,
13+
book_id INT UNSIGNED NOT NULL,
14+
CONSTRAINT `pk-author_book` PRIMARY KEY (author_id, book_id),
15+
CONSTRAINT `fk-author_book-author-id` FOREIGN KEY (author_id) REFERENCES author (id),
16+
CONSTRAINT `fk-author_book-book-id` FOREIGN KEY (book_id) REFERENCES book (id)
17+
);
18+
19+
/* name: RemoveAllAuthorsFromTheGreatGatsby :exec */
20+
DELETE author_book
21+
FROM
22+
author_book
23+
INNER JOIN book ON book.id = author_book.book_id
24+
WHERE
25+
book.title = 'The Great Gatsby';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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

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

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

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

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

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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: DeleteJoin :exec
16+
DELETE jt.*,
17+
pt.*
18+
FROM
19+
join_table as jt
20+
JOIN primary_table as pt ON jt.primary_table_id = pt.id
21+
WHERE
22+
jt.id = ?
23+
AND pt.user_id = ?;
24+
25+
-- name: DeleteLeftJoin :exec
26+
DELETE jt.*,
27+
pt.*
28+
FROM
29+
join_table as jt
30+
LEFT JOIN primary_table as pt ON jt.primary_table_id = pt.id
31+
WHERE
32+
jt.id = ?
33+
AND pt.user_id = ?;
34+
35+
-- name: DeleteRightJoin :exec
36+
DELETE jt.*,
37+
pt.*
38+
FROM
39+
join_table as jt
40+
RIGHT JOIN primary_table as pt ON jt.primary_table_id = pt.id
41+
WHERE
42+
jt.id = ?
43+
AND pt.user_id = ?;

0 commit comments

Comments
 (0)