Skip to content

Commit a0eb697

Browse files
committed
feat: add support for emitting pointers for nullable types in MySQL code generation
1 parent 6b2ed20 commit a0eb697

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

internal/codegen/golang/mysql_type.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
1313
columnType := sdk.DataType(col.Type)
1414
notNull := col.NotNull || col.IsArray
1515
unsigned := col.Unsigned
16+
emitPointersForNull := options.EmitPointersForNullTypes
1617

1718
switch columnType {
1819

1920
case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
2021
if notNull {
2122
return "string"
2223
}
24+
if emitPointersForNull {
25+
return "*string"
26+
}
2327
return "sql.NullString"
2428

2529
case "tinyint":
2630
if col.Length == 1 {
2731
if notNull {
2832
return "bool"
2933
}
34+
if emitPointersForNull {
35+
return "*bool"
36+
}
3037
return "sql.NullBool"
3138
} else {
3239
if notNull {
@@ -35,6 +42,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
3542
}
3643
return "int8"
3744
}
45+
if emitPointersForNull {
46+
if unsigned {
47+
return "*uint8"
48+
}
49+
return "*int8"
50+
}
3851
// The database/sql package does not have a sql.NullInt8 type, so we
3952
// use the smallest type they have which is NullInt16
4053
return "sql.NullInt16"
@@ -44,6 +57,9 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
4457
if notNull {
4558
return "int16"
4659
}
60+
if emitPointersForNull {
61+
return "*int16"
62+
}
4763
return "sql.NullInt16"
4864

4965
case "smallint":
@@ -53,6 +69,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
5369
}
5470
return "int16"
5571
}
72+
if emitPointersForNull {
73+
if unsigned {
74+
return "*uint16"
75+
}
76+
return "*int16"
77+
}
5678
return "sql.NullInt16"
5779

5880
case "int", "integer", "mediumint":
@@ -62,6 +84,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
6284
}
6385
return "int32"
6486
}
87+
if emitPointersForNull {
88+
if unsigned {
89+
return "*uint32"
90+
}
91+
return "*int32"
92+
}
6593
return "sql.NullInt32"
6694

6795
case "bigint":
@@ -71,6 +99,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
7199
}
72100
return "int64"
73101
}
102+
if emitPointersForNull {
103+
if unsigned {
104+
return "*uint64"
105+
}
106+
return "*int64"
107+
}
74108
return "sql.NullInt64"
75109

76110
case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
@@ -83,12 +117,18 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
83117
if notNull {
84118
return "float64"
85119
}
120+
if emitPointersForNull {
121+
return "*float64"
122+
}
86123
return "sql.NullFloat64"
87124

88125
case "decimal", "dec", "fixed":
89126
if notNull {
90127
return "string"
91128
}
129+
if emitPointersForNull {
130+
return "*string"
131+
}
92132
return "sql.NullString"
93133

94134
case "enum":
@@ -99,12 +139,18 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
99139
if notNull {
100140
return "time.Time"
101141
}
142+
if emitPointersForNull {
143+
return "*time.Time"
144+
}
102145
return "sql.NullTime"
103146

104147
case "boolean", "bool":
105148
if notNull {
106149
return "bool"
107150
}
151+
if emitPointersForNull {
152+
return "*bool"
153+
}
108154
return "sql.NullBool"
109155

110156
case "json":

0 commit comments

Comments
 (0)