string.go 685 B

1234567891011121314151617181920212223242526272829303132
  1. package types
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. )
  6. // NullString is an implementation of a string for the MySQL type char/varchar ....
  7. type NullString string
  8. // Value implements the driver.Valuer interface,
  9. // and turns the string into a bytes for MySQL storage.
  10. func (s NullString) Value() (driver.Value, error) {
  11. return []byte(s), nil
  12. }
  13. // Scan implements the sql.Scanner interface,
  14. // and turns the bytes incoming from MySQL into a string
  15. func (s *NullString) Scan(src interface{}) error {
  16. if src != nil {
  17. v, ok := src.([]byte)
  18. if !ok {
  19. return errors.New("bad []byte type assertion")
  20. }
  21. *s = NullString(v)
  22. return nil
  23. }
  24. *s = NullString("")
  25. return nil
  26. }