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

column.go 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "fmt"
  7. "reflect"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. TWOSIDES = iota + 1
  13. ONLYTODB
  14. ONLYFROMDB
  15. )
  16. // Column defines database column
  17. type Column struct {
  18. Name string
  19. TableName string
  20. FieldName string // Avaiable only when parsed from a struct
  21. SQLType SQLType
  22. IsJSON bool
  23. Length int
  24. Length2 int
  25. Nullable bool
  26. Default string
  27. Indexes map[string]int
  28. IsPrimaryKey bool
  29. IsAutoIncrement bool
  30. MapType int
  31. IsCreated bool
  32. IsUpdated bool
  33. IsDeleted bool
  34. IsCascade bool
  35. IsVersion bool
  36. DefaultIsEmpty bool // false means column has no default set, but not default value is empty
  37. EnumOptions map[string]int
  38. SetOptions map[string]int
  39. DisableTimeZone bool
  40. TimeZone *time.Location // column specified time zone
  41. Comment string
  42. }
  43. // NewColumn creates a new column
  44. func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
  45. return &Column{
  46. Name: name,
  47. TableName: "",
  48. FieldName: fieldName,
  49. SQLType: sqlType,
  50. Length: len1,
  51. Length2: len2,
  52. Nullable: nullable,
  53. Default: "",
  54. Indexes: make(map[string]int),
  55. IsPrimaryKey: false,
  56. IsAutoIncrement: false,
  57. MapType: TWOSIDES,
  58. IsCreated: false,
  59. IsUpdated: false,
  60. IsDeleted: false,
  61. IsCascade: false,
  62. IsVersion: false,
  63. DefaultIsEmpty: true, // default should be no default
  64. EnumOptions: make(map[string]int),
  65. Comment: "",
  66. }
  67. }
  68. // ValueOf returns column's filed of struct's value
  69. func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
  70. dataStruct := reflect.Indirect(reflect.ValueOf(bean))
  71. return col.ValueOfV(&dataStruct)
  72. }
  73. // ValueOfV returns column's filed of struct's value accept reflevt value
  74. func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
  75. var fieldValue reflect.Value
  76. fieldPath := strings.Split(col.FieldName, ".")
  77. if dataStruct.Type().Kind() == reflect.Map {
  78. keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
  79. fieldValue = dataStruct.MapIndex(keyValue)
  80. return &fieldValue, nil
  81. } else if dataStruct.Type().Kind() == reflect.Interface {
  82. structValue := reflect.ValueOf(dataStruct.Interface())
  83. dataStruct = &structValue
  84. }
  85. level := len(fieldPath)
  86. fieldValue = dataStruct.FieldByName(fieldPath[0])
  87. for i := 0; i < level-1; i++ {
  88. if !fieldValue.IsValid() {
  89. break
  90. }
  91. if fieldValue.Kind() == reflect.Struct {
  92. fieldValue = fieldValue.FieldByName(fieldPath[i+1])
  93. } else if fieldValue.Kind() == reflect.Ptr {
  94. if fieldValue.IsNil() {
  95. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  96. }
  97. fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
  98. } else {
  99. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  100. }
  101. }
  102. if !fieldValue.IsValid() {
  103. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  104. }
  105. return &fieldValue, nil
  106. }