Skip to content

Commit f434868

Browse files
committed
dolphin: Generate bools for tinyint(1)
1 parent bda1a00 commit f434868

File tree

7 files changed

+50
-14
lines changed

7 files changed

+50
-14
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/engine/dolphin/convert.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
3535
case pcast.AlterTableAddColumns:
3636
for _, def := range spec.NewColumns {
3737
name := def.Name.String()
38+
columnDef := ast.ColumnDef{
39+
Colname: def.Name.String(),
40+
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
41+
IsNotNull: isNotNull(def),
42+
}
43+
if def.Tp.Flen >= 0 {
44+
length := def.Tp.Flen
45+
columnDef.Length = &length
46+
}
3847
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
3948
Name: &name,
4049
Subtype: ast.AT_AddColumn,
41-
Def: &ast.ColumnDef{
42-
Colname: def.Name.String(),
43-
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
44-
IsNotNull: isNotNull(def),
45-
},
50+
Def: &columnDef,
4651
})
4752
}
4853

@@ -60,18 +65,23 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
6065
case pcast.AlterTableModifyColumn:
6166
for _, def := range spec.NewColumns {
6267
name := def.Name.String()
68+
columnDef := ast.ColumnDef{
69+
Colname: def.Name.String(),
70+
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
71+
IsNotNull: isNotNull(def),
72+
}
73+
if def.Tp.Flen >= 0 {
74+
length := def.Tp.Flen
75+
columnDef.Length = &length
76+
}
6377
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
6478
Name: &name,
6579
Subtype: ast.AT_DropColumn,
6680
})
6781
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
6882
Name: &name,
6983
Subtype: ast.AT_AddColumn,
70-
Def: &ast.ColumnDef{
71-
Colname: def.Name.String(),
72-
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
73-
IsNotNull: isNotNull(def),
74-
},
84+
Def: &columnDef,
7585
})
7686
}
7787

@@ -224,13 +234,18 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
224234
}
225235
}
226236
}
227-
create.Cols = append(create.Cols, &ast.ColumnDef{
237+
columnDef := ast.ColumnDef{
228238
Colname: def.Name.String(),
229239
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
230240
IsNotNull: isNotNull(def),
231241
Comment: comment,
232242
Vals: vals,
233-
})
243+
}
244+
if def.Tp.Flen >= 0 {
245+
length := def.Tp.Flen
246+
columnDef.Length = &length
247+
}
248+
create.Cols = append(create.Cols, &columnDef)
234249
}
235250
for _, opt := range n.Options {
236251
switch opt.Tp {

internal/sql/ast/column_def.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ColumnDef struct {
66
IsNotNull bool
77
IsArray bool
88
Vals *List
9+
Length *int
910

1011
// From pg.ColumnDef
1112
Inhcount int

internal/sql/catalog/catalog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ type Column struct {
193193
IsNotNull bool
194194
IsArray bool
195195
Comment string
196+
Length *int
196197
}
197198

198199
type Type interface {

internal/sql/catalog/table.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
7474
Type: *cmd.Def.TypeName,
7575
IsNotNull: cmd.Def.IsNotNull,
7676
IsArray: cmd.Def.IsArray,
77+
Length: cmd.Def.Length,
7778
})
7879

7980
case ast.AT_AlterColumnType:
@@ -160,6 +161,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
160161
IsNotNull: col.IsNotNull,
161162
IsArray: col.IsArray,
162163
Comment: col.Comment,
164+
Length: col.Length,
163165
}
164166
if col.Vals != nil {
165167
typeName := ast.TypeName{

0 commit comments

Comments
 (0)