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+
}

internal/endtoend/testdata/ddl_create_enum/postgresql/go/models.go

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

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

Lines changed: 23 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
-- name: Placeholder :exec
2-
SELECT 1;
1+
-- name: ListFoo :many
2+
SELECT * FROM foo;
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
CREATE TYPE status AS ENUM ('open', 'closed');
1+
CREATE TYPE foobar AS ENUM (
2+
-- Valid separators
3+
'foo-a',
4+
'foo_b',
5+
'foo:c',
6+
'foo/d',
7+
-- Strip unknown characters
8+
'foo@e',
9+
'foo+f',
10+
'foo!g'
11+
);
12+
13+
CREATE TABLE foo (val foobar NOT NULL);

internal/endtoend/testdata/enums/sql/enum.sql

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

internal/endtoend/testdata/enums/sql/query.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/endtoend/testdata/enums/sqlc.json

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

internal/engine/dolphin/convert.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dolphin
22

33
import (
44
"fmt"
5+
"log"
56

67
pcast "github.com/pingcap/parser/ast"
78
"github.com/pingcap/parser/opcode"
@@ -172,6 +173,7 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
172173
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
173174
IsNotNull: isNotNull(def),
174175
Comment: comment,
176+
Vals: vals,
175177
})
176178
}
177179
for _, opt := range n.Options {
@@ -492,7 +494,7 @@ func (c *cc) convert(node pcast.Node) ast.Node {
492494

493495
default:
494496
if debug.Active {
495-
fmt.Printf("dolphin.convert: Unknown node type %T\n", n)
497+
log.Printf("dolphin.convert: Unknown node type %T\n", n)
496498
}
497499
return &ast.TODO{}
498500
}

internal/sql/catalog/table.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,24 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
153153
}
154154
} else {
155155
for _, col := range stmt.Cols {
156-
tbl.Columns = append(tbl.Columns, &Column{
156+
tc := &Column{
157157
Name: col.Colname,
158158
Type: *col.TypeName,
159159
IsNotNull: col.IsNotNull,
160160
IsArray: col.IsArray,
161161
Comment: col.Comment,
162-
})
162+
}
163+
if col.Vals != nil {
164+
typeName := ast.TypeName{
165+
Name: col.Colname,
166+
}
167+
s := &ast.CreateEnumStmt{TypeName: &typeName, Vals: col.Vals}
168+
if err := c.createEnum(s); err != nil {
169+
return err
170+
}
171+
tc.Type = typeName
172+
}
173+
tbl.Columns = append(tbl.Columns, tc)
163174
}
164175
}
165176
schema.Tables = append(schema.Tables, &tbl)

0 commit comments

Comments
 (0)