diff options
| author | Ariel Mashraki <ariel@mashraki.co.il> | 2021-04-20 16:15:29 +0300 |
|---|---|---|
| committer | Emmanuel Odeke <emmanuel@orijtech.com> | 2021-05-04 17:31:29 +0000 |
| commit | 10d625d5b41f17c118da18a592c683e60fcdcb3b (patch) | |
| tree | b9377f8f24c7a99f26746525d3bf6bf618cd4226 /src/database/sql/sql.go | |
| parent | 371ea545c015627ffac2139338ea63acda4c1523 (diff) | |
| download | go-10d625d5b41f17c118da18a592c683e60fcdcb3b.tar.xz | |
database/sql: add NullInt16 and NullByte
Fixes #40082
Change-Id: I01cd4d0e23c0376a6ee6e0b196c9f840cd662325
Reviewed-on: https://go-review.googlesource.com/c/go/+/311572
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/database/sql/sql.go')
| -rw-r--r-- | src/database/sql/sql.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index 61b5018f0b..68fb392e0d 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -260,6 +260,60 @@ func (n NullInt32) Value() (driver.Value, error) { return int64(n.Int32), nil } +// NullInt16 represents an int16 that may be null. +// NullInt16 implements the Scanner interface so +// it can be used as a scan destination, similar to NullString. +type NullInt16 struct { + Int16 int16 + Valid bool // Valid is true if Int16 is not NULL +} + +// Scan implements the Scanner interface. +func (n *NullInt16) Scan(value interface{}) error { + if value == nil { + n.Int16, n.Valid = 0, false + return nil + } + err := convertAssign(&n.Int16, value) + n.Valid = err == nil + return err +} + +// Value implements the driver Valuer interface. +func (n NullInt16) Value() (driver.Value, error) { + if !n.Valid { + return nil, nil + } + return int64(n.Int16), nil +} + +// NullByte represents a byte that may be null. +// NullByte implements the Scanner interface so +// it can be used as a scan destination, similar to NullString. +type NullByte struct { + Byte byte + Valid bool // Valid is true if Byte is not NULL +} + +// Scan implements the Scanner interface. +func (n *NullByte) Scan(value interface{}) error { + if value == nil { + n.Byte, n.Valid = 0, false + return nil + } + err := convertAssign(&n.Byte, value) + n.Valid = err == nil + return err +} + +// Value implements the driver Valuer interface. +func (n NullByte) Value() (driver.Value, error) { + if !n.Valid { + return nil, nil + } + return int64(n.Byte), nil +} + // NullFloat64 represents a float64 that may be null. // NullFloat64 implements the Scanner interface so // it can be used as a scan destination, similar to NullString. |
