string.go 678 B

1234567891011121314151617181920212223242526272829303132
  1. package types
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. )
  6. // TextNull is an implementation of a string for the MySQL type char/varchar/text ....
  7. type TextNull string
  8. // Value implements the driver.Valuer interface,
  9. // and turns the string into a bytes for MySQL storage.
  10. func (s TextNull) 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 *TextNull) 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 = TextNull(v)
  22. return nil
  23. }
  24. *s = TextNull("")
  25. return nil
  26. }