numeric.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package types
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. )
  6. // NullInt is an implementation of a int for the MySQL type int/tinyint ....
  7. type NullInt int
  8. // Value implements the driver.Valuer interface,
  9. // and turns the bytes into a integer for MySQL storage.
  10. func (n NullInt) Value() (driver.Value, error) {
  11. return NullInt(n), nil
  12. }
  13. // Scan implements the sql.Scanner interface,
  14. // and turns the bytes incoming from MySQL into a integer
  15. func (n *NullInt) Scan(src interface{}) error {
  16. if src != nil {
  17. v, ok := src.(int64)
  18. if !ok {
  19. return errors.New("bad []byte type assertion")
  20. }
  21. *n = NullInt(v)
  22. return nil
  23. }
  24. *n = NullInt(0)
  25. return nil
  26. }
  27. // NullFloat is an implementation of a int for the MySQL type numeric ....
  28. type NullFloat float64
  29. // Scan implements the sql.Scanner interface,
  30. // and turns the bytes incoming from MySQL into a numeric
  31. func (n *NullFloat) Scan(src interface{}) error {
  32. if src != nil {
  33. v, ok := src.(int64)
  34. if !ok {
  35. return errors.New("bad []byte type assertion")
  36. }
  37. *n = NullFloat(v)
  38. return nil
  39. }
  40. *n = NullFloat(0)
  41. return nil
  42. }