Skip to content

Commit 9b9a2b6

Browse files
feat(mysql): Support unsigned integers (#1746)
* config: Target overrides at unsigned columns --------- Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent 308583e commit 9b9a2b6

File tree

58 files changed

+22609
-14974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+22609
-14974
lines changed

examples/ondeck/mysql/db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
6565
t.Fatal(err)
6666
}
6767

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

examples/ondeck/mysql/models.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/cmd/shim.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
3737
CodeType: "", // FIXME
3838
DbType: o.DBType,
3939
Nullable: o.Nullable,
40+
Unsigned: o.Unsigned,
4041
Column: o.Column,
4142
ColumnName: column,
4243
Table: &table,
@@ -166,10 +167,11 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
166167
Schema: c.Type.Schema,
167168
Name: c.Type.Name,
168169
},
169-
Comment: c.Comment,
170-
NotNull: c.IsNotNull,
171-
IsArray: c.IsArray,
172-
Length: int32(l),
170+
Comment: c.Comment,
171+
NotNull: c.IsNotNull,
172+
Unsigned: c.IsUnsigned,
173+
IsArray: c.IsArray,
174+
Length: int32(l),
173175
Table: &plugin.Identifier{
174176
Catalog: t.Rel.Catalog,
175177
Schema: t.Rel.Schema,
@@ -246,6 +248,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
246248
OriginalName: c.OriginalName,
247249
Comment: c.Comment,
248250
NotNull: c.NotNull,
251+
Unsigned: c.Unsigned,
249252
IsArray: c.IsArray,
250253
Length: int32(l),
251254
IsNamedParam: c.IsNamedParam,

internal/codegen/golang/go_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
6060
if oride.GoType.TypeName == "" {
6161
continue
6262
}
63-
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull {
63+
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
6464
return oride.GoType.TypeName
6565
}
6666
}

internal/codegen/golang/mysql_type.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
1212
columnType := sdk.DataType(col.Type)
1313
notNull := col.NotNull || col.IsArray
14+
unsigned := col.Unsigned
1415

1516
switch columnType {
1617

@@ -28,19 +29,28 @@ func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
2829
return "sql.NullBool"
2930
} else {
3031
if notNull {
32+
if unsigned {
33+
return "uint32"
34+
}
3135
return "int32"
3236
}
3337
return "sql.NullInt32"
3438
}
3539

3640
case "int", "integer", "smallint", "mediumint", "year":
3741
if notNull {
42+
if unsigned {
43+
return "uint32"
44+
}
3845
return "int32"
3946
}
4047
return "sql.NullInt32"
4148

4249
case "bigint":
4350
if notNull {
51+
if unsigned {
52+
return "uint64"
53+
}
4454
return "int64"
4555
}
4656
return "sql.NullInt64"

internal/compiler/output_columns.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ func (c *Compiler) OutputColumns(stmt ast.Node) ([]*catalog.Column, error) {
2626
catCols := make([]*catalog.Column, 0, len(cols))
2727
for _, col := range cols {
2828
catCols = append(catCols, &catalog.Column{
29-
Name: col.Name,
30-
Type: ast.TypeName{Name: col.DataType},
31-
IsNotNull: col.NotNull,
32-
IsArray: col.IsArray,
33-
Comment: col.Comment,
34-
Length: col.Length,
29+
Name: col.Name,
30+
Type: ast.TypeName{Name: col.DataType},
31+
IsNotNull: col.NotNull,
32+
IsUnsigned: col.Unsigned,
33+
IsArray: col.IsArray,
34+
Comment: col.Comment,
35+
Length: col.Length,
3536
})
3637
}
3738
return catCols, nil
@@ -256,6 +257,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
256257
TableAlias: t.Rel.Name,
257258
DataType: c.DataType,
258259
NotNull: c.NotNull,
260+
Unsigned: c.Unsigned,
259261
IsArray: c.IsArray,
260262
Length: c.Length,
261263
})
@@ -548,15 +550,16 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
548550

549551
cols = append(cols, &Column{
550552
Name: cname,
551-
OriginalName: c.Name,
552553
Type: c.Type,
553554
Table: c.Table,
554555
TableAlias: alias,
555556
DataType: c.DataType,
556557
NotNull: c.NotNull,
558+
Unsigned: c.Unsigned,
557559
IsArray: c.IsArray,
558560
Length: c.Length,
559561
EmbedTable: c.EmbedTable,
562+
OriginalName: c.Name,
560563
})
561564
}
562565
}

internal/compiler/query.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Column struct {
1919
OriginalName string
2020
DataType string
2121
NotNull bool
22+
Unsigned bool
2223
IsArray bool
2324
Comment string
2425
Length *int

internal/compiler/query_catalog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func ConvertColumn(rel *ast.TableName, c *catalog.Column) *Column {
5656
Name: c.Name,
5757
DataType: dataType(&c.Type),
5858
NotNull: c.IsNotNull,
59+
Unsigned: c.IsUnsigned,
5960
IsArray: c.IsArray,
6061
Type: &c.Type,
6162
Length: c.Length,

internal/compiler/resolve.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
215215
OriginalName: c.Name,
216216
DataType: dataType(&c.Type),
217217
NotNull: p.NotNull(),
218+
Unsigned: c.IsUnsigned,
218219
IsArray: c.IsArray,
219220
Length: c.Length,
220221
Table: table,
@@ -274,6 +275,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
274275
Name: p.Name(),
275276
DataType: dataType(&c.Type),
276277
NotNull: p.NotNull(),
278+
Unsigned: c.IsUnsigned,
277279
IsArray: c.IsArray,
278280
Table: table,
279281
IsNamedParam: isNamed,
@@ -553,6 +555,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
553555
Name: p.Name(),
554556
DataType: dataType(&c.Type),
555557
NotNull: c.IsNotNull,
558+
Unsigned: c.IsUnsigned,
556559
IsArray: c.IsArray,
557560
Table: table,
558561
IsNamedParam: isNamed,

internal/config/override.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ type Override struct {
2424
// for global overrides only when two different engines are in use
2525
Engine Engine `json:"engine,omitempty" yaml:"engine"`
2626

27-
// True if the GoType should override if the maching postgres type is nullable
27+
// True if the GoType should override if the matching type is nullable
2828
Nullable bool `json:"nullable" yaml:"nullable"`
29+
30+
// True if the GoType should override if the matching type is unsiged.
31+
Unsigned bool `json:"unsigned" yaml:"unsigned"`
32+
2933
// Deprecated. Use the `nullable` property instead
3034
Deprecated_Null bool `json:"null" yaml:"null"`
3135

internal/endtoend/testdata/alias/mysql/go/models.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/alias/mysql/go/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.

0 commit comments

Comments
 (0)