table.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package core
  2. import (
  3. "reflect"
  4. "strings"
  5. )
  6. // database table
  7. type Table struct {
  8. Name string
  9. Type reflect.Type
  10. columnsSeq []string
  11. columnsMap map[string][]*Column
  12. columns []*Column
  13. Indexes map[string]*Index
  14. PrimaryKeys []string
  15. AutoIncrement string
  16. Created map[string]bool
  17. Updated string
  18. Deleted string
  19. Version string
  20. Cacher Cacher
  21. StoreEngine string
  22. Charset string
  23. }
  24. func (table *Table) Columns() []*Column {
  25. return table.columns
  26. }
  27. func (table *Table) ColumnsSeq() []string {
  28. return table.columnsSeq
  29. }
  30. func NewEmptyTable() *Table {
  31. return NewTable("", nil)
  32. }
  33. func NewTable(name string, t reflect.Type) *Table {
  34. return &Table{Name: name, Type: t,
  35. columnsSeq: make([]string, 0),
  36. columns: make([]*Column, 0),
  37. columnsMap: make(map[string][]*Column),
  38. Indexes: make(map[string]*Index),
  39. Created: make(map[string]bool),
  40. PrimaryKeys: make([]string, 0),
  41. }
  42. }
  43. func (table *Table) columnsByName(name string) []*Column {
  44. n := len(name)
  45. for k := range table.columnsMap {
  46. if len(k) != n {
  47. continue
  48. }
  49. if strings.EqualFold(k, name) {
  50. return table.columnsMap[k]
  51. }
  52. }
  53. return nil
  54. }
  55. func (table *Table) GetColumn(name string) *Column {
  56. cols := table.columnsByName(name)
  57. if cols != nil {
  58. return cols[0]
  59. }
  60. return nil
  61. }
  62. func (table *Table) GetColumnIdx(name string, idx int) *Column {
  63. cols := table.columnsByName(name)
  64. if cols != nil && idx < len(cols) {
  65. return cols[idx]
  66. }
  67. return nil
  68. }
  69. // if has primary key, return column
  70. func (table *Table) PKColumns() []*Column {
  71. columns := make([]*Column, len(table.PrimaryKeys))
  72. for i, name := range table.PrimaryKeys {
  73. columns[i] = table.GetColumn(name)
  74. }
  75. return columns
  76. }
  77. func (table *Table) ColumnType(name string) reflect.Type {
  78. t, _ := table.Type.FieldByName(name)
  79. return t.Type
  80. }
  81. func (table *Table) AutoIncrColumn() *Column {
  82. return table.GetColumn(table.AutoIncrement)
  83. }
  84. func (table *Table) VersionColumn() *Column {
  85. return table.GetColumn(table.Version)
  86. }
  87. func (table *Table) UpdatedColumn() *Column {
  88. return table.GetColumn(table.Updated)
  89. }
  90. func (table *Table) DeletedColumn() *Column {
  91. return table.GetColumn(table.Deleted)
  92. }
  93. // add a column to table
  94. func (table *Table) AddColumn(col *Column) {
  95. table.columnsSeq = append(table.columnsSeq, col.Name)
  96. table.columns = append(table.columns, col)
  97. colName := strings.ToLower(col.Name)
  98. if c, ok := table.columnsMap[colName]; ok {
  99. table.columnsMap[colName] = append(c, col)
  100. } else {
  101. table.columnsMap[colName] = []*Column{col}
  102. }
  103. if col.IsPrimaryKey {
  104. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  105. }
  106. if col.IsAutoIncrement {
  107. table.AutoIncrement = col.Name
  108. }
  109. if col.IsCreated {
  110. table.Created[col.Name] = true
  111. }
  112. if col.IsUpdated {
  113. table.Updated = col.Name
  114. }
  115. if col.IsDeleted {
  116. table.Deleted = col.Name
  117. }
  118. if col.IsVersion {
  119. table.Version = col.Name
  120. }
  121. }
  122. // add an index or an unique to table
  123. func (table *Table) AddIndex(index *Index) {
  124. table.Indexes[index.Name] = index
  125. }