bool.go 703 B

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