Skip to content
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
9 changes: 7 additions & 2 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type tmplCtx struct {
OmitSqlcVersion bool
BuildTags string
WrapErrors bool
EmitPointersForNullTypes bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -140,7 +141,9 @@ func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Q
enumNames := make(map[string]struct{})
for _, enum := range enums {
enumNames[enum.Name] = struct{}{}
enumNames["Null"+enum.Name] = struct{}{}
if !options.EmitPointersForNullTypes {
enumNames["Null"+enum.Name] = struct{}{}
}
}
structNames := make(map[string]struct{})
for _, struckt := range structs {
Expand Down Expand Up @@ -192,6 +195,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
BuildTags: options.BuildTags,
OmitSqlcVersion: options.OmitSqlcVersion,
WrapErrors: options.WrapErrors,
EmitPointersForNullTypes: options.EmitPointersForNullTypes,
}

if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != opts.SQLDriverGoSQLDriverMySQL {
Expand Down Expand Up @@ -396,7 +400,8 @@ func filterUnusedStructs(enums []Enum, structs []Struct, queries []Query) ([]Enu
for _, enum := range enums {
_, keep := keepTypes[enum.Name]
_, keepNull := keepTypes["Null"+enum.Name]
if keep || keepNull {
_, keepPointer := keepTypes["*"+enum.Name]
if keep || keepNull || keepPointer {
keepEnums = append(keepEnums, enum)
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ func (i *importer) modelImports() fileImports {

if len(i.Enums) > 0 {
std["fmt"] = struct{}{}
std["database/sql/driver"] = struct{}{}
if !i.Options.EmitPointersForNullTypes {
std["database/sql/driver"] = struct{}{}
}
}

return sortedImports(std, pkg)
Expand Down
11 changes: 9 additions & 2 deletions internal/codegen/golang/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 {

Expand Down Expand Up @@ -127,10 +128,16 @@ func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.C
}
return StructName(schema.Name+"_"+enum.Name, options)
} else {
var enumTypeName string
if schema.Name == req.Catalog.DefaultSchema {
return "Null" + StructName(enum.Name, options)
enumTypeName = StructName(enum.Name, options)
} else {
enumTypeName = StructName(schema.Name+"_"+enum.Name, options)
}
return "Null" + StructName(schema.Name+"_"+enum.Name, options)
if emitPointersForNull {
return "*" + enumTypeName
}
return "Null" + enumTypeName
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,16 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
}
return StructName(schema.Name+"_"+enum.Name, options)
} else {
var enumTypeName string
if schema.Name == req.Catalog.DefaultSchema {
return "Null" + StructName(enum.Name, options)
enumTypeName = StructName(enum.Name, options)
} else {
enumTypeName = StructName(schema.Name+"_"+enum.Name, options)
}
return "Null" + StructName(schema.Name+"_"+enum.Name, options)
if emitPointersForNull {
return "*" + enumTypeName
}
return "Null" + enumTypeName
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (e *{{.Name}}) Scan(src interface{}) error {
return nil
}

{{ if not $.EmitPointersForNullTypes }}
type Null{{.Name}} struct {
{{.Name}} {{.Name}} {{if .NameTag}}{{$.Q}}{{.NameTag}}{{$.Q}}{{end}}
Valid bool {{if .ValidTag}}{{$.Q}}{{.ValidTag}}{{$.Q}}{{end}} // Valid is true if {{.Name}} is not NULL
Expand All @@ -131,6 +132,8 @@ func (ns Null{{.Name}}) Value() (driver.Value, error) {
return string(ns.{{.Name}}), nil
}

{{ end }}


{{ if $.EmitEnumValidMethod }}
func (e {{.Name}}) Valid() bool {
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,21 @@
-- name: GetAll :many
SELECT * FROM users;

-- name: NewUser :exec
INSERT INTO users (
first_name,
last_name,
age,
shoe_size,
shirt_size
) VALUES
(?, ?, ?, ?, ?);

-- name: UpdateSizes :exec
UPDATE users
SET shoe_size = ?, shirt_size = ?
WHERE id = ?;

-- name: DeleteBySize :exec
DELETE FROM users
WHERE shoe_size = ? AND shirt_size = ?;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE users (
id integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255),
age integer NOT NULL,
shoe_size ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL,
shirt_size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"name": "querytest",
"path": "go",
"schema": "schema.sql",
"queries": "query.sql",
"engine": "mysql",
"emit_methods_with_db_argument": true,
"emit_pointers_for_null_types": true
}
]
}

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

Loading
Loading