numeric.go 1.3 KB

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