Skip to content
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

feat(mysql): handle unsigned integers #1746

Merged
merged 15 commits into from
Jun 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/ondeck/mysql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
t.Fatal(err)
}

if diff := cmp.Diff(venue.ID, venueID); diff != "" {
if diff := cmp.Diff(venue.ID, uint64(venueID)); diff != "" {
t.Errorf("venue ID mismatch:\n%s", diff)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ondeck/mysql/models.go

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

11 changes: 7 additions & 4 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
CodeType: "", // FIXME
DbType: o.DBType,
Nullable: o.Nullable,
Unsigned: o.Unsigned,
Column: o.Column,
ColumnName: column,
Table: &table,
Expand Down Expand Up @@ -166,10 +167,11 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
Schema: c.Type.Schema,
Name: c.Type.Name,
},
Comment: c.Comment,
NotNull: c.IsNotNull,
IsArray: c.IsArray,
Length: int32(l),
Comment: c.Comment,
NotNull: c.IsNotNull,
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
Length: int32(l),
Table: &plugin.Identifier{
Catalog: t.Rel.Catalog,
Schema: t.Rel.Schema,
Expand Down Expand Up @@ -246,6 +248,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
OriginalName: c.OriginalName,
Comment: c.Comment,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
Length: int32(l),
IsNamedParam: c.IsNamedParam,
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if oride.GoType.TypeName == "" {
continue
}
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull {
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
return oride.GoType.TypeName
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/codegen/golang/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
unsigned := col.Unsigned

switch columnType {

Expand All @@ -28,19 +29,28 @@ func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
return "sql.NullBool"
} else {
if notNull {
if unsigned {
return "uint32"
}
return "int32"
}
return "sql.NullInt32"
}

case "int", "integer", "smallint", "mediumint", "year":
if notNull {
if unsigned {
return "uint32"
}
return "int32"
}
return "sql.NullInt32"

case "bigint":
if notNull {
if unsigned {
return "uint64"
}
return "int64"
}
return "sql.NullInt64"
Expand Down
17 changes: 10 additions & 7 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ func (c *Compiler) OutputColumns(stmt ast.Node) ([]*catalog.Column, error) {
catCols := make([]*catalog.Column, 0, len(cols))
for _, col := range cols {
catCols = append(catCols, &catalog.Column{
Name: col.Name,
Type: ast.TypeName{Name: col.DataType},
IsNotNull: col.NotNull,
IsArray: col.IsArray,
Comment: col.Comment,
Length: col.Length,
Name: col.Name,
Type: ast.TypeName{Name: col.DataType},
IsNotNull: col.NotNull,
IsUnsigned: col.Unsigned,
IsArray: col.IsArray,
Comment: col.Comment,
Length: col.Length,
})
}
return catCols, nil
Expand Down Expand Up @@ -256,6 +257,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
TableAlias: t.Rel.Name,
DataType: c.DataType,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
Length: c.Length,
})
Expand Down Expand Up @@ -548,15 +550,16 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)

cols = append(cols, &Column{
Name: cname,
OriginalName: c.Name,
Type: c.Type,
Table: c.Table,
TableAlias: alias,
DataType: c.DataType,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
Length: c.Length,
EmbedTable: c.EmbedTable,
OriginalName: c.Name,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Column struct {
OriginalName string
DataType string
NotNull bool
Unsigned bool
IsArray bool
Comment string
Length *int
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func ConvertColumn(rel *ast.TableName, c *catalog.Column) *Column {
Name: c.Name,
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
Type: &c.Type,
Length: c.Length,
Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
OriginalName: c.Name,
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
Length: c.Length,
Table: table,
Expand Down Expand Up @@ -274,6 +275,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
Table: table,
IsNamedParam: isNamed,
Expand Down Expand Up @@ -553,6 +555,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
Table: table,
IsNamedParam: isNamed,
Expand Down
6 changes: 5 additions & 1 deletion internal/config/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ type Override struct {
// for global overrides only when two different engines are in use
Engine Engine `json:"engine,omitempty" yaml:"engine"`

// True if the GoType should override if the maching postgres type is nullable
// True if the GoType should override if the matching type is nullable
Nullable bool `json:"nullable" yaml:"nullable"`

// True if the GoType should override if the matching type is unsiged.
Unsigned bool `json:"unsigned" yaml:"unsigned"`

// Deprecated. Use the `nullable` property instead
Deprecated_Null bool `json:"null" yaml:"null"`

Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/alias/mysql/go/models.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/alias/mysql/go/query.sql.go

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