Skip to content

Commit 0d3508d

Browse files
authored
fix(engine/mysql): Support nullable fields in joins for MySQL engine (#1249)
* Fix nullable fields in joins for MySQL engine
1 parent 63a755d commit 0d3508d

File tree

18 files changed

+38
-130
lines changed

18 files changed

+38
-130
lines changed

examples/booktest/mysql/db_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build examples
12
// +build examples
23

34
package booktest
@@ -150,7 +151,7 @@ func TestBooks(t *testing.T) {
150151
t.Fatal(err)
151152
}
152153
for _, ab := range res {
153-
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
154+
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name.String, ab.Isbn, ab.Tags)
154155
}
155156

156157
// TODO: call say_hello(varchar)

examples/booktest/mysql/query.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/kotlin/src/main/kotlin/com/example/booktest/mysql/QueriesImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WHERE tags = ?
2222
data class BooksByTagsRow (
2323
val bookId: Int,
2424
val title: String,
25-
val name: String,
25+
val name: String?,
2626
val isbn: String,
2727
val tags: String
2828
)

internal/compiler/output_columns.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,14 @@ func isTableRequired(n ast.Node, tableName string, prior int) int {
302302
case ast.JoinTypeFull:
303303
return helper(tableOptional, tableOptional)
304304
}
305+
case *ast.List:
306+
for _, item := range n.Items {
307+
if res := isTableRequired(item, tableName, prior); res != tableNotFound {
308+
return res
309+
}
310+
}
305311
}
312+
306313
return tableNotFound
307314
}
308315

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

internal/endtoend/testdata/join_full/mysql/query.sql

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/endtoend/testdata/join_full/mysql/sqlc.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/join_left/mysql/query.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--- https://github.com/kyleconroy/sqlc/issues/604
1+
-- https://github.com/kyleconroy/sqlc/issues/604
22
CREATE TABLE users (
33
user_id INT PRIMARY KEY,
44
city_id INT -- nullable
@@ -23,7 +23,8 @@ INNER JOIN mayors USING (mayor_id);
2323
-- name: GetMayorsOptional :many
2424
SELECT
2525
user_id,
26+
cities.city_id,
2627
mayors.full_name
2728
FROM users
2829
LEFT JOIN cities USING (city_id)
29-
LEFT JOIN mayors USING (mayor_id);
30+
LEFT JOIN mayors USING (mayor_id);

internal/endtoend/testdata/join_left/mysql/sqlc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"packages": [
44
{
55
"path": "go",
6-
"engine": "postgresql",
6+
"engine": "mysql",
77
"name": "querytest",
88
"schema": "query.sql",
99
"queries": "query.sql"

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

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

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

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

internal/endtoend/testdata/join_right/mysql/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ CREATE TABLE bar (id serial not null);
55
SELECT f.id, f.bar_id, b.id
66
FROM foo f
77
RIGHT JOIN bar b ON b.id = f.bar_id
8-
WHERE f.id = $1;
8+
WHERE f.id = ?;

internal/endtoend/testdata/join_right/mysql/sqlc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"packages": [
44
{
55
"path": "go",
6-
"engine": "postgresql",
6+
"engine": "mysql",
77
"name": "querytest",
88
"schema": "query.sql",
99
"queries": "query.sql"

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

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

internal/engine/dolphin/convert.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,18 @@ func (c *cc) convertJoin(n *pcast.Join) *ast.List {
849849
return &ast.List{}
850850
}
851851
if n.Right != nil && n.Left != nil {
852+
// MySQL doesn't have a FULL join type
853+
joinType := ast.JoinType(n.Tp)
854+
if joinType >= ast.JoinTypeFull {
855+
joinType++
856+
}
857+
852858
return &ast.List{
853859
Items: []ast.Node{&ast.JoinExpr{
854-
Larg: c.convert(n.Left),
855-
Rarg: c.convert(n.Right),
856-
Quals: c.convert(n.On),
860+
Jointype: joinType,
861+
Larg: c.convert(n.Left),
862+
Rarg: c.convert(n.Right),
863+
Quals: c.convert(n.On),
857864
}},
858865
}
859866
}

0 commit comments

Comments
 (0)