diff --git a/examples/batch/postgresql/models.go b/examples/batch/postgresql/models.go index 6bca343f6c..b519de4690 100644 --- a/examples/batch/postgresql/models.go +++ b/examples/batch/postgresql/models.go @@ -5,6 +5,7 @@ package batch import ( + "database/sql/driver" "fmt" "time" ) @@ -28,6 +29,36 @@ func (e *BookType) Scan(src interface{}) error { return nil } +// NullBookType is the nullable version of BookType. +type NullBookType struct { + BookType BookType + Valid bool +} + +func (e *NullBookType) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.BookType = BookType(s) + case string: + e.BookType = BookType(s) + default: + return fmt.Errorf("unsupported scan type for NullBookType: %T", src) + } + e.Valid = len(e.BookType) > 0 + return nil +} + +func (e *NullBookType) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.BookType), nil +} + type Author struct { AuthorID int32 `json:"author_id"` Name string `json:"name"` diff --git a/examples/booktest/mysql/models.go b/examples/booktest/mysql/models.go index 06c6e4749d..e8d1ecb19f 100644 --- a/examples/booktest/mysql/models.go +++ b/examples/booktest/mysql/models.go @@ -5,6 +5,7 @@ package booktest import ( + "database/sql/driver" "fmt" "time" ) @@ -28,6 +29,36 @@ func (e *BooksBookType) Scan(src interface{}) error { return nil } +// NullBooksBookType is the nullable version of BooksBookType. +type NullBooksBookType struct { + BooksBookType BooksBookType + Valid bool +} + +func (e *NullBooksBookType) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.BooksBookType = BooksBookType(s) + case string: + e.BooksBookType = BooksBookType(s) + default: + return fmt.Errorf("unsupported scan type for NullBooksBookType: %T", src) + } + e.Valid = len(e.BooksBookType) > 0 + return nil +} + +func (e *NullBooksBookType) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.BooksBookType), nil +} + type Author struct { AuthorID int32 Name string diff --git a/examples/booktest/postgresql/models.go b/examples/booktest/postgresql/models.go index d3e8401288..da79cd12dd 100644 --- a/examples/booktest/postgresql/models.go +++ b/examples/booktest/postgresql/models.go @@ -5,6 +5,7 @@ package booktest import ( + "database/sql/driver" "fmt" "time" ) @@ -28,6 +29,36 @@ func (e *BookType) Scan(src interface{}) error { return nil } +// NullBookType is the nullable version of BookType. +type NullBookType struct { + BookType BookType + Valid bool +} + +func (e *NullBookType) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.BookType = BookType(s) + case string: + e.BookType = BookType(s) + default: + return fmt.Errorf("unsupported scan type for NullBookType: %T", src) + } + e.Valid = len(e.BookType) > 0 + return nil +} + +func (e *NullBookType) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.BookType), nil +} + type Author struct { AuthorID int32 Name string diff --git a/examples/ondeck/mysql/models.go b/examples/ondeck/mysql/models.go index 907c782e07..084d790f5a 100644 --- a/examples/ondeck/mysql/models.go +++ b/examples/ondeck/mysql/models.go @@ -6,6 +6,7 @@ package ondeck import ( "database/sql" + "database/sql/driver" "fmt" "time" ) @@ -29,6 +30,36 @@ func (e *VenuesStatus) Scan(src interface{}) error { return nil } +// NullVenuesStatus is the nullable version of VenuesStatus. +type NullVenuesStatus struct { + VenuesStatus VenuesStatus + Valid bool +} + +func (e *NullVenuesStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.VenuesStatus = VenuesStatus(s) + case string: + e.VenuesStatus = VenuesStatus(s) + default: + return fmt.Errorf("unsupported scan type for NullVenuesStatus: %T", src) + } + e.Valid = len(e.VenuesStatus) > 0 + return nil +} + +func (e *NullVenuesStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.VenuesStatus), nil +} + type City struct { Slug string `json:"slug"` Name string `json:"name"` diff --git a/examples/ondeck/postgresql/models.go b/examples/ondeck/postgresql/models.go index 04dbce5f01..3740c12fa8 100644 --- a/examples/ondeck/postgresql/models.go +++ b/examples/ondeck/postgresql/models.go @@ -6,6 +6,7 @@ package ondeck import ( "database/sql" + "database/sql/driver" "fmt" "time" ) @@ -30,6 +31,36 @@ func (e *Status) Scan(src interface{}) error { return nil } +// NullStatus is the nullable version of Status. +type NullStatus struct { + Status Status + Valid bool +} + +func (e *NullStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Status = Status(s) + case string: + e.Status = Status(s) + default: + return fmt.Errorf("unsupported scan type for NullStatus: %T", src) + } + e.Valid = len(e.Status) > 0 + return nil +} + +func (e *NullStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Status), nil +} + type City struct { Slug string `json:"slug"` Name string `json:"name"` diff --git a/internal/codegen/golang/imports.go b/internal/codegen/golang/imports.go index 72c97b4fea..a3d04b6b3b 100644 --- a/internal/codegen/golang/imports.go +++ b/internal/codegen/golang/imports.go @@ -264,6 +264,7 @@ func (i *importer) modelImports() fileImports { if len(i.Enums) > 0 { std["fmt"] = struct{}{} + std["database/sql/driver"] = struct{}{} } return sortedImports(std, pkg) diff --git a/internal/codegen/golang/postgresql_type.go b/internal/codegen/golang/postgresql_type.go index afa5a21aa7..b3771f7346 100644 --- a/internal/codegen/golang/postgresql_type.go +++ b/internal/codegen/golang/postgresql_type.go @@ -279,11 +279,15 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string { } for _, enum := range schema.Enums { + nullPrefix := "" + if !notNull { + nullPrefix = "Null" + } if rel.Name == enum.Name && rel.Schema == schema.Name { if schema.Name == req.Catalog.DefaultSchema { - return StructName(enum.Name, req.Settings) + return nullPrefix + StructName(enum.Name, req.Settings) } - return StructName(schema.Name+"_"+enum.Name, req.Settings) + return nullPrefix + StructName(schema.Name+"_"+enum.Name, req.Settings) } } diff --git a/internal/codegen/golang/templates/template.tmpl b/internal/codegen/golang/templates/template.tmpl index 5af2566840..f522a2a86e 100644 --- a/internal/codegen/golang/templates/template.tmpl +++ b/internal/codegen/golang/templates/template.tmpl @@ -86,6 +86,36 @@ func (e *{{.Name}}) Scan(src interface{}) error { } return nil } + +// Null{{.Name}} is the nullable version of {{.Name}}. +type Null{{.Name}} struct { + {{.Name}} {{.Name}} + Valid bool +} + +func (e *Null{{.Name}}) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.{{.Name}} = {{.Name}}(s) + case string: + e.{{.Name}} = {{.Name}}(s) + default: + return fmt.Errorf("unsupported scan type for Null{{.Name}}: %T", src) + } + e.Valid = len(e.{{.Name}}) > 0 + return nil +} + +func (e *Null{{.Name}}) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.{{.Name}}), nil +} {{end}} {{range .Structs}} diff --git a/internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go b/internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go index 86bc5e167d..2dd9c55e9b 100644 --- a/internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -29,6 +30,36 @@ func (e *FooMood) Scan(src interface{}) error { return nil } +// NullFooMood is the nullable version of FooMood. +type NullFooMood struct { + FooMood FooMood + Valid bool +} + +func (e *NullFooMood) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooMood = FooMood(s) + case string: + e.FooMood = FooMood(s) + default: + return fmt.Errorf("unsupported scan type for NullFooMood: %T", src) + } + e.Valid = len(e.FooMood) > 0 + return nil +} + +func (e *NullFooMood) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooMood), nil +} + // this is the bar table type FooBar struct { // this is the baz column diff --git a/internal/endtoend/testdata/comment_on/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/comment_on/postgresql/stdlib/go/models.go index 86bc5e167d..2dd9c55e9b 100644 --- a/internal/endtoend/testdata/comment_on/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/comment_on/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -29,6 +30,36 @@ func (e *FooMood) Scan(src interface{}) error { return nil } +// NullFooMood is the nullable version of FooMood. +type NullFooMood struct { + FooMood FooMood + Valid bool +} + +func (e *NullFooMood) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooMood = FooMood(s) + case string: + e.FooMood = FooMood(s) + default: + return fmt.Errorf("unsupported scan type for NullFooMood: %T", src) + } + e.Valid = len(e.FooMood) > 0 + return nil +} + +func (e *NullFooMood) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooMood), nil +} + // this is the bar table type FooBar struct { // this is the baz column diff --git a/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/pgx/go/models.go index b89548aff6..87def7c7ef 100644 --- a/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,3 +28,33 @@ func (e *Status) Scan(src interface{}) error { } return nil } + +// NullStatus is the nullable version of Status. +type NullStatus struct { + Status Status + Valid bool +} + +func (e *NullStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Status = Status(s) + case string: + e.Status = Status(s) + default: + return fmt.Errorf("unsupported scan type for NullStatus: %T", src) + } + e.Valid = len(e.Status) > 0 + return nil +} + +func (e *NullStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Status), nil +} diff --git a/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/stdlib/go/models.go index b89548aff6..87def7c7ef 100644 --- a/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_add_value/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,3 +28,33 @@ func (e *Status) Scan(src interface{}) error { } return nil } + +// NullStatus is the nullable version of Status. +type NullStatus struct { + Status Status + Valid bool +} + +func (e *NullStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Status = Status(s) + case string: + e.Status = Status(s) + default: + return fmt.Errorf("unsupported scan type for NullStatus: %T", src) + } + e.Valid = len(e.Status) > 0 + return nil +} + +func (e *NullStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Status), nil +} diff --git a/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/pgx/go/models.go index 88a5fe9ef2..8f6598c371 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *NewEvent) Scan(src interface{}) error { return nil } +// NullNewEvent is the nullable version of NewEvent. +type NullNewEvent struct { + NewEvent NewEvent + Valid bool +} + +func (e *NullNewEvent) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.NewEvent = NewEvent(s) + case string: + e.NewEvent = NewEvent(s) + default: + return fmt.Errorf("unsupported scan type for NullNewEvent: %T", src) + } + e.Valid = len(e.NewEvent) > 0 + return nil +} + +func (e *NullNewEvent) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.NewEvent), nil +} + type LogLine struct { ID int64 Status NewEvent diff --git a/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/stdlib/go/models.go index 88a5fe9ef2..8f6598c371 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *NewEvent) Scan(src interface{}) error { return nil } +// NullNewEvent is the nullable version of NewEvent. +type NullNewEvent struct { + NewEvent NewEvent + Valid bool +} + +func (e *NullNewEvent) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.NewEvent = NewEvent(s) + case string: + e.NewEvent = NewEvent(s) + default: + return fmt.Errorf("unsupported scan type for NullNewEvent: %T", src) + } + e.Valid = len(e.NewEvent) > 0 + return nil +} + +func (e *NullNewEvent) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.NewEvent), nil +} + type LogLine struct { ID int64 Status NewEvent diff --git a/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/pgx/go/models.go index 88a5fe9ef2..8f6598c371 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *NewEvent) Scan(src interface{}) error { return nil } +// NullNewEvent is the nullable version of NewEvent. +type NullNewEvent struct { + NewEvent NewEvent + Valid bool +} + +func (e *NullNewEvent) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.NewEvent = NewEvent(s) + case string: + e.NewEvent = NewEvent(s) + default: + return fmt.Errorf("unsupported scan type for NullNewEvent: %T", src) + } + e.Valid = len(e.NewEvent) > 0 + return nil +} + +func (e *NullNewEvent) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.NewEvent), nil +} + type LogLine struct { ID int64 Status NewEvent diff --git a/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/stdlib/go/models.go index 88a5fe9ef2..8f6598c371 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename_and_update_columns/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *NewEvent) Scan(src interface{}) error { return nil } +// NullNewEvent is the nullable version of NewEvent. +type NullNewEvent struct { + NewEvent NewEvent + Valid bool +} + +func (e *NullNewEvent) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.NewEvent = NewEvent(s) + case string: + e.NewEvent = NewEvent(s) + default: + return fmt.Errorf("unsupported scan type for NullNewEvent: %T", src) + } + e.Valid = len(e.NewEvent) > 0 + return nil +} + +func (e *NullNewEvent) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.NewEvent), nil +} + type LogLine struct { ID int64 Status NewEvent diff --git a/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/pgx/go/models.go index 9c2f93f3c7..a18e57ff2c 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -26,3 +27,33 @@ func (e *Status) Scan(src interface{}) error { } return nil } + +// NullStatus is the nullable version of Status. +type NullStatus struct { + Status Status + Valid bool +} + +func (e *NullStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Status = Status(s) + case string: + e.Status = Status(s) + default: + return fmt.Errorf("unsupported scan type for NullStatus: %T", src) + } + e.Valid = len(e.Status) > 0 + return nil +} + +func (e *NullStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Status), nil +} diff --git a/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/stdlib/go/models.go index 9c2f93f3c7..a18e57ff2c 100644 --- a/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_alter_type_rename_value/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -26,3 +27,33 @@ func (e *Status) Scan(src interface{}) error { } return nil } + +// NullStatus is the nullable version of Status. +type NullStatus struct { + Status Status + Valid bool +} + +func (e *NullStatus) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Status = Status(s) + case string: + e.Status = Status(s) + default: + return fmt.Errorf("unsupported scan type for NullStatus: %T", src) + } + e.Valid = len(e.Status) > 0 + return nil +} + +func (e *NullStatus) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Status), nil +} diff --git a/internal/endtoend/testdata/ddl_comment/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_comment/postgresql/pgx/go/models.go index 23eb5cbab1..ca8621c408 100644 --- a/internal/endtoend/testdata/ddl_comment/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_comment/postgresql/pgx/go/models.go @@ -6,6 +6,7 @@ package querytest import ( "database/sql" + "database/sql/driver" "fmt" ) @@ -28,6 +29,36 @@ func (e *FooBat) Scan(src interface{}) error { return nil } +// NullFooBat is the nullable version of FooBat. +type NullFooBat struct { + FooBat FooBat + Valid bool +} + +func (e *NullFooBat) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooBat = FooBat(s) + case string: + e.FooBat = FooBat(s) + default: + return fmt.Errorf("unsupported scan type for NullFooBat: %T", src) + } + e.Valid = len(e.FooBat) > 0 + return nil +} + +func (e *NullFooBat) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooBat), nil +} + // Table comment type FooBar struct { // Column comment diff --git a/internal/endtoend/testdata/ddl_comment/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_comment/postgresql/stdlib/go/models.go index 23eb5cbab1..ca8621c408 100644 --- a/internal/endtoend/testdata/ddl_comment/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_comment/postgresql/stdlib/go/models.go @@ -6,6 +6,7 @@ package querytest import ( "database/sql" + "database/sql/driver" "fmt" ) @@ -28,6 +29,36 @@ func (e *FooBat) Scan(src interface{}) error { return nil } +// NullFooBat is the nullable version of FooBat. +type NullFooBat struct { + FooBat FooBat + Valid bool +} + +func (e *NullFooBat) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooBat = FooBat(s) + case string: + e.FooBat = FooBat(s) + default: + return fmt.Errorf("unsupported scan type for NullFooBat: %T", src) + } + e.Valid = len(e.FooBat) > 0 + return nil +} + +func (e *NullFooBat) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooBat), nil +} + // Table comment type FooBar struct { // Column comment diff --git a/internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go b/internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go index 4e739ea449..020adac897 100644 --- a/internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go +++ b/internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -37,6 +38,36 @@ func (e *FooDigit) Scan(src interface{}) error { return nil } +// NullFooDigit is the nullable version of FooDigit. +type NullFooDigit struct { + FooDigit FooDigit + Valid bool +} + +func (e *NullFooDigit) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooDigit = FooDigit(s) + case string: + e.FooDigit = FooDigit(s) + default: + return fmt.Errorf("unsupported scan type for NullFooDigit: %T", src) + } + e.Valid = len(e.FooDigit) > 0 + return nil +} + +func (e *NullFooDigit) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooDigit), nil +} + type FooFoobar string const ( @@ -61,6 +92,36 @@ func (e *FooFoobar) Scan(src interface{}) error { return nil } +// NullFooFoobar is the nullable version of FooFoobar. +type NullFooFoobar struct { + FooFoobar FooFoobar + Valid bool +} + +func (e *NullFooFoobar) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooFoobar = FooFoobar(s) + case string: + e.FooFoobar = FooFoobar(s) + default: + return fmt.Errorf("unsupported scan type for NullFooFoobar: %T", src) + } + e.Valid = len(e.FooFoobar) > 0 + return nil +} + +func (e *NullFooFoobar) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooFoobar), nil +} + type Foo struct { Foobar FooFoobar Digit FooDigit diff --git a/internal/endtoend/testdata/ddl_create_enum/postgresql/pgx/go/models.go b/internal/endtoend/testdata/ddl_create_enum/postgresql/pgx/go/models.go index 274527c521..5a752f7b63 100644 --- a/internal/endtoend/testdata/ddl_create_enum/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/ddl_create_enum/postgresql/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -37,6 +38,36 @@ func (e *Digit) Scan(src interface{}) error { return nil } +// NullDigit is the nullable version of Digit. +type NullDigit struct { + Digit Digit + Valid bool +} + +func (e *NullDigit) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Digit = Digit(s) + case string: + e.Digit = Digit(s) + default: + return fmt.Errorf("unsupported scan type for NullDigit: %T", src) + } + e.Valid = len(e.Digit) > 0 + return nil +} + +func (e *NullDigit) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Digit), nil +} + type Foobar string const ( @@ -61,6 +92,36 @@ func (e *Foobar) Scan(src interface{}) error { return nil } +// NullFoobar is the nullable version of Foobar. +type NullFoobar struct { + Foobar Foobar + Valid bool +} + +func (e *NullFoobar) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Foobar = Foobar(s) + case string: + e.Foobar = Foobar(s) + default: + return fmt.Errorf("unsupported scan type for NullFoobar: %T", src) + } + e.Valid = len(e.Foobar) > 0 + return nil +} + +func (e *NullFoobar) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Foobar), nil +} + type Foo struct { Val Foobar } diff --git a/internal/endtoend/testdata/ddl_create_enum/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/ddl_create_enum/postgresql/stdlib/go/models.go index 274527c521..5a752f7b63 100644 --- a/internal/endtoend/testdata/ddl_create_enum/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/ddl_create_enum/postgresql/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -37,6 +38,36 @@ func (e *Digit) Scan(src interface{}) error { return nil } +// NullDigit is the nullable version of Digit. +type NullDigit struct { + Digit Digit + Valid bool +} + +func (e *NullDigit) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Digit = Digit(s) + case string: + e.Digit = Digit(s) + default: + return fmt.Errorf("unsupported scan type for NullDigit: %T", src) + } + e.Valid = len(e.Digit) > 0 + return nil +} + +func (e *NullDigit) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Digit), nil +} + type Foobar string const ( @@ -61,6 +92,36 @@ func (e *Foobar) Scan(src interface{}) error { return nil } +// NullFoobar is the nullable version of Foobar. +type NullFoobar struct { + Foobar Foobar + Valid bool +} + +func (e *NullFoobar) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.Foobar = Foobar(s) + case string: + e.Foobar = Foobar(s) + default: + return fmt.Errorf("unsupported scan type for NullFoobar: %T", src) + } + e.Valid = len(e.Foobar) > 0 + return nil +} + +func (e *NullFoobar) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.Foobar), nil +} + type Foo struct { Val Foobar } diff --git a/internal/endtoend/testdata/rename/pgx/go/models.go b/internal/endtoend/testdata/rename/pgx/go/models.go index dab1639f7a..c349e0899a 100644 --- a/internal/endtoend/testdata/rename/pgx/go/models.go +++ b/internal/endtoend/testdata/rename/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -28,6 +29,36 @@ func (e *IPProtocol) Scan(src interface{}) error { return nil } +// NullIPProtocol is the nullable version of IPProtocol. +type NullIPProtocol struct { + IPProtocol IPProtocol + Valid bool +} + +func (e *NullIPProtocol) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.IPProtocol = IPProtocol(s) + case string: + e.IPProtocol = IPProtocol(s) + default: + return fmt.Errorf("unsupported scan type for NullIPProtocol: %T", src) + } + e.Valid = len(e.IPProtocol) > 0 + return nil +} + +func (e *NullIPProtocol) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.IPProtocol), nil +} + type BarNew struct { IDNew int32 IpOld IPProtocol diff --git a/internal/endtoend/testdata/rename/stdlib/go/models.go b/internal/endtoend/testdata/rename/stdlib/go/models.go index dab1639f7a..c349e0899a 100644 --- a/internal/endtoend/testdata/rename/stdlib/go/models.go +++ b/internal/endtoend/testdata/rename/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -28,6 +29,36 @@ func (e *IPProtocol) Scan(src interface{}) error { return nil } +// NullIPProtocol is the nullable version of IPProtocol. +type NullIPProtocol struct { + IPProtocol IPProtocol + Valid bool +} + +func (e *NullIPProtocol) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.IPProtocol = IPProtocol(s) + case string: + e.IPProtocol = IPProtocol(s) + default: + return fmt.Errorf("unsupported scan type for NullIPProtocol: %T", src) + } + e.Valid = len(e.IPProtocol) > 0 + return nil +} + +func (e *NullIPProtocol) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.IPProtocol), nil +} + type BarNew struct { IDNew int32 IpOld IPProtocol diff --git a/internal/endtoend/testdata/schema_scoped_enum/pgx/go/models.go b/internal/endtoend/testdata/schema_scoped_enum/pgx/go/models.go index 0e0fb99f2a..75ca26ee73 100644 --- a/internal/endtoend/testdata/schema_scoped_enum/pgx/go/models.go +++ b/internal/endtoend/testdata/schema_scoped_enum/pgx/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *FooTypeUserRole) Scan(src interface{}) error { return nil } +// NullFooTypeUserRole is the nullable version of FooTypeUserRole. +type NullFooTypeUserRole struct { + FooTypeUserRole FooTypeUserRole + Valid bool +} + +func (e *NullFooTypeUserRole) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooTypeUserRole = FooTypeUserRole(s) + case string: + e.FooTypeUserRole = FooTypeUserRole(s) + default: + return fmt.Errorf("unsupported scan type for NullFooTypeUserRole: %T", src) + } + e.Valid = len(e.FooTypeUserRole) > 0 + return nil +} + +func (e *NullFooTypeUserRole) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooTypeUserRole), nil +} + type FooUser struct { - Role FooTypeUserRole + Role NullFooTypeUserRole } diff --git a/internal/endtoend/testdata/schema_scoped_enum/pgx/go/query.sql.go b/internal/endtoend/testdata/schema_scoped_enum/pgx/go/query.sql.go index e602f69813..c0cb1125c7 100644 --- a/internal/endtoend/testdata/schema_scoped_enum/pgx/go/query.sql.go +++ b/internal/endtoend/testdata/schema_scoped_enum/pgx/go/query.sql.go @@ -13,15 +13,15 @@ const listUsersByRole = `-- name: ListUsersByRole :many SELECT role FROM foo.users WHERE role = $1 ` -func (q *Queries) ListUsersByRole(ctx context.Context, role FooTypeUserRole) ([]FooTypeUserRole, error) { +func (q *Queries) ListUsersByRole(ctx context.Context, role NullFooTypeUserRole) ([]NullFooTypeUserRole, error) { rows, err := q.db.Query(ctx, listUsersByRole, role) if err != nil { return nil, err } defer rows.Close() - var items []FooTypeUserRole + var items []NullFooTypeUserRole for rows.Next() { - var role FooTypeUserRole + var role NullFooTypeUserRole if err := rows.Scan(&role); err != nil { return nil, err } diff --git a/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/models.go b/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/models.go index 0e0fb99f2a..75ca26ee73 100644 --- a/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/models.go +++ b/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/models.go @@ -5,6 +5,7 @@ package querytest import ( + "database/sql/driver" "fmt" ) @@ -27,6 +28,36 @@ func (e *FooTypeUserRole) Scan(src interface{}) error { return nil } +// NullFooTypeUserRole is the nullable version of FooTypeUserRole. +type NullFooTypeUserRole struct { + FooTypeUserRole FooTypeUserRole + Valid bool +} + +func (e *NullFooTypeUserRole) Scan(src interface{}) error { + if src == nil { + e.Valid = false + return nil + } + switch s := src.(type) { + case []byte: + e.FooTypeUserRole = FooTypeUserRole(s) + case string: + e.FooTypeUserRole = FooTypeUserRole(s) + default: + return fmt.Errorf("unsupported scan type for NullFooTypeUserRole: %T", src) + } + e.Valid = len(e.FooTypeUserRole) > 0 + return nil +} + +func (e *NullFooTypeUserRole) Value() (driver.Value, error) { + if !e.Valid { + return nil, nil + } + return string(e.FooTypeUserRole), nil +} + type FooUser struct { - Role FooTypeUserRole + Role NullFooTypeUserRole } diff --git a/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/query.sql.go b/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/query.sql.go index 84e980cc46..75eb539fd9 100644 --- a/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/query.sql.go +++ b/internal/endtoend/testdata/schema_scoped_enum/stdlib/go/query.sql.go @@ -13,15 +13,15 @@ const listUsersByRole = `-- name: ListUsersByRole :many SELECT role FROM foo.users WHERE role = $1 ` -func (q *Queries) ListUsersByRole(ctx context.Context, role FooTypeUserRole) ([]FooTypeUserRole, error) { +func (q *Queries) ListUsersByRole(ctx context.Context, role NullFooTypeUserRole) ([]NullFooTypeUserRole, error) { rows, err := q.db.QueryContext(ctx, listUsersByRole, role) if err != nil { return nil, err } defer rows.Close() - var items []FooTypeUserRole + var items []NullFooTypeUserRole for rows.Next() { - var role FooTypeUserRole + var role NullFooTypeUserRole if err := rows.Scan(&role); err != nil { return nil, err }