You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

column.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. TableName: "",
  50. FieldName: fieldName,
  51. SQLType: sqlType,
  52. Length: len1,
  53. Length2: len2,
  54. Nullable: nullable,
  55. Default: "",
  56. Indexes: make(map[string]int),
  57. IsPrimaryKey: false,
  58. IsAutoIncrement: false,
  59. MapType: TWOSIDES,
  60. IsCreated: false,
  61. IsUpdated: false,
  62. IsDeleted: false,
  63. IsCascade: false,
  64. IsVersion: false,
  65. DefaultIsEmpty: true, // default should be no default
  66. EnumOptions: make(map[string]int),
  67. Comment: "",
  68. }
  69. }
  70. // ValueOf returns column's filed of struct's value
  71. func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
  72. dataStruct := reflect.Indirect(reflect.ValueOf(bean))
  73. return col.ValueOfV(&dataStruct)
  74. }
  75. // ValueOfV returns column's filed of struct's value accept reflevt value
  76. func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
  77. var fieldValue reflect.Value
  78. fieldPath := strings.Split(col.FieldName, ".")
  79. if dataStruct.Type().Kind() == reflect.Map {
  80. keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
  81. fieldValue = dataStruct.MapIndex(keyValue)
  82. return &fieldValue, nil
  83. } else if dataStruct.Type().Kind() == reflect.Interface {
  84. structValue := reflect.ValueOf(dataStruct.Interface())
  85. dataStruct = &structValue
  86. }
  87. level := len(fieldPath)
  88. fieldValue = dataStruct.FieldByName(fieldPath[0])
  89. for i := 0; i < level-1; i++ {
  90. if !fieldValue.IsValid() {
  91. break
  92. }
  93. if fieldValue.Kind() == reflect.Struct {
  94. fieldValue = fieldValue.FieldByName(fieldPath[i+1])
  95. } else if fieldValue.Kind() == reflect.Ptr {
  96. if fieldValue.IsNil() {
  97. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  98. }
  99. fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
  100. } else {
  101. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  102. }
  103. }
  104. if !fieldValue.IsValid() {
  105. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  106. }
  107. return &fieldValue, nil
  108. }
  109. // ConvertID converts id content to suitable type according column type
  110. func (col *Column) ConvertID(sid string) (interface{}, error) {
  111. if col.SQLType.IsNumeric() {
  112. n, err := strconv.ParseInt(sid, 10, 64)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return n, nil
  117. } else if col.SQLType.IsText() {
  118. return sid, nil
  119. }
  120. return nil, errors.New("not supported")
  121. }