diff options
| author | James P. Cooper <jamespcooper@gmail.com> | 2012-01-25 17:47:32 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2012-01-25 17:47:32 -0800 |
| commit | c21b343438dfd26a56e89278522b03ac6417926c (patch) | |
| tree | 062e61228c7f3d312bf15c01111c72b1be04c00e /src/pkg/database/sql/sql.go | |
| parent | 75e9d24213992ea2077283383cb8705fefc2973a (diff) | |
| download | go-c21b343438dfd26a56e89278522b03ac6417926c.tar.xz | |
database/sql: add NullInt64, NullFloat64, NullBool
Fixes #2699
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5557063
Diffstat (limited to 'src/pkg/database/sql/sql.go')
| -rw-r--r-- | src/pkg/database/sql/sql.go | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/src/pkg/database/sql/sql.go b/src/pkg/database/sql/sql.go index a8bf2a8b00..70499b9a95 100644 --- a/src/pkg/database/sql/sql.go +++ b/src/pkg/database/sql/sql.go @@ -47,7 +47,6 @@ type RawBytes []byte // // NULL value // } // -// TODO(bradfitz): add other types. type NullString struct { String string Valid bool // Valid is true if String is not NULL @@ -71,6 +70,84 @@ func (ns NullString) SubsetValue() (interface{}, error) { return ns.String, nil } +// NullInt64 represents an int64 that may be null. +// NullInt64 implements the ScannerInto interface so +// it can be used as a scan destination, similar to NullString. +type NullInt64 struct { + Int64 int64 + Valid bool // Valid is true if Int64 is not NULL +} + +// ScanInto implements the ScannerInto interface. +func (n *NullInt64) ScanInto(value interface{}) error { + if value == nil { + n.Int64, n.Valid = 0, false + return nil + } + n.Valid = true + return convertAssign(&n.Int64, value) +} + +// SubsetValue implements the driver SubsetValuer interface. +func (n NullInt64) SubsetValue() (interface{}, error) { + if !n.Valid { + return nil, nil + } + return n.Int64, nil +} + +// NullFloat64 represents a float64 that may be null. +// NullFloat64 implements the ScannerInto interface so +// it can be used as a scan destination, similar to NullString. +type NullFloat64 struct { + Float64 float64 + Valid bool // Valid is true if Float64 is not NULL +} + +// ScanInto implements the ScannerInto interface. +func (n *NullFloat64) ScanInto(value interface{}) error { + if value == nil { + n.Float64, n.Valid = 0, false + return nil + } + n.Valid = true + return convertAssign(&n.Float64, value) +} + +// SubsetValue implements the driver SubsetValuer interface. +func (n NullFloat64) SubsetValue() (interface{}, error) { + if !n.Valid { + return nil, nil + } + return n.Float64, nil +} + +// NullBool represents a bool that may be null. +// NullBool implements the ScannerInto interface so +// it can be used as a scan destination, similar to NullString. +type NullBool struct { + Bool bool + Valid bool // Valid is true if Bool is not NULL +} + +// ScanInto implements the ScannerInto interface. +func (n *NullBool) ScanInto(value interface{}) error { + if value == nil { + n.Bool, n.Valid = false, false + return nil + } + n.Valid = true + return convertAssign(&n.Bool, value) +} + +// SubsetValue implements the driver SubsetValuer interface. +func (n NullBool) SubsetValue() (interface{}, error) { + if !n.Valid { + return nil, nil + } + return n.Bool, nil +} + // ScannerInto is an interface used by Scan. type ScannerInto interface { // ScanInto assigns a value from a database driver. |
