column.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. Comment string
  38. }
  39. func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
  40. return &Column{
  41. Name: name,
  42. TableName: "",
  43. FieldName: fieldName,
  44. SQLType: sqlType,
  45. Length: len1,
  46. Length2: len2,
  47. Nullable: nullable,
  48. Default: "",
  49. Indexes: make(map[string]int),
  50. IsPrimaryKey: false,
  51. IsAutoIncrement: false,
  52. MapType: TWOSIDES,
  53. IsCreated: false,
  54. IsUpdated: false,
  55. IsDeleted: false,
  56. IsCascade: false,
  57. IsVersion: false,
  58. DefaultIsEmpty: false,
  59. EnumOptions: make(map[string]int),
  60. Comment: "",
  61. }
  62. }
  63. // generate column description string according dialect
  64. func (col *Column) String(d Dialect) string {
  65. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  66. sql += d.SqlType(col) + " "
  67. if col.IsPrimaryKey {
  68. sql += "PRIMARY KEY "
  69. if col.IsAutoIncrement {
  70. sql += d.AutoIncrStr() + " "
  71. }
  72. }
  73. if d.ShowCreateNull() {
  74. if col.Nullable {
  75. sql += "NULL "
  76. } else {
  77. sql += "NOT NULL "
  78. }
  79. }
  80. if col.Default != "" {
  81. sql += "DEFAULT " + col.Default + " "
  82. }
  83. return sql
  84. }
  85. func (col *Column) StringNoPk(d Dialect) string {
  86. sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
  87. sql += d.SqlType(col) + " "
  88. if d.ShowCreateNull() {
  89. if col.Nullable {
  90. sql += "NULL "
  91. } else {
  92. sql += "NOT NULL "
  93. }
  94. }
  95. if col.Default != "" {
  96. sql += "DEFAULT " + col.Default + " "
  97. }
  98. return sql
  99. }
  100. // return col's filed of struct's value
  101. func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
  102. dataStruct := reflect.Indirect(reflect.ValueOf(bean))
  103. return col.ValueOfV(&dataStruct)
  104. }
  105. func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
  106. var fieldValue reflect.Value
  107. fieldPath := strings.Split(col.FieldName, ".")
  108. if dataStruct.Type().Kind() == reflect.Map {
  109. keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
  110. fieldValue = dataStruct.MapIndex(keyValue)
  111. return &fieldValue, nil
  112. } else if dataStruct.Type().Kind() == reflect.Interface {
  113. structValue := reflect.ValueOf(dataStruct.Interface())
  114. dataStruct = &structValue
  115. }
  116. level := len(fieldPath)
  117. fieldValue = dataStruct.FieldByName(fieldPath[0])
  118. for i := 0; i < level-1; i++ {
  119. if !fieldValue.IsValid() {
  120. break
  121. }
  122. if fieldValue.Kind() == reflect.Struct {
  123. fieldValue = fieldValue.FieldByName(fieldPath[i+1])
  124. } else if fieldValue.Kind() == reflect.Ptr {
  125. if fieldValue.IsNil() {
  126. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  127. }
  128. fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
  129. } else {
  130. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  131. }
  132. }
  133. if !fieldValue.IsValid() {
  134. return nil, fmt.Errorf("field %v is not valid", col.FieldName)
  135. }
  136. return &fieldValue, nil
  137. }