Skip to content

fix(engine/mysql): Support nullable fields in joins for MySQL engine #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 1, 2021
Merged
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
3 changes: 2 additions & 1 deletion examples/booktest/mysql/db_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build examples
// +build examples

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

// TODO: call say_hello(varchar)
Expand Down
2 changes: 1 addition & 1 deletion examples/booktest/mysql/query.sql.go

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
Expand Up @@ -22,7 +22,7 @@ WHERE tags = ?
data class BooksByTagsRow (
val bookId: Int,
val title: String,
val name: String,
val name: String?,
val isbn: String,
val tags: String
)
Expand Down
7 changes: 7 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,14 @@ func isTableRequired(n ast.Node, tableName string, prior int) int {
case ast.JoinTypeFull:
return helper(tableOptional, tableOptional)
}
case *ast.List:
for _, item := range n.Items {
if res := isTableRequired(item, tableName, prior); res != tableNotFound {
return res
}
}
}

return tableNotFound
}

Expand Down
29 changes: 0 additions & 29 deletions internal/endtoend/testdata/join_full/mysql/go/db.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/endtoend/testdata/join_full/mysql/go/models.go

This file was deleted.

45 changes: 0 additions & 45 deletions internal/endtoend/testdata/join_full/mysql/go/query.sql.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/endtoend/testdata/join_full/mysql/query.sql

This file was deleted.

12 changes: 0 additions & 12 deletions internal/endtoend/testdata/join_full/mysql/sqlc.json

This file was deleted.

4 changes: 3 additions & 1 deletion internal/endtoend/testdata/join_left/mysql/go/query.sql.go

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

5 changes: 3 additions & 2 deletions internal/endtoend/testdata/join_left/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- https://github.com/kyleconroy/sqlc/issues/604
-- https://github.com/kyleconroy/sqlc/issues/604
CREATE TABLE users (
user_id INT PRIMARY KEY,
city_id INT -- nullable
Expand All @@ -23,7 +23,8 @@ INNER JOIN mayors USING (mayor_id);
-- name: GetMayorsOptional :many
SELECT
user_id,
cities.city_id,
mayors.full_name
FROM users
LEFT JOIN cities USING (city_id)
LEFT JOIN mayors USING (mayor_id);
LEFT JOIN mayors USING (mayor_id);
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/join_left/mysql/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages": [
{
"path": "go",
"engine": "postgresql",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
Expand Down
4 changes: 2 additions & 2 deletions internal/endtoend/testdata/join_right/mysql/go/models.go

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

8 changes: 4 additions & 4 deletions internal/endtoend/testdata/join_right/mysql/go/query.sql.go

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

2 changes: 1 addition & 1 deletion internal/endtoend/testdata/join_right/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ CREATE TABLE bar (id serial not null);
SELECT f.id, f.bar_id, b.id
FROM foo f
RIGHT JOIN bar b ON b.id = f.bar_id
WHERE f.id = $1;
WHERE f.id = ?;
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/join_right/mysql/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages": [
{
"path": "go",
"engine": "postgresql",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
Expand Down

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

13 changes: 10 additions & 3 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,18 @@ func (c *cc) convertJoin(n *pcast.Join) *ast.List {
return &ast.List{}
}
if n.Right != nil && n.Left != nil {
// MySQL doesn't have a FULL join type
joinType := ast.JoinType(n.Tp)
if joinType >= ast.JoinTypeFull {
joinType++
}

return &ast.List{
Items: []ast.Node{&ast.JoinExpr{
Larg: c.convert(n.Left),
Rarg: c.convert(n.Right),
Quals: c.convert(n.On),
Jointype: joinType,
Larg: c.convert(n.Left),
Rarg: c.convert(n.Right),
Quals: c.convert(n.On),
}},
}
}
Expand Down