column.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package core
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. )
  8. const (
  9. TWOSIDES = iota + 1
  10. ONLYTODB
  11. ONLYFROMDB
  12. )
  13. // database column
  14. type Column struct {
  15. Name string
  16. TableName string
  17. FieldName string
  18. SQLType SQLType
  19. Length int
  20. Length2 int
  21. Nullable bool
  22. Default string
  23. Indexes map[string]int
  24. IsPrimaryKey bool
  25. IsAutoIncrement bool
  26. MapType int
  27. IsCreated bool
  28. IsUpdated bool
  29. IsDeleted bool
  30. IsCascade bool
  31. IsVersion bool
  32. DefaultIsEmpty bool
  33. EnumOptions map[string]int
  34. SetOptions map[string]int
  35. DisableTimeZone bool
  36. TimeZone *time.Location // column specified time zone
  37. }
  38. func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
  39. return &Column{
  40. Name: name,
  41. TableName: "",
  42. FieldName: fieldName,
  43. SQLType: sqlType,
  44. Length: len1,
  45. Length2: len2,
  46. Nullable: nullable,
  47. Default: "",
  48. Indexes: make(map[string]int),
  49. IsPrimaryKey: false,
  50. IsAutoIncrement: false,
  51. MapType: TWOSIDES,
  52. IsCreated: false,
  53. IsUpdated: false,
  54. IsDeleted: false,
  55. IsCascade: false,
  56. IsVersion: false,
  57. DefaultIsEmpty: false,
  58. EnumOptions: make(map[string]int),
  59. }
  60. }
  61. // generate column description string according dialect
  62. func (col *Column) String(d Dialect) string {
  63. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  64. sql += d.SqlType(col) + " "
  65. if col.IsPrimaryKey {
  66. sql += "PRIMARY KEY "
  67. if col.IsAutoIncrement {
  68. sql += d.AutoIncrStr() + " "
  69. }
  70. }
  71. if d.ShowCreateNull() {
  72. if col.Nullable {
  73. sql += "NULL "
  74. } else {
  75. sql += "NOT NULL "
  76. }
  77. }
  78. if col.Default != "" {
  79. sql += "DEFAULT " + col.Default + " "
  80. }
  81. return sql
  82. }
  83. func (col *Column) StringNoPk(d Dialect) string {
  84. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  85. sql += d.SqlType(col) + " "
  86. if d.ShowCreateNull() {
  87. if col.Nullable {
  88. sql += "NULL "
  89. } else {
  90. sql += "NOT NULL "
  91. }
  92. }
  93. if col.Default != "" {
  94. sql += "DEFAULT " + col.Default + " "
  95. }
  96. return sql
  97. }
  98. // return col's filed of struct's value
  99. func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
  100. dataStruct := reflect.Indirect(reflect.ValueOf(bean))
  101. return col.ValueOfV(&dataStruct)
  102. }
  103. func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
  104. var fieldValue reflect.Value
  105. fieldPath := strings.Split(col.FieldName, ".")
  106. if dataStruct.Type().Kind() == reflect.Map {
  107. keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
  108. fieldValue = dataStruct.MapIndex(keyValue)
  109. return &fieldValue, nil
  110. } else if dataStruct.Type().Kind() == reflect.Interface {
  111. structValue := reflect.ValueOf(dataStruct.Interface())
  112. dataStruct = &structValue
  113. }
  114. level := len(fieldPath)
  115. fieldValue = dataStruct.FieldByName(fieldPath[0])
  116. for i := 0; i < level-1; i++ {
  117. if !fieldValue.IsValid() {
  118. break
  119. }
  120. if fieldValue.Kind() == reflect.Struct {
  121. fieldValue = fieldValue.FieldByName(fieldPath[i+1])
  122. } else if fieldValue.Kind() == reflect.Ptr {
  123. if fieldValue.IsNil() {
  124. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  125. }
  126. fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
  127. } else {
  128. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  129. }
  130. }
  131. if !fieldValue.IsValid() {
  132. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  133. }
  134. return &fieldValue, nil
  135. }