Skip to content
Next Next commit
feat: add support for emitting pointers for nullable types in MySQL c…
…ode generation
  • Loading branch information
yoRyuuuuu committed Mar 11, 2025
commit a0eb6971215a410aefd5e67d93930b90374d9700
46 changes: 46 additions & 0 deletions internal/codegen/golang/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
unsigned := col.Unsigned
emitPointersForNull := options.EmitPointersForNullTypes

switch columnType {

case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"

case "tinyint":
if col.Length == 1 {
if notNull {
return "bool"
}
if emitPointersForNull {
return "*bool"
}
return "sql.NullBool"
} else {
if notNull {
Expand All @@ -35,6 +42,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
}
return "int8"
}
if emitPointersForNull {
if unsigned {
return "*uint8"
}
return "*int8"
}
// The database/sql package does not have a sql.NullInt8 type, so we
// use the smallest type they have which is NullInt16
return "sql.NullInt16"
Expand All @@ -44,6 +57,9 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
if notNull {
return "int16"
}
if emitPointersForNull {
return "*int16"
}
return "sql.NullInt16"

case "smallint":
Expand All @@ -53,6 +69,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
}
return "int16"
}
if emitPointersForNull {
if unsigned {
return "*uint16"
}
return "*int16"
}
return "sql.NullInt16"

case "int", "integer", "mediumint":
Expand All @@ -62,6 +84,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
}
return "int32"
}
if emitPointersForNull {
if unsigned {
return "*uint32"
}
return "*int32"
}
return "sql.NullInt32"

case "bigint":
Expand All @@ -71,6 +99,12 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
}
return "int64"
}
if emitPointersForNull {
if unsigned {
return "*uint64"
}
return "*int64"
}
return "sql.NullInt64"

case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
Expand All @@ -83,12 +117,18 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
if notNull {
return "float64"
}
if emitPointersForNull {
return "*float64"
}
return "sql.NullFloat64"

case "decimal", "dec", "fixed":
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"

case "enum":
Expand All @@ -99,12 +139,18 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
if notNull {
return "time.Time"
}
if emitPointersForNull {
return "*time.Time"
}
return "sql.NullTime"

case "boolean", "bool":
if notNull {
return "bool"
}
if emitPointersForNull {
return "*bool"
}
return "sql.NullBool"

case "json":
Expand Down