diff options
Diffstat (limited to 'Godeps/_workspace/src/github.com/pborman/uuid/sql.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/pborman/uuid/sql.go | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/Godeps/_workspace/src/github.com/pborman/uuid/sql.go b/Godeps/_workspace/src/github.com/pborman/uuid/sql.go index 2d7679e2a..d015bfd13 100644 --- a/Godeps/_workspace/src/github.com/pborman/uuid/sql.go +++ b/Godeps/_workspace/src/github.com/pborman/uuid/sql.go @@ -5,6 +5,7 @@ package uuid import ( + "database/sql/driver" "errors" "fmt" ) @@ -15,6 +16,11 @@ import ( func (uuid *UUID) Scan(src interface{}) error { switch src.(type) { case string: + // if an empty UUID comes from a table, we return a null UUID + if src.(string) == "" { + return nil + } + // see uuid.Parse for required string format parsed := Parse(src.(string)) @@ -24,17 +30,37 @@ func (uuid *UUID) Scan(src interface{}) error { *uuid = parsed case []byte: - // assumes a simple slice of bytes, just check validity and store - u := UUID(src.([]byte)) + b := src.([]byte) - if u.Variant() == Invalid { - return errors.New("Scan: invalid UUID format") + // if an empty UUID comes from a table, we return a null UUID + if len(b) == 0 { + return nil + } + + // assumes a simple slice of bytes if 16 bytes + // otherwise attempts to parse + if len(b) == 16 { + *uuid = UUID(b) + } else { + u := Parse(string(b)) + + if u == nil { + return errors.New("Scan: invalid UUID format") + } + + *uuid = u } - *uuid = u default: return fmt.Errorf("Scan: unable to scan type %T into UUID", src) } return nil } + +// Value implements sql.Valuer so that UUIDs can be written to databases +// transparently. Currently, UUIDs map to strings. Please consult +// database-specific driver documentation for matching types. +func (uuid UUID) Value() (driver.Value, error) { + return uuid.String(), nil +} |