Skip to content

Commit 53c5057

Browse files
authored
mysql: Compile tinyint(1) to bool (#873)
* dolphin: Generate bools for tinyint(1) * Add tests for booleans
2 parents bda1a00 + f2bc6c0 commit 53c5057

File tree

18 files changed

+342
-15
lines changed

18 files changed

+342
-15
lines changed

internal/codegen/golang/mysql_type.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ func mysqlType(r *compiler.Result, col *compiler.Column, settings config.Combine
2121
}
2222
return "sql.NullString"
2323

24-
case "int", "integer", "tinyint", "smallint", "mediumint", "year":
24+
case "tinyint":
25+
if col.Length != nil && *col.Length == 1 {
26+
if notNull {
27+
return "bool"
28+
}
29+
return "sql.NullBool"
30+
} else {
31+
if notNull {
32+
return "int32"
33+
}
34+
return "sql.NullInt32"
35+
}
36+
37+
case "int", "integer", "smallint", "mediumint", "year":
2538
if notNull {
2639
return "int32"
2740
}

internal/compiler/query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package compiler
22

3-
import "github.com/kyleconroy/sqlc/internal/sql/ast"
3+
import (
4+
"github.com/kyleconroy/sqlc/internal/sql/ast"
5+
)
46

57
type Table struct {
68
Rel *ast.TableName
@@ -13,6 +15,7 @@ type Column struct {
1315
NotNull bool
1416
IsArray bool
1517
Comment string
18+
Length *int
1619

1720
// XXX: Figure out what PostgreSQL calls `foo.id`
1821
Scope string

internal/compiler/query_catalog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func ConvertColumn(rel *ast.TableName, c *catalog.Column) *Column {
5252
NotNull: c.IsNotNull,
5353
IsArray: c.IsArray,
5454
Type: &c.Type,
55+
Length: c.Length,
5556
}
5657
}
5758

internal/endtoend/testdata/data_type_boolean/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/data_type_boolean/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/data_type_boolean/mysql/db/query.sql.go

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE TABLE foo
2+
(
3+
col_a BOOL NOT NULL,
4+
col_b BOOLEAN NOT NULL,
5+
col_c TINYINT(1) NOT NULL
6+
);
7+
8+
-- name: ListFoo :many
9+
SELECT * FROM foo;
10+
11+
CREATE TABLE bar
12+
(
13+
col_a BOOL,
14+
col_b BOOLEAN,
15+
col_c TINYINT(1)
16+
);
17+
18+
-- name: ListBar :many
19+
SELECT * FROM bar;
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+
"path": "db",
6+
"engine": "mysql",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}
12+

internal/endtoend/testdata/data_type_boolean/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/data_type_boolean/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.

0 commit comments

Comments
 (0)