numeric.go 1.3 KB

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