column.go 3.9 KB

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