bool.go 736 B

123456789101112131415161718192021222324252627282930
  1. package types
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. )
  6. // BitBool is an implementation of a bool for the MySQL type BIT(1).
  7. // This type allows you to avoid wasting an entire byte for MySQL's boolean type TINYINT.
  8. type BitBool bool
  9. // Value implements the driver.Valuer interface,
  10. // and turns the BitBool into a bitfield (BIT(1)) for MySQL storage.
  11. func (b BitBool) Value() (driver.Value, error) {
  12. if b {
  13. return []byte{1}, nil
  14. }
  15. return []byte{0}, nil
  16. }
  17. // Scan implements the sql.Scanner interface,
  18. // and turns the bitfield incoming from MySQL into a BitBool
  19. func (b *BitBool) Scan(src interface{}) error {
  20. v, ok := src.([]byte)
  21. if !ok {
  22. return errors.New("bad []byte type assertion")
  23. }
  24. *b = v[0] == 1
  25. return nil
  26. }