Skip to content

Commit 613de79

Browse files
authored
dolphin: Add ENUM() support (#676)
* dolphin: Add ENUM() support * codegen: Only print warnings in debug mode * dolphin: Unify enum names
1 parent 74ea3b9 commit 613de79

File tree

19 files changed

+172
-48
lines changed

19 files changed

+172
-48
lines changed

examples/booktest/mysql/models.go

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

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.

internal/codegen/golang/mysql_type.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
"github.com/kyleconroy/sqlc/internal/compiler"
77
"github.com/kyleconroy/sqlc/internal/config"
8+
"github.com/kyleconroy/sqlc/internal/debug"
9+
"github.com/kyleconroy/sqlc/internal/sql/catalog"
810
)
911

1012
func mysqlType(r *compiler.Result, col *compiler.Column, settings config.CombinedSettings) string {
@@ -69,7 +71,22 @@ func mysqlType(r *compiler.Result, col *compiler.Column, settings config.Combine
6971
return "interface{}"
7072

7173
default:
72-
log.Printf("unknown MySQL type: %s\n", columnType)
74+
for _, schema := range r.Catalog.Schemas {
75+
for _, typ := range schema.Types {
76+
switch t := typ.(type) {
77+
case *catalog.Enum:
78+
if t.Name == columnType {
79+
if schema.Name == r.Catalog.DefaultSchema {
80+
return StructName(t.Name, settings)
81+
}
82+
return StructName(schema.Name+"_"+t.Name, settings)
83+
}
84+
}
85+
}
86+
}
87+
if debug.Active {
88+
log.Printf("Unknown MySQL type: %s\n", columnType)
89+
}
7390
return "interface{}"
7491

7592
}

internal/codegen/golang/postgresql_type.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/kyleconroy/sqlc/internal/compiler"
77
"github.com/kyleconroy/sqlc/internal/config"
8+
"github.com/kyleconroy/sqlc/internal/debug"
89
"github.com/kyleconroy/sqlc/internal/sql/catalog"
910
)
1011

@@ -166,8 +167,9 @@ func postgresType(r *compiler.Result, col *compiler.Column, settings config.Comb
166167
}
167168
}
168169
}
169-
170-
log.Printf("unknown PostgreSQL type: %s\n", columnType)
170+
if debug.Active {
171+
log.Printf("unknown PostgreSQL type: %s\n", columnType)
172+
}
171173
return "interface{}"
172174
}
173175
}

internal/endtoend/testdata/enums/go/db.go renamed to internal/endtoend/testdata/ddl_create_enum/mysql/go/db.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.

internal/endtoend/testdata/enums/go/models.go renamed to internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go

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

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

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* name: ListFoo :many */
2+
SELECT * FROM foo;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE foo (
2+
foobar ENUM ('foo-a', 'foo_b', 'foo:c', 'foo/d', 'foo@e', 'foo+f', 'foo!g') NOT NULL
3+
);
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": "go",
6+
"engine": "mysql:beta",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)