type.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package sqltypes
  2. // These bit flags can be used to query on the
  3. // common properties of types.
  4. const (
  5. flagIsIntegral = int(Flag_ISINTEGRAL)
  6. flagIsUnsigned = int(Flag_ISUNSIGNED)
  7. flagIsFloat = int(Flag_ISFLOAT)
  8. flagIsQuoted = int(Flag_ISQUOTED)
  9. flagIsText = int(Flag_ISTEXT)
  10. flagIsBinary = int(Flag_ISBINARY)
  11. )
  12. // IsIntegral returns true if Type is an integral
  13. // (signed/unsigned) that can be represented using
  14. // up to 64 binary bits.
  15. // If you have a Value object, use its member function.
  16. func IsIntegral(t Type) bool {
  17. return int(t)&flagIsIntegral == flagIsIntegral
  18. }
  19. // IsSigned returns true if Type is a signed integral.
  20. // If you have a Value object, use its member function.
  21. func IsSigned(t Type) bool {
  22. return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral
  23. }
  24. // IsUnsigned returns true if Type is an unsigned integral.
  25. // Caution: this is not the same as !IsSigned.
  26. // If you have a Value object, use its member function.
  27. func IsUnsigned(t Type) bool {
  28. return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral|flagIsUnsigned
  29. }
  30. // IsFloat returns true is Type is a floating point.
  31. // If you have a Value object, use its member function.
  32. func IsFloat(t Type) bool {
  33. return int(t)&flagIsFloat == flagIsFloat
  34. }
  35. // IsQuoted returns true if Type is a quoted text or binary.
  36. // If you have a Value object, use its member function.
  37. func IsQuoted(t Type) bool {
  38. return (int(t)&flagIsQuoted == flagIsQuoted) && t != Bit
  39. }
  40. // IsText returns true if Type is a text.
  41. // If you have a Value object, use its member function.
  42. func IsText(t Type) bool {
  43. return int(t)&flagIsText == flagIsText
  44. }
  45. // IsBinary returns true if Type is a binary.
  46. // If you have a Value object, use its member function.
  47. func IsBinary(t Type) bool {
  48. return int(t)&flagIsBinary == flagIsBinary
  49. }
  50. // isNumber returns true if the type is any type of number.
  51. func isNumber(t Type) bool {
  52. return IsIntegral(t) || IsFloat(t) || t == Decimal
  53. }
  54. // IsTemporal returns true if Value is time type.
  55. func IsTemporal(t Type) bool {
  56. switch t {
  57. case Timestamp, Date, Time, Datetime:
  58. return true
  59. }
  60. return false
  61. }
  62. // Vitess data types. These are idiomatically
  63. // named synonyms for the Type values.
  64. const (
  65. Null = Type_NULL_TYPE
  66. Int8 = Type_INT8
  67. Uint8 = Type_UINT8
  68. Int16 = Type_INT16
  69. Uint16 = Type_UINT16
  70. Int32 = Type_INT32
  71. Uint32 = Type_UINT32
  72. Int64 = Type_INT64
  73. Uint64 = Type_UINT64
  74. Float32 = Type_FLOAT32
  75. Float64 = Type_FLOAT64
  76. Timestamp = Type_TIMESTAMP
  77. Date = Type_DATE
  78. Time = Type_TIME
  79. Datetime = Type_DATETIME
  80. Year = Type_YEAR
  81. Decimal = Type_DECIMAL
  82. Text = Type_TEXT
  83. Blob = Type_BLOB
  84. VarChar = Type_VARCHAR
  85. VarBinary = Type_VARBINARY
  86. Char = Type_CHAR
  87. Binary = Type_BINARY
  88. Bit = Type_BIT
  89. TypeJSON = Type_JSON
  90. )