Skip to content

Support emit_pointers_for_null_types in MySQL #3879

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE table dt_boolean (
a boolean,
b bool,
c tinyint(1)
);

CREATE table dt_boolean_not_null (
a boolean not null,
b bool not null,
c tinyint(1) not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE TABLE dt_integer (
a tinyint,
b smallint,
c int,
d integer,
e mediumint,
f bigint
);

CREATE TABLE dt_integer_not_null (
a tinyint not null,
b smallint not null,
c int not null,
d integer not null,
e mediumint not null,
f bigint not null
);

CREATE TABLE dt_unsigned_integer (
a tinyint unsigned,
b smallint unsigned,
c int unsigned,
d integer unsigned,
e mediumint unsigned,
f bigint unsigned
);


CREATE TABLE dt_unsigned_integer_not_null (
a tinyint unsigned not null,
b smallint unsigned not null,
c int unsigned not null,
d integer unsigned not null,
e mediumint unsigned not null,
f bigint unsigned not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: Test :one
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE dt_real_number (
a double,
b double precision,
c real,
d float,
e decimal,
f dec,
g fixed
);

CREATE TABLE dt_real_number_not_null (
a double not null,
b double precision not null,
c real not null,
d float not null,
e decimal not null,
f dec not null,
g fixed not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "datatype",
"schema": "sql/",
"queries": "sql/",
"emit_pointers_for_null_types": true
}
]
}
Loading