column.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package schemas
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. TWOSIDES = iota + 1
  15. ONLYTODB
  16. ONLYFROMDB
  17. )
  18. // Column defines database column
  19. type Column struct {
  20. Name string
  21. TableName string
  22. FieldName string // Avaiable only when parsed from a struct
  23. SQLType SQLType
  24. IsJSON bool
  25. Length int
  26. Length2 int
  27. Nullable bool
  28. Default string
  29. Indexes map[string]int
  30. IsPrimaryKey bool
  31. IsAutoIncrement bool
  32. MapType int
  33. IsCreated bool
  34. IsUpdated bool
  35. IsDeleted bool
  36. IsCascade bool
  37. IsVersion bool
  38. DefaultIsEmpty bool // false means column has no default set, but not default value is empty
  39. EnumOptions map[string]int
  40. SetOptions map[string]int
  41. DisableTimeZone bool
  42. TimeZone *time.Location // column specified time zone
  43. Comment string
  44. }
  45. // NewColumn creates a new column
  46. func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
  47. return &Column{
  48. Name: name,
  49. IsJSON: sqlType.IsJson(),
  50. TableName: "",
  51. FieldName: fieldName,
  52. SQLType: sqlType,
  53. Length: len1,
  54. Length2: len2,
  55. Nullable: nullable,
  56. Default: "",
  57. Indexes: make(map[string]int),
  58. IsPrimaryKey: false,
  59. IsAutoIncrement: false,
  60. MapType: TWOSIDES,
  61. IsCreated: false,
  62. IsUpdated: false,
  63. IsDeleted: false,
  64. IsCascade: false,
  65. IsVersion: false,
  66. DefaultIsEmpty: true, // default should be no default
  67. EnumOptions: make(map[string]int),
  68. Comment: "",
  69. }
  70. }
  71. // ValueOf returns column's filed of struct's value
  72. func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
  73. dataStruct := reflect.Indirect(reflect.ValueOf(bean))
  74. return col.ValueOfV(&dataStruct)
  75. }
  76. // ValueOfV returns column's filed of struct's value accept reflevt value
  77. func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
  78. var fieldValue reflect.Value
  79. fieldPath := strings.Split(col.FieldName, ".")
  80. if dataStruct.Type().Kind() == reflect.Map {
  81. keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
  82. fieldValue = dataStruct.MapIndex(keyValue)
  83. return &fieldValue, nil
  84. } else if dataStruct.Type().Kind() == reflect.Interface {
  85. structValue := reflect.ValueOf(dataStruct.Interface())
  86. dataStruct = &structValue
  87. }
  88. level := len(fieldPath)
  89. fieldValue = dataStruct.FieldByName(fieldPath[0])
  90. for i := 0; i < level-1; i++ {
  91. if !fieldValue.IsValid() {
  92. break
  93. }
  94. if fieldValue.Kind() == reflect.Struct {
  95. fieldValue = fieldValue.FieldByName(fieldPath[i+1])
  96. } else if fieldValue.Kind() == reflect.Ptr {
  97. if fieldValue.IsNil() {
  98. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  99. }
  100. fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
  101. } else {
  102. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  103. }
  104. }
  105. if !fieldValue.IsValid() {
  106. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  107. }
  108. return &fieldValue, nil
  109. }
  110. // ConvertID converts id content to suitable type according column type
  111. func (col *Column) ConvertID(sid string) (interface{}, error) {
  112. if col.SQLType.IsNumeric() {
  113. n, err := strconv.ParseInt(sid, 10, 64)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return n, nil
  118. } else if col.SQLType.IsText() {
  119. return sid, nil
  120. }
  121. return nil, errors.New("not supported")
  122. }