1
0

sql.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package uuid
  22. import (
  23. "bytes"
  24. "database/sql/driver"
  25. "encoding/json"
  26. "fmt"
  27. )
  28. // Value implements the driver.Valuer interface.
  29. func (u UUID) Value() (driver.Value, error) {
  30. return u.String(), nil
  31. }
  32. // Scan implements the sql.Scanner interface.
  33. // A 16-byte slice will be handled by UnmarshalBinary, while
  34. // a longer byte slice or a string will be handled by UnmarshalText.
  35. func (u *UUID) Scan(src interface{}) error {
  36. switch src := src.(type) {
  37. case UUID: // support gorm convert from UUID to NullUUID
  38. *u = src
  39. return nil
  40. case []byte:
  41. if len(src) == Size {
  42. return u.UnmarshalBinary(src)
  43. }
  44. return u.UnmarshalText(src)
  45. case string:
  46. return u.UnmarshalText([]byte(src))
  47. }
  48. return fmt.Errorf("uuid: cannot convert %T to UUID", src)
  49. }
  50. // NullUUID can be used with the standard sql package to represent a
  51. // UUID value that can be NULL in the database.
  52. type NullUUID struct {
  53. UUID UUID
  54. Valid bool
  55. }
  56. // Value implements the driver.Valuer interface.
  57. func (u NullUUID) Value() (driver.Value, error) {
  58. if !u.Valid {
  59. return nil, nil
  60. }
  61. // Delegate to UUID Value function
  62. return u.UUID.Value()
  63. }
  64. // Scan implements the sql.Scanner interface.
  65. func (u *NullUUID) Scan(src interface{}) error {
  66. if src == nil {
  67. u.UUID, u.Valid = Nil, false
  68. return nil
  69. }
  70. // Delegate to UUID Scan function
  71. u.Valid = true
  72. return u.UUID.Scan(src)
  73. }
  74. // MarshalJSON marshals the NullUUID as null or the nested UUID
  75. func (u NullUUID) MarshalJSON() ([]byte, error) {
  76. if !u.Valid {
  77. return json.Marshal(nil)
  78. }
  79. return json.Marshal(u.UUID)
  80. }
  81. // UnmarshalJSON unmarshals a NullUUID
  82. func (u *NullUUID) UnmarshalJSON(b []byte) error {
  83. if bytes.Equal(b, []byte("null")) {
  84. u.UUID, u.Valid = Nil, false
  85. return nil
  86. }
  87. if err := json.Unmarshal(b, &u.UUID); err != nil {
  88. return err
  89. }
  90. u.Valid = true
  91. return nil
  92. }